Skip to content

Commit 236b296

Browse files
Do not fail on empty repositories (#1914)
Co-authored-by: Azeem Shaikh <azeems@google.com>
1 parent b1ab7eb commit 236b296

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

checks/raw/vulnerabilities.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,21 @@
1515
package raw
1616

1717
import (
18-
"errors"
1918
"fmt"
2019

2120
"github.com/ossf/scorecard/v4/checker"
2221
"github.com/ossf/scorecard/v4/clients"
2322
)
2423

25-
var errNoCommitFound = errors.New("no commit found")
26-
2724
// Vulnerabilities retrieves the raw data for the Vulnerabilities check.
2825
func Vulnerabilities(c *checker.CheckRequest) (checker.VulnerabilitiesData, error) {
2926
commits, err := c.RepoClient.ListCommits()
3027
if err != nil {
3128
return checker.VulnerabilitiesData{}, fmt.Errorf("repoClient.ListCommits: %w", err)
3229
}
3330

34-
if len(commits) < 1 || commits[0].SHA == "" {
35-
return checker.VulnerabilitiesData{}, fmt.Errorf("%w", errNoCommitFound)
31+
if len(commits) < 1 || allOf(commits, hasEmptySHA) {
32+
return checker.VulnerabilitiesData{}, nil
3633
}
3734

3835
resp, err := c.VulnerabilitiesClient.HasUnfixedVulnerabilities(c.Ctx, commits[0].SHA)
@@ -52,6 +49,21 @@ func Vulnerabilities(c *checker.CheckRequest) (checker.VulnerabilitiesData, erro
5249
return checker.VulnerabilitiesData{Vulnerabilities: vulns}, nil
5350
}
5451

52+
type predicateOnCommitFn func(clients.Commit) bool
53+
54+
var hasEmptySHA predicateOnCommitFn = func(c clients.Commit) bool {
55+
return c.SHA == ""
56+
}
57+
58+
func allOf(commits []clients.Commit, predicate func(clients.Commit) bool) bool {
59+
for i := range commits {
60+
if !predicate(commits[i]) {
61+
return false
62+
}
63+
}
64+
return true
65+
}
66+
5567
func getVulnerabilities(resp *clients.VulnerabilitiesResponse) []string {
5668
ids := make([]string, 0, len(resp.Vulns))
5769
for _, vuln := range resp.Vulns {

checks/raw/vulnerabilities_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ func TestVulnerabilities(t *testing.T) {
5454
vulnsResponse: clients.VulnerabilitiesResponse{},
5555
},
5656
{
57-
name: "err response",
58-
wantErr: true,
57+
name: "no commits",
58+
wantErr: false,
5959
numberofCommits: 0,
6060
vulnsResponse: clients.VulnerabilitiesResponse{},
6161
},

cron/format/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func AsJSON(r *pkg.ScorecardResult, showDetails bool, logLevel log.Level, writer
9393
Metadata: r.Metadata,
9494
}
9595

96-
//nolint
96+
9797
for _, checkResult := range r.Checks {
9898
tmpResult := jsonCheckResult{
9999
Name: checkResult.Name,
@@ -142,7 +142,7 @@ func AsJSON2(r *pkg.ScorecardResult, showDetails bool,
142142
AggregateScore: jsonFloatScore(score),
143143
}
144144

145-
//nolint
145+
146146
for _, checkResult := range r.Checks {
147147
doc, e := checkDocs.GetCheck(checkResult.Name)
148148
if e != nil {

0 commit comments

Comments
 (0)