-
Notifications
You must be signed in to change notification settings - Fork 124
/
task_annotations.go
202 lines (189 loc) · 6.55 KB
/
task_annotations.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
package task
import (
"time"
"github.com/evergreen-ci/evergreen/db"
"github.com/evergreen-ci/evergreen/model/annotations"
"github.com/mongodb/anser/bsonutil"
adb "github.com/mongodb/anser/db"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
// MoveIssueToSuspectedIssue removes an issue from an existing annotation and adds it to its suspected issues,
// and unsets its associated task document as having annotations if this was the last issue removed from the
// annotation.
func MoveIssueToSuspectedIssue(taskId string, taskExecution int, issue annotations.IssueLink, username string) error {
newIssue := issue
newIssue.Source = &annotations.Source{Requester: annotations.UIRequester, Author: username, Time: time.Now()}
q := annotations.ByTaskIdAndExecution(taskId, taskExecution)
q[bsonutil.GetDottedKeyName(annotations.IssuesKey, annotations.IssueLinkIssueKey)] = issue.IssueKey
annotation := &annotations.TaskAnnotation{}
_, err := db.FindAndModify(
annotations.Collection,
q,
nil,
adb.Change{
Update: bson.M{
"$pull": bson.M{annotations.IssuesKey: bson.M{annotations.IssueLinkIssueKey: issue.IssueKey}},
"$push": bson.M{annotations.SuspectedIssuesKey: newIssue},
},
ReturnNew: true,
},
annotation,
)
if err != nil {
return errors.Wrapf(err, "finding and modifying task annotation for execution %d of task '%s'", taskExecution, taskId)
}
if annotation != nil && len(annotation.Issues) == 0 {
return UnsetHasAnnotations(taskId, taskExecution)
}
return nil
}
// MoveSuspectedIssueToIssue removes a suspected issue from an existing annotation and adds it to its issues,
// and marks its associated task document as having annotations.
func MoveSuspectedIssueToIssue(taskId string, taskExecution int, issue annotations.IssueLink, username string) error {
newIssue := issue
newIssue.Source = &annotations.Source{Requester: annotations.UIRequester, Author: username, Time: time.Now()}
q := annotations.ByTaskIdAndExecution(taskId, taskExecution)
q[bsonutil.GetDottedKeyName(annotations.SuspectedIssuesKey, annotations.IssueLinkIssueKey)] = issue.IssueKey
if err := db.Update(
annotations.Collection,
q,
bson.M{
"$pull": bson.M{annotations.SuspectedIssuesKey: bson.M{annotations.IssueLinkIssueKey: issue.IssueKey}},
"$push": bson.M{annotations.IssuesKey: newIssue},
},
); err != nil {
return err
}
return SetHasAnnotations(taskId, taskExecution)
}
// AddIssueToAnnotation adds an issue onto an existing annotation and marks its associated task document
// as having annotations.
func AddIssueToAnnotation(taskId string, execution int, issue annotations.IssueLink, username string) error {
issue.Source = &annotations.Source{
Author: username,
Time: time.Now(),
Requester: annotations.UIRequester,
}
if _, err := db.Upsert(
annotations.Collection,
annotations.ByTaskIdAndExecution(taskId, execution),
bson.M{
"$push": bson.M{annotations.IssuesKey: issue},
},
); err != nil {
return errors.Wrapf(err, "adding task annotation issue for task '%s'", taskId)
}
return SetHasAnnotations(taskId, execution)
}
// RemoveIssueFromAnnotation removes an issue from an existing annotation, and unsets its
// associated task document as having annotations if this was the last issue removed from the annotation.
func RemoveIssueFromAnnotation(taskId string, execution int, issue annotations.IssueLink) error {
annotation := &annotations.TaskAnnotation{}
_, err := db.FindAndModify(
annotations.Collection,
annotations.ByTaskIdAndExecution(taskId, execution),
nil,
adb.Change{
Update: bson.M{"$pull": bson.M{annotations.IssuesKey: issue}},
ReturnNew: true,
},
annotation,
)
if err != nil {
return errors.Wrapf(err, "finding and removing issue for task annotation for execution %d of task '%s'", execution, taskId)
}
if annotation != nil && len(annotation.Issues) == 0 {
return UnsetHasAnnotations(taskId, execution)
}
return nil
}
// UpsertAnnotation upserts a task annotation, and marks its associated task document
// as having annotations if the upsert includes a non-nil Issues field.
func UpsertAnnotation(a *annotations.TaskAnnotation, userDisplayName string) error {
source := &annotations.Source{
Author: userDisplayName,
Time: time.Now(),
Requester: annotations.APIRequester,
}
update := bson.M{}
if a.Metadata != nil {
update[annotations.MetadataKey] = a.Metadata
}
if a.Note != nil {
a.Note.Source = source
update[annotations.NoteKey] = a.Note
}
if a.MetadataLinks != nil {
for i := range a.MetadataLinks {
a.MetadataLinks[i].Source = source
}
update[annotations.MetadataLinksKey] = a.MetadataLinks
}
if a.Issues != nil {
for i := range a.Issues {
a.Issues[i].Source = source
}
update[annotations.IssuesKey] = a.Issues
}
if a.SuspectedIssues != nil {
for i := range a.SuspectedIssues {
a.SuspectedIssues[i].Source = source
}
update[annotations.SuspectedIssuesKey] = a.SuspectedIssues
}
if len(update) == 0 {
return nil
}
if _, err := db.Upsert(
annotations.Collection,
annotations.ByTaskIdAndExecution(a.TaskId, a.TaskExecution),
bson.M{
"$set": update,
},
); err != nil {
return errors.Wrapf(err, "adding task annotation for task '%s'", a.TaskId)
}
if a.Issues != nil && len(a.Issues) > 0 {
return SetHasAnnotations(a.TaskId, a.TaskExecution)
}
return nil
}
// PatchAnnotation adds issues onto existing annotations, and marks its associated task document
// as having annotations if the patch includes a non-nil Issues field.
func PatchAnnotation(a *annotations.TaskAnnotation, userDisplayName string, upsert bool) error {
existingAnnotation, err := annotations.FindOneByTaskIdAndExecution(a.TaskId, a.TaskExecution)
if err != nil {
return errors.Wrapf(err, "finding annotation for task '%s' and execution %d", a.TaskId, a.TaskExecution)
}
if existingAnnotation == nil {
if !upsert {
return errors.Errorf("annotation for task '%s' and execution %d not found", a.TaskId, a.TaskExecution)
} else {
return UpsertAnnotation(a, userDisplayName)
}
}
if a.Note != nil {
return errors.New("note should be empty")
}
if a.Metadata != nil {
return errors.New("metadata should be empty")
}
update := annotations.CreateAnnotationUpdate(a, userDisplayName)
if len(update) == 0 {
return nil
}
if err = db.Update(
annotations.Collection,
annotations.ByTaskIdAndExecution(a.TaskId, a.TaskExecution),
bson.M{
"$push": update,
},
); err != nil {
return errors.Wrapf(err, "updating task annotation for '%s'", a.TaskId)
}
if a.Issues != nil && len(a.Issues) > 0 {
return SetHasAnnotations(a.TaskId, a.TaskExecution)
}
return nil
}