Replace checker.CIIBadge with clients.CIIBadge (#1945)

Co-authored-by: Azeem Shaikh <azeems@google.com>
This commit is contained in:
Azeem Shaikh 2022-05-23 13:30:56 -07:00 committed by GitHub
parent 108f88d056
commit 1a2f08827f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 44 deletions

View File

@ -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.

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)

View File

@ -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
}