Skip to content

Fix duplicate Reviewed-by trailers #24796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions models/fixtures/review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,30 @@
official: true
updated_unix: 1603196749
created_unix: 1603196749

-
id: 13
type: 1
reviewer_id: 5
issue_id: 11
content: "old review from user5"
updated_unix: 946684820
created_unix: 946684820

-
id: 14
type: 1
reviewer_id: 5
issue_id: 11
content: "duplicate review from user5 (latest)"
updated_unix: 946684830
created_unix: 946684830

-
id: 15
type: 1
reviewer_id: 6
issue_id: 11
content: "singular review from user6 and final review for this pr"
updated_unix: 946684831
created_unix: 946684831
1 change: 1 addition & 0 deletions models/issues/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ func (pr *PullRequest) getReviewedByLines(writer io.Writer) error {
Type: ReviewTypeApprove,
IssueID: pr.IssueID,
OfficialOnly: setting.Repository.PullRequest.DefaultMergeMessageOfficialApproversOnly,
LatestOnly: true,
})
if err != nil {
log.Error("Unable to FindReviews for PR ID %d: %v", pr.ID, err)
Expand Down
12 changes: 12 additions & 0 deletions models/issues/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -325,3 +326,14 @@ func TestParseCodeOwnersLine(t *testing.T) {
assert.Equal(t, g.Tokens, tokens, "Codeowners tokenizer failed")
}
}

func TestGetApprovers(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 5})
// Official reviews are already deduplicated. Allow unofficial reviews
// to assert that there are no duplicated approvers.
setting.Repository.PullRequest.DefaultMergeMessageOfficialApproversOnly = false
approvers := pr.GetApprovers()
expected := "Reviewed-by: User Five <[email protected]>\nReviewed-by: User Six <[email protected]>\n"
assert.EqualValues(t, expected, approvers)
}
11 changes: 10 additions & 1 deletion models/issues/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ type FindReviewOptions struct {
IssueID int64
ReviewerID int64
OfficialOnly bool
LatestOnly bool // Latest per-reviewer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading the code again, I think it's better to add a FindLatestReviews , instead of adding LatestOnly to FindReviewOptions.

The reason is, the newly added LatestOnly is not respected by toCond, but FindReviewOptions is shared by FindReviewOptions and CountReviewOptions. So, if LatestOnly=true, FindReviewOptions and CountReviewOptions behave differently, it would cause bugs or confuse developers in the future.


If we introduce a FindLatestReviews, then everything is clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very good point, thanks for the feedback. I've made the requested revisions.

}

func (opts *FindReviewOptions) toCond() builder.Cond {
Expand All @@ -265,10 +266,18 @@ func (opts *FindReviewOptions) toCond() builder.Cond {
// FindReviews returns reviews passing FindReviewOptions
func FindReviews(ctx context.Context, opts FindReviewOptions) ([]*Review, error) {
reviews := make([]*Review, 0, 10)
sess := db.GetEngine(ctx).Where(opts.toCond())
cond := opts.toCond()
sess := db.GetEngine(ctx).Where(cond)
if opts.Page > 0 {
sess = db.SetSessionPagination(sess, &opts)
}
if opts.LatestOnly {
sess.In("id", builder.
Select("max ( id ) ").
From("review").
Where(cond).
GroupBy("reviewer_id"))
}
return reviews, sess.
Asc("created_unix").
Asc("id").
Expand Down
25 changes: 25 additions & 0 deletions models/issues/review_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ func TestFindReviews(t *testing.T) {
assert.Equal(t, "Demo Review", reviews[0].Content)
}

func TestFindReviews_NotLatestOnly(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
reviews, err := issues_model.FindReviews(db.DefaultContext, issues_model.FindReviewOptions{
Type: issues_model.ReviewTypeApprove,
IssueID: 11,
LatestOnly: false,
})
assert.NoError(t, err)
assert.Len(t, reviews, 3)
assert.Equal(t, "old review from user5", reviews[0].Content)
}

func TestFindReviews_LatestOnly(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
reviews, err := issues_model.FindReviews(db.DefaultContext, issues_model.FindReviewOptions{
Type: issues_model.ReviewTypeApprove,
IssueID: 11,
LatestOnly: true,
})
assert.NoError(t, err)
assert.Len(t, reviews, 2)
assert.Equal(t, "duplicate review from user5 (latest)", reviews[0].Content)
assert.Equal(t, "singular review from user6 and final review for this pr", reviews[1].Content)
}

func TestGetCurrentReview(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
Expand Down