Skip to content

Commit

Permalink
Do not fail on empty repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
azeemsgoogle committed May 15, 2022
1 parent b1ab7eb commit 97d6a27
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 17 additions & 2 deletions checks/raw/vulnerabilities.go
Expand Up @@ -31,8 +31,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)
Expand All @@ -52,6 +52,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 _, c := range commits {
if !predicate(c) {
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

0 comments on commit 97d6a27

Please sign in to comment.