/
github_status_refresh.go
257 lines (226 loc) · 6.95 KB
/
github_status_refresh.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
package units
import (
"context"
"fmt"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/db"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/build"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/thirdparty"
"github.com/mongodb/amboy"
"github.com/mongodb/amboy/job"
"github.com/mongodb/amboy/registry"
"github.com/mongodb/grip"
"github.com/mongodb/grip/level"
"github.com/mongodb/grip/message"
"github.com/mongodb/grip/send"
"github.com/mongodb/grip/sometimes"
"github.com/pkg/errors"
)
const (
githubStatusRefreshJobName = "github-status-refresh"
)
func init() {
registry.AddJobType(githubStatusRefreshJobName, func() amboy.Job { return makeGithubStatusRefreshJob() })
}
// NewGithubStatusRefreshJob is a job that re-sends github statuses to the PR associated with the given patch.
func NewGithubStatusRefreshJob(p *patch.Patch) amboy.Job {
job := makeGithubStatusRefreshJob()
job.FetchID = p.Version
job.patch = p
job.SetID(fmt.Sprintf("%s:%s-%s", githubStatusRefreshJobName, p.Version, time.Now().String()))
return job
}
type githubStatusRefreshJob struct {
job.Base `bson:"job_base" json:"job_base" yaml:"job_base"`
env evergreen.Environment
sender send.Sender
urlBase string
patch *patch.Patch
builds []build.Build
childPatches []patch.Patch
FetchID string `bson:"fetch_id" json:"fetch_id" yaml:"fetch_id"`
}
func makeGithubStatusRefreshJob() *githubStatusRefreshJob {
j := &githubStatusRefreshJob{
Base: job.Base{
JobType: amboy.JobType{
Name: githubStatusRefreshJobName,
Version: 0,
},
},
}
j.SetPriority(1)
return j
}
func (j *githubStatusRefreshJob) shouldUpdate(ctx context.Context) (bool, error) {
flags, err := evergreen.GetServiceFlags(ctx)
if err != nil {
return false, errors.Wrap(err, "error retrieving admin settings")
}
if flags.GithubStatusAPIDisabled {
grip.InfoWhen(sometimes.Percent(evergreen.DegradedLoggingPercent), message.Fields{
"job": j.Name,
"message": "GitHub status updates are disabled, not updating status",
})
return false, nil
}
return true, nil
}
func (j *githubStatusRefreshJob) fetch(ctx context.Context) error {
if j.env == nil {
j.env = evergreen.GetEnvironment()
}
uiConfig := evergreen.UIConfig{}
var err error
if err := uiConfig.Get(ctx); err != nil {
return errors.Wrap(err, "retrieving UI config")
}
j.urlBase = uiConfig.Url
if j.urlBase == "" {
return errors.New("url base doesn't exist")
}
j.sender, err = j.env.GetGitHubSender(j.patch.GithubPatchData.BaseOwner, j.patch.GithubPatchData.BaseRepo)
if err != nil {
return err
}
if j.patch == nil {
j.patch, err = patch.FindOneId(j.FetchID)
if err != nil {
return errors.Wrap(err, "finding patch")
}
if j.patch == nil {
return errors.New("patch not found")
}
}
j.builds, err = build.Find(build.ByVersion(j.FetchID))
if err != nil {
return errors.Wrap(err, "finding builds")
}
if len(j.patch.Triggers.ChildPatches) > 0 {
j.childPatches, err = patch.Find(patch.ByStringIds(j.patch.Triggers.ChildPatches))
if err != nil {
return errors.Wrap(err, "finding child patches")
}
}
return nil
}
func (j *githubStatusRefreshJob) sendStatus(status *message.GithubStatus) {
c := message.MakeGithubStatusMessageWithRepo(*status)
if !c.Loggable() {
j.AddError(errors.Errorf("status message is invalid: %+v", status))
return
}
j.AddError(c.SetPriority(level.Notice))
j.sender.Send(c)
grip.Info(message.Fields{
"ticket": thirdparty.GithubInvestigation,
"message": "called github status refresh",
"caller": githubStatusRefreshJobName,
"context": status.Context,
"patch_id": j.FetchID,
"job_id": j.ID(),
})
}
// sendChildPatchStatuses iterates through child patches if relevant and builds/sends statuses.
func (j *githubStatusRefreshJob) sendChildPatchStatuses() error {
if len(j.childPatches) == 0 {
return nil
}
status := &message.GithubStatus{
Owner: j.patch.GithubPatchData.BaseOwner,
Repo: j.patch.GithubPatchData.BaseRepo,
Ref: j.patch.GithubPatchData.HeadHash,
}
for _, childPatch := range j.childPatches {
projectIdentifier, err := model.GetIdentifierForProject(childPatch.Project)
if err != nil {
return errors.Wrap(err, "finding project identifier")
}
status.Context, err = patch.GetGithubContextForChildPatch(projectIdentifier, j.patch, &childPatch)
if err != nil {
return errors.Wrapf(err, "getting github context for child patch '%s'", childPatch.Id.Hex())
}
status.URL = childPatch.GetURL(j.urlBase)
status.State, status.Description = getGithubStateAndDescriptionForPatch(&childPatch)
j.sendStatus(status)
}
return nil
}
func getGithubStateAndDescriptionForPatch(p *patch.Patch) (message.GithubState, string) {
var state message.GithubState
if evergreen.IsSuccessfulVersionStatus(p.Status) {
state = message.GithubStateSuccess
} else if p.Status == evergreen.VersionFailed {
state = message.GithubStateFailure
} else {
return message.GithubStatePending, evergreen.PRTasksRunningDescription
}
duration := p.FinishTime.Sub(p.StartTime).String()
name := "version"
if p.IsChild() {
name = "child patch"
}
return state, fmt.Sprintf("%s finished in %s", name, duration)
}
func (j *githubStatusRefreshJob) sendBuildStatuses() {
status := &message.GithubStatus{
Owner: j.patch.GithubPatchData.BaseOwner,
Repo: j.patch.GithubPatchData.BaseRepo,
Ref: j.patch.GithubPatchData.HeadHash,
}
for _, b := range j.builds {
status.Context = fmt.Sprintf("%s/%s", evergreenContext, b.BuildVariant)
status.URL = b.GetURL(j.urlBase)
switch b.Status {
case evergreen.BuildSucceeded:
status.State = message.GithubStateSuccess
case evergreen.BuildFailed:
status.State = message.GithubStateFailure
default:
status.State = message.GithubStatePending
}
query := db.Query(task.ByBuildId(b.Id)).WithFields(task.StatusKey, task.IsEssentialToSucceedKey, task.ActivatedKey)
tasks, err := task.FindAll(query)
if err != nil {
j.AddError(errors.Wrapf(err, "finding tasks in build '%s'", b.Id))
continue
}
status.Description = b.GetPRNotificationDescription(tasks)
j.sendStatus(status)
}
}
func (j *githubStatusRefreshJob) Run(ctx context.Context) {
shouldUpdate, err := j.shouldUpdate(ctx)
if err != nil {
j.AddError(err)
return
}
if !shouldUpdate {
return
}
if err = j.fetch(ctx); err != nil {
j.AddError(err)
return
}
status := &message.GithubStatus{
URL: j.patch.GetURL(j.urlBase),
Context: evergreenContext,
Owner: j.patch.GithubPatchData.BaseOwner,
Repo: j.patch.GithubPatchData.BaseRepo,
Ref: j.patch.GithubPatchData.HeadHash,
}
status.State, status.Description = getGithubStateAndDescriptionForPatch(j.patch)
// Send patch status
j.sendStatus(status)
// Send child patch statuses.
if err := j.sendChildPatchStatuses(); err != nil {
j.AddError(err)
return
}
// For each build, send build status.
j.sendBuildStatuses()
}