Skip to content
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

🌱 Code Review: treat merging a PR as code review #2413

Merged
merged 5 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 2 additions & 6 deletions checks/evaluation/code_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,8 @@ func reviewScoreForChangeset(changeset *checker.Changeset) (score reviewScore) {
return reviewedOutsideGithub
}

for i := range changeset.Commits {
for _, review := range changeset.Commits[i].AssociatedMergeRequest.Reviews {
if review.State == "APPROVED" {
return changesReviewed
}
}
if changeset.ReviewPlatform == checker.ReviewPlatformGitHub {
return changesReviewed
raghavkaul marked this conversation as resolved.
Show resolved Hide resolved
}

return noReview
Expand Down
18 changes: 18 additions & 0 deletions checks/evaluation/code_review_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ func TestCodeReview(t *testing.T) {
},
},
},
{
name: "implicit maintainer approval through github merge",
expected: scut.TestReturn{
Score: checker.MaxResultScore,
},
rawData: &checker.CodeReviewData{
DefaultBranchChangesets: []checker.Changeset{
{
ReviewPlatform: checker.ReviewPlatformGitHub,
Commits: []clients.Commit{{SHA: "1"}},
},
{
ReviewPlatform: checker.ReviewPlatformGitHub,
Commits: []clients.Commit{{SHA: "2"}},
},
},
},
},
}

for _, tt := range tests {
Expand Down
3 changes: 3 additions & 0 deletions checks/raw/code_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func getChangesets(commits []clients.Commit) []checker.Changeset {

for i := range commits {
rev := detectCommitRevisionInfo(&commits[i])
if rev.ID == "" {
rev.ID = commits[i].SHA
}

if changeset, ok := changesetsByRevInfo[rev]; !ok {
newChangeset := checker.Changeset{
Expand Down
25 changes: 24 additions & 1 deletion e2e/code_review_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ package e2e

import (
"context"
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/checks"
"github.com/ossf/scorecard/v4/checks/raw"
"github.com/ossf/scorecard/v4/clients"
"github.com/ossf/scorecard/v4/clients/githubrepo"
scut "github.com/ossf/scorecard/v4/utests"
)

// TODO: use dedicated repo that don't change.
// TODO: need negative results.
var _ = Describe("E2E TEST:"+checks.CheckCodeReview, func() {
Context("E2E TEST:Validating use of code reviews", func() {
It("Should return use of code reviews", func() {
Expand Down Expand Up @@ -81,5 +82,27 @@ var _ = Describe("E2E TEST:"+checks.CheckCodeReview, func() {
Expect(scut.ValidateTestReturn(nil, "use code reviews", &expected, &result, &dl)).Should(BeTrue())
Expect(repoClient.Close()).Should(BeNil())
})
It("Should return use of implicit code reviews at commit", func() {
repo, err := githubrepo.MakeGithubRepo("spring-projects/spring-framework")
Expect(err).Should(BeNil())
repoClient := githubrepo.CreateGithubRepoClient(context.Background(), logger)
err = repoClient.InitRepo(repo, "ca5e453f87f7e84033bb90a2fb54ee9f7fc94d61")
Expect(err).Should(BeNil())

reviewData, err := raw.CodeReview(repoClient)
Expect(err).Should(BeNil())
Expect(reviewData.DefaultBranchChangesets).ShouldNot(BeEmpty())

gh := 0
for _, cs := range reviewData.DefaultBranchChangesets {
if cs.ReviewPlatform == checker.ReviewPlatformGitHub {
fmt.Printf("found github revision %s in spring-framework", cs.RevisionID)
gh += 1
}
}
Expect(gh).Should(BeNumerically("==", 2))

Expect(repoClient.Close()).Should(BeNil())
})
})
})
38 changes: 35 additions & 3 deletions pkg/json_raw_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type jsonUser struct {
Organizations []jsonOrganization `json:"organization,omitempty"`
// Companies refer to a claim by a user in their profile.
Companies []jsonCompany `json:"company,omitempty"`
NumContributions int `json:"NumContributions"`
NumContributions int `json:"NumContributions,omitempty"`
}

type jsonContributors struct {
Expand Down Expand Up @@ -527,10 +527,42 @@ func (r *jsonScorecardRawResult) setDefaultCommitData(changesets []checker.Chang
})
}

reviews := []jsonReview{}
for j := range cs.Commits {
mr := cs.Commits[j].AssociatedMergeRequest
if mr.Reviews == nil {
continue
}
for k := range mr.Reviews {
r := mr.Reviews[k]
reviews = append(reviews, jsonReview{
State: r.State,
Reviewer: jsonUser{
Login: r.Author.Login,
},
})
}
}

// Only add the Merge Request opener as the PR author
authors := []jsonUser{}
for j := range cs.Commits {
mr := cs.Commits[j].AssociatedMergeRequest
if mr.Author.Login != "" {
authors = append(authors, jsonUser{
Login: mr.Author.Login,
})
break
}
}

r.Results.DefaultBranchChangesets = append(r.Results.DefaultBranchChangesets,
jsonDefaultBranchChangeset{
RevisionID: cs.RevisionID,
Commits: commits,
RevisionID: cs.RevisionID,
ReviewPlatform: cs.ReviewPlatform,
Commits: commits,
Reviews: reviews,
Authors: authors,
},
)
}
Expand Down