From a0ca8197d9ef2c7c08ae232c9387178152ed6ead Mon Sep 17 00:00:00 2001 From: Azeem Shaikh Date: Mon, 16 May 2022 00:15:42 +0000 Subject: [PATCH] Do not fail on empty repositories --- checks/raw/vulnerabilities.go | 22 +++++++++++++++++----- checks/raw/vulnerabilities_test.go | 4 ++-- cron/format/json.go | 4 ++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/checks/raw/vulnerabilities.go b/checks/raw/vulnerabilities.go index 80d148f3817..500a9c305de 100644 --- a/checks/raw/vulnerabilities.go +++ b/checks/raw/vulnerabilities.go @@ -15,15 +15,12 @@ package raw import ( - "errors" "fmt" "github.com/ossf/scorecard/v4/checker" "github.com/ossf/scorecard/v4/clients" ) -var errNoCommitFound = errors.New("no commit found") - // Vulnerabilities retrieves the raw data for the Vulnerabilities check. func Vulnerabilities(c *checker.CheckRequest) (checker.VulnerabilitiesData, error) { commits, err := c.RepoClient.ListCommits() @@ -31,8 +28,8 @@ func Vulnerabilities(c *checker.CheckRequest) (checker.VulnerabilitiesData, erro return checker.VulnerabilitiesData{}, fmt.Errorf("repoClient.ListCommits: %w", err) } - if len(commits) < 1 || commits[0].SHA == "" { - return checker.VulnerabilitiesData{}, fmt.Errorf("%w", errNoCommitFound) + if len(commits) < 1 || allOf(commits, hasEmptySHA) { + return checker.VulnerabilitiesData{}, nil } resp, err := c.VulnerabilitiesClient.HasUnfixedVulnerabilities(c.Ctx, commits[0].SHA) @@ -52,6 +49,21 @@ func Vulnerabilities(c *checker.CheckRequest) (checker.VulnerabilitiesData, erro return checker.VulnerabilitiesData{Vulnerabilities: vulns}, nil } +type predicateOnCommitFn func(clients.Commit) bool + +var hasEmptySHA predicateOnCommitFn = func(c clients.Commit) bool { + return c.SHA == "" +} + +func allOf(commits []clients.Commit, predicate func(clients.Commit) bool) bool { + for i := range commits { + if !predicate(commits[i]) { + return false + } + } + return true +} + func getVulnerabilities(resp *clients.VulnerabilitiesResponse) []string { ids := make([]string, 0, len(resp.Vulns)) for _, vuln := range resp.Vulns { diff --git a/checks/raw/vulnerabilities_test.go b/checks/raw/vulnerabilities_test.go index bd24c938c14..9de4b316db9 100644 --- a/checks/raw/vulnerabilities_test.go +++ b/checks/raw/vulnerabilities_test.go @@ -54,8 +54,8 @@ func TestVulnerabilities(t *testing.T) { vulnsResponse: clients.VulnerabilitiesResponse{}, }, { - name: "err response", - wantErr: true, + name: "no commits", + wantErr: false, numberofCommits: 0, vulnsResponse: clients.VulnerabilitiesResponse{}, }, diff --git a/cron/format/json.go b/cron/format/json.go index 41e3e96be4a..850ae01a136 100644 --- a/cron/format/json.go +++ b/cron/format/json.go @@ -93,7 +93,7 @@ func AsJSON(r *pkg.ScorecardResult, showDetails bool, logLevel log.Level, writer Metadata: r.Metadata, } - //nolint + for _, checkResult := range r.Checks { tmpResult := jsonCheckResult{ Name: checkResult.Name, @@ -142,7 +142,7 @@ func AsJSON2(r *pkg.ScorecardResult, showDetails bool, AggregateScore: jsonFloatScore(score), } - //nolint + for _, checkResult := range r.Checks { doc, e := checkDocs.GetCheck(checkResult.Name) if e != nil {