mirror of
https://github.com/ossf/scorecard.git
synced 2024-09-17 11:57:12 +03:00
Replace checker.Commit
with clients.Commit
(#1950)
Co-authored-by: Azeem Shaikh <azeems@google.com>
This commit is contained in:
parent
96fac8a941
commit
25c7e1c7f2
@ -15,8 +15,6 @@
|
|||||||
package checker
|
package checker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ossf/scorecard/v4/clients"
|
"github.com/ossf/scorecard/v4/clients"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -49,7 +47,7 @@ type FuzzingData struct {
|
|||||||
// for the Maintained check.
|
// for the Maintained check.
|
||||||
type MaintainedData struct {
|
type MaintainedData struct {
|
||||||
Issues []clients.Issue
|
Issues []clients.Issue
|
||||||
DefaultBranchCommits []DefaultBranchCommit
|
DefaultBranchCommits []clients.Commit
|
||||||
ArchivedStatus ArchivedStatus
|
ArchivedStatus ArchivedStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,12 +60,12 @@ type LicenseData struct {
|
|||||||
// CodeReviewData contains the raw results
|
// CodeReviewData contains the raw results
|
||||||
// for the Code-Review check.
|
// for the Code-Review check.
|
||||||
type CodeReviewData struct {
|
type CodeReviewData struct {
|
||||||
DefaultBranchCommits []DefaultBranchCommit
|
DefaultBranchCommits []clients.Commit
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContributorsData represents contributor information.
|
// ContributorsData represents contributor information.
|
||||||
type ContributorsData struct {
|
type ContributorsData struct {
|
||||||
Users []User
|
Users []clients.Contributor
|
||||||
}
|
}
|
||||||
|
|
||||||
// VulnerabilitiesData contains the raw results
|
// VulnerabilitiesData contains the raw results
|
||||||
@ -127,7 +125,7 @@ type Tool struct {
|
|||||||
// Issues created by the tool.
|
// Issues created by the tool.
|
||||||
Issues []clients.Issue
|
Issues []clients.Issue
|
||||||
// Merge requests created by the tool.
|
// Merge requests created by the tool.
|
||||||
MergeRequests []MergeRequest
|
MergeRequests []clients.PullRequest
|
||||||
|
|
||||||
// TODO: CodeCoverage, jsonWorkflowJob.
|
// TODO: CodeCoverage, jsonWorkflowJob.
|
||||||
}
|
}
|
||||||
@ -138,93 +136,12 @@ type Run struct {
|
|||||||
// TODO: add fields, e.g., Result=["success", "failure"]
|
// TODO: add fields, e.g., Result=["success", "failure"]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comment represents a comment for a pull request or an issue.
|
|
||||||
type Comment struct {
|
|
||||||
CreatedAt *time.Time
|
|
||||||
Author *User
|
|
||||||
// TODO: add ields if needed, e.g., content.
|
|
||||||
}
|
|
||||||
|
|
||||||
// ArchivedStatus definess the archived status.
|
// ArchivedStatus definess the archived status.
|
||||||
type ArchivedStatus struct {
|
type ArchivedStatus struct {
|
||||||
Status bool
|
Status bool
|
||||||
// TODO: add fields, e.g., date of archival.
|
// TODO: add fields, e.g., date of archival.
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultBranchCommit represents a commit
|
|
||||||
// to the default branch.
|
|
||||||
type DefaultBranchCommit struct {
|
|
||||||
// Fields below are taken directly from cloud
|
|
||||||
// version control systems, e.g. GitHub.
|
|
||||||
SHA string
|
|
||||||
CommitMessage string
|
|
||||||
MergeRequest *MergeRequest
|
|
||||||
CommitDate *time.Time
|
|
||||||
Committer User
|
|
||||||
}
|
|
||||||
|
|
||||||
// MergeRequest represents a merge request.
|
|
||||||
// nolint:govet
|
|
||||||
type MergeRequest struct {
|
|
||||||
Number int
|
|
||||||
Labels []string
|
|
||||||
Reviews []Review
|
|
||||||
Author User
|
|
||||||
MergedAt time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
// Review represent a review using the built-in review system.
|
|
||||||
type Review struct {
|
|
||||||
State string
|
|
||||||
Reviewer User
|
|
||||||
// TODO(Review): add fields here if needed.
|
|
||||||
}
|
|
||||||
|
|
||||||
// User represent a user.
|
|
||||||
type User struct {
|
|
||||||
RepoAssociation *RepoAssociation
|
|
||||||
Login string
|
|
||||||
// Orgnization refers to a GitHub org.
|
|
||||||
Organizations []Organization
|
|
||||||
// Companies refer to a claim by a user in their profile.
|
|
||||||
Companies []Company
|
|
||||||
NumContributions uint
|
|
||||||
}
|
|
||||||
|
|
||||||
// Organization represents a GitHub organization.
|
|
||||||
type Organization struct {
|
|
||||||
Login string
|
|
||||||
// TODO: other info.
|
|
||||||
}
|
|
||||||
|
|
||||||
// Company represents a company in a user's profile.
|
|
||||||
type Company struct {
|
|
||||||
Name string
|
|
||||||
// TODO: other info.
|
|
||||||
}
|
|
||||||
|
|
||||||
// RepoAssociation represents a user relationship with a repo.
|
|
||||||
type RepoAssociation string
|
|
||||||
|
|
||||||
const (
|
|
||||||
// RepoAssociationCollaborator has been invited to collaborate on the repository.
|
|
||||||
RepoAssociationCollaborator RepoAssociation = "collaborator"
|
|
||||||
// RepoAssociationContributor is an contributor to the repository.
|
|
||||||
RepoAssociationContributor RepoAssociation = "contributor"
|
|
||||||
// RepoAssociationOwner is an owner of the repository.
|
|
||||||
RepoAssociationOwner RepoAssociation = "owner"
|
|
||||||
// RepoAssociationMember is a member of the organization that owns the repository.
|
|
||||||
RepoAssociationMember RepoAssociation = "member"
|
|
||||||
// RepoAssociationFirstTimer has previously committed to the repository.
|
|
||||||
RepoAssociationFirstTimer RepoAssociation = "first-timer"
|
|
||||||
// RepoAssociationFirstTimeContributor has not previously committed to the repository.
|
|
||||||
RepoAssociationFirstTimeContributor RepoAssociation = "first-timer-contributor"
|
|
||||||
// RepoAssociationMannequin is a placeholder for an unclaimed user.
|
|
||||||
RepoAssociationMannequin RepoAssociation = "unknown"
|
|
||||||
// RepoAssociationNone has no association with the repository.
|
|
||||||
RepoAssociationNone RepoAssociation = "none"
|
|
||||||
)
|
|
||||||
|
|
||||||
// File represents a file.
|
// File represents a file.
|
||||||
type File struct {
|
type File struct {
|
||||||
Path string
|
Path string
|
||||||
|
@ -61,7 +61,8 @@ func TestContributors(t *testing.T) {
|
|||||||
name: "Valid contributors with enough contributors and companies",
|
name: "Valid contributors with enough contributors and companies",
|
||||||
contrib: []clients.Contributor{
|
contrib: []clients.Contributor{
|
||||||
{
|
{
|
||||||
Company: "company1",
|
|
||||||
|
Companies: []string{"company1"},
|
||||||
NumContributions: 10,
|
NumContributions: 10,
|
||||||
Organizations: []clients.User{
|
Organizations: []clients.User{
|
||||||
{
|
{
|
||||||
@ -73,7 +74,7 @@ func TestContributors(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Company: "company2",
|
Companies: []string{"company2"},
|
||||||
NumContributions: 10,
|
NumContributions: 10,
|
||||||
Organizations: []clients.User{
|
Organizations: []clients.User{
|
||||||
{
|
{
|
||||||
@ -85,7 +86,7 @@ func TestContributors(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Company: "company3",
|
Companies: []string{"company3"},
|
||||||
NumContributions: 10,
|
NumContributions: 10,
|
||||||
Organizations: []clients.User{
|
Organizations: []clients.User{
|
||||||
{
|
{
|
||||||
@ -97,7 +98,7 @@ func TestContributors(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Company: "company4",
|
Companies: []string{"company4"},
|
||||||
NumContributions: 10,
|
NumContributions: 10,
|
||||||
Organizations: []clients.User{
|
Organizations: []clients.User{
|
||||||
{
|
{
|
||||||
@ -109,7 +110,7 @@ func TestContributors(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Company: "company5",
|
Companies: []string{"company5"},
|
||||||
NumContributions: 10,
|
NumContributions: 10,
|
||||||
Organizations: []clients.User{
|
Organizations: []clients.User{
|
||||||
{
|
{
|
||||||
@ -121,7 +122,7 @@ func TestContributors(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Company: "company6",
|
Companies: []string{"company6"},
|
||||||
Organizations: []clients.User{
|
Organizations: []clients.User{
|
||||||
{
|
{
|
||||||
Login: "org1",
|
Login: "org1",
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ossf/scorecard/v4/checker"
|
"github.com/ossf/scorecard/v4/checker"
|
||||||
|
"github.com/ossf/scorecard/v4/clients"
|
||||||
sce "github.com/ossf/scorecard/v4/errors"
|
sce "github.com/ossf/scorecard/v4/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -105,7 +106,7 @@ func isBot(name string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func getApprovedReviewSystem(c *checker.DefaultBranchCommit, dl checker.DetailLogger) string {
|
func getApprovedReviewSystem(c *clients.Commit, dl checker.DetailLogger) string {
|
||||||
switch {
|
switch {
|
||||||
case isReviewedOnGitHub(c, dl):
|
case isReviewedOnGitHub(c, dl):
|
||||||
return reviewPlatformGitHub
|
return reviewPlatformGitHub
|
||||||
@ -121,9 +122,9 @@ func getApprovedReviewSystem(c *checker.DefaultBranchCommit, dl checker.DetailLo
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func isReviewedOnGitHub(c *checker.DefaultBranchCommit, dl checker.DetailLogger) bool {
|
func isReviewedOnGitHub(c *clients.Commit, dl checker.DetailLogger) bool {
|
||||||
mr := c.MergeRequest
|
mr := c.AssociatedMergeRequest
|
||||||
if mr == nil || mr.MergedAt.IsZero() {
|
if mr.MergedAt.IsZero() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +153,7 @@ func isReviewedOnGitHub(c *checker.DefaultBranchCommit, dl checker.DetailLogger)
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func isReviewedOnProw(c *checker.DefaultBranchCommit, dl checker.DetailLogger) bool {
|
func isReviewedOnProw(c *clients.Commit, dl checker.DetailLogger) bool {
|
||||||
if isBot(c.Committer.Login) {
|
if isBot(c.Committer.Login) {
|
||||||
dl.Debug(&checker.LogMessage{
|
dl.Debug(&checker.LogMessage{
|
||||||
Text: fmt.Sprintf("skip commit %s from bot account: %s", c.SHA, c.Committer.Login),
|
Text: fmt.Sprintf("skip commit %s from bot account: %s", c.SHA, c.Committer.Login),
|
||||||
@ -160,12 +161,12 @@ func isReviewedOnProw(c *checker.DefaultBranchCommit, dl checker.DetailLogger) b
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.MergeRequest != nil && !c.MergeRequest.MergedAt.IsZero() {
|
if !c.AssociatedMergeRequest.MergedAt.IsZero() {
|
||||||
for _, l := range c.MergeRequest.Labels {
|
for _, l := range c.AssociatedMergeRequest.Labels {
|
||||||
if l == "lgtm" || l == "approved" {
|
if l.Name == "lgtm" || l.Name == "approved" {
|
||||||
dl.Debug(&checker.LogMessage{
|
dl.Debug(&checker.LogMessage{
|
||||||
Text: fmt.Sprintf("commit %s review was through %s #%d approved merge request",
|
Text: fmt.Sprintf("commit %s review was through %s #%d approved merge request",
|
||||||
c.SHA, reviewPlatformProw, c.MergeRequest.Number),
|
c.SHA, reviewPlatformProw, c.AssociatedMergeRequest.Number),
|
||||||
})
|
})
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -174,7 +175,7 @@ func isReviewedOnProw(c *checker.DefaultBranchCommit, dl checker.DetailLogger) b
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func isReviewedOnGerrit(c *checker.DefaultBranchCommit, dl checker.DetailLogger) bool {
|
func isReviewedOnGerrit(c *clients.Commit, dl checker.DetailLogger) bool {
|
||||||
if isBot(c.Committer.Login) {
|
if isBot(c.Committer.Login) {
|
||||||
dl.Debug(&checker.LogMessage{
|
dl.Debug(&checker.LogMessage{
|
||||||
Text: fmt.Sprintf("skip commit %s from bot account: %s", c.SHA, c.Committer.Login),
|
Text: fmt.Sprintf("skip commit %s from bot account: %s", c.SHA, c.Committer.Login),
|
||||||
@ -182,7 +183,7 @@ func isReviewedOnGerrit(c *checker.DefaultBranchCommit, dl checker.DetailLogger)
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
m := c.CommitMessage
|
m := c.Message
|
||||||
if strings.Contains(m, "\nReviewed-on: ") &&
|
if strings.Contains(m, "\nReviewed-on: ") &&
|
||||||
strings.Contains(m, "\nReviewed-by: ") {
|
strings.Contains(m, "\nReviewed-by: ") {
|
||||||
dl.Debug(&checker.LogMessage{
|
dl.Debug(&checker.LogMessage{
|
||||||
@ -193,7 +194,7 @@ func isReviewedOnGerrit(c *checker.DefaultBranchCommit, dl checker.DetailLogger)
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func isReviewedOnPhabricator(c *checker.DefaultBranchCommit, dl checker.DetailLogger) bool {
|
func isReviewedOnPhabricator(c *clients.Commit, dl checker.DetailLogger) bool {
|
||||||
if isBot(c.Committer.Login) {
|
if isBot(c.Committer.Login) {
|
||||||
dl.Debug(&checker.LogMessage{
|
dl.Debug(&checker.LogMessage{
|
||||||
Text: fmt.Sprintf("skip commit %s from bot account: %s", c.SHA, c.Committer.Login),
|
Text: fmt.Sprintf("skip commit %s from bot account: %s", c.SHA, c.Committer.Login),
|
||||||
@ -201,7 +202,7 @@ func isReviewedOnPhabricator(c *checker.DefaultBranchCommit, dl checker.DetailLo
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
m := c.CommitMessage
|
m := c.Message
|
||||||
if strings.Contains(m, "\nDifferential Revision: ") &&
|
if strings.Contains(m, "\nDifferential Revision: ") &&
|
||||||
strings.Contains(m, "\nReviewed By: ") {
|
strings.Contains(m, "\nReviewed By: ") {
|
||||||
dl.Debug(&checker.LogMessage{
|
dl.Debug(&checker.LogMessage{
|
||||||
@ -212,8 +213,8 @@ func isReviewedOnPhabricator(c *checker.DefaultBranchCommit, dl checker.DetailLo
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func isReviewedOnPiper(c *checker.DefaultBranchCommit, dl checker.DetailLogger) bool {
|
func isReviewedOnPiper(c *clients.Commit, dl checker.DetailLogger) bool {
|
||||||
m := c.CommitMessage
|
m := c.Message
|
||||||
if strings.Contains(m, "\nPiperOrigin-RevId: ") {
|
if strings.Contains(m, "\nPiperOrigin-RevId: ") {
|
||||||
dl.Debug(&checker.LogMessage{
|
dl.Debug(&checker.LogMessage{
|
||||||
Text: fmt.Sprintf("commit %s was approved through %s", c.SHA, reviewPlatformPiper),
|
Text: fmt.Sprintf("commit %s was approved through %s", c.SHA, reviewPlatformPiper),
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ossf/scorecard/v4/checker"
|
"github.com/ossf/scorecard/v4/checker"
|
||||||
|
"github.com/ossf/scorecard/v4/clients"
|
||||||
sce "github.com/ossf/scorecard/v4/errors"
|
sce "github.com/ossf/scorecard/v4/errors"
|
||||||
scut "github.com/ossf/scorecard/v4/utests"
|
scut "github.com/ossf/scorecard/v4/utests"
|
||||||
)
|
)
|
||||||
@ -53,7 +54,7 @@ func TestCodeReview(t *testing.T) {
|
|||||||
NumberOfWarn: 2,
|
NumberOfWarn: 2,
|
||||||
},
|
},
|
||||||
rawData: &checker.CodeReviewData{
|
rawData: &checker.CodeReviewData{
|
||||||
DefaultBranchCommits: []checker.DefaultBranchCommit{
|
DefaultBranchCommits: []clients.Commit{
|
||||||
{
|
{
|
||||||
SHA: "1",
|
SHA: "1",
|
||||||
},
|
},
|
||||||
|
@ -49,7 +49,7 @@ func Contributors(name string, dl checker.DetailLogger,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, comp := range user.Companies {
|
for _, comp := range user.Companies {
|
||||||
entities[comp.Name] = true
|
entities[comp] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ func Maintained(name string, dl checker.DetailLogger, r *checker.MaintainedData)
|
|||||||
threshold := time.Now().AddDate(0 /*years*/, 0 /*months*/, -1*lookBackDays /*days*/)
|
threshold := time.Now().AddDate(0 /*years*/, 0 /*months*/, -1*lookBackDays /*days*/)
|
||||||
commitsWithinThreshold := 0
|
commitsWithinThreshold := 0
|
||||||
for i := range r.DefaultBranchCommits {
|
for i := range r.DefaultBranchCommits {
|
||||||
if r.DefaultBranchCommits[i].CommitDate.After(threshold) {
|
if r.DefaultBranchCommits[i].CommittedDate.After(threshold) {
|
||||||
commitsWithinThreshold++
|
commitsWithinThreshold++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,70 +23,13 @@ import (
|
|||||||
|
|
||||||
// CodeReview retrieves the raw data for the Code-Review check.
|
// CodeReview retrieves the raw data for the Code-Review check.
|
||||||
func CodeReview(c clients.RepoClient) (checker.CodeReviewData, error) {
|
func CodeReview(c clients.RepoClient) (checker.CodeReviewData, error) {
|
||||||
results := []checker.DefaultBranchCommit{}
|
|
||||||
|
|
||||||
// Look at the latest commits.
|
// Look at the latest commits.
|
||||||
commits, err := c.ListCommits()
|
commits, err := c.ListCommits()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return checker.CodeReviewData{}, fmt.Errorf("%w", err)
|
return checker.CodeReviewData{}, fmt.Errorf("%w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range commits {
|
return checker.CodeReviewData{
|
||||||
results = append(results, getRawDataFromCommit(&commits[i]))
|
DefaultBranchCommits: commits,
|
||||||
}
|
}, nil
|
||||||
|
|
||||||
return checker.CodeReviewData{DefaultBranchCommits: results}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getRawDataFromCommit(c *clients.Commit) checker.DefaultBranchCommit {
|
|
||||||
r := checker.DefaultBranchCommit{
|
|
||||||
Committer: checker.User{
|
|
||||||
Login: c.Committer.Login,
|
|
||||||
},
|
|
||||||
SHA: c.SHA,
|
|
||||||
CommitMessage: c.Message,
|
|
||||||
CommitDate: &c.CommittedDate,
|
|
||||||
MergeRequest: mergeRequest(&c.AssociatedMergeRequest),
|
|
||||||
}
|
|
||||||
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
func mergeRequest(mr *clients.PullRequest) *checker.MergeRequest {
|
|
||||||
r := checker.MergeRequest{
|
|
||||||
Number: mr.Number,
|
|
||||||
Author: checker.User{
|
|
||||||
Login: mr.Author.Login,
|
|
||||||
},
|
|
||||||
MergedAt: mr.MergedAt,
|
|
||||||
Labels: labels(mr),
|
|
||||||
Reviews: reviews(mr),
|
|
||||||
}
|
|
||||||
return &r
|
|
||||||
}
|
|
||||||
|
|
||||||
func labels(mr *clients.PullRequest) []string {
|
|
||||||
labels := []string{}
|
|
||||||
for _, l := range mr.Labels {
|
|
||||||
labels = append(labels, l.Name)
|
|
||||||
}
|
|
||||||
return labels
|
|
||||||
}
|
|
||||||
|
|
||||||
func reviews(mr *clients.PullRequest) []checker.Review {
|
|
||||||
reviews := []checker.Review{}
|
|
||||||
for _, m := range mr.Reviews {
|
|
||||||
r := checker.Review{
|
|
||||||
State: m.State,
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.Author != nil &&
|
|
||||||
m.Author.Login != "" {
|
|
||||||
r.Reviewer = checker.User{
|
|
||||||
Login: m.Author.Login,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reviews = append(reviews, r)
|
|
||||||
}
|
|
||||||
return reviews
|
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,7 @@ package raw
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
@ -28,212 +26,6 @@ import (
|
|||||||
mockrepo "github.com/ossf/scorecard/v4/clients/mockclients"
|
mockrepo "github.com/ossf/scorecard/v4/clients/mockclients"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Test_reviews tests the reviews function.
|
|
||||||
func Test_reviews(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
type args struct {
|
|
||||||
mr *clients.PullRequest
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want []checker.Review
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Test_reviews",
|
|
||||||
args: args{
|
|
||||||
mr: &clients.PullRequest{
|
|
||||||
Reviews: []clients.Review{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
want: []checker.Review{},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test_reviews",
|
|
||||||
args: args{
|
|
||||||
mr: &clients.PullRequest{
|
|
||||||
Reviews: []clients.Review{
|
|
||||||
{
|
|
||||||
State: "APPROVED",
|
|
||||||
Author: &clients.User{
|
|
||||||
Login: "user",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
State: "APPROVED",
|
|
||||||
Author: &clients.User{
|
|
||||||
Login: "user",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
want: []checker.Review{
|
|
||||||
{
|
|
||||||
Reviewer: checker.User{
|
|
||||||
Login: "user",
|
|
||||||
},
|
|
||||||
State: "APPROVED",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Reviewer: checker.User{
|
|
||||||
Login: "user",
|
|
||||||
},
|
|
||||||
State: "APPROVED",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
tt := tt
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
if got := reviews(tt.args.mr); !reflect.DeepEqual(got, tt.want) {
|
|
||||||
t.Errorf("reviews() = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test_labels tests the labels function.
|
|
||||||
func Test_labels(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
type args struct {
|
|
||||||
mr *clients.PullRequest
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want []string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Test_labels",
|
|
||||||
args: args{
|
|
||||||
mr: &clients.PullRequest{
|
|
||||||
Labels: []clients.Label{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
want: []string{},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test_labels",
|
|
||||||
args: args{
|
|
||||||
mr: &clients.PullRequest{
|
|
||||||
Labels: []clients.Label{
|
|
||||||
{
|
|
||||||
Name: "label",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "label",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
want: []string{"label", "label"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
tt := tt
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
if got := labels(tt.args.mr); !reflect.DeepEqual(got, tt.want) {
|
|
||||||
t.Errorf("labels() = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test_mergeRequest tests the mergeRequest function.
|
|
||||||
func Test_mergeRequest(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
type args struct {
|
|
||||||
mr *clients.PullRequest
|
|
||||||
}
|
|
||||||
//nolint
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want *checker.MergeRequest
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Test_mergeRequest",
|
|
||||||
args: args{
|
|
||||||
mr: &clients.PullRequest{
|
|
||||||
MergedAt: time.Time{},
|
|
||||||
HeadSHA: "sha",
|
|
||||||
Labels: []clients.Label{
|
|
||||||
{
|
|
||||||
Name: "label",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "label",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
want: &checker.MergeRequest{
|
|
||||||
MergedAt: time.Time{},
|
|
||||||
Labels: []string{"label", "label"},
|
|
||||||
Author: checker.User{},
|
|
||||||
Reviews: []checker.Review{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
tt := tt
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
if got := mergeRequest(tt.args.mr); !cmp.Equal(got, tt.want) {
|
|
||||||
t.Errorf("mergeRequest() = %+v, want %+v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test_getRawDataFromCommit tests the getRawDataFromCommit function.
|
|
||||||
func Test_getRawDataFromCommit(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
type args struct {
|
|
||||||
c *clients.Commit
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want checker.DefaultBranchCommit
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Test_getRawDataFromCommit",
|
|
||||||
args: args{
|
|
||||||
c: &clients.Commit{
|
|
||||||
CommittedDate: time.Time{},
|
|
||||||
Message: "message",
|
|
||||||
SHA: "sha",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
want: checker.DefaultBranchCommit{
|
|
||||||
SHA: "sha",
|
|
||||||
CommitMessage: "message",
|
|
||||||
CommitDate: &time.Time{},
|
|
||||||
MergeRequest: &checker.MergeRequest{
|
|
||||||
Labels: []string{},
|
|
||||||
Reviews: []checker.Review{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
tt := tt
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
if got := getRawDataFromCommit(tt.args.c); !cmp.Equal(got, tt.want) {
|
|
||||||
t.Errorf(cmp.Diff(got, tt.want))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestCodeReviews tests the CodeReviews function.
|
// TestCodeReviews tests the CodeReviews function.
|
||||||
func TestCodeReview(t *testing.T) {
|
func TestCodeReview(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
@ -257,12 +49,12 @@ func TestCodeReview(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
mr := mockrepo.NewMockRepoClient(ctrl)
|
mr := mockrepo.NewMockRepoClient(ctrl)
|
||||||
mr.EXPECT().ListCommits().DoAndReturn(func() ([]*clients.Commit, error) {
|
mr.EXPECT().ListCommits().DoAndReturn(func() ([]clients.Commit, error) {
|
||||||
if tt.wantErr {
|
if tt.wantErr {
|
||||||
//nolint
|
//nolint
|
||||||
return nil, errors.New("error")
|
return nil, errors.New("error")
|
||||||
}
|
}
|
||||||
return []*clients.Commit{
|
return []clients.Commit{
|
||||||
{
|
{
|
||||||
SHA: "sha",
|
SHA: "sha",
|
||||||
},
|
},
|
||||||
|
@ -24,7 +24,7 @@ import (
|
|||||||
|
|
||||||
// Contributors retrieves the raw data for the Contributors check.
|
// Contributors retrieves the raw data for the Contributors check.
|
||||||
func Contributors(c clients.RepoClient) (checker.ContributorsData, error) {
|
func Contributors(c clients.RepoClient) (checker.ContributorsData, error) {
|
||||||
var users []checker.User
|
var users []clients.Contributor
|
||||||
|
|
||||||
contribs, err := c.ListContributors()
|
contribs, err := c.ListContributors()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -32,23 +32,21 @@ func Contributors(c clients.RepoClient) (checker.ContributorsData, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, contrib := range contribs {
|
for _, contrib := range contribs {
|
||||||
user := checker.User{
|
user := clients.Contributor{
|
||||||
Login: contrib.User.Login,
|
User: contrib.User,
|
||||||
NumContributions: uint(contrib.NumContributions),
|
NumContributions: contrib.NumContributions,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, org := range contrib.Organizations {
|
for _, org := range contrib.Organizations {
|
||||||
if org.Login != "" && !orgContains(user.Organizations, org.Login) {
|
if org.Login != "" && !orgContains(user.Organizations, org.Login) {
|
||||||
user.Organizations = append(user.Organizations,
|
user.Organizations = append(user.Organizations, org)
|
||||||
checker.Organization{
|
|
||||||
Login: org.Login,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
company := contrib.Company
|
for _, company := range contrib.Companies {
|
||||||
if company != "" {
|
if company == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
company = strings.ToLower(company)
|
company = strings.ToLower(company)
|
||||||
company = strings.ReplaceAll(company, "inc.", "")
|
company = strings.ReplaceAll(company, "inc.", "")
|
||||||
company = strings.ReplaceAll(company, "llc", "")
|
company = strings.ReplaceAll(company, "llc", "")
|
||||||
@ -56,11 +54,7 @@ func Contributors(c clients.RepoClient) (checker.ContributorsData, error) {
|
|||||||
company = strings.TrimLeft(company, "@")
|
company = strings.TrimLeft(company, "@")
|
||||||
company = strings.Trim(company, " ")
|
company = strings.Trim(company, " ")
|
||||||
if company != "" && !companyContains(user.Companies, company) {
|
if company != "" && !companyContains(user.Companies, company) {
|
||||||
user.Companies = append(user.Companies,
|
user.Companies = append(user.Companies, company)
|
||||||
checker.Company{
|
|
||||||
Name: company,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,16 +64,16 @@ func Contributors(c clients.RepoClient) (checker.ContributorsData, error) {
|
|||||||
return checker.ContributorsData{Users: users}, nil
|
return checker.ContributorsData{Users: users}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func companyContains(cs []checker.Company, name string) bool {
|
func companyContains(cs []string, name string) bool {
|
||||||
for _, a := range cs {
|
for _, a := range cs {
|
||||||
if a.Name == name {
|
if a == name {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func orgContains(os []checker.Organization, login string) bool {
|
func orgContains(os []clients.User, login string) bool {
|
||||||
for _, a := range os {
|
for _, a := range os {
|
||||||
if a.Login == login {
|
if a.Login == login {
|
||||||
return true
|
return true
|
||||||
|
@ -36,12 +36,7 @@ func Maintained(c *checker.CheckRequest) (checker.MaintainedData, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return result, fmt.Errorf("%w", err)
|
return result, fmt.Errorf("%w", err)
|
||||||
}
|
}
|
||||||
|
result.DefaultBranchCommits = commits
|
||||||
for i := range commits {
|
|
||||||
// Note: getRawDataFromCommit() is defined in Code-Review check.
|
|
||||||
result.DefaultBranchCommits = append(result.DefaultBranchCommits,
|
|
||||||
getRawDataFromCommit(&commits[i]))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recent issues.
|
// Recent issues.
|
||||||
issues, err := c.RepoClient.ListIssues()
|
issues, err := c.RepoClient.ListIssues()
|
||||||
|
@ -16,7 +16,7 @@ package clients
|
|||||||
|
|
||||||
// Contributor represents a contributor to a repo.
|
// Contributor represents a contributor to a repo.
|
||||||
type Contributor struct {
|
type Contributor struct {
|
||||||
Company string
|
Companies []string
|
||||||
User User
|
User User
|
||||||
Organizations []User
|
Organizations []User
|
||||||
NumContributions int
|
NumContributions int
|
||||||
|
@ -77,7 +77,7 @@ func (handler *contributorsHandler) setup() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
handler.errSetup = fmt.Errorf("error during Users.Get: %w", err)
|
handler.errSetup = fmt.Errorf("error during Users.Get: %w", err)
|
||||||
}
|
}
|
||||||
contributor.Company = user.GetCompany()
|
contributor.Companies = append(contributor.Companies, user.GetCompany())
|
||||||
handler.contributors = append(handler.contributors, contributor)
|
handler.contributors = append(handler.contributors, contributor)
|
||||||
}
|
}
|
||||||
handler.errSetup = nil
|
handler.errSetup = nil
|
||||||
|
@ -124,36 +124,34 @@ func addCodeReviewRawResults(r *jsonScorecardRawResult, cr *checker.CodeReviewDa
|
|||||||
Committer: jsonUser{
|
Committer: jsonUser{
|
||||||
Login: commit.Committer.Login,
|
Login: commit.Committer.Login,
|
||||||
},
|
},
|
||||||
CommitMessage: commit.CommitMessage,
|
CommitMessage: commit.Message,
|
||||||
SHA: commit.SHA,
|
SHA: commit.SHA,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge request field.
|
// Merge request field.
|
||||||
if commit.MergeRequest != nil {
|
mr := jsonMergeRequest{
|
||||||
mr := jsonMergeRequest{
|
Number: commit.AssociatedMergeRequest.Number,
|
||||||
Number: commit.MergeRequest.Number,
|
Author: jsonUser{
|
||||||
Author: jsonUser{
|
Login: commit.AssociatedMergeRequest.Author.Login,
|
||||||
Login: commit.MergeRequest.Author.Login,
|
},
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(commit.MergeRequest.Labels) > 0 {
|
|
||||||
mr.Labels = commit.MergeRequest.Labels
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, r := range commit.MergeRequest.Reviews {
|
|
||||||
mr.Reviews = append(mr.Reviews, jsonReview{
|
|
||||||
State: r.State,
|
|
||||||
Reviewer: jsonUser{
|
|
||||||
Login: r.Reviewer.Login,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
com.MergeRequest = &mr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
com.CommitMessage = commit.CommitMessage
|
for _, l := range commit.AssociatedMergeRequest.Labels {
|
||||||
|
mr.Labels = append(mr.Labels, l.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range commit.AssociatedMergeRequest.Reviews {
|
||||||
|
mr.Reviews = append(mr.Reviews, jsonReview{
|
||||||
|
State: r.State,
|
||||||
|
Reviewer: jsonUser{
|
||||||
|
Login: r.Author.Login,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
com.MergeRequest = &mr
|
||||||
|
|
||||||
|
com.CommitMessage = commit.Message
|
||||||
|
|
||||||
r.Results.DefaultBranchCommits = append(r.Results.DefaultBranchCommits, com)
|
r.Results.DefaultBranchCommits = append(r.Results.DefaultBranchCommits, com)
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ossf/scorecard/v4/checker"
|
"github.com/ossf/scorecard/v4/checker"
|
||||||
|
"github.com/ossf/scorecard/v4/clients"
|
||||||
sce "github.com/ossf/scorecard/v4/errors"
|
sce "github.com/ossf/scorecard/v4/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -82,7 +83,7 @@ type jsonUser struct {
|
|||||||
Organizations []jsonOrganization `json:"organization,omitempty"`
|
Organizations []jsonOrganization `json:"organization,omitempty"`
|
||||||
// Companies refer to a claim by a user in their profile.
|
// Companies refer to a claim by a user in their profile.
|
||||||
Companies []jsonCompany `json:"company,omitempty"`
|
Companies []jsonCompany `json:"company,omitempty"`
|
||||||
NumContributions uint `json:"NumContributions"`
|
NumContributions int `json:"NumContributions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type jsonContributors struct {
|
type jsonContributors struct {
|
||||||
@ -246,7 +247,7 @@ func (r *jsonScorecardRawResult) addContributorsRawResults(cr *checker.Contribut
|
|||||||
|
|
||||||
for _, user := range cr.Users {
|
for _, user := range cr.Users {
|
||||||
u := jsonUser{
|
u := jsonUser{
|
||||||
Login: user.Login,
|
Login: user.User.Login,
|
||||||
NumContributions: user.NumContributions,
|
NumContributions: user.NumContributions,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,7 +262,7 @@ func (r *jsonScorecardRawResult) addContributorsRawResults(cr *checker.Contribut
|
|||||||
for _, comp := range user.Companies {
|
for _, comp := range user.Companies {
|
||||||
u.Companies = append(u.Companies,
|
u.Companies = append(u.Companies,
|
||||||
jsonCompany{
|
jsonCompany{
|
||||||
Name: comp.Name,
|
Name: comp,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -338,7 +339,7 @@ func getStrPtr(s string) *string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Function shared between addMaintainedRawResults() and addCodeReviewRawResults().
|
// Function shared between addMaintainedRawResults() and addCodeReviewRawResults().
|
||||||
func (r *jsonScorecardRawResult) setDefaultCommitData(commits []checker.DefaultBranchCommit) error {
|
func (r *jsonScorecardRawResult) setDefaultCommitData(commits []clients.Commit) error {
|
||||||
if len(r.Results.DefaultBranchCommits) > 0 {
|
if len(r.Results.DefaultBranchCommits) > 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -353,36 +354,34 @@ func (r *jsonScorecardRawResult) setDefaultCommitData(commits []checker.DefaultB
|
|||||||
// try to use issue information to set it, but we're likely to miss
|
// try to use issue information to set it, but we're likely to miss
|
||||||
// many anyway.
|
// many anyway.
|
||||||
},
|
},
|
||||||
CommitMessage: commit.CommitMessage,
|
CommitMessage: commit.Message,
|
||||||
SHA: commit.SHA,
|
SHA: commit.SHA,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge request field.
|
// Merge request field.
|
||||||
if commit.MergeRequest != nil {
|
mr := jsonMergeRequest{
|
||||||
mr := jsonMergeRequest{
|
Number: commit.AssociatedMergeRequest.Number,
|
||||||
Number: commit.MergeRequest.Number,
|
Author: jsonUser{
|
||||||
Author: jsonUser{
|
Login: commit.AssociatedMergeRequest.Author.Login,
|
||||||
Login: commit.MergeRequest.Author.Login,
|
},
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(commit.MergeRequest.Labels) > 0 {
|
|
||||||
mr.Labels = commit.MergeRequest.Labels
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, r := range commit.MergeRequest.Reviews {
|
|
||||||
mr.Reviews = append(mr.Reviews, jsonReview{
|
|
||||||
State: r.State,
|
|
||||||
Reviewer: jsonUser{
|
|
||||||
Login: r.Reviewer.Login,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
com.MergeRequest = &mr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
com.CommitMessage = commit.CommitMessage
|
for _, l := range commit.AssociatedMergeRequest.Labels {
|
||||||
|
mr.Labels = append(mr.Labels, l.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range commit.AssociatedMergeRequest.Reviews {
|
||||||
|
mr.Reviews = append(mr.Reviews, jsonReview{
|
||||||
|
State: r.State,
|
||||||
|
Reviewer: jsonUser{
|
||||||
|
Login: r.Author.Login,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
com.MergeRequest = &mr
|
||||||
|
|
||||||
|
com.CommitMessage = commit.Message
|
||||||
|
|
||||||
r.Results.DefaultBranchCommits = append(r.Results.DefaultBranchCommits, com)
|
r.Results.DefaultBranchCommits = append(r.Results.DefaultBranchCommits, com)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user