/
check_autobranch.go
350 lines (280 loc) · 10.1 KB
/
check_autobranch.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
package epic
import (
"context"
"fmt"
"log"
"github.com/google/go-github/v24/github"
"github.com/student-kyushu/frau/operation"
"github.com/student-kyushu/frau/queue"
"github.com/student-kyushu/frau/setting"
)
type StateChangeInfo struct {
Status string
Owner string
Name string
NotHandle bool
ID int64
SHA string
IsRelatedToAutoBranchBody func(string) bool
}
func CheckAutoBranchWithStatusEvent(ctx context.Context, client *github.Client, autoMergeRepo *queue.AutoMergeQRepo, ev *github.StatusEvent) {
info := StateChangeInfo{
Status: *ev.State,
Owner: *ev.Repo.Owner.Login,
Name: *ev.Repo.Name,
NotHandle: *ev.State == "pending",
ID: *ev.ID,
SHA: *ev.SHA,
IsRelatedToAutoBranchBody: isRelatedToAutoBranchBodyWithStatusEvent(ev),
}
checkAutoBranch(ctx, client, autoMergeRepo, info)
}
func CheckAutoBranchWithCheckSuiteEvent(ctx context.Context, client *github.Client, autoMergeRepo *queue.AutoMergeQRepo, ev *github.CheckSuiteEvent) {
info := StateChangeInfo{
Status: *ev.CheckSuite.Conclusion,
Owner: *ev.Repo.Owner.Login,
Name: *ev.Repo.Name,
NotHandle: *ev.CheckSuite.Status != "completed",
ID: *ev.CheckSuite.ID,
SHA: *ev.CheckSuite.HeadSHA,
IsRelatedToAutoBranchBody: isRelatedToAutoBranchBodyWithCheckSuiteEvent(ev),
}
checkAutoBranch(ctx, client, autoMergeRepo, info)
}
func checkAutoBranch(ctx context.Context, client *github.Client, autoMergeRepo *queue.AutoMergeQRepo, info StateChangeInfo) {
log.Println("info: Start: checkAutoBranch")
defer log.Println("info: End: checkAutoBranch")
if info.NotHandle {
log.Printf("info: Not handle %v in this event\n", info.Status)
return
}
log.Printf("info: Start to handle in this event: %v\n", info.Status)
log.Printf("info: Target repository is %v/%v\n", info.Owner, info.Name)
repoInfo := GetRepositoryInfo(ctx, client.Repositories, info.Owner, info.Name)
if repoInfo == nil {
log.Println("debug: cannot get repositoryInfo")
return
}
log.Println("info: success to load the configure.")
if !repoInfo.EnableAutoMerge {
log.Println("info: this repository does not enable merging into master automatically.")
return
}
log.Println("info: Start to handle auto merging the branch.")
qHandle := autoMergeRepo.Get(info.Owner, info.Name)
if qHandle == nil {
log.Println("error: cannot get the queue handle")
return
}
qHandle.Lock()
defer qHandle.Unlock()
q := qHandle.Load()
if !q.HasActive() {
log.Println("info: there is no testing item")
return
}
active := q.GetActive()
if active == nil {
log.Println("error: `active` should not be null")
return
}
log.Println("info: got the active item.")
if !isRelatedToAutoBranch(active, info, repoInfo.AutoBranchName) {
log.Printf("info: The event's tip sha does not equal to the one which is tesing actively in %v/%v\n", info.Owner, info.Name)
return
}
log.Println("info: the status event is related to auto branch.")
mergeSucceedItem(ctx, client, info.Owner, info.Name, repoInfo, q, info)
q.RemoveActive()
q.Save()
tryNextItem(ctx, client, info.Owner, info.Name, q, repoInfo.AutoBranchName)
log.Println("info: complete to start the next trying")
}
func isRelatedToAutoBranchBodyWithStatusEvent(ev *github.StatusEvent) func(string) bool {
return func(autoBranch string) bool {
return operation.IsIncludeAutoBranch(ev.Branches, autoBranch)
}
}
func isRelatedToAutoBranchBodyWithCheckSuiteEvent(ev *github.CheckSuiteEvent) func(string) bool {
return func(autoBranch string) bool {
if ev.CheckSuite.HeadBranch == nil {
return false
}
return *ev.CheckSuite.HeadBranch == autoBranch
}
}
func isRelatedToAutoBranch(active *queue.AutoMergeQueueItem, info StateChangeInfo, autoBranch string) bool {
if !info.IsRelatedToAutoBranchBody(autoBranch) {
log.Printf("warn: this event (%v) is not the auto branch\n", info.ID)
return false
}
if ok := checkCommitHashOnTrying(active, info); !ok {
return false
}
log.Println("info: the tip of auto branch is same as `active.SHA`")
return true
}
func checkCommitHashOnTrying(active *queue.AutoMergeQueueItem, info StateChangeInfo) bool {
autoTipSha := active.AutoBranchHead
if autoTipSha == nil {
return false
}
if *autoTipSha != info.SHA {
log.Printf("debug: The commit hash which contained by the event: %v\n", info.SHA)
log.Printf("debug: The commit hash is pinned to the status queue as the tip of auto branch: %v\n", autoTipSha)
return false
}
return true
}
func mergeSucceedItem(
ctx context.Context,
client *github.Client,
owner string,
name string,
repoInfo *setting.RepositoryInfo,
q *queue.AutoMergeQueue,
info StateChangeInfo) bool {
active := q.GetActive()
prNum := active.PullRequest
prInfo, _, err := client.PullRequests.Get(ctx, owner, name, prNum)
if err != nil {
log.Println("info: could not fetch the pull request information.")
return false
}
if *prInfo.State != "open" {
log.Printf("info: the pull request #%v has been resolved the state\n", prNum)
return true
}
if info.Status != "success" {
log.Println("info: could not merge pull request")
comment := ":collision: The result of what tried to merge this pull request is `" + info.Status + "`."
commentStatus(ctx, client, owner, name, prNum, comment, repoInfo.AutoBranchName)
currentLabels := operation.GetLabelsByIssue(ctx, client.Issues, owner, name, prNum)
if currentLabels == nil {
return false
}
labels := operation.AddFailsTestsWithUpsreamLabel(currentLabels)
_, _, err = client.Issues.ReplaceLabelsForIssue(ctx, owner, name, prNum, labels)
if err != nil {
log.Println("warn: could not change labels of the issue")
}
return false
}
comment := ":tada: The result of what tried to merge this pull request is `" + info.Status + "`."
commentStatus(ctx, client, owner, name, prNum, comment, repoInfo.AutoBranchName)
if ok := operation.MergePullRequest(ctx, client, owner, name, prInfo, active.PrHead); !ok {
log.Printf("info: cannot merge pull request #%v\n", prNum)
return false
}
if repoInfo.DeleteAfterAutoMerge {
operation.DeleteBranchByPullRequest(ctx, client.Git, prInfo)
}
log.Printf("info: complete merging #%v into master\n", prNum)
return true
}
func commentStatus(ctx context.Context, client *github.Client, owner, name string, prNum int, comment string, autoBranch string) {
status, _, err := client.Repositories.GetCombinedStatus(ctx, owner, name, autoBranch, nil)
if err != nil {
log.Println("error: could not get the status about the auto branch.")
}
if status != nil {
comment += "\n\n"
for _, s := range status.Statuses {
if s.TargetURL == nil {
continue
}
var item string
if s.Description == nil || *s.Description == "" {
item = fmt.Sprintf("* %v\n", *s.TargetURL)
} else {
item = fmt.Sprintf("* [%v](%v)\n", *s.Description, *s.TargetURL)
}
comment += item
}
}
if ok := operation.AddComment(ctx, client.Issues, owner, name, prNum, comment); !ok {
log.Println("error: could not write the comment about the result of auto branch.")
}
}
func tryNextItem(ctx context.Context, client *github.Client, owner, name string, q *queue.AutoMergeQueue, autoBranch string) (ok, hasNext bool) {
defer q.Save()
next, nextInfo := getNextAvailableItem(ctx, client, owner, name, q)
if next == nil {
log.Printf("info: there is no awating item in the queue of %v/%v\n", owner, name)
return true, false
}
nextNum := next.PullRequest
ok, commit := operation.TryWithMaster(ctx, client, owner, name, nextInfo, autoBranch)
if !ok {
log.Printf("info: we cannot try #%v with the latest `master`.", nextNum)
return tryNextItem(ctx, client, owner, name, q, autoBranch)
}
next.AutoBranchHead = &commit
q.SetActive(next)
log.Printf("info: pin #%v as the active item to queue\n", nextNum)
return true, true
}
func getNextAvailableItem(
ctx context.Context,
client *github.Client,
owner string,
name string,
queue *queue.AutoMergeQueue) (*queue.AutoMergeQueueItem, *github.PullRequest) {
issueSvc := client.Issues
prSvc := client.PullRequests
log.Println("Start to find the next item")
defer log.Println("End to find the next item")
for {
ok, next := queue.TakeNext()
if !ok || next == nil {
log.Printf("debug: there is no awating item in the queue of %v/%v\n", owner, name)
return nil, nil
}
log.Println("debug: the next item has fetched from queue.")
prNum := next.PullRequest
nextInfo, _, err := prSvc.Get(ctx, owner, name, prNum)
if err != nil {
log.Println("debug: could not fetch the pull request information.")
continue
}
if next.PrHead != *nextInfo.Head.SHA {
operation.CommentHeadIsDifferentFromAccepted(ctx, issueSvc, owner, name, prNum)
continue
}
if state := *nextInfo.State; state != "open" {
log.Printf("debug: the pull request #%v has been resolved the state as `%v`\n", prNum, state)
continue
}
ok, mergeable := operation.IsMergeable(ctx, prSvc, owner, name, prNum, nextInfo)
if !ok {
log.Println("info: We treat it as 'mergeable' to avoid miss detection because we could not fetch the pr info,")
continue
}
if !mergeable {
comment := ":lock: Merge conflict"
if ok := operation.AddComment(ctx, issueSvc, owner, name, prNum, comment); !ok {
log.Println("error: could not write the comment about the result of auto branch.")
}
currentLabels := operation.GetLabelsByIssue(ctx, issueSvc, owner, name, prNum)
if currentLabels == nil {
continue
}
labels := operation.AddNeedRebaseLabel(currentLabels)
log.Printf("debug: the changed labels: %v\n", labels)
_, _, err = issueSvc.ReplaceLabelsForIssue(ctx, owner, name, prNum, labels)
if err != nil {
log.Println("warn: could not change labels of the issue")
}
continue
} else {
label := operation.GetLabelsByIssue(ctx, issueSvc, owner, name, prNum)
if label == nil {
continue
}
if !operation.HasLabelInList(label, operation.LABEL_AWAITING_MERGE) {
continue
}
}
return next, nextInfo
}
}