summaryrefslogtreecommitdiffstats
path: root/services/automerge
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2024-05-21 17:23:22 +0200
committerEarl Warren <contact@earl-warren.org>2024-05-26 19:01:36 +0200
commit7d7ea45465d6cd1ea0ec549a71f67b4a8ff930cf (patch)
treeb50a363da095840299246502d0c0ae533d203398 /services/automerge
parentDon't include link of deleted branch when listing branches (#31028) (diff)
downloadforgejo-7d7ea45465d6cd1ea0ec549a71f67b4a8ff930cf.tar.xz
forgejo-7d7ea45465d6cd1ea0ec549a71f67b4a8ff930cf.zip
Fix automerge will not work because of some events haven't been triggered (#30780)
Replace #25741 Close #24445 Close #30658 Close #20646 ~Depends on #30805~ Since #25741 has been rewritten totally, to make the contribution easier, I will continue the work in this PR. Thanks @6543 --------- Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> (cherry picked from commit c6cf96d31d80ab79d370a6192fd761b4443daec2) Conflicts: tests/integration/editor_test.go trivial context conflict because of 75ce1e2ac1 [GITEA] Allow user to select email for file operations in Web UI tests/integration/pull_merge_test.go trivial context conflicts in imports because more tests were added in Forgejo
Diffstat (limited to 'services/automerge')
-rw-r--r--services/automerge/automerge.go108
-rw-r--r--services/automerge/notify.go46
2 files changed, 116 insertions, 38 deletions
diff --git a/services/automerge/automerge.go b/services/automerge/automerge.go
index bd1317c7f4..10f3c28d56 100644
--- a/services/automerge/automerge.go
+++ b/services/automerge/automerge.go
@@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/queue"
+ notify_service "code.gitea.io/gitea/services/notify"
pull_service "code.gitea.io/gitea/services/pull"
)
@@ -30,6 +31,8 @@ var prAutoMergeQueue *queue.WorkerPoolQueue[string]
// Init runs the task queue to that handles auto merges
func Init() error {
+ notify_service.RegisterNotifier(NewNotifier())
+
prAutoMergeQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "pr_auto_merge", handler)
if prAutoMergeQueue == nil {
return fmt.Errorf("unable to create pr_auto_merge queue")
@@ -47,7 +50,7 @@ func handler(items ...string) []string {
log.Error("could not parse data from pr_auto_merge queue (%v): %v", s, err)
continue
}
- handlePull(id, sha)
+ handlePullRequestAutoMerge(id, sha)
}
return nil
}
@@ -62,16 +65,6 @@ func addToQueue(pr *issues_model.PullRequest, sha string) {
// ScheduleAutoMerge if schedule is false and no error, pull can be merged directly
func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest, style repo_model.MergeStyle, message string) (scheduled bool, err error) {
err = db.WithTx(ctx, func(ctx context.Context) error {
- lastCommitStatus, err := pull_service.GetPullRequestCommitStatusState(ctx, pull)
- if err != nil {
- return err
- }
-
- // we don't need to schedule
- if lastCommitStatus.IsSuccess() {
- return nil
- }
-
if err := pull_model.ScheduleAutoMerge(ctx, doer, pull.ID, style, message); err != nil {
return err
}
@@ -95,8 +88,8 @@ func RemoveScheduledAutoMerge(ctx context.Context, doer *user_model.User, pull *
})
}
-// MergeScheduledPullRequest merges a previously scheduled pull request when all checks succeeded
-func MergeScheduledPullRequest(ctx context.Context, sha string, repo *repo_model.Repository) error {
+// StartPRCheckAndAutoMergeBySHA start an automerge check and auto merge task for all pull requests of repository and SHA
+func StartPRCheckAndAutoMergeBySHA(ctx context.Context, sha string, repo *repo_model.Repository) error {
pulls, err := getPullRequestsByHeadSHA(ctx, sha, repo, func(pr *issues_model.PullRequest) bool {
return !pr.HasMerged && pr.CanAutoMerge()
})
@@ -111,6 +104,32 @@ func MergeScheduledPullRequest(ctx context.Context, sha string, repo *repo_model
return nil
}
+// StartPRCheckAndAutoMerge start an automerge check and auto merge task for a pull request
+func StartPRCheckAndAutoMerge(ctx context.Context, pull *issues_model.PullRequest) {
+ if pull == nil || pull.HasMerged || !pull.CanAutoMerge() {
+ return
+ }
+
+ if err := pull.LoadBaseRepo(ctx); err != nil {
+ log.Error("LoadBaseRepo: %v", err)
+ return
+ }
+
+ gitRepo, err := gitrepo.OpenRepository(ctx, pull.BaseRepo)
+ if err != nil {
+ log.Error("OpenRepository: %v", err)
+ return
+ }
+ defer gitRepo.Close()
+ commitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
+ if err != nil {
+ log.Error("GetRefCommitID: %v", err)
+ return
+ }
+
+ addToQueue(pull, commitID)
+}
+
func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.Repository, filter func(*issues_model.PullRequest) bool) (map[int64]*issues_model.PullRequest, error) {
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
if err != nil {
@@ -161,7 +180,8 @@ func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.
return pulls, nil
}
-func handlePull(pullID int64, sha string) {
+// handlePullRequestAutoMerge merge the pull request if all checks are successful
+func handlePullRequestAutoMerge(pullID int64, sha string) {
ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(),
fmt.Sprintf("Handle AutoMerge of PR[%d] with sha[%s]", pullID, sha))
defer finished()
@@ -182,24 +202,50 @@ func handlePull(pullID int64, sha string) {
return
}
+ if err = pr.LoadBaseRepo(ctx); err != nil {
+ log.Error("%-v LoadBaseRepo: %v", pr, err)
+ return
+ }
+
+ // check the sha is the same as pull request head commit id
+ baseGitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo)
+ if err != nil {
+ log.Error("OpenRepository: %v", err)
+ return
+ }
+ defer baseGitRepo.Close()
+
+ headCommitID, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
+ if err != nil {
+ log.Error("GetRefCommitID: %v", err)
+ return
+ }
+ if headCommitID != sha {
+ log.Warn("Head commit id of auto merge %-v does not match sha [%s], it may means the head branch has been updated. Just ignore this request because a new request expected in the queue", pr, sha)
+ return
+ }
+
// Get all checks for this pr
// We get the latest sha commit hash again to handle the case where the check of a previous push
// did not succeed or was not finished yet.
-
if err = pr.LoadHeadRepo(ctx); err != nil {
log.Error("%-v LoadHeadRepo: %v", pr, err)
return
}
- headGitRepo, err := gitrepo.OpenRepository(ctx, pr.HeadRepo)
- if err != nil {
- log.Error("OpenRepository %-v: %v", pr.HeadRepo, err)
- return
+ var headGitRepo *git.Repository
+ if pr.BaseRepoID == pr.HeadRepoID {
+ headGitRepo = baseGitRepo
+ } else {
+ headGitRepo, err = gitrepo.OpenRepository(ctx, pr.HeadRepo)
+ if err != nil {
+ log.Error("OpenRepository %-v: %v", pr.HeadRepo, err)
+ return
+ }
+ defer headGitRepo.Close()
}
- defer headGitRepo.Close()
headBranchExist := headGitRepo.IsBranchExist(pr.HeadBranch)
-
if pr.HeadRepo == nil || !headBranchExist {
log.Warn("Head branch of auto merge %-v does not exist [HeadRepoID: %d, Branch: %s]", pr, pr.HeadRepoID, pr.HeadBranch)
return
@@ -238,25 +284,11 @@ func handlePull(pullID int64, sha string) {
return
}
- var baseGitRepo *git.Repository
- if pr.BaseRepoID == pr.HeadRepoID {
- baseGitRepo = headGitRepo
- } else {
- if err = pr.LoadBaseRepo(ctx); err != nil {
- log.Error("%-v LoadBaseRepo: %v", pr, err)
- return
- }
-
- baseGitRepo, err = gitrepo.OpenRepository(ctx, pr.BaseRepo)
- if err != nil {
- log.Error("OpenRepository %-v: %v", pr.BaseRepo, err)
- return
- }
- defer baseGitRepo.Close()
- }
-
if err := pull_service.Merge(ctx, pr, doer, baseGitRepo, scheduledPRM.MergeStyle, "", scheduledPRM.Message, true); err != nil {
log.Error("pull_service.Merge: %v", err)
+ // FIXME: if merge failed, we should display some error message to the pull request page.
+ // The resolution is add a new column on automerge table named `error_message` to store the error message and displayed
+ // on the pull request page. But this should not be finished in a bug fix PR which will be backport to release branch.
return
}
}
diff --git a/services/automerge/notify.go b/services/automerge/notify.go
new file mode 100644
index 0000000000..cb078214f6
--- /dev/null
+++ b/services/automerge/notify.go
@@ -0,0 +1,46 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package automerge
+
+import (
+ "context"
+
+ issues_model "code.gitea.io/gitea/models/issues"
+ user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/log"
+ notify_service "code.gitea.io/gitea/services/notify"
+)
+
+type automergeNotifier struct {
+ notify_service.NullNotifier
+}
+
+var _ notify_service.Notifier = &automergeNotifier{}
+
+// NewNotifier create a new automergeNotifier notifier
+func NewNotifier() notify_service.Notifier {
+ return &automergeNotifier{}
+}
+
+func (n *automergeNotifier) PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) {
+ // as a missing / blocking reviews could have blocked a pending automerge let's recheck
+ if review.Type == issues_model.ReviewTypeApprove {
+ if err := StartPRCheckAndAutoMergeBySHA(ctx, review.CommitID, pr.BaseRepo); err != nil {
+ log.Error("StartPullRequestAutoMergeCheckBySHA: %v", err)
+ }
+ }
+}
+
+func (n *automergeNotifier) PullReviewDismiss(ctx context.Context, doer *user_model.User, review *issues_model.Review, comment *issues_model.Comment) {
+ if err := review.LoadIssue(ctx); err != nil {
+ log.Error("LoadIssue: %v", err)
+ return
+ }
+ if err := review.Issue.LoadPullRequest(ctx); err != nil {
+ log.Error("LoadPullRequest: %v", err)
+ return
+ }
+ // as reviews could have blocked a pending automerge let's recheck
+ StartPRCheckAndAutoMerge(ctx, review.Issue.PullRequest)
+}