@@ -164,9 +164,11 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
164
164
}
165
165
166
166
// handle pull request merging, a pull request action should push at least 1 commit
167
- handlePullRequestMerging (ctx , opts , ownerName , repoName , updates )
168
- if ctx .Written () {
169
- return
167
+ if opts .PushTrigger == repo_module .PushTriggerPRMergeToBase {
168
+ handlePullRequestMerging (ctx , opts , ownerName , repoName , updates )
169
+ if ctx .Written () {
170
+ return
171
+ }
170
172
}
171
173
172
174
isPrivate := opts .GitPushOptions .Bool (private .GitPushOptionRepoPrivate )
@@ -183,9 +185,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
183
185
wasEmpty = repo .IsEmpty
184
186
}
185
187
186
- pusher , err := cache .GetWithContextCache (ctx , contextCachePusherKey , opts .UserID , func () (* user_model.User , error ) {
187
- return user_model .GetUserByID (ctx , opts .UserID )
188
- })
188
+ pusher , err := loadContextCacheUser (ctx , opts .UserID )
189
189
if err != nil {
190
190
log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
191
191
ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
@@ -321,55 +321,55 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
321
321
})
322
322
}
323
323
324
- const contextCachePusherKey = "hook_post_receive_pusher"
324
+ func loadContextCacheUser (ctx context.Context , id int64 ) (* user_model.User , error ) {
325
+ return cache .GetWithContextCache (ctx , "hook_post_receive_user" , id , func () (* user_model.User , error ) {
326
+ return user_model .GetUserByID (ctx , id )
327
+ })
328
+ }
325
329
326
330
// handlePullRequestMerging handle pull request merging, a pull request action should only push 1 commit
327
331
func handlePullRequestMerging (ctx * gitea_context.PrivateContext , opts * private.HookOptions , ownerName , repoName string , updates []* repo_module.PushUpdateOptions ) {
328
- if opts .PushTrigger != string (repo_module .PushTriggerPRMergeToBase ) || len (updates ) < 1 {
332
+ if len (updates ) == 0 {
333
+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
334
+ Err : fmt .Sprintf ("Pushing a merged PR (pr:%d) no commits pushed " , opts .PullRequestID ),
335
+ })
329
336
return
330
337
}
338
+ if len (updates ) != 1 && ! setting .IsProd {
339
+ // it shouldn't happen
340
+ panic (fmt .Sprintf ("Pushing a merged PR (pr:%d) should only push 1 commit, but got %d" , opts .PullRequestID , len (updates )))
341
+ }
331
342
332
- // Get the pull request
333
343
pr , err := issues_model .GetPullRequestByID (ctx , opts .PullRequestID )
334
344
if err != nil {
335
345
log .Error ("GetPullRequestByID[%d]: %v" , opts .PullRequestID , err )
336
- ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
337
- Err : fmt .Sprintf ("GetPullRequestByID[%d]: %v" , opts .PullRequestID , err ),
338
- })
346
+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {Err : "GetPullRequestByID failed" })
339
347
return
340
348
}
341
349
342
- pusher , err := cache .GetWithContextCache (ctx , contextCachePusherKey , opts .UserID , func () (* user_model.User , error ) {
343
- return user_model .GetUserByID (ctx , opts .UserID )
344
- })
350
+ pusher , err := loadContextCacheUser (ctx , opts .UserID )
345
351
if err != nil {
346
352
log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
347
- ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
348
- Err : fmt .Sprintf ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
349
- })
353
+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {Err : "Load pusher user failed" })
350
354
return
351
355
}
352
356
353
357
pr .MergedCommitID = updates [len (updates )- 1 ].NewCommitID
354
358
pr .MergedUnix = timeutil .TimeStampNow ()
355
359
pr .Merger = pusher
356
- pr .MergerID = opts .UserID
357
-
358
- if err := db .WithTx (ctx , func (ctx context.Context ) error {
360
+ pr .MergerID = pusher .ID
361
+ err = db .WithTx (ctx , func (ctx context.Context ) error {
359
362
// Removing an auto merge pull and ignore if not exist
360
363
if err := pull_model .DeleteScheduledAutoMerge (ctx , pr .ID ); err != nil && ! db .IsErrNotExist (err ) {
361
364
return fmt .Errorf ("DeleteScheduledAutoMerge[%d]: %v" , opts .PullRequestID , err )
362
365
}
363
-
364
366
if _ , err := pr .SetMerged (ctx ); err != nil {
365
- return fmt .Errorf ("Failed to SetMerged: %s/%s Error: %v" , ownerName , repoName , err )
367
+ return fmt .Errorf ("SetMerged failed : %s/%s Error: %v" , ownerName , repoName , err )
366
368
}
367
369
return nil
368
- }); err != nil {
369
- log .Error ("%v" , err )
370
- ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
371
- Err : err .Error (),
372
- })
373
- return
370
+ })
371
+ if err != nil {
372
+ log .Error ("Failed to update PR to merged: %v" , err )
373
+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {Err : "Failed to update PR to merged" })
374
374
}
375
375
}
0 commit comments