diff options
author | 6543 <6543@obermui.de> | 2022-05-08 15:46:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-08 15:46:34 +0200 |
commit | 6a969681cd862bf153fa7921485278be1e8a092a (patch) | |
tree | f9a23ee85652c0ee179f215b23d928058176ae6c /models/pull | |
parent | 4344a6410788f30848e5153f6356dcdd0774bebc (diff) | |
download | gitea-6a969681cd862bf153fa7921485278be1e8a092a.tar.gz gitea-6a969681cd862bf153fa7921485278be1e8a092a.zip |
Delete related PullAutoMerge and ReviewState on User/Repo Deletion (#19649)
* delete pullautomerges on repo/user deletion
* delete reviewstates on repo/user deletion
* optimize automerhe code
* add index to reviewstate
Diffstat (limited to 'models/pull')
-rw-r--r-- | models/pull/automerge.go | 65 | ||||
-rw-r--r-- | models/pull/review_state.go | 8 |
2 files changed, 14 insertions, 59 deletions
diff --git a/models/pull/automerge.go b/models/pull/automerge.go index fd73f2b0fb..d0aca2e85f 100644 --- a/models/pull/automerge.go +++ b/models/pull/automerge.go @@ -8,7 +8,6 @@ import ( "context" "fmt" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" @@ -59,21 +58,12 @@ func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pullID int64, return ErrAlreadyScheduledToAutoMerge{PullID: pullID} } - if _, err := db.GetEngine(ctx).Insert(&AutoMerge{ + _, err := db.GetEngine(ctx).Insert(&AutoMerge{ DoerID: doer.ID, PullID: pullID, MergeStyle: style, Message: message, - }); err != nil { - return err - } - - pr, err := models.GetPullRequestByID(ctx, pullID) - if err != nil { - return err - } - - _, err = createAutoMergeComment(ctx, models.CommentTypePRScheduledToAutoMerge, pr, doer) + }) return err } @@ -94,50 +84,15 @@ func GetScheduledMergeByPullID(ctx context.Context, pullID int64) (bool, *AutoMe return true, scheduledPRM, nil } -// RemoveScheduledAutoMerge cancels a previously scheduled pull request -func RemoveScheduledAutoMerge(ctx context.Context, doer *user_model.User, pullID int64, comment bool) error { - return db.WithTx(func(ctx context.Context) error { - exist, scheduledPRM, err := GetScheduledMergeByPullID(ctx, pullID) - if err != nil { - return err - } else if !exist { - return models.ErrNotExist{ID: pullID} - } - - if _, err := db.GetEngine(ctx).ID(scheduledPRM.ID).Delete(&AutoMerge{}); err != nil { - return err - } - - // if pull got merged we don't need to add "auto-merge canceled comment" - if !comment || doer == nil { - return nil - } - - pr, err := models.GetPullRequestByID(ctx, pullID) - if err != nil { - return err - } - - _, err = createAutoMergeComment(ctx, models.CommentTypePRUnScheduledToAutoMerge, pr, doer) +// DeleteScheduledAutoMerge delete a scheduled pull request +func DeleteScheduledAutoMerge(ctx context.Context, pullID int64) error { + exist, scheduledPRM, err := GetScheduledMergeByPullID(ctx, pullID) + if err != nil { return err - }, ctx) -} - -// createAutoMergeComment is a internal function, only use it for CommentTypePRScheduledToAutoMerge and CommentTypePRUnScheduledToAutoMerge CommentTypes -func createAutoMergeComment(ctx context.Context, typ models.CommentType, pr *models.PullRequest, doer *user_model.User) (comment *models.Comment, err error) { - if err = pr.LoadIssueCtx(ctx); err != nil { - return + } else if !exist { + return db.ErrNotExist{ID: pullID} } - if err = pr.LoadBaseRepoCtx(ctx); err != nil { - return - } - - comment, err = models.CreateCommentCtx(ctx, &models.CreateCommentOptions{ - Type: typ, - Doer: doer, - Repo: pr.BaseRepo, - Issue: pr.Issue, - }) - return + _, err = db.GetEngine(ctx).ID(scheduledPRM.ID).Delete(&AutoMerge{}) + return err } diff --git a/models/pull/review_state.go b/models/pull/review_state.go index 59a03c20e8..1c465bf766 100644 --- a/models/pull/review_state.go +++ b/models/pull/review_state.go @@ -38,10 +38,10 @@ func (viewedState ViewedState) String() string { type ReviewState struct { ID int64 `xorm:"pk autoincr"` UserID int64 `xorm:"NOT NULL UNIQUE(pull_commit_user)"` - PullID int64 `xorm:"NOT NULL UNIQUE(pull_commit_user) DEFAULT 0"` // Which PR was the review on? - CommitSHA string `xorm:"NOT NULL VARCHAR(40) UNIQUE(pull_commit_user)"` // Which commit was the head commit for the review? - UpdatedFiles map[string]ViewedState `xorm:"NOT NULL LONGTEXT JSON"` // Stores for each of the changed files of a PR whether they have been viewed, changed since last viewed, or not viewed - UpdatedUnix timeutil.TimeStamp `xorm:"updated"` // Is an accurate indicator of the order of commits as we do not expect it to be possible to make reviews on previous commits + PullID int64 `xorm:"NOT NULL INDEX UNIQUE(pull_commit_user) DEFAULT 0"` // Which PR was the review on? + CommitSHA string `xorm:"NOT NULL VARCHAR(40) UNIQUE(pull_commit_user)"` // Which commit was the head commit for the review? + UpdatedFiles map[string]ViewedState `xorm:"NOT NULL LONGTEXT JSON"` // Stores for each of the changed files of a PR whether they have been viewed, changed since last viewed, or not viewed + UpdatedUnix timeutil.TimeStamp `xorm:"updated"` // Is an accurate indicator of the order of commits as we do not expect it to be possible to make reviews on previous commits } func init() { |