forked from evergreen-ci/evergreen
/
build.go
274 lines (243 loc) · 8.22 KB
/
build.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package ui
import (
"encoding/json"
"fmt"
"github.com/10gen-labs/slogger/v1"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/user"
"github.com/evergreen-ci/evergreen/model/version"
"github.com/evergreen-ci/evergreen/plugin"
"github.com/gorilla/mux"
"io/ioutil"
"net/http"
"strconv"
"time"
)
func (uis *UIServer) buildPage(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
if projCtx.Build == nil {
uis.LoggedError(w, r, http.StatusNotFound, fmt.Errorf("not found"))
return
}
flashes := PopFlashes(uis.CookieStore, r, w)
buildAsUI := &uiBuild{
Build: *projCtx.Build,
CurrentTime: time.Now().UnixNano(),
Elapsed: time.Now().Sub(projCtx.Build.StartTime),
RepoOwner: projCtx.ProjectRef.Owner,
Repo: projCtx.ProjectRef.Repo,
Version: *projCtx.Version,
}
// add in the tasks
tasks, err := model.FindTasksForBuild(projCtx.Build)
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
uiTasks := make([]uiTask, 0, len(tasks))
for _, task := range tasks {
taskAsUI := uiTask{Task: task}
uiTasks = append(uiTasks, taskAsUI)
}
buildAsUI.Tasks = sortUiTasks(uiTasks)
if projCtx.Build.Requester == evergreen.PatchVersionRequester {
buildOnBaseCommit, err := projCtx.Build.FindBuildOnBaseCommit()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if buildOnBaseCommit == nil {
evergreen.Logger.Logf(slogger.WARN,
"Could not find build for base commit of patch build: %v", projCtx.Build.Id)
}
diffs := model.StatusDiffBuilds(buildOnBaseCommit, projCtx.Build)
baseId := ""
if buildOnBaseCommit != nil {
baseId = buildOnBaseCommit.Id
}
buildAsUI.PatchInfo = &uiPatch{Patch: *projCtx.Patch, BaseBuildId: baseId, StatusDiffs: diffs.Tasks}
}
// set data for plugin data function injection
pluginContext := projCtx.ToPluginContext(uis.Settings, GetUser(r))
pluginContent := getPluginDataAndHTML(uis, plugin.BuildPage, pluginContext)
uis.WriteHTML(w, http.StatusOK, struct {
ProjectData projectContext
User *user.DBUser
Flashes []interface{}
Build *uiBuild
PluginContent pluginData
}{projCtx, GetUser(r), flashes, buildAsUI, pluginContent}, "base", "build.html", "base_angular.html", "menu.html")
}
func (uis *UIServer) modifyBuild(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
if projCtx.Build == nil {
http.Error(w, "Not found", http.StatusNotFound)
return
}
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer r.Body.Close()
putParams := struct {
Action string `json:"action"`
Active bool `json:"active"`
Abort bool `json:"abort"`
Priority string `json:"priority"`
}{}
err = json.Unmarshal(reqBody, &putParams)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// determine what action needs to be taken
switch putParams.Action {
case "abort":
if err := model.AbortBuild(projCtx.Build.Id); err != nil {
http.Error(w, fmt.Sprintf("Error aborting build %v", projCtx.Build.Id), http.StatusInternalServerError)
return
}
model.RefreshTasksCache(projCtx.Build.Id)
uis.WriteJSON(w, http.StatusOK, "Successfully marked tasks as aborted")
return
case "set_priority":
priority, err := strconv.Atoi(putParams.Priority)
if err != nil {
http.Error(w, "Bad priority value; must be int", http.StatusBadRequest)
return
}
err = model.SetBuildPriority(projCtx.Build.Id, priority)
if err != nil {
http.Error(w, fmt.Sprintf("Error setting priority on build %v", projCtx.Build.Id),
http.StatusInternalServerError)
return
}
PushFlash(uis.CookieStore, r, w, NewSuccessFlash(fmt.Sprintf("Priority for build set to %v.", priority)))
uis.WriteJSON(w, http.StatusOK, "Successfully set priority")
return
case "set_active":
err := model.SetBuildActivation(projCtx.Build.Id, putParams.Active)
if err != nil {
http.Error(w, fmt.Sprintf("Error marking build %v as activated=%v", projCtx.Build.Id, putParams.Active),
http.StatusInternalServerError)
return
}
if putParams.Active {
PushFlash(uis.CookieStore, r, w, NewSuccessFlash("Build scheduled."))
} else {
PushFlash(uis.CookieStore, r, w, NewSuccessFlash("Build unscheduled."))
}
uis.WriteJSON(w, http.StatusOK, fmt.Sprintf("Successfully marked build as active=%v", putParams.Active))
return
case "restart":
if err := model.RestartBuild(projCtx.Build.Id, putParams.Abort); err != nil {
http.Error(w, fmt.Sprintf("Error restarting build %v", projCtx.Build.Id), http.StatusInternalServerError)
return
}
PushFlash(uis.CookieStore, r, w, NewSuccessFlash(fmt.Sprintf("Build %v restarted.", projCtx.Build.Id)))
uis.WriteJSON(w, http.StatusOK, fmt.Sprintf("Successfully restarted build", putParams.Active))
return
default:
uis.WriteJSON(w, http.StatusBadRequest, "Unrecognized action")
return
}
}
func (uis *UIServer) lastGreenHandler(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
// queryParams should list build variants, example:
// GET /ui/json/last_green/mongodb-mongo-master?linux-64=1&windows-64=1
queryParams := r.URL.Query()
if projCtx.Project == nil {
http.Error(w, "project not found", http.StatusNotFound)
return
}
// Make sure all query params are valid build variants and put them in an array
var buildVariantNames []string
for key, _ := range queryParams {
if projCtx.Project.FindBuildVariant(key) != nil {
buildVariantNames = append(buildVariantNames, key)
} else {
http.Error(w, "build variant not found", http.StatusNotFound)
return
}
}
// Get latest version for which all the given build variants passed.
version, err := model.FindLastPassingVersionForBuildVariants(*projCtx.Project,
buildVariantNames)
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
if version == nil {
http.Error(w, "Couldn't find latest green version", http.StatusNotFound)
return
}
uis.WriteJSON(w, http.StatusOK, version)
}
func (uis *UIServer) buildHistory(w http.ResponseWriter, r *http.Request) {
buildId := mux.Vars(r)["build_id"]
before, err := getIntValue(r, "before", 3)
if err != nil {
http.Error(w, fmt.Sprintf("invalid param 'before': %v", r.FormValue("before")), http.StatusBadRequest)
return
}
after, err := getIntValue(r, "after", 3)
if err != nil {
http.Error(w, fmt.Sprintf("invalid param 'after': %v", r.FormValue("after")), http.StatusBadRequest)
return
}
builds, err := getBuildVariantHistory(buildId, before, after)
if err != nil {
http.Error(w, fmt.Sprintf("error getting build history: %v", err), http.StatusInternalServerError)
return
}
history := &struct {
Builds []*uiBuild `json:"builds"`
LastSuccess *uiBuild `json:"lastSuccess"`
}{}
history.Builds = make([]*uiBuild, len(builds))
for i := 0; i < len(builds); i++ {
v, err := version.FindOne(version.ById(builds[i].Version))
if err != nil {
http.Error(w, fmt.Sprintf("error getting version for build %v: %v", builds[i].Id, err), http.StatusInternalServerError)
return
}
if v == nil {
http.Error(w, fmt.Sprintf("no version found for build %v", builds[i].Id), http.StatusNotFound)
return
}
history.Builds[i] = &uiBuild{
Build: builds[i],
CurrentTime: time.Now().UnixNano(),
Elapsed: time.Now().Sub(builds[i].StartTime),
RepoOwner: v.Owner,
Repo: v.Repo,
Version: *v,
}
}
lastSuccess, err := getBuildVariantHistoryLastSuccess(buildId)
if err == nil && lastSuccess != nil {
v, err := version.FindOne(version.ById(lastSuccess.Version))
if err != nil {
http.Error(
w, fmt.Sprintf("error getting last successful build version: %v", err),
http.StatusInternalServerError)
return
}
if v == nil {
http.Error(w, fmt.Sprintf("no version '%v' found", lastSuccess.Version), http.StatusNotFound)
return
}
history.LastSuccess = &uiBuild{
Build: *lastSuccess,
CurrentTime: time.Now().UnixNano(),
Elapsed: time.Now().Sub(lastSuccess.StartTime),
RepoOwner: v.Owner,
Repo: v.Repo,
Version: *v,
}
}
uis.WriteJSON(w, http.StatusOK, history)
}