2020-10-09 17:47:59 +03:00
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/dlorenc/scorecard/checker"
|
|
|
|
"github.com/google/go-github/v32/github"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-10-13 21:55:14 +03:00
|
|
|
registerCheck("Code-Review", DoesCodeReview)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DoesCodeReview attempts to determine whether a project requires review before code gets merged.
|
|
|
|
// It uses a set of heuristics:
|
|
|
|
// - Looking at the repo configuration to see if reviews are required
|
|
|
|
// - Checking if most of the recent merged PRs were "Approved"
|
|
|
|
// - Looking for other well-known review labels
|
2020-10-13 18:35:55 +03:00
|
|
|
func DoesCodeReview(c checker.Checker) checker.CheckResult {
|
|
|
|
return checker.MultiCheck(
|
2020-10-09 17:47:59 +03:00
|
|
|
IsPrReviewRequired,
|
|
|
|
GithubCodeReview,
|
|
|
|
ProwCodeReview,
|
|
|
|
)(c)
|
|
|
|
}
|
|
|
|
|
2020-10-13 18:35:55 +03:00
|
|
|
func GithubCodeReview(c checker.Checker) checker.CheckResult {
|
2020-10-09 17:47:59 +03:00
|
|
|
// Look at some merged PRs to see if they were reviewed
|
|
|
|
prs, _, err := c.Client.PullRequests.List(c.Ctx, c.Owner, c.Repo, &github.PullRequestListOptions{
|
|
|
|
State: "closed",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-13 18:35:55 +03:00
|
|
|
return checker.InconclusiveResult
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
totalMerged := 0
|
|
|
|
totalReviewed := 0
|
|
|
|
for _, pr := range prs {
|
|
|
|
if pr.MergedAt == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
totalMerged++
|
|
|
|
// Merged PR!
|
|
|
|
reviews, _, err := c.Client.PullRequests.ListReviews(c.Ctx, c.Owner, c.Repo, pr.GetNumber(), &github.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, r := range reviews {
|
|
|
|
if r.GetState() == "APPROVED" {
|
|
|
|
totalReviewed++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 18:35:55 +03:00
|
|
|
return checker.ProportionalResult(totalReviewed, totalMerged, .75)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:35:55 +03:00
|
|
|
func IsPrReviewRequired(c checker.Checker) checker.CheckResult {
|
2020-10-09 17:47:59 +03:00
|
|
|
// Look to see if review is enforced.
|
|
|
|
r, _, err := c.Client.Repositories.Get(c.Ctx, c.Owner, c.Repo)
|
|
|
|
if err != nil {
|
2020-10-13 18:35:55 +03:00
|
|
|
return checker.RetryResult(err)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the branch protection rules, we may not be able to get these though.
|
|
|
|
bp, _, err := c.Client.Repositories.GetBranchProtection(c.Ctx, c.Owner, c.Repo, r.GetDefaultBranch())
|
|
|
|
if err != nil {
|
2020-10-13 18:35:55 +03:00
|
|
|
return checker.InconclusiveResult
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
if bp.GetRequiredPullRequestReviews().RequiredApprovingReviewCount >= 1 {
|
2020-10-13 18:35:55 +03:00
|
|
|
return checker.CheckResult{
|
2020-10-09 17:47:59 +03:00
|
|
|
Pass: true,
|
|
|
|
Confidence: 10,
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 18:35:55 +03:00
|
|
|
return checker.InconclusiveResult
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:35:55 +03:00
|
|
|
func ProwCodeReview(c checker.Checker) checker.CheckResult {
|
2020-10-09 17:47:59 +03:00
|
|
|
// Look at some merged PRs to see if they were reviewed
|
|
|
|
prs, _, err := c.Client.PullRequests.List(c.Ctx, c.Owner, c.Repo, &github.PullRequestListOptions{
|
|
|
|
State: "closed",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-13 18:35:55 +03:00
|
|
|
return checker.InconclusiveResult
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
totalMerged := 0
|
|
|
|
totalReviewed := 0
|
|
|
|
for _, pr := range prs {
|
|
|
|
if pr.MergedAt == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
totalMerged++
|
|
|
|
for _, l := range pr.Labels {
|
|
|
|
if l.GetName() == "lgtm" || l.GetName() == "approved" {
|
|
|
|
totalReviewed++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 20:18:40 +03:00
|
|
|
|
|
|
|
if totalReviewed == 0 {
|
2020-10-13 18:35:55 +03:00
|
|
|
return checker.InconclusiveResult
|
2020-10-12 20:18:40 +03:00
|
|
|
}
|
2020-10-13 17:32:38 +03:00
|
|
|
|
2020-10-13 18:35:55 +03:00
|
|
|
c.Logf("prow code reviews found")
|
|
|
|
|
|
|
|
return checker.ProportionalResult(totalReviewed, totalMerged, .75)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|