diff options
author | 6543 <m.huber@kithara.com> | 2024-02-19 14:42:18 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-19 14:42:18 +0100 |
commit | 217d71c48a10265e08b95cc961656b921f61f9ff (patch) | |
tree | 511e26061246ae3dd395136591e3f8ea71fb62f8 /models/issues/review.go | |
parent | 39a77d92d9677b0a0049cb8696960d6d2ac052d6 (diff) | |
download | gitea-217d71c48a10265e08b95cc961656b921f61f9ff.tar.gz gitea-217d71c48a10265e08b95cc961656b921f61f9ff.zip |
Workaround to clean up old reviews on creating a new one (#28554)
close #28542
blocks #28544
---
*Sponsored by Kithara Software GmbH*
Diffstat (limited to 'models/issues/review.go')
-rw-r--r-- | models/issues/review.go | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/models/issues/review.go b/models/issues/review.go index 3aa9d3e2a8..fc110630e0 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -292,8 +292,14 @@ func IsOfficialReviewerTeam(ctx context.Context, issue *Issue, team *organizatio // CreateReview creates a new review based on opts func CreateReview(ctx context.Context, opts CreateReviewOptions) (*Review, error) { + ctx, committer, err := db.TxContext(ctx) + if err != nil { + return nil, err + } + defer committer.Close() + sess := db.GetEngine(ctx) + review := &Review{ - Type: opts.Type, Issue: opts.Issue, IssueID: opts.Issue.ID, Reviewer: opts.Reviewer, @@ -303,15 +309,39 @@ func CreateReview(ctx context.Context, opts CreateReviewOptions) (*Review, error CommitID: opts.CommitID, Stale: opts.Stale, } + if opts.Reviewer != nil { + review.Type = opts.Type review.ReviewerID = opts.Reviewer.ID - } else { - if review.Type != ReviewTypeRequest { - review.Type = ReviewTypeRequest + + reviewCond := builder.Eq{"reviewer_id": opts.Reviewer.ID, "issue_id": opts.Issue.ID} + // make sure user review requests are cleared + if opts.Type != ReviewTypePending { + if _, err := sess.Where(reviewCond.And(builder.Eq{"type": ReviewTypeRequest})).Delete(new(Review)); err != nil { + return nil, err + } } + // make sure if the created review gets dismissed no old review surface + // other types can be ignored, as they don't affect branch protection + if opts.Type == ReviewTypeApprove || opts.Type == ReviewTypeReject { + if _, err := sess.Where(reviewCond.And(builder.In("type", ReviewTypeApprove, ReviewTypeReject))). + Cols("dismissed").Update(&Review{Dismissed: true}); err != nil { + return nil, err + } + } + + } else if opts.ReviewerTeam != nil { + review.Type = ReviewTypeRequest review.ReviewerTeamID = opts.ReviewerTeam.ID + + } else { + return nil, fmt.Errorf("provide either reviewer or reviewer team") + } + + if _, err := sess.Insert(review); err != nil { + return nil, err } - return review, db.Insert(ctx, review) + return review, committer.Commit() } // GetCurrentReview returns the current pending review of reviewer for given issue |