diff --git a/checker/raw_result.go b/checker/raw_result.go index 7a370715..a145578f 100644 --- a/checker/raw_result.go +++ b/checker/raw_result.go @@ -14,7 +14,11 @@ package checker -import "time" +import ( + "time" + + "github.com/ossf/scorecard/v4/clients" +) // RawResults contains results before a policy // is applied. @@ -290,28 +294,9 @@ type ReleaseAsset struct { URL string } -// CIIBadge corresponds to CII-Best-Practices badges. -// https://bestpractices.coreinfrastructure.org/en -type CIIBadge string - -const ( - // CIIBadgeUnknown or non-parsable CII Best Practices badge. - CIIBadgeUnknown CIIBadge = "unknown" - // CIIBadgeNotFound represents when CII Best Practices returns an empty response for a project. - CIIBadgeNotFound CIIBadge = "not_found" - // CIIBadgeInProgress state of CII Best Practices badge. - CIIBadgeInProgress CIIBadge = "in_progress" - // CIIBadgePassing for CII Best Practices badge. - CIIBadgePassing CIIBadge = "passing" - // CIIBadgeSilver for CII Best Practices badge. - CIIBadgeSilver CIIBadge = "silver" - // CIIBadgeGold for CII Best Practices badge. - CIIBadgeGold CIIBadge = "gold" -) - // CIIBestPracticesData contains data foor CIIBestPractices check. type CIIBestPracticesData struct { - Badge CIIBadge + Badge clients.BadgeLevel } // DangerousWorkflowType represents a type of dangerous workflow. diff --git a/checks/evaluation/cii_best_practices.go b/checks/evaluation/cii_best_practices.go index ee711c1a..b18b7231 100644 --- a/checks/evaluation/cii_best_practices.go +++ b/checks/evaluation/cii_best_practices.go @@ -18,6 +18,7 @@ import ( "fmt" "github.com/ossf/scorecard/v4/checker" + "github.com/ossf/scorecard/v4/clients" sce "github.com/ossf/scorecard/v4/errors" ) @@ -39,21 +40,21 @@ func CIIBestPractices(name string, dl checker.DetailLogger, r *checker.CIIBestPr var results checker.CheckResult switch r.Badge { - case checker.CIIBadgeNotFound: + case clients.NotFound: results = checker.CreateMinScoreResult(name, "no badge detected") - case checker.CIIBadgeInProgress: + case clients.InProgress: msg := fmt.Sprintf("badge detected: %v", r.Badge) results = checker.CreateResultWithScore(name, msg, inProgressScore) - case checker.CIIBadgePassing: + case clients.Passing: msg := fmt.Sprintf("badge detected: %v", r.Badge) results = checker.CreateResultWithScore(name, msg, passingScore) - case checker.CIIBadgeSilver: + case clients.Silver: msg := fmt.Sprintf("badge detected: %v", r.Badge) results = checker.CreateResultWithScore(name, msg, silverScore) - case checker.CIIBadgeGold: + case clients.Gold: msg := fmt.Sprintf("badge detected: %v", r.Badge) results = checker.CreateMaxScoreResult(name, msg) - case checker.CIIBadgeUnknown: + case clients.Unknown: e := sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("unsupported badge: %v", r.Badge)) results = checker.CreateRuntimeErrorResult(name, e) } diff --git a/checks/raw/cii_best_practices.go b/checks/raw/cii_best_practices.go index fb2aee36..032005a9 100644 --- a/checks/raw/cii_best_practices.go +++ b/checks/raw/cii_best_practices.go @@ -19,7 +19,6 @@ import ( "fmt" "github.com/ossf/scorecard/v4/checker" - "github.com/ossf/scorecard/v4/clients" ) var errEmptyClient = errors.New("CII client is nil") @@ -35,21 +34,7 @@ func CIIBestPractices(c *checker.CheckRequest) (checker.CIIBestPracticesData, er if err != nil { return results, fmt.Errorf("%w", err) } - - switch badge { - case clients.NotFound: - results.Badge = checker.CIIBadgeNotFound - case clients.InProgress: - results.Badge = checker.CIIBadgeInProgress - case clients.Passing: - results.Badge = checker.CIIBadgePassing - case clients.Silver: - results.Badge = checker.CIIBadgeSilver - case clients.Gold: - results.Badge = checker.CIIBadgeGold - case clients.Unknown: - results.Badge = checker.CIIBadgeUnknown - } + results.Badge = badge return results, nil } diff --git a/clients/cii_client.go b/clients/cii_client.go index 03a6c2a9..d28287d5 100644 --- a/clients/cii_client.go +++ b/clients/cii_client.go @@ -37,6 +37,26 @@ const ( // https://bestpractices.coreinfrastructure.org/en type BadgeLevel uint +// String returns a string value for BadgeLevel enum. +func (badge BadgeLevel) String() string { + switch badge { + case Unknown: + return "Unknown" + case NotFound: + return "not_found" + case InProgress: + return "in_progress" + case Passing: + return "passing" + case Silver: + return "silver" + case Gold: + return "gold" + default: + return "" + } +} + // CIIBestPracticesClient interface returns the BadgeLevel for a repo URL. type CIIBestPracticesClient interface { GetBadgeLevel(ctx context.Context, uri string) (BadgeLevel, error) diff --git a/pkg/json_raw_results.go b/pkg/json_raw_results.go index 1bf69ac9..2e2d9e49 100644 --- a/pkg/json_raw_results.go +++ b/pkg/json_raw_results.go @@ -395,7 +395,7 @@ func (r *jsonScorecardRawResult) setDefaultCommitData(commits []checker.DefaultB //nolint:unparam func (r *jsonScorecardRawResult) addOssfBestPracticesRawResults(cbp *checker.CIIBestPracticesData) error { - r.Results.OssfBestPractices.Badge = string(cbp.Badge) + r.Results.OssfBestPractices.Badge = cbp.Badge.String() return nil }