mirror of
https://github.com/ossf/scorecard.git
synced 2024-09-11 00:45:36 +03:00
Do not fail on empty repositories (#1914)
Co-authored-by: Azeem Shaikh <azeems@google.com>
This commit is contained in:
parent
b1ab7eb9bb
commit
236b296403
@ -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 {
|
||||
|
@ -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{},
|
||||
},
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user