forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builds.go
85 lines (72 loc) · 2.13 KB
/
builds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package database
import (
. "github.com/drone/drone/pkg/model"
"github.com/russross/meddler"
)
// Name of the Build table in the database
const buildTable = "builds"
// SQL Queries to retrieve a list of all Commits belonging to a Repo.
const buildStmt = `
SELECT id, commit_id, slug, status, started, finished, duration, created, updated, stdout, buildscript
FROM builds
WHERE commit_id = ?
ORDER BY slug ASC
`
// SQL Queries to retrieve a Build by id.
const buildFindStmt = `
SELECT id, commit_id, slug, status, started, finished, duration, created, updated, stdout, buildscript
FROM builds
WHERE id = ?
LIMIT 1
`
// SQL Queries to retrieve a Commit by name and repo id.
const buildFindSlugStmt = `
SELECT id, commit_id, slug, status, started, finished, duration, created, updated, stdout, buildscript
FROM builds
WHERE slug = ? AND commit_id = ?
LIMIT 1
`
// SQL Queries to fail all builds that are running or pending
const buildFailUnfinishedStmt = `
UPDATE builds
SET status = 'Failure'
WHERE status IN ('Started', 'Pending')
`
// SQL Queries to delete a Commit.
const buildDeleteStmt = `
DELETE FROM builds WHERE id = ?
`
// Returns the Build with the given ID.
func GetBuild(id int64) (*Build, error) {
build := Build{}
err := meddler.QueryRow(db, &build, buildFindStmt, id)
return &build, err
}
// Returns the Build with the given slug.
func GetBuildSlug(slug string, commit int64) (*Build, error) {
build := Build{}
err := meddler.QueryRow(db, &build, buildFindSlugStmt, slug, commit)
return &build, err
}
// Creates a new Build.
func SaveBuild(build *Build) error {
return meddler.Save(db, buildTable, build)
}
// Deletes an existing Build.
func DeleteBuild(id int64) error {
_, err := db.Exec(buildDeleteStmt, id)
return err
}
// Returns a list of all Builds associated
// with the specified Commit ID and branch.
func ListBuilds(id int64) ([]*Build, error) {
var builds []*Build
err := meddler.QueryAll(db, &builds, buildStmt, id)
return builds, err
}
// FailUnfinishedBuilds sets status=Failure to all builds
// in the Pending and Started states
func FailUnfinishedBuilds() error {
_, err := db.Exec(buildFailUnfinishedStmt)
return err
}