Do not fail on empty repositories (#1914)

Co-authored-by: Azeem Shaikh <azeems@google.com>
This commit is contained in:
Azeem Shaikh 2022-05-15 17:41:17 -07:00 committed by GitHub
parent b1ab7eb9bb
commit 236b296403
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 9 deletions

View File

@ -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 {

View File

@ -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{},
},

View File

@ -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 {