🌱 Refactor the osv check into a interface

Refactor the osv check into a interface for that it can be tested.
This commit is contained in:
naveen 2021-12-22 21:49:55 +00:00 committed by Naveen
parent c11772788a
commit c8f15a495e
2 changed files with 17 additions and 4 deletions

View File

@ -41,9 +41,16 @@ type osvResponse struct {
} `json:"vulns"`
}
// Vulnerabilities cheks for vulnerabilities in api.osv.dev.
type Vulnerabilities interface {
HasUnfixedVulnerabilities(c *checker.CheckRequest) checker.CheckResult
}
type vulns struct{}
//nolint:gochecknoinits
func init() {
registerCheck(CheckVulnerabilities, HasUnfixedVulnerabilities)
v := &vulns{}
registerCheck(CheckVulnerabilities, v.HasUnfixedVulnerabilities)
}
func (resp *osvResponse) getVulnerabilities() []string {
@ -54,8 +61,13 @@ func (resp *osvResponse) getVulnerabilities() []string {
return ids
}
// NewVulnerabilities creates a new Vulnerabilities check.
func NewVulnerabilities() Vulnerabilities {
return &vulns{}
}
// HasUnfixedVulnerabilities runs Vulnerabilities check.
func HasUnfixedVulnerabilities(c *checker.CheckRequest) checker.CheckResult {
func (v *vulns) HasUnfixedVulnerabilities(c *checker.CheckRequest) checker.CheckResult {
commits, err := c.RepoClient.ListCommits()
if err != nil {
e := sce.WithMessage(sce.ErrScorecardInternal, "Client.Repositories.ListCommits")

View File

@ -49,7 +49,8 @@ var _ = Describe("E2E TEST:Vulnerabilities", func() {
NumberOfInfo: 0,
NumberOfDebug: 0,
}
result := checks.HasUnfixedVulnerabilities(&req)
result := checks.NewVulnerabilities().HasUnfixedVulnerabilities(&req)
// UPGRADEv2: to remove.
// Old version.
Expect(result.Error).Should(BeNil())
@ -80,7 +81,7 @@ var _ = Describe("E2E TEST:Vulnerabilities", func() {
NumberOfInfo: 0,
NumberOfDebug: 0,
}
result := checks.HasUnfixedVulnerabilities(&checkRequest)
result := checks.NewVulnerabilities().HasUnfixedVulnerabilities(&checkRequest)
// UPGRADEv2: to remove.
// Old version.
Expect(result.Error).Should(BeNil())