aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
author6543 <m.huber@kithara.com>2024-02-19 14:42:18 +0100
committerGitHub <noreply@github.com>2024-02-19 14:42:18 +0100
commit217d71c48a10265e08b95cc961656b921f61f9ff (patch)
tree511e26061246ae3dd395136591e3f8ea71fb62f8 /models
parent39a77d92d9677b0a0049cb8696960d6d2ac052d6 (diff)
downloadgitea-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')
-rw-r--r--models/issues/review.go40
-rw-r--r--models/unittest/unit_tests.go8
2 files changed, 39 insertions, 9 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
diff --git a/models/unittest/unit_tests.go b/models/unittest/unit_tests.go
index d47bceea1e..75898436fc 100644
--- a/models/unittest/unit_tests.go
+++ b/models/unittest/unit_tests.go
@@ -131,8 +131,8 @@ func AssertSuccessfulInsert(t assert.TestingT, beans ...any) {
}
// AssertCount assert the count of a bean
-func AssertCount(t assert.TestingT, bean, expected any) {
- assert.EqualValues(t, expected, GetCount(t, bean))
+func AssertCount(t assert.TestingT, bean, expected any) bool {
+ return assert.EqualValues(t, expected, GetCount(t, bean))
}
// AssertInt64InRange assert value is in range [low, high]
@@ -150,7 +150,7 @@ func GetCountByCond(t assert.TestingT, tableName string, cond builder.Cond) int6
}
// AssertCountByCond test the count of database entries matching bean
-func AssertCountByCond(t assert.TestingT, tableName string, cond builder.Cond, expected int) {
- assert.EqualValues(t, expected, GetCountByCond(t, tableName, cond),
+func AssertCountByCond(t assert.TestingT, tableName string, cond builder.Cond, expected int) bool {
+ return assert.EqualValues(t, expected, GetCountByCond(t, tableName, cond),
"Failed consistency test, the counted bean (of table %s) was %+v", tableName, cond)
}