Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Do not fail on empty repositories #1914

Merged
merged 1 commit into from May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions checks/raw/vulnerabilities.go
Expand Up @@ -15,24 +15,21 @@
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()
if err != nil {
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)
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions checks/raw/vulnerabilities_test.go
Expand Up @@ -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{},
},
Expand Down
4 changes: 2 additions & 2 deletions cron/format/json.go
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down