@@ -323,51 +323,53 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
323
323
324
324
const contextCachePusherKey = "hook_post_receive_pusher"
325
325
326
+ // handlePullRequestMerging handle pull request merging, a pull request action should only push 1 commit
326
327
func handlePullRequestMerging (ctx * gitea_context.PrivateContext , opts * private.HookOptions , ownerName , repoName string , updates []* repo_module.PushUpdateOptions ) {
327
- // handle pull request merging, a pull request action should only push 1 commit
328
- if opts .PullRequestAction == repo_module .PullRequestActionMerge && len (updates ) >= 1 {
329
- // Get the pull request
330
- pr , err := issues_model .GetPullRequestByID (ctx , opts .PullRequestID )
331
- if err != nil {
332
- log .Error ("GetPullRequestByID[%d]: %v" , opts .PullRequestID , err )
333
- ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
334
- Err : fmt .Sprintf ("GetPullRequestByID[%d]: %v" , opts .PullRequestID , err ),
335
- })
336
- return
337
- }
328
+ if opts .PushTrigger != string (repo_module .PushTriggerPRMergeToBase ) || len (updates ) < 1 {
329
+ return
330
+ }
338
331
339
- pusher , err := cache .GetWithContextCache (ctx , contextCachePusherKey , opts .UserID , func () (* user_model.User , error ) {
340
- return user_model .GetUserByID (ctx , opts .UserID )
332
+ // Get the pull request
333
+ pr , err := issues_model .GetPullRequestByID (ctx , opts .PullRequestID )
334
+ if err != nil {
335
+ 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 ),
341
338
})
342
- if err != nil {
343
- log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
344
- ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
345
- Err : fmt .Sprintf ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
346
- })
347
- return
348
- }
339
+ return
340
+ }
349
341
350
- pr .MergedCommitID = updates [len (updates )- 1 ].NewCommitID
351
- pr .MergedUnix = timeutil .TimeStampNow ()
352
- pr .Merger = pusher
353
- pr .MergerID = opts .UserID
342
+ pusher , err := cache .GetWithContextCache (ctx , contextCachePusherKey , opts .UserID , func () (* user_model.User , error ) {
343
+ return user_model .GetUserByID (ctx , opts .UserID )
344
+ })
345
+ if err != nil {
346
+ 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
+ })
350
+ return
351
+ }
354
352
355
- if err := db .WithTx (ctx , func (ctx context.Context ) error {
356
- // Removing an auto merge pull and ignore if not exist
357
- if err := pull_model .DeleteScheduledAutoMerge (ctx , pr .ID ); err != nil && ! db .IsErrNotExist (err ) {
358
- return fmt .Errorf ("DeleteScheduledAutoMerge[%d]: %v" , opts .PullRequestID , err )
359
- }
353
+ pr .MergedCommitID = updates [len (updates )- 1 ].NewCommitID
354
+ pr .MergedUnix = timeutil .TimeStampNow ()
355
+ pr .Merger = pusher
356
+ pr .MergerID = opts .UserID
360
357
361
- if _ , err := pr .SetMerged (ctx ); err != nil {
362
- return fmt .Errorf ("Failed to SetMerged: %s/%s Error: %v" , ownerName , repoName , err )
363
- }
364
- return nil
365
- }); err != nil {
366
- log .Error ("%v" , err )
367
- ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
368
- Err : err .Error (),
369
- })
370
- return
358
+ if err := db .WithTx (ctx , func (ctx context.Context ) error {
359
+ // Removing an auto merge pull and ignore if not exist
360
+ if err := pull_model .DeleteScheduledAutoMerge (ctx , pr .ID ); err != nil && ! db .IsErrNotExist (err ) {
361
+ return fmt .Errorf ("DeleteScheduledAutoMerge[%d]: %v" , opts .PullRequestID , err )
371
362
}
363
+
364
+ if _ , err := pr .SetMerged (ctx ); err != nil {
365
+ return fmt .Errorf ("Failed to SetMerged: %s/%s Error: %v" , ownerName , repoName , err )
366
+ }
367
+ return nil
368
+ }); err != nil {
369
+ log .Error ("%v" , err )
370
+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
371
+ Err : err .Error (),
372
+ })
373
+ return
372
374
}
373
375
}
0 commit comments