forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builds.go
213 lines (189 loc) · 4.63 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package datastore
import (
"fmt"
"time"
"github.com/drone/drone/model"
"github.com/drone/drone/store/datastore/sql"
"github.com/russross/meddler"
)
func (db *datastore) GetBuild(id int64) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.Load(db, buildTable, build, id)
return build, err
}
func (db *datastore) GetBuildNumber(repo *model.Repo, num int) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.QueryRow(db, build, rebind(buildNumberQuery), repo.ID, num)
return build, err
}
func (db *datastore) GetBuildRef(repo *model.Repo, ref string) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.QueryRow(db, build, rebind(buildRefQuery), repo.ID, ref)
return build, err
}
func (db *datastore) GetBuildCommit(repo *model.Repo, sha, branch string) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.QueryRow(db, build, rebind(buildCommitQuery), repo.ID, sha, branch)
return build, err
}
func (db *datastore) GetBuildLast(repo *model.Repo, branch string) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.QueryRow(db, build, rebind(buildLastQuery), repo.ID, branch)
return build, err
}
func (db *datastore) GetBuildLastBefore(repo *model.Repo, branch string, num int64) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.QueryRow(db, build, rebind(buildLastBeforeQuery), repo.ID, branch, num)
return build, err
}
func (db *datastore) GetBuildList(repo *model.Repo) ([]*model.Build, error) {
var builds = []*model.Build{}
var err = meddler.QueryAll(db, &builds, rebind(buildListQuery), repo.ID)
return builds, err
}
func (db *datastore) GetBuildQueue() ([]*model.Feed, error) {
feed := []*model.Feed{}
err := meddler.QueryAll(db, &feed, buildQueueList)
return feed, err
}
func (db *datastore) CreateBuild(build *model.Build, procs ...*model.Proc) error {
id, err := db.incrementRepoRetry(build.RepoID)
if err != nil {
return err
}
build.Number = id
build.Created = time.Now().UTC().Unix()
build.Enqueued = build.Created
err = meddler.Insert(db, buildTable, build)
if err != nil {
return err
}
for _, proc := range procs {
proc.BuildID = build.ID
err = meddler.Insert(db, "procs", proc)
if err != nil {
return err
}
}
return nil
}
func (db *datastore) incrementRepoRetry(id int64) (int, error) {
repo, err := db.GetRepo(id)
if err != nil {
return 0, fmt.Errorf("database: cannot fetch repository: %s", err)
}
for i := 0; i < 10; i++ {
seq, err := db.incrementRepo(repo.ID, repo.Counter+i, repo.Counter+i+1)
if err != nil {
return 0, err
}
if seq == 0 {
continue
}
return seq, nil
}
return 0, fmt.Errorf("cannot increment next build number")
}
func (db *datastore) incrementRepo(id int64, old, new int) (int, error) {
results, err := db.Exec(sql.Lookup(db.driver, "repo-update-counter"), new, old, id)
if err != nil {
return 0, fmt.Errorf("database: update repository counter: %s", err)
}
updated, err := results.RowsAffected()
if err != nil {
return 0, fmt.Errorf("database: update repository counter: %s", err)
}
if updated == 0 {
return 0, nil
}
return new, nil
}
func (db *datastore) UpdateBuild(build *model.Build) error {
return meddler.Update(db, buildTable, build)
}
func (db *datastore) GetBuildCount() (count int, err error) {
err = db.QueryRow(
sql.Lookup(db.driver, "count-builds"),
).Scan(&count)
return
}
const buildTable = "builds"
const buildListQuery = `
SELECT *
FROM builds
WHERE build_repo_id = ?
ORDER BY build_number DESC
LIMIT 50
`
const buildNumberQuery = `
SELECT *
FROM builds
WHERE build_repo_id = ?
AND build_number = ?
LIMIT 1;
`
const buildLastQuery = `
SELECT *
FROM builds
WHERE build_repo_id = ?
AND build_branch = ?
AND build_event = 'push'
ORDER BY build_number DESC
LIMIT 1
`
const buildLastBeforeQuery = `
SELECT *
FROM builds
WHERE build_repo_id = ?
AND build_branch = ?
AND build_id < ?
ORDER BY build_number DESC
LIMIT 1
`
const buildCommitQuery = `
SELECT *
FROM builds
WHERE build_repo_id = ?
AND build_commit = ?
AND build_branch = ?
LIMIT 1
`
const buildRefQuery = `
SELECT *
FROM builds
WHERE build_repo_id = ?
AND build_ref = ?
LIMIT 1
`
const buildNumberLast = `
SELECT MAX(build_number)
FROM builds
WHERE build_repo_id = ?
`
const buildQueueList = `
SELECT
repo_owner
,repo_name
,repo_full_name
,build_number
,build_event
,build_status
,build_created
,build_started
,build_finished
,build_commit
,build_branch
,build_ref
,build_refspec
,build_remote
,build_title
,build_message
,build_author
,build_email
,build_avatar
FROM
builds b
,repos r
WHERE b.build_repo_id = r.repo_id
AND b.build_status IN ('pending','running')
`