forked from evergreen-ci/evergreen
/
api_patch.go
380 lines (332 loc) · 10.6 KB
/
api_patch.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package service
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/db"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/evergreen-ci/evergreen/thirdparty"
"github.com/evergreen-ci/evergreen/units"
"github.com/evergreen-ci/evergreen/util"
"github.com/evergreen-ci/evergreen/validator"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"gopkg.in/mgo.v2/bson"
"gopkg.in/yaml.v2"
)
const formMimeType = "application/x-www-form-urlencoded"
// PatchAPIResponse is returned by all patch-related API calls
type PatchAPIResponse struct {
Message string `json:"message"`
Action string `json:"action"`
Patch *patch.Patch `json:"patch"`
}
// submitPatch creates the Patch document, adds the patched project config to it,
// and saves the patches to GridFS to be retrieved
func (as *APIServer) submitPatch(w http.ResponseWriter, r *http.Request) {
dbUser := MustHaveUser(r)
data := struct {
Description string `json:"desc"`
Project string `json:"project"`
Patch string `json:"patch"`
Githash string `json:"githash"`
Variants string `json:"buildvariants"`
Tasks []string `json:"tasks"`
Finalize bool `json:"finalize"`
Alias string `json:"alias"`
}{}
if err := util.ReadJSONInto(util.NewRequestReaderWithSize(r, patch.SizeLimit), &data); err != nil {
as.LoggedError(w, r, http.StatusBadRequest, err)
return
}
if len(data.Patch) > patch.SizeLimit {
as.LoggedError(w, r, http.StatusBadRequest, errors.New("Patch is too large."))
return
}
variants := strings.Split(data.Variants, ",")
intent, err := patch.NewCliIntent(dbUser.Id, data.Project, data.Githash, r.FormValue("module"), data.Patch, data.Description, data.Finalize, variants, data.Tasks, data.Alias)
if err != nil {
as.LoggedError(w, r, http.StatusBadRequest, err)
return
}
if intent == nil {
as.LoggedError(w, r, http.StatusBadRequest, errors.New("intent could not be created from supplied data"))
return
}
if err = intent.Insert(); err != nil {
as.LoggedError(w, r, http.StatusBadRequest, err)
return
}
patchID := bson.NewObjectId()
job := units.NewPatchIntentProcessor(patchID, intent)
job.Run(r.Context())
if err = job.Error(); err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "error processing patch"))
return
}
patchDoc, err := patch.FindOne(patch.ById(patchID))
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, errors.New("can't fetch patch data"))
return
}
if patchDoc == nil {
as.LoggedError(w, r, http.StatusInternalServerError, errors.New("patch couldn't be found"))
return
}
as.WriteJSON(w, http.StatusCreated, PatchAPIResponse{Patch: patchDoc})
}
// Get the patch with the specified request it
func getPatchFromRequest(r *http.Request) (*patch.Patch, error) {
// get id and secret from the request.
vars := mux.Vars(r)
patchIdStr := vars["patchId"]
if len(patchIdStr) == 0 {
return nil, errors.New("no patch id supplied")
}
if !patch.IsValidId(patchIdStr) {
return nil, errors.Errorf("patch id '%v' is not valid object id", patchIdStr)
}
// find the patch
existingPatch, err := patch.FindOne(patch.ById(patch.NewId(patchIdStr)))
if err != nil {
return nil, err
}
if existingPatch == nil {
return nil, errors.Errorf("no existing request with id: %v", patchIdStr)
}
return existingPatch, nil
}
func (as *APIServer) updatePatchModule(w http.ResponseWriter, r *http.Request) {
p, err := getPatchFromRequest(r)
if err != nil {
as.WriteJSON(w, http.StatusBadRequest, err.Error())
return
}
githubOauthToken, err := as.Settings.GetGithubOauthToken()
if err != nil {
as.WriteJSON(w, http.StatusBadRequest, err)
return
}
var moduleName, patchContent, githash string
if r.Header.Get("Content-Type") == formMimeType {
moduleName, patchContent, githash = r.FormValue("module"), r.FormValue("patch"), r.FormValue("githash")
} else {
data := struct {
Module string `json:"module"`
Patch string `json:"patch"`
Githash string `json:"githash"`
}{}
if err := util.ReadJSONInto(util.NewRequestReader(r), &data); err != nil {
as.LoggedError(w, r, http.StatusBadRequest, err)
return
}
moduleName, patchContent, githash = data.Module, data.Patch, data.Githash
}
projectRef, err := model.FindOneProjectRef(p.Project)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, errors.Wrapf(err, "Error getting project ref with id %v", p.Project))
return
}
project, err := model.FindProject("", projectRef)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "Error getting patch"))
return
}
if project == nil {
as.LoggedError(w, r, http.StatusNotFound, errors.Errorf("can't find project: %v", p.Project))
return
}
module, err := project.GetModuleByName(moduleName)
if err != nil || module == nil {
as.LoggedError(w, r, http.StatusBadRequest, errors.Errorf("No such module: %s", moduleName))
return
}
summaries, err := thirdparty.GetPatchSummaries(patchContent)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
}
repoOwner, repo := module.GetRepoOwnerAndName()
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
_, err = thirdparty.GetCommitEvent(ctx, githubOauthToken, repoOwner, repo, githash)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
// write the patch content into a GridFS file under a new ObjectId.
patchFileId := bson.NewObjectId().Hex()
err = db.WriteGridFile(patch.GridFSPrefix, patchFileId, strings.NewReader(patchContent))
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "failed to write patch file to db"))
return
}
modulePatch := patch.ModulePatch{
ModuleName: moduleName,
Githash: githash,
PatchSet: patch.PatchSet{
PatchFileId: patchFileId,
Summary: summaries,
},
}
if err = p.UpdateModulePatch(modulePatch); err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
as.WriteJSON(w, http.StatusOK, "Patch module updated")
}
// listPatches returns a user's "n" most recent patches.
func (as *APIServer) listPatches(w http.ResponseWriter, r *http.Request) {
dbUser := MustHaveUser(r)
n, err := util.GetIntValue(r, "n", 0)
if err != nil {
as.LoggedError(w, r, http.StatusBadRequest, errors.Wrap(err, "cannot read value n"))
return
}
query := patch.ByUser(dbUser.Id).Sort([]string{"-" + patch.CreateTimeKey})
if n > 0 {
query = query.Limit(n)
}
patches, err := patch.Find(query)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError,
errors.Wrapf(err, "error finding patches for user %s", dbUser.Id))
return
}
as.WriteJSON(w, http.StatusOK, patches)
}
func (as *APIServer) existingPatchRequest(w http.ResponseWriter, r *http.Request) {
dbUser := MustHaveUser(r)
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
p, err := getPatchFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
var action, desc string
if r.Header.Get("Content-Type") == formMimeType {
action = r.FormValue("action")
} else {
data := struct {
PatchId string `json:"patch_id"`
Action string `json:"action"`
Description string `json:"description"`
}{}
if err = util.ReadJSONInto(util.NewRequestReader(r), &data); err != nil {
as.LoggedError(w, r, http.StatusBadRequest, err)
return
}
action, desc = data.Action, data.Description
}
// dispatch to handlers based on specified action
switch action {
case "update":
err = p.SetDescription(desc)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
as.WriteJSON(w, http.StatusOK, "patch updated")
case "finalize":
var githubOauthToken string
githubOauthToken, err = as.Settings.GetGithubOauthToken()
if err != nil {
as.WriteJSON(w, http.StatusInternalServerError, err)
return
}
if p.Activated {
http.Error(w, "patch is already finalized", http.StatusBadRequest)
return
}
patchedProject, err := validator.GetPatchedProject(ctx, p, githubOauthToken)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
projectYamlBytes, err := yaml.Marshal(patchedProject)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "error marshaling patched config"))
return
}
p.PatchedConfig = string(projectYamlBytes)
_, err = model.FinalizePatch(ctx, p, evergreen.PatchVersionRequester, githubOauthToken)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
as.WriteJSON(w, http.StatusOK, "patch finalized")
case "cancel":
err = model.CancelPatch(p, dbUser.Id)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
as.WriteJSON(w, http.StatusOK, "patch deleted")
default:
http.Error(w, fmt.Sprintf("Unrecognized action: %v", action), http.StatusBadRequest)
}
}
func (as *APIServer) summarizePatch(w http.ResponseWriter, r *http.Request) {
p, err := getPatchFromRequest(r)
if err != nil {
http.Error(w, "not found", http.StatusNotFound)
return
}
as.WriteJSON(w, http.StatusOK, PatchAPIResponse{Patch: p})
}
func (as *APIServer) listPatchModules(w http.ResponseWriter, r *http.Request) {
_, project := MustHaveProject(r)
p, err := getPatchFromRequest(r)
if err != nil {
http.Error(w, "not found", http.StatusNotFound)
return
}
data := struct {
Project string `json:"project"`
Modules []string `json:"modules"`
}{
Project: project.Identifier,
}
mods := map[string]struct{}{}
for _, m := range project.Modules {
if m.Name == "" {
continue
}
mods[m.Name] = struct{}{}
}
for _, m := range p.Patches {
mods[m.ModuleName] = struct{}{}
}
for m := range mods {
data.Modules = append(data.Modules, m)
}
as.WriteJSON(w, http.StatusOK, &data)
}
func (as *APIServer) deletePatchModule(w http.ResponseWriter, r *http.Request) {
p, err := getPatchFromRequest(r)
if err != nil {
as.LoggedError(w, r, http.StatusBadRequest, err)
return
}
moduleName := r.FormValue("module")
if moduleName == "" {
as.WriteJSON(w, http.StatusBadRequest, "You must specify a module to delete")
return
}
// don't mess with already finalized requests
if p.Activated {
response := fmt.Sprintf("Can't delete module - path already finalized")
as.WriteJSON(w, http.StatusBadRequest, response)
return
}
err = p.RemoveModulePatch(moduleName)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
as.WriteJSON(w, http.StatusOK, PatchAPIResponse{Message: "module removed from patch."})
}