summaryrefslogtreecommitdiffstats
path: root/services/automerge/automerge.go
diff options
context:
space:
mode:
Diffstat (limited to 'services/automerge/automerge.go')
-rw-r--r--services/automerge/automerge.go40
1 files changed, 31 insertions, 9 deletions
diff --git a/services/automerge/automerge.go b/services/automerge/automerge.go
index e098f2cec0..85af2659c6 100644
--- a/services/automerge/automerge.go
+++ b/services/automerge/automerge.go
@@ -12,6 +12,7 @@ import (
"strings"
"code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/models/db"
pull_model "code.gitea.io/gitea/models/pull"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
@@ -61,17 +62,38 @@ func addToQueue(pr *models.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 *models.PullRequest, style repo_model.MergeStyle, message string) (scheduled bool, err error) {
- lastCommitStatus, err := pull_service.GetPullRequestCommitStatusState(ctx, pull)
- if err != nil {
- return false, err
- }
+ err = db.WithTx(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 false, nil
- }
+ // 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
+ }
+ scheduled = true
+
+ _, err = models.CreateAutoMergeComment(ctx, models.CommentTypePRScheduledToAutoMerge, pull, doer)
+ return err
+ }, ctx)
+ return
+}
- return true, pull_model.ScheduleAutoMerge(ctx, doer, pull.ID, style, message)
+// RemoveScheduledAutoMerge cancels a previously scheduled pull request
+func RemoveScheduledAutoMerge(ctx context.Context, doer *user_model.User, pull *models.PullRequest) error {
+ return db.WithTx(func(ctx context.Context) error {
+ if err := pull_model.DeleteScheduledAutoMerge(ctx, pull.ID); err != nil {
+ return err
+ }
+
+ _, err := models.CreateAutoMergeComment(ctx, models.CommentTypePRUnScheduledToAutoMerge, pull, doer)
+ return err
+ }, ctx)
}
// MergeScheduledPullRequest merges a previously scheduled pull request when all checks succeeded