-
Notifications
You must be signed in to change notification settings - Fork 124
/
mutation_resolver.go
1437 lines (1309 loc) · 58.4 KB
/
mutation_resolver.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package graphql
import (
"context"
"fmt"
"net/http"
"net/url"
"runtime/debug"
"sort"
"time"
"github.com/99designs/gqlgen/graphql"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/api"
"github.com/evergreen-ci/evergreen/cloud"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/annotations"
"github.com/evergreen-ci/evergreen/model/build"
"github.com/evergreen-ci/evergreen/model/commitqueue"
"github.com/evergreen-ci/evergreen/model/distro"
"github.com/evergreen-ci/evergreen/model/event"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/model/parsley"
"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/rest/data"
restModel "github.com/evergreen-ci/evergreen/rest/model"
"github.com/evergreen-ci/evergreen/units"
"github.com/evergreen-ci/evergreen/util"
"github.com/evergreen-ci/evergreen/validator"
"github.com/evergreen-ci/gimlet"
"github.com/evergreen-ci/utility"
"github.com/mongodb/amboy"
adb "github.com/mongodb/anser/db"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
werrors "github.com/pkg/errors"
)
// BbCreateTicket is the resolver for the bbCreateTicket field.
func (r *mutationResolver) BbCreateTicket(ctx context.Context, taskID string, execution *int) (bool, error) {
err := annotationPermissionHelper(ctx, taskID, execution)
if err != nil {
return false, err
}
httpStatus, err := data.BbFileTicket(ctx, taskID, *execution)
if err != nil {
return false, mapHTTPStatusToGqlError(ctx, httpStatus, err)
}
return true, nil
}
// AddAnnotationIssue is the resolver for the addAnnotationIssue field.
func (r *mutationResolver) AddAnnotationIssue(ctx context.Context, taskID string, execution int, apiIssue restModel.APIIssueLink, isIssue bool) (bool, error) {
err := annotationPermissionHelper(ctx, taskID, utility.ToIntPtr(execution))
if err != nil {
return false, err
}
usr := mustHaveUser(ctx)
issue := restModel.APIIssueLinkToService(apiIssue)
if err := util.CheckURL(issue.URL); err != nil {
return false, InputValidationError.Send(ctx, fmt.Sprintf("issue does not have valid URL: %s", err.Error()))
}
if isIssue {
if err := task.AddIssueToAnnotation(taskID, execution, *issue, usr.Username()); err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("couldn't add issue: %s", err.Error()))
}
return true, nil
} else {
if err := annotations.AddSuspectedIssueToAnnotation(taskID, execution, *issue, usr.Username()); err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("couldn't add suspected issue: %s", err.Error()))
}
return true, nil
}
}
// EditAnnotationNote is the resolver for the editAnnotationNote field.
func (r *mutationResolver) EditAnnotationNote(ctx context.Context, taskID string, execution int, originalMessage string, newMessage string) (bool, error) {
err := annotationPermissionHelper(ctx, taskID, utility.ToIntPtr(execution))
if err != nil {
return false, err
}
usr := mustHaveUser(ctx)
if err := annotations.UpdateAnnotationNote(taskID, execution, originalMessage, newMessage, usr.Username()); err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("couldn't update note: %s", err.Error()))
}
return true, nil
}
// MoveAnnotationIssue is the resolver for the moveAnnotationIssue field.
func (r *mutationResolver) MoveAnnotationIssue(ctx context.Context, taskID string, execution int, apiIssue restModel.APIIssueLink, isIssue bool) (bool, error) {
err := annotationPermissionHelper(ctx, taskID, utility.ToIntPtr(execution))
if err != nil {
return false, err
}
usr := mustHaveUser(ctx)
issue := restModel.APIIssueLinkToService(apiIssue)
if isIssue {
if err := task.MoveIssueToSuspectedIssue(taskID, execution, *issue, usr.Username()); err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("couldn't move issue to suspected issues: %s", err.Error()))
}
return true, nil
} else {
if err := task.MoveSuspectedIssueToIssue(taskID, execution, *issue, usr.Username()); err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("couldn't move issue to suspected issues: %s", err.Error()))
}
return true, nil
}
}
// RemoveAnnotationIssue is the resolver for the removeAnnotationIssue field.
func (r *mutationResolver) RemoveAnnotationIssue(ctx context.Context, taskID string, execution int, apiIssue restModel.APIIssueLink, isIssue bool) (bool, error) {
err := annotationPermissionHelper(ctx, taskID, utility.ToIntPtr(execution))
if err != nil {
return false, err
}
issue := restModel.APIIssueLinkToService(apiIssue)
if isIssue {
if err := task.RemoveIssueFromAnnotation(taskID, execution, *issue); err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("couldn't delete issue: %s", err.Error()))
}
return true, nil
} else {
if err := annotations.RemoveSuspectedIssueFromAnnotation(taskID, execution, *issue); err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("couldn't delete suspected issue: %s", err.Error()))
}
return true, nil
}
}
// SetAnnotationMetadataLinks is the resolver for the setAnnotationMetadataLinks field.
func (r *mutationResolver) SetAnnotationMetadataLinks(ctx context.Context, taskID string, execution int, metadataLinks []*restModel.APIMetadataLink) (bool, error) {
err := annotationPermissionHelper(ctx, taskID, utility.ToIntPtr(execution))
if err != nil {
return false, err
}
usr := mustHaveUser(ctx)
modelMetadataLinks := restModel.APIMetadataLinksToService(metadataLinks)
if err := annotations.ValidateMetadataLinks(modelMetadataLinks...); err != nil {
return false, InputValidationError.Send(ctx, fmt.Sprintf("invalid metadata link: %s", err.Error()))
}
if err := annotations.SetAnnotationMetadataLinks(ctx, taskID, execution, usr.Username(), modelMetadataLinks...); err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("couldn't add issue: %s", err.Error()))
}
return true, nil
}
// DeleteDistro is the resolver for the deleteDistro field.
func (r *mutationResolver) DeleteDistro(ctx context.Context, opts DeleteDistroInput) (*DeleteDistroPayload, error) {
usr := mustHaveUser(ctx)
if err := data.DeleteDistroById(ctx, usr, opts.DistroID); err != nil {
gimletErr, ok := err.(gimlet.ErrorResponse)
if ok {
return nil, mapHTTPStatusToGqlError(ctx, gimletErr.StatusCode, err)
}
return nil, InternalServerError.Send(ctx, fmt.Sprintf("deleting distro: %s", err.Error()))
}
return &DeleteDistroPayload{
DeletedDistroID: opts.DistroID,
}, nil
}
// CopyDistro is the resolver for the copyDistro field.
func (r *mutationResolver) CopyDistro(ctx context.Context, opts data.CopyDistroOpts) (*NewDistroPayload, error) {
usr := mustHaveUser(ctx)
if err := data.CopyDistro(ctx, usr, opts); err != nil {
gimletErr, ok := err.(gimlet.ErrorResponse)
if ok {
return nil, mapHTTPStatusToGqlError(ctx, gimletErr.StatusCode, err)
}
return nil, InternalServerError.Send(ctx, fmt.Sprintf("copying distro: %s", err.Error()))
}
return &NewDistroPayload{
NewDistroID: opts.NewDistroId,
}, nil
}
// CreateDistro is the resolver for the createDistro field.
func (r *mutationResolver) CreateDistro(ctx context.Context, opts CreateDistroInput) (*NewDistroPayload, error) {
usr := mustHaveUser(ctx)
if err := data.CreateDistro(ctx, usr, opts.NewDistroID); err != nil {
gimletErr, ok := err.(gimlet.ErrorResponse)
if ok {
return nil, mapHTTPStatusToGqlError(ctx, gimletErr.StatusCode, err)
}
return nil, InternalServerError.Send(ctx, fmt.Sprintf("creating distro: %s", err.Error()))
}
return &NewDistroPayload{
NewDistroID: opts.NewDistroID,
}, nil
}
// SaveDistro is the resolver for the saveDistro field. The entire distro object is provided as input (not just the updated fields) in order to validate all distro settings.
func (r *mutationResolver) SaveDistro(ctx context.Context, opts SaveDistroInput) (*SaveDistroPayload, error) {
usr := mustHaveUser(ctx)
d := opts.Distro.ToService()
oldDistro, err := distro.FindOneId(ctx, d.Id)
if err != nil || oldDistro == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("could not find distro '%s'", d.Id))
}
settings, err := evergreen.GetConfig(ctx)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("getting settings: %s", err.Error()))
}
validationErrs, err := validator.CheckDistro(ctx, d, settings, false)
if err != nil {
return nil, InternalServerError.Send(ctx, err.Error())
}
if len(validationErrs) != 0 {
return nil, InputValidationError.Send(ctx, fmt.Sprintf("validating changes for distro '%s': '%s'", d.Id, validationErrs.String()))
}
if err = data.UpdateDistro(ctx, oldDistro, d); err != nil {
gimletErr, ok := err.(gimlet.ErrorResponse)
if ok {
return nil, mapHTTPStatusToGqlError(ctx, gimletErr.StatusCode, err)
}
return nil, InternalServerError.Send(ctx, fmt.Sprintf("updating distro: %s", err.Error()))
}
event.LogDistroModified(d.Id, usr.Username(), oldDistro.DistroData(), d.DistroData())
// AMI events are not displayed in the event log, but are used by the backend to determine if hosts have become stale.
if d.GetDefaultAMI() != oldDistro.GetDefaultAMI() {
event.LogDistroAMIModified(d.Id, usr.Username())
}
numHostsUpdated, err := handleDistroOnSaveOperation(ctx, d.Id, opts.OnSave, usr.Username())
if err != nil {
graphql.AddError(ctx, PartialError.Send(ctx, err.Error()))
}
return &SaveDistroPayload{
Distro: opts.Distro,
HostCount: numHostsUpdated,
}, nil
}
// ReprovisionToNew is the resolver for the reprovisionToNew field.
func (r *mutationResolver) ReprovisionToNew(ctx context.Context, hostIds []string) (int, error) {
user := mustHaveUser(ctx)
hosts, permissions, httpStatus, err := api.GetHostsAndUserPermissions(ctx, user, hostIds)
if err != nil {
return 0, mapHTTPStatusToGqlError(ctx, httpStatus, err)
}
hostsUpdated, httpStatus, err := api.ModifyHostsWithPermissions(hosts, permissions, api.GetReprovisionToNewCallback(ctx, evergreen.GetEnvironment(), user.Username()))
if err != nil {
return 0, mapHTTPStatusToGqlError(ctx, httpStatus, werrors.Errorf("Error marking selected hosts as needing to reprovision: %s", err.Error()))
}
return hostsUpdated, nil
}
// RestartJasper is the resolver for the restartJasper field.
func (r *mutationResolver) RestartJasper(ctx context.Context, hostIds []string) (int, error) {
user := mustHaveUser(ctx)
hosts, permissions, httpStatus, err := api.GetHostsAndUserPermissions(ctx, user, hostIds)
if err != nil {
return 0, mapHTTPStatusToGqlError(ctx, httpStatus, err)
}
hostsUpdated, httpStatus, err := api.ModifyHostsWithPermissions(hosts, permissions, api.GetRestartJasperCallback(ctx, evergreen.GetEnvironment(), user.Username()))
if err != nil {
return 0, mapHTTPStatusToGqlError(ctx, httpStatus, werrors.Errorf("Error marking selected hosts as needing Jasper service restarted: %s", err.Error()))
}
return hostsUpdated, nil
}
// UpdateHostStatus is the resolver for the updateHostStatus field.
func (r *mutationResolver) UpdateHostStatus(ctx context.Context, hostIds []string, status string, notes *string) (int, error) {
user := mustHaveUser(ctx)
hosts, permissions, httpStatus, err := api.GetHostsAndUserPermissions(ctx, user, hostIds)
if err != nil {
return 0, mapHTTPStatusToGqlError(ctx, httpStatus, err)
}
rq := evergreen.GetEnvironment().RemoteQueue()
hostsUpdated, httpStatus, err := api.ModifyHostsWithPermissions(hosts, permissions, api.GetUpdateHostStatusCallback(ctx, evergreen.GetEnvironment(), rq, status, *notes, user))
if err != nil {
return 0, mapHTTPStatusToGqlError(ctx, httpStatus, err)
}
return hostsUpdated, nil
}
// EnqueuePatch is the resolver for the enqueuePatch field.
func (r *mutationResolver) EnqueuePatch(ctx context.Context, patchID string, commitMessage *string) (*restModel.APIPatch, error) {
user := mustHaveUser(ctx)
existingPatch, err := data.FindPatchById(patchID)
if err != nil {
gimletErr, ok := err.(gimlet.ErrorResponse)
if ok {
return nil, mapHTTPStatusToGqlError(ctx, gimletErr.StatusCode, err)
}
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error getting patch '%s'", patchID))
}
projectID := utility.FromStringPtr(existingPatch.ProjectId)
proj, err := data.FindProjectById(projectID, false, false)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error getting project '%s': %s", projectID, err.Error()))
}
if proj == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("project '%s' not found", projectID))
}
if proj.CommitQueue.MergeQueue == model.MergeQueueGitHub {
return nil, Forbidden.Send(ctx, "Can't enqueue patches for projects with GitHub merge queue. Click the merge button on the PR instead.")
}
patch, err := existingPatch.ToService()
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("converting APIPatch to patch '%s'", patchID))
}
if !userCanModifyPatch(user, patch) {
return nil, Forbidden.Send(ctx, "can't enqueue another user's patch")
}
if commitMessage == nil {
commitMessage = existingPatch.Description
}
if utility.FromStringPtr(existingPatch.Requester) == evergreen.GithubPRRequester {
info := commitqueue.EnqueuePRInfo{
PR: existingPatch.GithubPatchData.PRNumber,
Repo: utility.FromStringPtr(existingPatch.GithubPatchData.BaseRepo),
Owner: utility.FromStringPtr(existingPatch.GithubPatchData.BaseOwner),
CommitMessage: utility.FromStringPtr(commitMessage),
Username: utility.FromStringPtr(existingPatch.GithubPatchData.Author),
}
newPatch, err := data.EnqueuePRToCommitQueue(ctx, evergreen.GetEnvironment(), r.sc, info)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("enqueueing patch '%s': %s", patchID, err.Error()))
}
return newPatch, nil
}
newPatch, err := data.CreatePatchForMerge(ctx, evergreen.GetEnvironment().Settings(), patchID, utility.FromStringPtr(commitMessage))
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error creating new patch: %s", err.Error()))
}
item := restModel.APICommitQueueItem{
Issue: newPatch.Id,
PatchId: newPatch.Id,
Source: utility.ToStringPtr(commitqueue.SourceDiff)}
_, err = data.EnqueueItem(utility.FromStringPtr(newPatch.ProjectId), item, false)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error enqueuing new patch: %s", err.Error()))
}
return newPatch, nil
}
// SetPatchVisibility is the resolver for the setPatchVisibility field.
func (r *mutationResolver) SetPatchVisibility(ctx context.Context, patchIds []string, hidden bool) ([]*restModel.APIPatch, error) {
user := mustHaveUser(ctx)
updatedPatches := []*restModel.APIPatch{}
patches, err := patch.Find(patch.ByStringIds(patchIds))
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error occurred fetching patches '%s': %s", patchIds, err.Error()))
}
for _, p := range patches {
if !userCanModifyPatch(user, p) {
return nil, Forbidden.Send(ctx, fmt.Sprintf("not authorized to change patch '%s' visibility", p.Id))
}
err = p.SetPatchVisibility(hidden)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error occurred setting patch '%s' visibility: %s", p.Id, err.Error()))
}
apiPatch := restModel.APIPatch{}
err = apiPatch.BuildFromService(p, &restModel.APIPatchArgs{IncludeProjectIdentifier: true})
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error occurred building patch '%s' API model: %s", p.Id, err.Error()))
}
updatedPatches = append(updatedPatches, &apiPatch)
}
return updatedPatches, nil
}
// SchedulePatch is the resolver for the schedulePatch field.
func (r *mutationResolver) SchedulePatch(ctx context.Context, patchID string, configure PatchConfigure) (*restModel.APIPatch, error) {
patchUpdateReq := buildFromGqlInput(configure)
usr := mustHaveUser(ctx)
patchUpdateReq.Caller = usr.Id
version, err := model.VersionFindOneId(patchID)
if err != nil && !adb.ResultsNotFound(err) {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error occurred fetching patch '%s': %s", patchID, err.Error()))
}
statusCode, err := units.SchedulePatch(ctx, evergreen.GetEnvironment(), patchID, version, patchUpdateReq)
if err != nil {
return nil, mapHTTPStatusToGqlError(ctx, statusCode, werrors.Errorf("Error scheduling patch '%s': %s", patchID, err.Error()))
}
scheduledPatch, err := data.FindPatchById(patchID)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error getting scheduled patch '%s': %s", patchID, err))
}
return scheduledPatch, nil
}
// AttachProjectToNewRepo is the resolver for the attachProjectToNewRepo field.
func (r *mutationResolver) AttachProjectToNewRepo(ctx context.Context, project MoveProjectInput) (*restModel.APIProjectRef, error) {
usr := mustHaveUser(ctx)
pRef, err := data.FindProjectById(project.ProjectID, false, false)
if err != nil || pRef == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("Could not find project: %s : %s", project.ProjectID, err.Error()))
}
pRef.Owner = project.NewOwner
pRef.Repo = project.NewRepo
if err = pRef.AttachToNewRepo(usr); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error updating owner/repo: %s", err.Error()))
}
res := &restModel.APIProjectRef{}
if err = res.BuildFromService(*pRef); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error building APIProjectRef: %s", err.Error()))
}
return res, nil
}
// AttachProjectToRepo is the resolver for the attachProjectToRepo field.
func (r *mutationResolver) AttachProjectToRepo(ctx context.Context, projectID string) (*restModel.APIProjectRef, error) {
usr := mustHaveUser(ctx)
pRef, err := data.FindProjectById(projectID, false, false)
if err != nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("error finding project %s: %s", projectID, err.Error()))
}
if pRef == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("cannot find project %s", projectID))
}
if err = pRef.AttachToRepo(ctx, usr); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error attaching to repo: %s", err.Error()))
}
res := &restModel.APIProjectRef{}
if err := res.BuildFromService(*pRef); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error building project from service: %s", err.Error()))
}
return res, nil
}
// CreateProject is the resolver for the createProject field.
func (r *mutationResolver) CreateProject(ctx context.Context, project restModel.APIProjectRef, requestS3Creds *bool) (*restModel.APIProjectRef, error) {
dbProjectRef, err := project.ToService()
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error converting project ref to service model: %s", err.Error()))
}
u := gimlet.GetUser(ctx).(*user.DBUser)
if created, err := data.CreateProject(ctx, evergreen.GetEnvironment(), dbProjectRef, u); err != nil {
if !created {
apiErr, ok := err.(gimlet.ErrorResponse)
if ok {
if apiErr.StatusCode == http.StatusBadRequest {
return nil, InputValidationError.Send(ctx, apiErr.Message)
}
}
return nil, InternalServerError.Send(ctx, err.Error())
}
graphql.AddError(ctx, PartialError.Send(ctx, err.Error()))
}
projectRef, err := model.FindBranchProjectRef(*project.Identifier)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error looking in project collection: %s", err.Error()))
}
if projectRef == nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error finding project: %s", err.Error()))
}
apiProjectRef := restModel.APIProjectRef{}
if err = apiProjectRef.BuildFromService(*projectRef); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error building APIProjectRef from service: %s", err.Error()))
}
if utility.FromBoolPtr(requestS3Creds) {
if err = data.RequestS3Creds(ctx, *apiProjectRef.Identifier, u.EmailAddress); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error creating jira ticket to request S3 credentials: %s", err.Error()))
}
}
return &apiProjectRef, nil
}
// CopyProject is the resolver for the copyProject field.
func (r *mutationResolver) CopyProject(ctx context.Context, project data.CopyProjectOpts, requestS3Creds *bool) (*restModel.APIProjectRef, error) {
projectRef, err := data.CopyProject(ctx, evergreen.GetEnvironment(), project)
if projectRef == nil && err != nil {
apiErr, ok := err.(gimlet.ErrorResponse) // make sure bad request errors are handled correctly; all else should be treated as internal server error
if ok {
if apiErr.StatusCode == http.StatusBadRequest {
return nil, InputValidationError.Send(ctx, apiErr.Message)
}
// StatusNotFound and other error codes are really internal errors bc we determine this input
return nil, InternalServerError.Send(ctx, apiErr.Message)
}
return nil, InternalServerError.Send(ctx, err.Error())
}
if err != nil {
// Use AddError to bypass gqlgen restriction that data and errors cannot be returned in the same response
// https://github.com/99designs/gqlgen/issues/1191
graphql.AddError(ctx, PartialError.Send(ctx, err.Error()))
}
if utility.FromBoolPtr(requestS3Creds) {
usr := mustHaveUser(ctx)
if err = data.RequestS3Creds(ctx, *projectRef.Identifier, usr.EmailAddress); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error creating jira ticket to request AWS access: %s", err.Error()))
}
}
return projectRef, nil
}
// DeactivateStepbackTask is the resolver for the deactivateStepbackTask field.
func (r *mutationResolver) DeactivateStepbackTask(ctx context.Context, projectID string, buildVariantName string, taskName string) (bool, error) {
usr := mustHaveUser(ctx)
if err := task.DeactivateStepbackTask(projectID, buildVariantName, taskName, usr.Username()); err != nil {
return false, InternalServerError.Send(ctx, err.Error())
}
return true, nil
}
// DefaultSectionToRepo is the resolver for the defaultSectionToRepo field.
func (r *mutationResolver) DefaultSectionToRepo(ctx context.Context, projectID string, section ProjectSettingsSection) (*string, error) {
usr := mustHaveUser(ctx)
if err := model.DefaultSectionToRepo(projectID, model.ProjectPageSection(section), usr.Username()); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error defaulting to repo for section: %s", err.Error()))
}
return &projectID, nil
}
// DeleteProject is the resolver for the deleteProject field.
func (r *mutationResolver) DeleteProject(ctx context.Context, projectID string) (bool, error) {
if err := data.HideBranch(projectID); err != nil {
gimletErr, ok := err.(gimlet.ErrorResponse)
if ok {
return false, mapHTTPStatusToGqlError(ctx, gimletErr.StatusCode, err)
}
return false, InternalServerError.Send(ctx, fmt.Sprintf("deleting project '%s': %s", projectID, err.Error()))
}
return true, nil
}
// DetachProjectFromRepo is the resolver for the detachProjectFromRepo field.
func (r *mutationResolver) DetachProjectFromRepo(ctx context.Context, projectID string) (*restModel.APIProjectRef, error) {
usr := mustHaveUser(ctx)
pRef, err := data.FindProjectById(projectID, false, false)
if err != nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("error finding project %s: %s", projectID, err.Error()))
}
if pRef == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("cannot find project %s", projectID))
}
if err = pRef.DetachFromRepo(usr); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error detaching from repo: %s", err.Error()))
}
res := &restModel.APIProjectRef{}
if err := res.BuildFromService(*pRef); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error building project from service: %s", err.Error()))
}
return res, nil
}
// ForceRepotrackerRun is the resolver for the forceRepotrackerRun field.
func (r *mutationResolver) ForceRepotrackerRun(ctx context.Context, projectID string) (bool, error) {
ts := utility.RoundPartOfHour(1).Format(units.TSFormat)
j := units.NewRepotrackerJob(fmt.Sprintf("catchup-%s", ts), projectID)
if err := amboy.EnqueueUniqueJob(ctx, evergreen.GetEnvironment().RemoteQueue(), j); err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("error creating Repotracker job: %s", err.Error()))
}
return true, nil
}
// PromoteVarsToRepo is the resolver for the promoteVarsToRepo field.
func (r *mutationResolver) PromoteVarsToRepo(ctx context.Context, projectID string, varNames []string) (bool, error) {
usr := mustHaveUser(ctx)
if err := data.PromoteVarsToRepo(projectID, varNames, usr.Username()); err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("promoting variables to repo for project '%s': %s", projectID, err.Error()))
}
return true, nil
}
// SaveProjectSettingsForSection is the resolver for the saveProjectSettingsForSection field.
func (r *mutationResolver) SaveProjectSettingsForSection(ctx context.Context, projectSettings *restModel.APIProjectSettings, section ProjectSettingsSection) (*restModel.APIProjectSettings, error) {
projectId := utility.FromStringPtr(projectSettings.ProjectRef.Id)
usr := mustHaveUser(ctx)
changes, err := data.SaveProjectSettingsForSection(ctx, projectId, projectSettings, model.ProjectPageSection(section), false, usr.Username())
if err != nil {
return nil, InternalServerError.Send(ctx, err.Error())
}
return changes, nil
}
// SaveRepoSettingsForSection is the resolver for the saveRepoSettingsForSection field.
func (r *mutationResolver) SaveRepoSettingsForSection(ctx context.Context, repoSettings *restModel.APIProjectSettings, section ProjectSettingsSection) (*restModel.APIProjectSettings, error) {
projectId := utility.FromStringPtr(repoSettings.ProjectRef.Id)
usr := mustHaveUser(ctx)
changes, err := data.SaveProjectSettingsForSection(ctx, projectId, repoSettings, model.ProjectPageSection(section), true, usr.Username())
if err != nil {
return nil, InternalServerError.Send(ctx, err.Error())
}
return changes, nil
}
// SetLastRevision is the resolver for the setLastRevision field.
func (r *mutationResolver) SetLastRevision(ctx context.Context, opts SetLastRevisionInput) (*SetLastRevisionPayload, error) {
if len(opts.Revision) < gitHashLength {
return nil, InputValidationError.Send(ctx, fmt.Sprintf("insufficient length: must provide %d characters for revision", gitHashLength))
}
project, err := model.FindBranchProjectRef(opts.ProjectIdentifier)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("finding project '%s': %s", opts.ProjectIdentifier, err.Error()))
}
if project == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("project '%s' not found", opts.ProjectIdentifier))
}
if err = model.UpdateLastRevision(project.Id, opts.Revision); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("updating last revision for '%s': %s", opts.ProjectIdentifier, err.Error()))
}
if err = project.SetRepotrackerError(&model.RepositoryErrorDetails{}); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("clearing repotracker error for '%s': %s", opts.ProjectIdentifier, err.Error()))
}
// Run repotracker job because the last revision for the project has been updated.
ts := utility.RoundPartOfHour(1).Format(units.TSFormat)
j := units.NewRepotrackerJob(fmt.Sprintf("catchup-%s", ts), project.Id)
if err = amboy.EnqueueUniqueJob(ctx, evergreen.GetEnvironment().RemoteQueue(), j); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("creating Repotracker catchup job: %s", err.Error()))
}
return &SetLastRevisionPayload{
MergeBaseRevision: opts.Revision,
}, nil
}
// AttachVolumeToHost is the resolver for the attachVolumeToHost field.
func (r *mutationResolver) AttachVolumeToHost(ctx context.Context, volumeAndHost VolumeHost) (bool, error) {
statusCode, err := cloud.AttachVolume(ctx, volumeAndHost.VolumeID, volumeAndHost.HostID)
if err != nil {
return false, mapHTTPStatusToGqlError(ctx, statusCode, err)
}
return statusCode == http.StatusOK, nil
}
// DetachVolumeFromHost is the resolver for the detachVolumeFromHost field.
func (r *mutationResolver) DetachVolumeFromHost(ctx context.Context, volumeID string) (bool, error) {
statusCode, err := cloud.DetachVolume(ctx, volumeID)
if err != nil {
return false, mapHTTPStatusToGqlError(ctx, statusCode, err)
}
return statusCode == http.StatusOK, nil
}
// EditSpawnHost is the resolver for the editSpawnHost field.
func (r *mutationResolver) EditSpawnHost(ctx context.Context, spawnHost *EditSpawnHostInput) (*restModel.APIHost, error) {
var v *host.Volume
usr := mustHaveUser(ctx)
h, err := host.FindOneByIdOrTag(ctx, spawnHost.HostID)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error finding host by id: %s", err))
}
if h == nil {
return nil, ResourceNotFound.Send(ctx, "Host not found")
}
if !host.CanUpdateSpawnHost(h, usr) {
return nil, Forbidden.Send(ctx, "You are not authorized to modify this host")
}
opts := host.HostModifyOptions{}
if spawnHost.DisplayName != nil {
opts.NewName = *spawnHost.DisplayName
}
if spawnHost.NoExpiration != nil {
opts.NoExpiration = spawnHost.NoExpiration
}
if spawnHost.Expiration != nil {
opts.AddHours = (*spawnHost.Expiration).Sub(h.ExpirationTime)
}
if spawnHost.InstanceType != nil {
var config *evergreen.Settings
config, err = evergreen.GetConfig(ctx)
if err != nil {
return nil, InternalServerError.Send(ctx, "unable to retrieve server config")
}
allowedTypes := config.Providers.AWS.AllowedInstanceTypes
err = cloud.CheckInstanceTypeValid(ctx, h.Distro, *spawnHost.InstanceType, allowedTypes)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error validating instance type: %s", err))
}
opts.InstanceType = *spawnHost.InstanceType
}
if spawnHost.AddedInstanceTags != nil || spawnHost.DeletedInstanceTags != nil {
addedTags := []host.Tag{}
deletedTags := []string{}
for _, tag := range spawnHost.AddedInstanceTags {
tag.CanBeModified = true
addedTags = append(addedTags, *tag)
}
for _, tag := range spawnHost.DeletedInstanceTags {
deletedTags = append(deletedTags, tag.Key)
}
opts.AddInstanceTags = addedTags
opts.DeleteInstanceTags = deletedTags
}
if spawnHost.Volume != nil {
v, err = host.FindVolumeByID(*spawnHost.Volume)
if err != nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("Error finding requested volume id: %s", err))
}
if v.AvailabilityZone != h.Zone {
return nil, InputValidationError.Send(ctx, "Error mounting volume to spawn host, They must be in the same availability zone.")
}
opts.AttachVolume = *spawnHost.Volume
}
if spawnHost.PublicKey != nil {
if h.Status != evergreen.HostRunning {
return nil, InputValidationError.Send(ctx, fmt.Sprintf("Host must be running to add a public key but is '%s'", h.Status))
}
if utility.FromBoolPtr(spawnHost.SavePublicKey) {
if err = savePublicKey(ctx, *spawnHost.PublicKey); err != nil {
return nil, err
}
}
opts.AddKey = spawnHost.PublicKey.Key
if opts.AddKey == "" {
opts.AddKey, err = usr.GetPublicKey(spawnHost.PublicKey.Name)
if err != nil {
return nil, InputValidationError.Send(ctx, fmt.Sprintf("No matching key found for name '%s'", spawnHost.PublicKey.Name))
}
}
}
if spawnHost.SleepSchedule != nil {
if err = h.UpdateSleepSchedule(ctx, *spawnHost.SleepSchedule); err != nil {
gimletErr, ok := err.(gimlet.ErrorResponse)
if ok {
return nil, mapHTTPStatusToGqlError(ctx, gimletErr.StatusCode, err)
}
return nil, InternalServerError.Send(ctx, fmt.Sprintf("setting sleep schedule: '%s'", err.Error()))
}
}
if err = cloud.ModifySpawnHost(ctx, evergreen.GetEnvironment(), h, opts); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error modifying spawn host: %s", err))
}
if spawnHost.ServicePassword != nil {
_, err = cloud.SetHostRDPPassword(ctx, evergreen.GetEnvironment(), h, *spawnHost.ServicePassword)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error setting spawn host password: %s", err))
}
}
apiHost := restModel.APIHost{}
apiHost.BuildFromService(h, nil)
return &apiHost, nil
}
// MigrateVolume is the resolver for the migrateVolume field.
func (r *mutationResolver) MigrateVolume(ctx context.Context, volumeID string, spawnHostInput *SpawnHostInput) (bool, error) {
usr := mustHaveUser(ctx)
options, err := getHostRequestOptions(ctx, usr, spawnHostInput)
if err != nil {
return false, err
}
return data.MigrateVolume(ctx, volumeID, options, usr, evergreen.GetEnvironment())
}
// SpawnHost is the resolver for the spawnHost field.
func (r *mutationResolver) SpawnHost(ctx context.Context, spawnHostInput *SpawnHostInput) (*restModel.APIHost, error) {
usr := mustHaveUser(ctx)
options, err := getHostRequestOptions(ctx, usr, spawnHostInput)
if err != nil {
return nil, err
}
spawnHost, err := data.NewIntentHost(ctx, options, usr, evergreen.GetEnvironment())
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error spawning host: %s", err))
}
if spawnHost == nil {
return nil, InternalServerError.Send(ctx, "An error occurred Spawn host is nil")
}
if spawnHostInput.SleepSchedule != nil {
if err = spawnHost.UpdateSleepSchedule(ctx, *spawnHostInput.SleepSchedule); err != nil {
gimletErr, ok := err.(gimlet.ErrorResponse)
if ok {
return nil, mapHTTPStatusToGqlError(ctx, gimletErr.StatusCode, err)
}
return nil, InternalServerError.Send(ctx, fmt.Sprintf("setting sleep schedule: '%s'", err.Error()))
}
}
apiHost := restModel.APIHost{}
apiHost.BuildFromService(spawnHost, nil)
return &apiHost, nil
}
// SpawnVolume is the resolver for the spawnVolume field.
func (r *mutationResolver) SpawnVolume(ctx context.Context, spawnVolumeInput SpawnVolumeInput) (bool, error) {
err := validateVolumeExpirationInput(ctx, spawnVolumeInput.Expiration, spawnVolumeInput.NoExpiration)
if err != nil {
return false, err
}
volumeRequest := host.Volume{
AvailabilityZone: spawnVolumeInput.AvailabilityZone,
Size: int32(spawnVolumeInput.Size),
Type: spawnVolumeInput.Type,
CreatedBy: mustHaveUser(ctx).Id,
}
vol, statusCode, err := cloud.RequestNewVolume(ctx, volumeRequest)
if err != nil {
return false, mapHTTPStatusToGqlError(ctx, statusCode, err)
}
if vol == nil {
return false, InternalServerError.Send(ctx, "Unable to create volume")
}
errorTemplate := "Volume %s has been created but an error occurred."
var additionalOptions restModel.VolumeModifyOptions
if spawnVolumeInput.Expiration != nil {
var newExpiration time.Time
newExpiration, err = restModel.FromTimePtr(spawnVolumeInput.Expiration)
if err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("Volume '%s' has been created but an error occurred: %s", vol.ID, err.Error()))
}
additionalOptions.Expiration = newExpiration
} else if spawnVolumeInput.NoExpiration != nil && *spawnVolumeInput.NoExpiration {
// this value should only ever be true or nil
additionalOptions.NoExpiration = true
}
err = applyVolumeOptions(ctx, *vol, additionalOptions)
if err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("Unable to apply expiration options to volume %s: %s", vol.ID, err.Error()))
}
if spawnVolumeInput.Host != nil {
statusCode, err := cloud.AttachVolume(ctx, vol.ID, *spawnVolumeInput.Host)
if err != nil {
return false, mapHTTPStatusToGqlError(ctx, statusCode, werrors.Wrapf(err, errorTemplate, vol.ID))
}
}
return true, nil
}
// RemoveVolume is the resolver for the removeVolume field.
func (r *mutationResolver) RemoveVolume(ctx context.Context, volumeID string) (bool, error) {
statusCode, err := cloud.DeleteVolume(ctx, volumeID)
if err != nil {
return false, mapHTTPStatusToGqlError(ctx, statusCode, err)
}
return statusCode == http.StatusOK, nil
}
// UpdateSpawnHostStatus is the resolver for the updateSpawnHostStatus field.
func (r *mutationResolver) UpdateSpawnHostStatus(ctx context.Context, hostID *string, action *SpawnHostStatusActions, updateSpawnHostStatusInput *UpdateSpawnHostStatusInput) (*restModel.APIHost, error) {
shouldKeepOff := false
// TODO: Use input object throughout resolver once deprecated fields are removed
if updateSpawnHostStatusInput != nil {
hostID = &updateSpawnHostStatusInput.HostID
action = &updateSpawnHostStatusInput.Action
shouldKeepOff = utility.FromBoolPtr(updateSpawnHostStatusInput.ShouldKeepOff)
}
h, err := host.FindOneByIdOrTag(ctx, *hostID)
if err != nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("Error finding host by id: %s", err))
}
usr := mustHaveUser(ctx)
env := evergreen.GetEnvironment()
if !host.CanUpdateSpawnHost(h, usr) {
return nil, Forbidden.Send(ctx, "You are not authorized to modify this host")
}
var httpStatus int
switch *action {
case SpawnHostStatusActionsStart:
httpStatus, err = data.StartSpawnHost(ctx, env, usr, h)
case SpawnHostStatusActionsStop:
httpStatus, err = data.StopSpawnHost(ctx, env, usr, h, shouldKeepOff)
case SpawnHostStatusActionsTerminate:
httpStatus, err = data.TerminateSpawnHost(ctx, env, usr, h)
default:
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("Could not find matching status for action : %s", action))
}
if err != nil {
if httpStatus == http.StatusInternalServerError {
var parsedUrl, _ = url.Parse("/graphql/query")
grip.Error(message.WrapError(err, message.Fields{
"method": "POST",
"url": parsedUrl,
"code": httpStatus,
"action": action,
"request": gimlet.GetRequestID(ctx),
"stack": string(debug.Stack()),
}))
}
return nil, mapHTTPStatusToGqlError(ctx, httpStatus, err)
}
apiHost := restModel.APIHost{}
apiHost.BuildFromService(h, nil)
return &apiHost, nil
}
// UpdateVolume is the resolver for the updateVolume field.
func (r *mutationResolver) UpdateVolume(ctx context.Context, updateVolumeInput UpdateVolumeInput) (bool, error) {
volume, err := host.FindVolumeByID(updateVolumeInput.VolumeID)
if err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("Error finding volume by id %s: %s", updateVolumeInput.VolumeID, err.Error()))
}
if volume == nil {
return false, ResourceNotFound.Send(ctx, fmt.Sprintf("Unable to find volume %s", updateVolumeInput.VolumeID))
}
err = validateVolumeExpirationInput(ctx, updateVolumeInput.Expiration, updateVolumeInput.NoExpiration)
if err != nil {
return false, err
}
err = validateVolumeName(ctx, updateVolumeInput.Name)
if err != nil {
return false, err
}
var updateOptions restModel.VolumeModifyOptions
if updateVolumeInput.NoExpiration != nil {
if *updateVolumeInput.NoExpiration {
// this value should only ever be true or nil
updateOptions.NoExpiration = true
} else {
// this value should only ever be true or nil
updateOptions.HasExpiration = true
}
}
if updateVolumeInput.Expiration != nil {
var newExpiration time.Time
newExpiration, err = restModel.FromTimePtr(updateVolumeInput.Expiration)
if err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("Error parsing time %s", err))
}
updateOptions.Expiration = newExpiration
}
if updateVolumeInput.Name != nil {
updateOptions.NewName = *updateVolumeInput.Name
}
err = applyVolumeOptions(ctx, *volume, updateOptions)
if err != nil {
return false, InternalServerError.Send(ctx, fmt.Sprintf("Unable to update volume %s: %s", volume.ID, err.Error()))
}
return true, nil
}
// AbortTask is the resolver for the abortTask field.
func (r *mutationResolver) AbortTask(ctx context.Context, taskID string) (*restModel.APITask, error) {
t, err := task.FindOneId(taskID)
if err != nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("error finding task by id %s: %s", taskID, err.Error()))
}
if t == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("cannot find task with id %s", taskID))
}
user := gimlet.GetUser(ctx).Username()
err = model.AbortTask(ctx, taskID, user)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("Error aborting task %s: %s", taskID, err.Error()))
}
t, err = task.FindOneId(taskID)
if err != nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("error finding task by id %s: %s", taskID, err.Error()))
}
if t == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("cannot find task with id %s", taskID))
}
apiTask, err := getAPITaskFromTask(ctx, r.sc.GetURL(), *t)
return apiTask, err
}
// OverrideTaskDependencies is the resolver for the overrideTaskDependencies field.
func (r *mutationResolver) OverrideTaskDependencies(ctx context.Context, taskID string) (*restModel.APITask, error) {
currentUser := mustHaveUser(ctx)
t, err := task.FindByIdExecution(taskID, nil)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error finding task %s: %s", taskID, err.Error()))
}
if t == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("cannot find task with id %s", taskID))
}
if err = t.SetOverrideDependencies(currentUser.Username()); err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("error overriding dependencies for task %s: %s", taskID, err.Error()))
}