Skip to content

Commit 970d85e

Browse files
author
Cory Todd
committed
Add tests for FindReviews and GetApprovers
Not all SQL dialects will work with Distinct so use a subquery grouping instead. Add 3 reviews to the test data to simulate the scenario being fixed by this patch. - One reviewer submitting 2 reviews. Only the latest should be returned. - One reviewer submitting 1 review. This is the oldest review and should still be the first approver. Omitting LatestOnly should preserve existing behavior as it is used by the REST API. Signed-off-by: Cory Todd <cory.todd@canonical.com>
1 parent 8031b34 commit 970d85e

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

models/fixtures/review.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,30 @@
105105
official: true
106106
updated_unix: 1603196749
107107
created_unix: 1603196749
108+
109+
-
110+
id: 13
111+
type: 1
112+
reviewer_id: 5
113+
issue_id: 11
114+
content: "old review from user5"
115+
updated_unix: 946684820
116+
created_unix: 946684820
117+
118+
-
119+
id: 14
120+
type: 1
121+
reviewer_id: 5
122+
issue_id: 11
123+
content: "duplicate review from user5 (latest)"
124+
updated_unix: 946684830
125+
created_unix: 946684830
126+
127+
-
128+
id: 15
129+
type: 1
130+
reviewer_id: 6
131+
issue_id: 11
132+
content: "singular review from user6 and final review for this pr"
133+
updated_unix: 946684831
134+
created_unix: 946684831

models/issues/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ func (pr *PullRequest) getReviewedByLines(writer io.Writer) error {
408408
Type: ReviewTypeApprove,
409409
IssueID: pr.IssueID,
410410
OfficialOnly: setting.Repository.PullRequest.DefaultMergeMessageOfficialApproversOnly,
411-
Distinct: true,
411+
LatestOnly: true,
412412
})
413413
if err != nil {
414414
log.Error("Unable to FindReviews for PR ID %d: %v", pr.ID, err)

models/issues/pull_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
issues_model "code.gitea.io/gitea/models/issues"
1111
"code.gitea.io/gitea/models/unittest"
1212
user_model "code.gitea.io/gitea/models/user"
13+
"code.gitea.io/gitea/modules/setting"
1314

1415
"github.com/stretchr/testify/assert"
1516
)
@@ -325,3 +326,14 @@ func TestParseCodeOwnersLine(t *testing.T) {
325326
assert.Equal(t, g.Tokens, tokens, "Codeowners tokenizer failed")
326327
}
327328
}
329+
330+
func TestGetApprovers(t *testing.T) {
331+
assert.NoError(t, unittest.PrepareTestDatabase())
332+
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 5})
333+
// Official reviews are already deduplicated. Allow unofficial reviews
334+
// to assert that there are no duplicated approvers.
335+
setting.Repository.PullRequest.DefaultMergeMessageOfficialApproversOnly = false
336+
approvers := pr.GetApprovers()
337+
expected := "Reviewed-by: User Five <user5@example.com>\nReviewed-by: User Six <user6@example.com>\n"
338+
assert.EqualValues(t, expected, approvers)
339+
}

models/issues/review.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ type FindReviewOptions struct {
243243
IssueID int64
244244
ReviewerID int64
245245
OfficialOnly bool
246-
Distinct bool
246+
LatestOnly bool // Latest per-reviewer
247247
}
248248

249249
func (opts *FindReviewOptions) toCond() builder.Cond {
@@ -270,14 +270,15 @@ func FindReviews(ctx context.Context, opts FindReviewOptions) ([]*Review, error)
270270
if opts.Page > 0 {
271271
sess = db.SetSessionPagination(sess, &opts)
272272
}
273-
distinct := ""
274-
if opts.Distinct {
275-
distinct = "reviewer_id"
273+
if opts.LatestOnly {
274+
sess.In("id", builder.
275+
Select("max ( id ) ").
276+
From("review").
277+
GroupBy("reviewer_id"))
276278
}
277279
return reviews, sess.
278280
Asc("created_unix").
279281
Asc("id").
280-
Distinct(distinct).
281282
Find(&reviews)
282283
}
283284

models/issues/review_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ func TestFindReviews(t *testing.T) {
7171
assert.Equal(t, "Demo Review", reviews[0].Content)
7272
}
7373

74+
func TestFindReviews_NotLatestOnly(t *testing.T) {
75+
assert.NoError(t, unittest.PrepareTestDatabase())
76+
reviews, err := issues_model.FindReviews(db.DefaultContext, issues_model.FindReviewOptions{
77+
Type: issues_model.ReviewTypeApprove,
78+
IssueID: 11,
79+
LatestOnly: false,
80+
})
81+
assert.NoError(t, err)
82+
assert.Len(t, reviews, 3)
83+
assert.Equal(t, "old review from user5", reviews[0].Content)
84+
}
85+
86+
func TestFindReviews_LatestOnly(t *testing.T) {
87+
assert.NoError(t, unittest.PrepareTestDatabase())
88+
reviews, err := issues_model.FindReviews(db.DefaultContext, issues_model.FindReviewOptions{
89+
Type: issues_model.ReviewTypeApprove,
90+
IssueID: 11,
91+
LatestOnly: true,
92+
})
93+
assert.NoError(t, err)
94+
assert.Len(t, reviews, 2)
95+
assert.Equal(t, "duplicate review from user5 (latest)", reviews[0].Content)
96+
assert.Equal(t, "singular review from user6 and final review for this pr", reviews[1].Content)
97+
}
98+
7499
func TestGetCurrentReview(t *testing.T) {
75100
assert.NoError(t, unittest.PrepareTestDatabase())
76101
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})

0 commit comments

Comments
 (0)