Skip to content

Commit

Permalink
Modify github approval verification logic (#29)
Browse files Browse the repository at this point in the history
* Modify github approval verification logic

If reviews are not required for a repo, then only consider review status
as failed if someone has explicitly disapproved the repo

Fixes #27

* Update github.go
  • Loading branch information
ryanmcnamara authored and ajlake committed Feb 12, 2018
1 parent 9ff04f1 commit 467be24
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,21 +427,30 @@ func (client *Client) ReviewStatus(pr *github.PullRequest) (bool, error) {
}

approval := false
disapproval := false
for _, review := range reviews {
perms, _, err := client.Repositories.GetPermissionLevel(client.Ctx, owner, name, review.User.GetLogin())
if err != nil {
return false, errors.Wrapf(err, "cannot get permission level for %s on %s", review.User.GetLogin(), repo.GetFullName())
}

hasWrite := utils.StringInSlice(perms.GetPermission(), AcceptedPermLevels)
if review.GetState() == "APPROVED" && hasWrite {
logger.WithFields(logrus.Fields{
"repo": repo.GetFullName(),
"pr": pr.GetNumber(),
"approver": review.User.GetLogin(),
}).Info("Review approved")
approval = true
break
if hasWrite {
if review.GetState() == "APPROVED" {
logger.WithFields(logrus.Fields{
"repo": repo.GetFullName(),
"pr": pr.GetNumber(),
"approver": review.User.GetLogin(),
}).Info("Review approved")
approval = true
} else if review.GetState() == "CHANGES_REQUESTED" {
logger.WithFields(logrus.Fields{
"repo": repo.GetFullName(),
"pr": pr.GetNumber(),
"approver": review.User.GetLogin(),
}).Info("Review not approved")
disapproval = true
}
}
}

Expand All @@ -456,13 +465,13 @@ func (client *Client) ReviewStatus(pr *github.PullRequest) (bool, error) {
return true, nil
}
}
reviewRequired := protection.RequiredPullRequestReviews != nil

if len(reviewers.Users) == 0 && len(reviews) == 0 && !reviewRequired {
reviewRequired := protection.RequiredPullRequestReviews != nil
if !reviewRequired && !disapproval {
logger.WithFields(logrus.Fields{
"repo": repo.GetFullName(),
"pr": pr.GetNumber(),
}).Info("Pull request has 0 reviewers and 0 reviews, considering status true")
}).Info("Review not required and no one has disapproved, considering status true")
return true, nil
}

Expand Down

0 comments on commit 467be24

Please sign in to comment.