Skip to content

Commit 15b2cde

Browse files
committed
some improvements
1 parent fb5eb50 commit 15b2cde

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed

routers/private/hook_post_receive.go

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -164,49 +164,10 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
164164

165165
var pusher *user_model.User
166166

167-
// handle pull request merging, a pull request action should only push 1 commit
168-
if opts.PullRequestAction == repo_module.PullRequestActionMerge && len(updates) >= 1 {
169-
// Get the pull request
170-
pr, err := issues_model.GetPullRequestByID(ctx, opts.PullRequestID)
171-
if err != nil {
172-
log.Error("GetPullRequestByID[%d]: %v", opts.PullRequestID, err)
173-
ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
174-
Err: fmt.Sprintf("GetPullRequestByID[%d]: %v", opts.PullRequestID, err),
175-
})
176-
return
177-
}
178-
179-
pusher, err = user_model.GetUserByID(ctx, opts.UserID)
180-
if err != nil {
181-
log.Error("Failed to Update: %s/%s Error: %v", ownerName, repoName, err)
182-
ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
183-
Err: fmt.Sprintf("Failed to Update: %s/%s Error: %v", ownerName, repoName, err),
184-
})
185-
return
186-
}
187-
188-
pr.MergedCommitID = updates[len(updates)-1].NewCommitID
189-
pr.MergedUnix = timeutil.TimeStampNow()
190-
pr.Merger = pusher
191-
pr.MergerID = opts.UserID
192-
193-
if err := db.WithTx(ctx, func(ctx context.Context) error {
194-
// Removing an auto merge pull and ignore if not exist
195-
if err := pull_model.DeleteScheduledAutoMerge(ctx, pr.ID); err != nil && !db.IsErrNotExist(err) {
196-
return fmt.Errorf("DeleteScheduledAutoMerge[%d]: %v", opts.PullRequestID, err)
197-
}
198-
199-
if _, err := pr.SetMerged(ctx); err != nil {
200-
return fmt.Errorf("Failed to SetMerged: %s/%s Error: %v", ownerName, repoName, err)
201-
}
202-
return nil
203-
}); err != nil {
204-
log.Error("%v", err)
205-
ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
206-
Err: err.Error(),
207-
})
208-
return
209-
}
167+
// handle pull request merging, a pull request action should push at least 1 commit
168+
handlePullRequestMerging(ctx, opts, pusher, ownerName, repoName, updates)
169+
if ctx.Written() {
170+
return
210171
}
211172

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

0 commit comments

Comments
 (0)