summaryrefslogtreecommitdiffstats
path: root/services/pull
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-07-20 15:18:52 +0800
committerGitHub <noreply@github.com>2023-07-20 15:18:52 +0800
commit01c04607c76391e09620c6f2475b722207d2ee73 (patch)
treeff1cdb3ef3b37f6e8ab3108999e1def1215414fe /services/pull
parentcf467119ffa82177258226fd04946a2105ee7aa0 (diff)
downloadgitea-01c04607c76391e09620c6f2475b722207d2ee73.tar.gz
gitea-01c04607c76391e09620c6f2475b722207d2ee73.zip
Fix bug when pushing to a pull request which enabled dismiss approval automatically (#25882)
Fix #25858 The option `dissmiss stale approvals` was listed on protected branch but never implemented. This PR fixes that. <img width="1006" alt="图片" src="https://github.com/go-gitea/gitea/assets/81045/60bfa968-4db7-4c24-b8be-2e5978f91bb9"> <img width="1021" alt="图片" src="https://github.com/go-gitea/gitea/assets/81045/8dabc14d-2dfe-40c2-94ed-24fcbf6e0e8f">
Diffstat (limited to 'services/pull')
-rw-r--r--services/pull/pull.go11
-rw-r--r--services/pull/review.go55
2 files changed, 60 insertions, 6 deletions
diff --git a/services/pull/pull.go b/services/pull/pull.go
index 0f562b9ee3..5f55a36f33 100644
--- a/services/pull/pull.go
+++ b/services/pull/pull.go
@@ -309,6 +309,17 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string,
if err := issues_model.MarkReviewsAsStale(pr.IssueID); err != nil {
log.Error("MarkReviewsAsStale: %v", err)
}
+
+ // dismiss all approval reviews if protected branch rule item enabled.
+ pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
+ if err != nil {
+ log.Error("GetFirstMatchProtectedBranchRule: %v", err)
+ }
+ if pb != nil && pb.DismissStaleApprovals {
+ if err := DismissApprovalReviews(ctx, doer, pr); err != nil {
+ log.Error("DismissApprovalReviews: %v", err)
+ }
+ }
}
if err := issues_model.MarkReviewsAsNotStale(pr.IssueID, newCommitID); err != nil {
log.Error("MarkReviewsAsNotStale: %v", err)
diff --git a/services/pull/review.go b/services/pull/review.go
index e920bc8c16..59cc607912 100644
--- a/services/pull/review.go
+++ b/services/pull/review.go
@@ -316,6 +316,52 @@ func SubmitReview(ctx context.Context, doer *user_model.User, gitRepo *git.Repos
return review, comm, nil
}
+// DismissApprovalReviews dismiss all approval reviews because of new commits
+func DismissApprovalReviews(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest) error {
+ reviews, err := issues_model.FindReviews(ctx, issues_model.FindReviewOptions{
+ ListOptions: db.ListOptions{
+ ListAll: true,
+ },
+ IssueID: pull.IssueID,
+ Type: issues_model.ReviewTypeApprove,
+ Dismissed: util.OptionalBoolFalse,
+ })
+ if err != nil {
+ return err
+ }
+
+ if err := reviews.LoadIssues(ctx); err != nil {
+ return err
+ }
+
+ return db.WithTx(ctx, func(subCtx context.Context) error {
+ for _, review := range reviews {
+ if err := issues_model.DismissReview(subCtx, review, true); err != nil {
+ return err
+ }
+
+ comment, err := issue_service.CreateComment(ctx, &issues_model.CreateCommentOptions{
+ Doer: doer,
+ Content: "New commits pushed, approval review dismissed automatically according to repository settings",
+ Type: issues_model.CommentTypeDismissReview,
+ ReviewID: review.ID,
+ Issue: review.Issue,
+ Repo: review.Issue.Repo,
+ })
+ if err != nil {
+ return err
+ }
+
+ comment.Review = review
+ comment.Poster = doer
+ comment.Issue = review.Issue
+
+ notification.NotifyPullReviewDismiss(ctx, doer, review, comment)
+ }
+ return nil
+ })
+}
+
// DismissReview dismissing stale review by repo admin
func DismissReview(ctx context.Context, reviewID, repoID int64, message string, doer *user_model.User, isDismiss, dismissPriors bool) (comment *issues_model.Comment, err error) {
review, err := issues_model.GetReviewByID(ctx, reviewID)
@@ -337,12 +383,12 @@ func DismissReview(ctx context.Context, reviewID, repoID int64, message string,
return nil, fmt.Errorf("reviews's repository is not the same as the one we expect")
}
- if err := issues_model.DismissReview(review, isDismiss); err != nil {
+ if err := issues_model.DismissReview(ctx, review, isDismiss); err != nil {
return nil, err
}
if dismissPriors {
- reviews, err := issues_model.GetReviews(ctx, &issues_model.GetReviewOptions{
+ reviews, err := issues_model.FindReviews(ctx, issues_model.FindReviewOptions{
IssueID: review.IssueID,
ReviewerID: review.ReviewerID,
Dismissed: util.OptionalBoolFalse,
@@ -351,7 +397,7 @@ func DismissReview(ctx context.Context, reviewID, repoID int64, message string,
return nil, err
}
for _, oldReview := range reviews {
- if err = issues_model.DismissReview(oldReview, true); err != nil {
+ if err = issues_model.DismissReview(ctx, oldReview, true); err != nil {
return nil, err
}
}
@@ -361,9 +407,6 @@ func DismissReview(ctx context.Context, reviewID, repoID int64, message string,
return nil, nil
}
- if err := review.Issue.LoadPullRequest(ctx); err != nil {
- return nil, err
- }
if err := review.Issue.LoadAttributes(ctx); err != nil {
return nil, err
}