2020-10-26 23:22:13 +03:00
|
|
|
// Copyright 2020 Security Scorecard Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-10-09 17:47:59 +03:00
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
2021-05-17 23:03:39 +03:00
|
|
|
"errors"
|
2020-11-19 18:33:21 +03:00
|
|
|
"strings"
|
|
|
|
|
2020-10-09 17:47:59 +03:00
|
|
|
"github.com/google/go-github/v32/github"
|
2021-07-02 01:32:57 +03:00
|
|
|
"github.com/shurcooL/githubv4"
|
2021-05-24 06:51:52 +03:00
|
|
|
|
2020-10-27 22:23:48 +03:00
|
|
|
"github.com/ossf/scorecard/checker"
|
2020-10-09 17:47:59 +03:00
|
|
|
)
|
|
|
|
|
2021-06-29 20:26:16 +03:00
|
|
|
const (
|
|
|
|
// CheckCodeReview is the registered name for DoesCodeReview.
|
2021-07-02 01:32:57 +03:00
|
|
|
CheckCodeReview = "Code-Review"
|
|
|
|
crPassThreshold = .75
|
|
|
|
pullRequestsToAnalyze = 30
|
|
|
|
reviewsToAnalyze = 30
|
|
|
|
labelsToAnalyze = 30
|
2021-06-29 20:26:16 +03:00
|
|
|
)
|
2021-04-10 15:26:56 +03:00
|
|
|
|
2021-07-02 01:32:57 +03:00
|
|
|
var (
|
|
|
|
// ErrorNoReviews indicates no reviews were found for this repo.
|
|
|
|
ErrorNoReviews = errors.New("no reviews found")
|
|
|
|
|
|
|
|
// nolint: govet
|
|
|
|
prHistory struct {
|
|
|
|
Repository struct {
|
|
|
|
DefaultBranchRef struct {
|
|
|
|
Name githubv4.String
|
|
|
|
BranchProtectionRule struct {
|
|
|
|
RequiredApprovingReviewCount githubv4.Int
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PullRequests struct {
|
|
|
|
Nodes []struct {
|
|
|
|
Number githubv4.Int
|
|
|
|
MergeCommit struct {
|
|
|
|
AuthoredByCommitter githubv4.Boolean
|
|
|
|
}
|
|
|
|
MergedAt githubv4.DateTime
|
|
|
|
Labels struct {
|
|
|
|
Nodes []struct {
|
|
|
|
Name githubv4.String
|
|
|
|
}
|
|
|
|
} `graphql:"labels(last: $labelsToAnalyze)"`
|
|
|
|
LatestReviews struct {
|
|
|
|
Nodes []struct {
|
|
|
|
State githubv4.String
|
|
|
|
}
|
|
|
|
} `graphql:"latestReviews(last: $reviewsToAnalyze)"`
|
|
|
|
}
|
|
|
|
} `graphql:"pullRequests(last: $pullRequestsToAnalyze, states: MERGED)"`
|
|
|
|
} `graphql:"repository(owner: $owner, name: $name)"`
|
|
|
|
}
|
|
|
|
)
|
2021-05-22 22:36:47 +03:00
|
|
|
|
2021-05-22 20:19:52 +03:00
|
|
|
//nolint:gochecknoinits
|
2020-10-09 17:47:59 +03:00
|
|
|
func init() {
|
2021-05-28 00:54:34 +03:00
|
|
|
registerCheck(CheckCodeReview, 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:
|
2021-05-02 01:26:23 +03:00
|
|
|
// - 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.
|
2021-04-19 22:18:34 +03:00
|
|
|
func DoesCodeReview(c *checker.CheckRequest) checker.CheckResult {
|
2021-07-02 01:32:57 +03:00
|
|
|
vars := map[string]interface{}{
|
|
|
|
"owner": githubv4.String(c.Owner),
|
|
|
|
"name": githubv4.String(c.Repo),
|
|
|
|
"pullRequestsToAnalyze": githubv4.Int(pullRequestsToAnalyze),
|
|
|
|
"reviewsToAnalyze": githubv4.Int(reviewsToAnalyze),
|
|
|
|
"labelsToAnalyze": githubv4.Int(labelsToAnalyze),
|
|
|
|
}
|
|
|
|
if err := c.GraphClient.Query(c.Ctx, &prHistory, vars); err != nil {
|
|
|
|
return checker.MakeInconclusiveResult(CheckCodeReview, err)
|
|
|
|
}
|
2021-05-17 23:03:39 +03:00
|
|
|
return checker.MultiCheckOr(
|
2020-10-09 17:47:59 +03:00
|
|
|
IsPrReviewRequired,
|
|
|
|
GithubCodeReview,
|
|
|
|
ProwCodeReview,
|
2021-01-25 02:24:30 +03:00
|
|
|
CommitMessageHints,
|
2020-10-09 17:47:59 +03:00
|
|
|
)(c)
|
|
|
|
}
|
|
|
|
|
2021-04-19 22:18:34 +03:00
|
|
|
func GithubCodeReview(c *checker.CheckRequest) checker.CheckResult {
|
2021-07-02 01:32:57 +03:00
|
|
|
// Look at some merged PRs to see if they were reviewed.
|
2020-10-09 17:47:59 +03:00
|
|
|
totalMerged := 0
|
|
|
|
totalReviewed := 0
|
2021-07-02 01:32:57 +03:00
|
|
|
for _, pr := range prHistory.Repository.PullRequests.Nodes {
|
|
|
|
if pr.MergedAt.IsZero() {
|
2020-10-09 17:47:59 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
totalMerged++
|
2020-11-21 00:21:59 +03:00
|
|
|
|
|
|
|
// check if the PR is approved by a reviewer
|
|
|
|
foundApprovedReview := false
|
2021-07-02 01:32:57 +03:00
|
|
|
for _, r := range pr.LatestReviews.Nodes {
|
|
|
|
if r.State == "APPROVED" {
|
|
|
|
c.Logf("found review approved pr: %d", pr.Number)
|
2020-10-09 17:47:59 +03:00
|
|
|
totalReviewed++
|
2020-11-21 00:21:59 +03:00
|
|
|
foundApprovedReview = true
|
2020-10-09 17:47:59 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-11-21 00:21:59 +03:00
|
|
|
|
|
|
|
// check if the PR is committed by someone other than author. this is kind
|
|
|
|
// of equivalent to a review and is done several times on small prs to save
|
|
|
|
// time on clicking the approve button.
|
|
|
|
if !foundApprovedReview {
|
2021-07-02 01:32:57 +03:00
|
|
|
if !pr.MergeCommit.AuthoredByCommitter {
|
|
|
|
c.Logf("found pr with committer different than author: %d", pr.Number)
|
|
|
|
totalReviewed++
|
2020-11-21 00:21:59 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
2020-10-24 20:24:27 +03:00
|
|
|
if totalReviewed > 0 {
|
|
|
|
c.Logf("github code reviews found")
|
2020-10-24 20:20:49 +03:00
|
|
|
}
|
2021-06-29 20:26:16 +03:00
|
|
|
return checker.MakeProportionalResult(CheckCodeReview, totalReviewed, totalMerged, crPassThreshold)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
2021-04-19 22:18:34 +03:00
|
|
|
func IsPrReviewRequired(c *checker.CheckRequest) checker.CheckResult {
|
2020-10-09 17:47:59 +03:00
|
|
|
// Look to see if review is enforced.
|
|
|
|
// Check the branch protection rules, we may not be able to get these though.
|
2021-07-02 01:32:57 +03:00
|
|
|
if prHistory.Repository.DefaultBranchRef.BranchProtectionRule.RequiredApprovingReviewCount >= 1 {
|
2020-10-18 03:09:35 +03:00
|
|
|
c.Logf("pr review policy enforced")
|
2021-02-17 04:55:36 +03:00
|
|
|
const confidence = 5
|
2020-10-13 18:35:55 +03:00
|
|
|
return checker.CheckResult{
|
2021-05-28 00:54:34 +03:00
|
|
|
Name: CheckCodeReview,
|
2020-10-09 17:47:59 +03:00
|
|
|
Pass: true,
|
2021-02-17 04:55:36 +03:00
|
|
|
Confidence: confidence,
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
}
|
2021-05-28 00:54:34 +03:00
|
|
|
return checker.MakeInconclusiveResult(CheckCodeReview, nil)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
2021-04-19 22:18:34 +03:00
|
|
|
func ProwCodeReview(c *checker.CheckRequest) checker.CheckResult {
|
2020-10-09 17:47:59 +03:00
|
|
|
// Look at some merged PRs to see if they were reviewed
|
|
|
|
totalMerged := 0
|
|
|
|
totalReviewed := 0
|
2021-07-02 01:32:57 +03:00
|
|
|
for _, pr := range prHistory.Repository.PullRequests.Nodes {
|
|
|
|
if pr.MergedAt.IsZero() {
|
2020-10-09 17:47:59 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
totalMerged++
|
2021-07-02 01:32:57 +03:00
|
|
|
for _, l := range pr.Labels.Nodes {
|
|
|
|
if l.Name == "lgtm" || l.Name == "approved" {
|
2020-10-09 17:47:59 +03:00
|
|
|
totalReviewed++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 20:18:40 +03:00
|
|
|
|
|
|
|
if totalReviewed == 0 {
|
2021-05-28 00:54:34 +03:00
|
|
|
return checker.MakeInconclusiveResult(CheckCodeReview, ErrorNoReviews)
|
2020-10-12 20:18:40 +03:00
|
|
|
}
|
2020-10-13 18:35:55 +03:00
|
|
|
c.Logf("prow code reviews found")
|
2021-06-29 20:26:16 +03:00
|
|
|
return checker.MakeProportionalResult(CheckCodeReview, totalReviewed, totalMerged, crPassThreshold)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
2020-11-19 18:33:21 +03:00
|
|
|
|
2021-04-19 22:18:34 +03:00
|
|
|
func CommitMessageHints(c *checker.CheckRequest) checker.CheckResult {
|
2020-11-19 18:33:21 +03:00
|
|
|
commits, _, err := c.Client.Repositories.ListCommits(c.Ctx, c.Owner, c.Repo, &github.CommitsListOptions{})
|
|
|
|
if err != nil {
|
2021-05-28 00:54:34 +03:00
|
|
|
return checker.MakeRetryResult(CheckCodeReview, err)
|
2020-11-19 18:33:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
total := 0
|
|
|
|
totalReviewed := 0
|
|
|
|
for _, commit := range commits {
|
|
|
|
isBot := false
|
|
|
|
committer := commit.GetCommitter().GetLogin()
|
|
|
|
for _, substring := range []string{"bot", "gardener"} {
|
|
|
|
if strings.Contains(committer, substring) {
|
|
|
|
isBot = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isBot {
|
|
|
|
c.Logf("skip commit from bot account: %s", committer)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
total++
|
|
|
|
|
|
|
|
// check for gerrit use via Reviewed-on and Reviewed-by
|
|
|
|
commitMessage := commit.GetCommit().GetMessage()
|
|
|
|
if strings.Contains(commitMessage, "\nReviewed-on: ") &&
|
|
|
|
strings.Contains(commitMessage, "\nReviewed-by: ") {
|
|
|
|
totalReviewed++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if totalReviewed == 0 {
|
2021-05-28 00:54:34 +03:00
|
|
|
return checker.MakeInconclusiveResult(CheckCodeReview, ErrorNoReviews)
|
2020-11-19 18:33:21 +03:00
|
|
|
}
|
2021-01-25 02:24:30 +03:00
|
|
|
c.Logf("code reviews found")
|
2021-06-29 20:26:16 +03:00
|
|
|
return checker.MakeProportionalResult(CheckCodeReview, totalReviewed, total, crPassThreshold)
|
2020-11-19 18:33:21 +03:00
|
|
|
}
|