Skip to content

Approvals at Branch Protection #5350

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 14 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add type = approval and group by reviewer_id to review
  • Loading branch information
jonasfranz committed Nov 18, 2018
commit 49092a7052b714c07848af1935f174eab983552c
7 changes: 2 additions & 5 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,14 @@ func (protectBranch *ProtectedBranch) HasEnoughApprovals(pr *PullRequest) bool {

// GetGrantedApprovalsCount returns the number of granted approvals for pr. A granted approval must be authored by a user in an approval whitelist.
func (protectBranch *ProtectedBranch) GetGrantedApprovalsCount(pr *PullRequest) int64 {
reviews, err := GetReviewsByPullRequestID(pr.ID)
reviews, err := GetUniqueApprovalsByPullRequestID(pr.ID)
if err != nil {
log.Error(1, "GetReviewsByPullRequestID:", err)
log.Error(1, "GetUniqueApprovalsByPullRequestID:", err)
return 0
}
approvals := int64(0)
userIDs := make([]int64, 0)
for _, review := range reviews {
if review.Type != ReviewTypeApprove {
continue
}
if base.Int64sContains(protectBranch.ApprovalsWhitelistUserIDs, review.ReviewerID) {
approvals++
continue
Expand Down
14 changes: 9 additions & 5 deletions models/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,21 @@ func GetReviewByID(id int64) (*Review, error) {
return getReviewByID(x, id)
}

func getReviewsByPullRequestID(e Engine, prID int64) (reviews []*Review, err error) {
func getUniqueApprovalsByPullRequestID(e Engine, prID int64) (reviews []*Review, err error) {
reviews = make([]*Review, 0)
if err := e.Where("issue_id = ?", prID).Find(&reviews); err != nil {
if err := e.
Where("issue_id = ? AND type = ?", prID, ReviewTypeApprove).
OrderBy("updated_unix").
GroupBy("reviewer_id").
Find(&reviews); err != nil {
return nil, err
}
return
}

// GetReviewsByPullRequestID returns all reviews submitted for a specific pull request
func GetReviewsByPullRequestID(prID int64) ([]*Review, error) {
return getReviewsByPullRequestID(x, prID)
// GetUniqueApprovalsByPullRequestID returns all reviews submitted for a specific pull request
func GetUniqueApprovalsByPullRequestID(prID int64) ([]*Review, error) {
return getUniqueApprovalsByPullRequestID(x, prID)
}

// FindReviewOptions represent possible filters to find reviews
Expand Down
10 changes: 10 additions & 0 deletions models/review_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,13 @@ func TestUpdateReview(t *testing.T) {
assert.NoError(t, UpdateReview(review))
AssertExistsAndLoadBean(t, &Review{ID: 1, Content: "Updated Review"})
}

func TestGetUniqueApprovalsByPullRequestID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
approvals, err := GetUniqueApprovalsByPullRequestID(2)
assert.NoError(t, err)
assert.Len(t, approvals, 1)
approvals, err = GetUniqueApprovalsByPullRequestID(354295349965036)
assert.NoError(t, err)
assert.Len(t, approvals, 0)
}