/
accept_changeset.go
296 lines (241 loc) · 8.02 KB
/
accept_changeset.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
package epic
import (
"context"
"log"
"reflect"
"strings"
"github.com/google/go-github/github"
"errors"
"fmt"
"github.com/student-kyushu/frau/input"
"github.com/student-kyushu/frau/operation"
"github.com/student-kyushu/frau/queue"
"github.com/student-kyushu/frau/setting"
)
type AcceptCommand struct {
Owner string
Name string
Client *github.Client
BotName string
Info *setting.RepositoryInfo
AutoMergeRepo *queue.AutoMergeQRepo
}
func (c *AcceptCommand) AcceptChangesetByOthers(ctx context.Context, ev *github.IssueCommentEvent, cmd *input.AcceptChangeByOthersCommand) (bool, error) {
log.Printf("info: Start: merge the pull request by %v\n", *ev.Comment.ID)
defer log.Printf("info: End: merge the pull request by %v\n", *ev.Comment.ID)
if c.BotName != cmd.BotName() {
log.Printf("info: this command works only if target user is actual our bot.")
return false, nil
}
sender := *ev.Sender.Login
log.Printf("debug: command is sent from %v\n", sender)
if c.Info.IsReviewer(sender) {
log.Printf("info: this bot try to merge #%v by the reviewer (`%v`)\n", ev.Issue.GetID(), sender)
return c.acceptChangeset(ctx, ev, cmd)
}
if c.Info.IsInMergeableUserList(sender) {
opener := ev.Issue.User.GetName()
if isMergeableByMergeableUser(sender, opener, cmd.Reviewer) {
log.Printf("info: this bot try to merge #%v (opened by `%v`) by the mergeable user (`%v`) with reviewer (%v)\n", ev.Issue.GetID(), opener, sender, cmd.Reviewer)
return c.acceptChangeset(ctx, ev, cmd)
}
}
log.Printf("info: %v cannnot merge the pull request #%v\n", sender, ev.Issue.GetID())
return false, nil
}
func isMergeableByMergeableUser(commander, opener string, reviewer []string) bool {
if commander != opener {
log.Printf("info: commander `(%v)` is diffetent from the opener (`%v`) for this pull request\n", commander, opener)
return false
}
for _, r := range reviewer {
if r == commander {
log.Printf("info: commander `(%v)` could not review this pull request by self (reviewer: %v)\n", commander, reviewer)
return false
}
}
return true
}
func (c *AcceptCommand) AcceptChangesetByReviewer(ctx context.Context, ev *github.IssueCommentEvent, cmd *input.AcceptChangeByReviewerCommand) (bool, error) {
log.Printf("info: Start: merge the pull request by %v\n", *ev.Comment.ID)
defer log.Printf("info: End: merge the pull request by %v\n", *ev.Comment.ID)
if c.BotName != cmd.BotName() {
log.Printf("info: this command works only if target user is actual our bot.")
return false, nil
}
sender := *ev.Sender.Login
log.Printf("debug: command is sent from %v\n", sender)
if !c.Info.IsReviewer(sender) {
log.Printf("info: %v is not an reviewer registred to this bot.\n", sender)
return false, nil
}
return c.acceptChangeset(ctx, ev, cmd)
}
func (c *AcceptCommand) acceptChangeset(ctx context.Context, ev *github.IssueCommentEvent, cmd input.AcceptChangesetCommand) (bool, error) {
sender := *ev.Sender.Login
client := c.Client
issueSvc := client.Issues
repoOwner := c.Owner
repoName := c.Name
issue := *ev.Issue.Number
log.Printf("debug: issue number is %v\n", issue)
currentLabels := operation.GetLabelsByIssue(ctx, issueSvc, repoOwner, repoName, issue)
if currentLabels == nil {
return false, nil
}
doNotMergeLabel, _, err := issueSvc.GetLabel(ctx, repoOwner, repoName, "S-do-not-merge")
if err != nil {
log.Println("info: could not find label `S-do-not-merge`")
return false, err
}
if containsDoNotMerge := containsLabel(currentLabels, doNotMergeLabel); containsDoNotMerge {
comment := fmt.Sprint(":warning: Forbid to merge by label `S-do-not-merge`")
if ok := operation.AddComment(ctx, issueSvc, repoOwner, repoName, issue, comment); !ok {
log.Println("info: could not create the comment to declare the head is approved.")
return false, nil
}
log.Println("info: forbid to merge by label `S-do-not-merge`")
return false, nil
}
labels := operation.AddAwaitingMergeLabel(currentLabels)
// https://github.com/nekoya/popuko/blob/master/web.py
_, _, err = issueSvc.ReplaceLabelsForIssue(ctx, repoOwner, repoName, issue, labels)
if err != nil {
log.Println("info: could not change labels by the issue")
return false, err
}
prSvc := client.PullRequests
pr, _, err := prSvc.Get(ctx, repoOwner, repoName, issue)
if err != nil {
log.Println("info: could not fetch the pull request information.")
return false, err
}
headSha := *pr.Head.SHA
if ok := commentApprovedSha(ctx, cmd, issueSvc, repoOwner, repoName, issue, headSha, sender); !ok {
log.Println("info: could not create the comment to declare the head is approved.")
return false, err
}
if c.Info.EnableAutoMerge {
qHandle := c.AutoMergeRepo.Get(repoOwner, repoName)
if qHandle == nil {
log.Println("error: cannot get the queue handle")
return false, errors.New("error: cannot get the queue handle")
}
qHandle.Lock()
defer qHandle.Unlock()
q := qHandle.Load()
item := &queue.AutoMergeQueueItem{
PullRequest: issue,
PrHead: headSha,
}
ok, mutated := queuePullReq(q, item)
if !ok {
return false, errors.New("error: we cannot recover the error")
}
if mutated {
q.Save()
}
if q.HasActive() {
commentAsPostponed(ctx, issueSvc, repoOwner, repoName, issue)
return true, nil
}
if next := q.Front(); next != item {
commentAsPostponed(ctx, issueSvc, repoOwner, repoName, issue)
}
tryNextItem(ctx, client, repoOwner, repoName, q, c.Info.AutoBranchName)
}
log.Printf("info: complete merge the pull request %v\n", issue)
return true, nil
}
func commentApprovedSha(
ctx context.Context,
cmd input.AcceptChangesetCommand,
issues *github.IssuesService,
owner,
name string,
number int,
sha string,
sender string) bool {
var reviewers string
switch cmd := cmd.(type) {
case *input.AcceptChangeByOthersCommand:
{
list := make([]string, 0, len(cmd.Reviewer))
for _, name := range cmd.Reviewer {
list = append(list, fmt.Sprintf("`%v`", name))
}
if containsMe, index := contains(list, "`me`"); containsMe {
list[index] = sender
}
reviewers = strings.Join(list, ", ")
}
case *input.AcceptChangeByReviewerCommand:
reviewers = fmt.Sprintf("`%v`", sender)
default:
log.Printf("error: %+v is not handled.", cmd)
return false
}
comment := fmt.Sprintf(":pushpin: Commit %v has been approved by %v", sha, reviewers)
if ok := operation.AddComment(ctx, issues, owner, name, number, comment); !ok {
log.Println("info: could not create the comment to declare the head is approved.")
return false
}
return true
}
func queuePullReq(queue *queue.AutoMergeQueue, item *queue.AutoMergeQueueItem) (ok bool, mutated bool) {
if queue.HasActive() {
active := queue.GetActive()
if active.PullRequest == item.PullRequest {
if active.PrHead == item.PrHead {
// noop
return true, false
}
queue.RemoveActive()
if ok := queue.Push(item); !ok {
return false, false
}
return true, true
}
}
has, awaiting := queue.IsAwaiting(item.PullRequest)
if has {
if sameHead := (awaiting.PrHead == item.PrHead); sameHead {
return true, false
}
if ok := queue.RemoveAwaiting(item.PullRequest); !ok {
log.Println("error: ASSERT!: cannot remove awaiting item")
log.Printf("error: queue %+v\n", queue)
log.Printf("error: item %+v\n", item)
return false, false
}
}
if ok := queue.Push(item); !ok {
return false, false
}
return true, true
}
func commentAsPostponed(ctx context.Context, issueSvc *github.IssuesService, owner, name string, issue int) {
log.Printf("info: pull request (%v) has been queued but other is active.\n", issue)
{
comment := ":postbox: This pull request is queued. Please await the time."
if ok := operation.AddComment(ctx, issueSvc, owner, name, issue, comment); !ok {
log.Println("info: could not create the comment to declare to merge this.")
}
}
}
func contains(s []string, e string) (bool, int) {
for i, v := range s {
if e == v {
return true, i
}
}
return false, -1
}
func containsLabel(s []*github.Label, e *github.Label) bool {
for _, v := range s {
if reflect.DeepEqual(e, v) {
return true
}
}
return false
}