-
Notifications
You must be signed in to change notification settings - Fork 124
/
api_patch.go
576 lines (520 loc) · 18.2 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package service
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/db"
mgobson "github.com/evergreen-ci/evergreen/db/mgo/bson"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/model/user"
"github.com/evergreen-ci/evergreen/thirdparty"
"github.com/evergreen-ci/evergreen/units"
"github.com/evergreen-ci/evergreen/util"
"github.com/evergreen-ci/gimlet"
"github.com/evergreen-ci/utility"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
)
const formMimeType = "application/x-www-form-urlencoded"
var cliOutOfDateError = errors.New("CLI is out of date: use 'evergreen get-update --install'")
// 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"`
}
// getAuthor returns the author for the patch. If githubAuthor or patchAuthor is provided and exists, will use that
// author instead of the submitter if the submitter is authorized to submit patches on behalf of users.
// Returns the author, status code, and error.
func (as *APIServer) getAuthor(data patchData, dbUser *user.DBUser, projectId, patchID string) (string, int, error) {
author := dbUser.Id
if data.GithubAuthor == "" && data.PatchAuthor == "" {
return author, http.StatusOK, nil
}
opts := gimlet.PermissionOpts{
Resource: projectId,
ResourceType: evergreen.ProjectResourceType,
Permission: evergreen.PermissionPatches,
RequiredLevel: evergreen.PatchSubmitAdmin.Value,
}
if !dbUser.HasPermission(opts) {
return "", http.StatusUnauthorized, errors.New("user is not authorized to patch on behalf of other users")
}
if data.GithubAuthor != "" {
specifiedUser, err := user.FindByGithubName(data.GithubAuthor)
if err != nil {
return "", http.StatusInternalServerError, errors.Wrapf(err, "error looking for github author '%s'", data.GithubAuthor)
}
if specifiedUser != nil {
grip.Info(message.Fields{
"message": "overriding patch author as specified by the submitter",
"submitter": dbUser.Id,
"new_author": specifiedUser.Id,
"given_github_username": data.GithubAuthor,
"patch_id": patchID,
})
author = specifiedUser.Id
}
grip.DebugWhen(specifiedUser == nil, message.Fields{
"message": "github user not found",
"github_username": data.GithubAuthor,
"patch_id": patchID,
})
} else if data.PatchAuthor != "" {
specifiedUser, err := user.FindOneById(data.PatchAuthor)
if err != nil {
return "", http.StatusInternalServerError, errors.Wrapf(err, "error looking for author '%s'", data.PatchAuthor)
}
if specifiedUser != nil {
grip.Info(message.Fields{
"message": "overriding patch author as specified by the submitter",
"submitter": dbUser.Id,
"new_author": data.PatchAuthor,
"patch_id": patchID,
})
author = specifiedUser.Id
}
grip.DebugWhen(specifiedUser == nil, message.Fields{
"message": "patch user not found",
"username": data.PatchAuthor,
"patch_id": patchID,
})
}
return author, http.StatusOK, nil
}
type patchData struct {
Description string `json:"desc"`
Path string `json:"path"`
Project string `json:"project"`
BackportInfo patch.BackportInfo `json:"backport_info"`
GitMetadata *patch.GitMetadata `json:"git_metadata"`
PatchBytes []byte `json:"patch_bytes"`
Githash string `json:"githash"`
Parameters []patch.Parameter `json:"parameters"`
Variants []string `json:"buildvariants_new"`
Tasks []string `json:"tasks"`
RegexVariants []string `json:"regex_buildvariants"`
RegexTasks []string `json:"regex_tasks"`
SyncBuildVariants []string `json:"sync_build_variants"`
SyncTasks []string `json:"sync_tasks"`
SyncStatuses []string `json:"sync_statuses"`
SyncTimeout time.Duration `json:"sync_timeout"`
Finalize bool `json:"finalize"`
TriggerAliases []string `json:"trigger_aliases"`
Alias string `json:"alias"`
RepeatFailed bool `json:"repeat_failed"`
RepeatDefinition bool `json:"reuse_definition"`
RepeatPatchId string `json:"repeat_patch_id"`
GithubAuthor string `json:"github_author"`
PatchAuthor string `json:"patch_author"`
}
// 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 := patchData{}
if err := utility.ReadJSON(utility.NewRequestReaderWithSize(r, patch.SizeLimit), &data); err != nil {
as.LoggedError(w, r, http.StatusBadRequest, err)
return
}
pref, err := model.FindMergedProjectRef(data.Project, "", true)
if err != nil {
as.LoggedError(w, r, http.StatusBadRequest, errors.Wrapf(err, "project '%s' is not specified", data.Project))
return
}
if pref == nil {
gimlet.WriteJSONResponse(w, http.StatusNotFound,
gimlet.ErrorResponse{
StatusCode: http.StatusNotFound,
Message: fmt.Sprintf("project '%s' is not found", data.Project),
})
return
}
hasPermission := dbUser.HasPermission(gimlet.PermissionOpts{
Resource: pref.Id,
ResourceType: evergreen.ProjectResourceType,
Permission: evergreen.PermissionPatches,
RequiredLevel: evergreen.PatchSubmit.Value,
})
if !hasPermission {
as.LoggedError(w, r, http.StatusUnauthorized, errors.Errorf("not authorized to patch for project '%s'", data.Project))
return
}
patchString := string(data.PatchBytes)
if len(patchString) > patch.SizeLimit {
as.LoggedError(w, r, http.StatusBadRequest, errors.New("Patch is too large"))
return
}
if data.Alias == evergreen.CommitQueueAlias && len(patchString) != 0 && !patch.IsMailboxDiff(patchString) {
as.LoggedError(w, r, http.StatusBadRequest, cliOutOfDateError)
return
}
if pref.IsPatchingDisabled() || !pref.Enabled {
as.LoggedError(w, r, http.StatusBadRequest, errors.New("patching is disabled"))
return
}
if !pref.TaskSync.IsPatchEnabled() && (len(data.SyncTasks) != 0 || len(data.SyncBuildVariants) != 0) {
as.LoggedError(w, r, http.StatusBadRequest, errors.New("task sync at the end of a patched task is disabled by project settings"))
return
}
patchID := mgobson.NewObjectId()
author, statusCode, err := as.getAuthor(data, dbUser, pref.Id, patchID.Hex())
if err != nil {
as.LoggedError(w, r, statusCode, err)
return
}
intent, err := patch.NewCliIntent(patch.CLIIntentParams{
User: author,
Project: pref.Id,
Path: data.Path,
BaseGitHash: data.Githash,
Module: r.FormValue("module"),
PatchContent: patchString,
Description: data.Description,
Finalize: data.Finalize,
Parameters: data.Parameters,
Variants: data.Variants,
Tasks: data.Tasks,
RegexVariants: data.RegexVariants,
RegexTasks: data.RegexTasks,
Alias: data.Alias,
TriggerAliases: data.TriggerAliases,
BackportOf: data.BackportInfo,
GitInfo: data.GitMetadata,
RepeatDefinition: data.RepeatDefinition,
RepeatFailed: data.RepeatFailed,
RepeatPatchId: data.RepeatPatchId,
SyncParams: patch.SyncAtEndOptions{
BuildVariants: data.SyncBuildVariants,
Tasks: data.SyncTasks,
Statuses: data.SyncStatuses,
Timeout: data.SyncTimeout,
},
})
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
}
grip.Info(message.Fields{
"operation": "patch creation",
"message": "creating patch",
"from": "CLI",
"patch_id": patchID,
"finalizing": data.Finalize,
"variants": data.Variants,
"tasks": data.Tasks,
"alias": data.Alias,
})
job := units.NewPatchIntentProcessor(as.env, 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
}
gimlet.WriteJSONResponse(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.
patchIdStr := gimlet.GetVars(r)["patchId"]
if len(patchIdStr) == 0 {
return nil, errors.New("no patch id supplied")
}
// find the patch
existingPatch, err := patch.FindOneId(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) {
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
p, err := getPatchFromRequest(r)
if err != nil {
gimlet.WriteJSONError(w, err.Error())
return
}
if p.Version != "" && p.IsCommitQueuePatch() {
as.LoggedError(w, r, http.StatusBadRequest, errors.New("can't update modules for in-flight commit queue tests"))
return
}
data := struct {
Module string `json:"module"`
PatchBytes []byte `json:"patch_bytes"`
Githash string `json:"githash"`
}{}
if err = utility.ReadJSON(utility.NewRequestReader(r), &data); err != nil {
as.LoggedError(w, r, http.StatusBadRequest, err)
return
}
patchContent := string(data.PatchBytes)
if p.IsCommitQueuePatch() && len(patchContent) != 0 && !patch.IsMailboxDiff(patchContent) {
as.LoggedError(w, r, http.StatusBadRequest, errors.New("You may be using 'set-module' instead of 'commit-queue set-module', or your CLI may be out of date.\n"+
"Please update your CLI if it is not up to date, and use 'commit-queue set-module' instead of 'set-module' for commit queue patches."))
return
}
moduleName, githash := data.Module, data.Githash
var summaries []thirdparty.Summary
var commitMessages []string
if patch.IsMailboxDiff(patchContent) {
summaries, commitMessages, err = thirdparty.GetPatchSummariesFromMboxPatch(patchContent)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, errors.Errorf("Error getting summaries by commit"))
return
}
} else {
summaries, err = thirdparty.GetPatchSummaries(patchContent)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
}
// write the patch content into a GridFS file under a new ObjectId.
patchFileId := mgobson.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,
IsMbox: len(patchContent) == 0 || patch.IsMailboxDiff(patchContent),
PatchSet: patch.PatchSet{
PatchFileId: patchFileId,
Summary: summaries,
CommitMessages: commitMessages,
},
}
if err = p.UpdateModulePatch(modulePatch); err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
if p.IsCommitQueuePatch() {
projectRef, err := model.FindBranchProjectRef(p.Project)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, errors.Wrapf(err, "Error getting project ref with id %v", p.Project))
return
}
proj, _, err := model.FindAndTranslateProjectForPatch(ctx, &as.Settings, p)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, errors.Errorf("finding project for patch '%s'", p.Id))
}
if err = p.SetDescription(model.MakeCommitQueueDescription(p.Patches, projectRef, proj, p.IsGithubMergePatch(), p.GithubMergeData)); err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
}
gimlet.WriteJSON(w, "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
}
filterCommitQueue := r.FormValue("filter_commit_queue") == "true"
query := patch.ByUserAndCommitQueue(dbUser.Id, filterCommitQueue).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
}
gimlet.WriteJSON(w, 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 = utility.ReadJSON(utility.NewRequestReader(r), &data); err != nil {
as.LoggedError(w, r, http.StatusBadRequest, err)
return
}
action, desc = data.Action, data.Description
}
if p.IsCommitQueuePatch() {
as.LoggedError(w, r, http.StatusBadRequest, errors.New("can't modify a commit queue patch"))
return
}
// 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
}
gimlet.WriteJSON(w, "patch updated")
case "finalize":
var githubOauthToken string
githubOauthToken, err = as.Settings.GetGithubOauthToken()
if err != nil {
gimlet.WriteJSONInternalError(w, err)
return
}
if p.Activated {
http.Error(w, "patch is already finalized", http.StatusBadRequest)
return
}
if p.ProjectStorageMethod != "" {
// New patches already create the parser project at the same time as
// the patch, so there's no need to get the patched parser project
// for them.
projectConfig, err := model.GetPatchedProjectConfig(ctx, &as.Settings, p, githubOauthToken)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
p.PatchedProjectConfig = projectConfig
} else {
// In the fallback case, old unfinalized patches had their parser
// projects stored as a string, so it gets stored here.
_, patchConfig, err := model.GetPatchedProject(ctx, &as.Settings, p, githubOauthToken)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
p.PatchedProjectConfig = patchConfig.PatchedProjectConfig
}
_, err = model.FinalizePatch(ctx, p, evergreen.PatchVersionRequester, githubOauthToken)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
grip.Info(message.Fields{
"operation": "patch creation",
"message": "finalized patch",
"from": "CLI",
"patch_id": p.Id,
"variants": p.BuildVariants,
"tasks": p.Tasks,
"variant_tasks": p.VariantsTasks,
"alias": p.Alias,
})
gimlet.WriteJSON(w, "patch finalized")
case "cancel":
err = model.CancelPatch(ctx, p, task.AbortInfo{User: dbUser.Id})
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
gimlet.WriteJSON(w, "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
}
gimlet.WriteJSON(w, 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
}
projectName := project.Identifier // this might be the ID, so use identifier if we can
identifier, _ := model.GetIdentifierForProject(project.Identifier)
if identifier != "" {
projectName = identifier
}
data := struct {
Project string `json:"project"`
Modules []string `json:"modules"`
}{
Project: projectName,
}
mods := map[string]struct{}{}
for _, m := range project.Modules {
if m.Name == "" {
continue
}
mods[m.Name] = struct{}{}
}
for _, m := range p.Patches {
if m.ModuleName == "" {
continue
}
mods[m.ModuleName] = struct{}{}
}
for m := range mods {
data.Modules = append(data.Modules, m)
}
gimlet.WriteJSON(w, &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 == "" {
gimlet.WriteJSONError(w, "You must specify a module to delete")
return
}
// don't mess with already finalized requests
if p.Activated {
response := "Can't delete module - path already finalized"
gimlet.WriteJSONError(w, response)
return
}
err = p.RemoveModulePatch(moduleName)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
gimlet.WriteJSON(w, PatchAPIResponse{Message: "module removed from patch."})
}