@@ -50,7 +50,7 @@ func handler(items ...string) []string {
50
50
log .Error ("could not parse data from pr_auto_merge queue (%v): %v" , s , err )
51
51
continue
52
52
}
53
- handlePull (id , sha )
53
+ handlePullRequestAutoMerge (id , sha )
54
54
}
55
55
return nil
56
56
}
@@ -98,8 +98,8 @@ func RemoveScheduledAutoMerge(ctx context.Context, doer *user_model.User, pull *
98
98
})
99
99
}
100
100
101
- // MergeScheduledPullRequestsBySha merges a previously scheduled pull request(s) when all checks succeeded
102
- func MergeScheduledPullRequestsBySha (ctx context.Context , sha string , repo * repo_model.Repository ) error {
101
+ // StartPullRequestAutoMergeCheckBySHA start an automerge check task for repository and SHA
102
+ func StartPullRequestAutoMergeCheckBySHA (ctx context.Context , sha string , repo * repo_model.Repository ) error {
103
103
pulls , err := getPullRequestsByHeadSHA (ctx , sha , repo , func (pr * issues_model.PullRequest ) bool {
104
104
return ! pr .HasMerged && pr .CanAutoMerge ()
105
105
})
@@ -114,13 +114,31 @@ func MergeScheduledPullRequestsBySha(ctx context.Context, sha string, repo *repo
114
114
return nil
115
115
}
116
116
117
- // MergeScheduledPullRequest merges a previously scheduled pull request when all checks succeeded
118
- func MergeScheduledPullRequest ( pull * issues_model.PullRequest ) {
117
+ // StartPullRequestAutoMergeCheck start an automerge check task for a pull request
118
+ func StartPullRequestAutoMergeCheck ( ctx context. Context , pull * issues_model.PullRequest ) {
119
119
if pull == nil || pull .HasMerged || ! pull .CanAutoMerge () {
120
120
return
121
121
}
122
122
123
- addToQueue (pull , pull .HeadCommitID )
123
+ if err := pull .LoadBaseRepo (ctx ); err != nil {
124
+ log .Error ("LoadBaseRepo: %v" , err )
125
+ return
126
+ }
127
+
128
+ gitRepo , err := gitrepo .OpenRepository (ctx , pull .BaseRepo )
129
+ if err != nil {
130
+ log .Error ("OpenRepository: %v" , err )
131
+ return
132
+ }
133
+ defer gitRepo .Close ()
134
+
135
+ commitID , err := gitRepo .GetRefCommitID (pull .GetGitRefName ())
136
+ if err != nil {
137
+ log .Error ("GetRefCommitID: %v" , err )
138
+ return
139
+ }
140
+
141
+ addToQueue (pull , commitID )
124
142
}
125
143
126
144
func getPullRequestsByHeadSHA (ctx context.Context , sha string , repo * repo_model.Repository , filter func (* issues_model.PullRequest ) bool ) (map [int64 ]* issues_model.PullRequest , error ) {
@@ -173,7 +191,8 @@ func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.
173
191
return pulls , nil
174
192
}
175
193
176
- func handlePull (pullID int64 , sha string ) {
194
+ // handlePullRequestAutoMerge merge the pull request if all checks are successful
195
+ func handlePullRequestAutoMerge (pullID int64 , sha string ) {
177
196
ctx , _ , finished := process .GetManager ().AddContext (graceful .GetManager ().HammerContext (),
178
197
fmt .Sprintf ("Handle AutoMerge of PR[%d] with sha[%s]" , pullID , sha ))
179
198
defer finished ()
@@ -194,24 +213,50 @@ func handlePull(pullID int64, sha string) {
194
213
return
195
214
}
196
215
216
+ if err = pr .LoadBaseRepo (ctx ); err != nil {
217
+ log .Error ("%-v LoadBaseRepo: %v" , pr , err )
218
+ return
219
+ }
220
+
221
+ // check the sha is the same as pull request head commit id
222
+ baseGitRepo , err := gitrepo .OpenRepository (ctx , pr .BaseRepo )
223
+ if err != nil {
224
+ log .Error ("OpenRepository: %v" , err )
225
+ return
226
+ }
227
+ defer baseGitRepo .Close ()
228
+
229
+ headCommitID , err := baseGitRepo .GetRefCommitID (pr .GetGitRefName ())
230
+ if err != nil {
231
+ log .Error ("GetRefCommitID: %v" , err )
232
+ return
233
+ }
234
+ if headCommitID != sha {
235
+ log .Warn ("Head commit id of auto merge %-v does not match sha [%s]" , pr , sha )
236
+ return
237
+ }
238
+
197
239
// Get all checks for this pr
198
240
// We get the latest sha commit hash again to handle the case where the check of a previous push
199
241
// did not succeed or was not finished yet.
200
-
201
242
if err = pr .LoadHeadRepo (ctx ); err != nil {
202
243
log .Error ("%-v LoadHeadRepo: %v" , pr , err )
203
244
return
204
245
}
205
246
206
- headGitRepo , err := gitrepo .OpenRepository (ctx , pr .HeadRepo )
207
- if err != nil {
208
- log .Error ("OpenRepository %-v: %v" , pr .HeadRepo , err )
209
- return
247
+ var headGitRepo * git.Repository
248
+ if pr .BaseRepoID == pr .HeadRepoID {
249
+ headGitRepo = baseGitRepo
250
+ } else {
251
+ headGitRepo , err = gitrepo .OpenRepository (ctx , pr .HeadRepo )
252
+ if err != nil {
253
+ log .Error ("OpenRepository %-v: %v" , pr .HeadRepo , err )
254
+ return
255
+ }
256
+ defer headGitRepo .Close ()
210
257
}
211
- defer headGitRepo .Close ()
212
258
213
259
headBranchExist := headGitRepo .IsBranchExist (pr .HeadBranch )
214
-
215
260
if pr .HeadRepo == nil || ! headBranchExist {
216
261
log .Warn ("Head branch of auto merge %-v does not exist [HeadRepoID: %d, Branch: %s]" , pr , pr .HeadRepoID , pr .HeadBranch )
217
262
return
@@ -250,23 +295,6 @@ func handlePull(pullID int64, sha string) {
250
295
return
251
296
}
252
297
253
- var baseGitRepo * git.Repository
254
- if pr .BaseRepoID == pr .HeadRepoID {
255
- baseGitRepo = headGitRepo
256
- } else {
257
- if err = pr .LoadBaseRepo (ctx ); err != nil {
258
- log .Error ("%-v LoadBaseRepo: %v" , pr , err )
259
- return
260
- }
261
-
262
- baseGitRepo , err = gitrepo .OpenRepository (ctx , pr .BaseRepo )
263
- if err != nil {
264
- log .Error ("OpenRepository %-v: %v" , pr .BaseRepo , err )
265
- return
266
- }
267
- defer baseGitRepo .Close ()
268
- }
269
-
270
298
if err := pull_service .Merge (ctx , pr , doer , baseGitRepo , scheduledPRM .MergeStyle , "" , scheduledPRM .Message , true ); err != nil {
271
299
log .Error ("pull_service.Merge: %v" , err )
272
300
return
0 commit comments