diff --git a/clients/githubrepo/client.go b/clients/githubrepo/client.go index dade0a97c8c9..d0f38e83d712 100644 --- a/clients/githubrepo/client.go +++ b/clients/githubrepo/client.go @@ -239,7 +239,8 @@ func CreateOssFuzzRepoClient(ctx context.Context, logger *log.Logger) (clients.R return ossFuzzRepoClient, nil } -// TODO(repo): Pass a `http.RoundTripper` here +// GetClients returns a list of clients for running scorecard checks. +// TODO(repo): Pass a `http.RoundTripper` here. func GetClients(ctx context.Context, repoURI, localURI string, logger *log.Logger) ( clients.Repo, // repo clients.RepoClient, // repoClient @@ -261,7 +262,6 @@ func GetClients(ctx context.Context, repoURI, localURI string, logger *log.Logge githubRepo, errGitHub = MakeGithubRepo(repoURI) if errGitHub != nil { - // nolint: wrapcheck return githubRepo, nil, nil, diff --git a/cmd/root.go b/cmd/root.go index 0734bcbfe95a..32021be80968 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -106,6 +106,7 @@ func scorecardCmd(cmd *cobra.Command, args []string) { RunScorecard(args) } +// RunScorecard runs scorecard checks given a set of arguments. // TODO(cmd): Is `args` required? func RunScorecard(args []string) { // TODO(cmd): Catch validation errors @@ -167,8 +168,17 @@ func RunScorecard(args []string) { } } - repoResult, err := pkg.RunScorecards(ctx, repoURI, opts.Commit, opts.Format == options.FormatRaw, enabledChecks, repoClient, - ossFuzzRepoClient, ciiClient, vulnsClient) + repoResult, err := pkg.RunScorecards( + ctx, + repoURI, + opts.Commit, + opts.Format == options.FormatRaw, + enabledChecks, + repoClient, + ossFuzzRepoClient, + ciiClient, + vulnsClient, + ) if err != nil { log.Panic(err) } @@ -186,9 +196,9 @@ func RunScorecard(args []string) { fmt.Println("\nRESULTS\n-------") } - resultsErr := format.FormatResults( + resultsErr := format.Results( opts, - repoResult, + &repoResult, checkDocs, pol, ) diff --git a/format/format.go b/format/format.go index 2a3d4d81d20b..8f5f57f8231e 100644 --- a/format/format.go +++ b/format/format.go @@ -26,9 +26,10 @@ import ( spol "github.com/ossf/scorecard/v4/policy" ) -func FormatResults( +// Results formats scorecard results. +func Results( opts *options.Options, - results pkg.ScorecardResult, + results *pkg.ScorecardResult, docs checks.Doc, policy *spol.ScorecardPolicy, ) error { @@ -55,7 +56,7 @@ func FormatResults( } if err != nil { - return fmt.Errorf("Failed to output results: %v", err) + return fmt.Errorf("failed to output results: %w", err) } return nil diff --git a/options/options.go b/options/options.go index b91d879cd194..84846ec5ed74 100644 --- a/options/options.go +++ b/options/options.go @@ -12,17 +12,18 @@ // See the License for the specific language governing permissions and // limitations under the License. -// package options implements Scorecard options. +// Package options implements Scorecard options. package options import ( - "fmt" + "errors" "os" "github.com/ossf/scorecard/v4/clients" "github.com/ossf/scorecard/v4/log" ) +// Options define common options for configuring scorecard. type Options struct { Repo string Local string @@ -33,35 +34,58 @@ type Options struct { PyPI string RubyGems string PolicyFile string - ShowDetails bool ChecksToRun []string Metadata []string + ShowDetails bool } +// New creates a new instance of `Options`. func New() *Options { return &Options{} } const ( + // DefaultCommit specifies the default commit reference to use. DefaultCommit = clients.HeadSHA - // Formats - FormatJSON = "json" - FormatSarif = "sarif" + // Formats. + + // FormatJSON specifies that results should be output in JSON format. + FormatJSON = "json" + // FormatSarif specifies that results should be output in SARIF format. + FormatSarif = "sarif" + // FormatDefault specifies that results should be output in default format. FormatDefault = "default" - FormatRaw = "raw" + // FormatRaw specifies that results should be output in raw format. + FormatRaw = "raw" - // Environment variables + // Environment variables. + + // EnvVarEnableSarif is the environment variable which controls enabling + // SARIF logging. EnvVarEnableSarif = "ENABLE_SARIF" + // EnvVarScorecardV6 is the environment variable which enables scorecard v6 + // options. EnvVarScorecardV6 = "SCORECARD_V6" ) var ( + // DefaultLogLevel retrieves the default log level. DefaultLogLevel = log.DefaultLevel.String() + + errCommitIsEmpty = errors.New("commit should be non-empty") + errCommitOptionNotSupported = errors.New("commit option is not supported yet") + errFormatNotSupported = errors.New("unsupported format") + errPolicyFileNotSupported = errors.New("policy file is not supported yet") + errRawOptionNotSupported = errors.New("raw option is not supported yet") + errRepoOptionMustBeSet = errors.New( + "exactly one of `repo`, `npm`, `pypi`, `rubygems` or `local` must be set", + ) + errSARIFNotSupported = errors.New("SARIF format is not supported yet") ) -// TODO(options): Create explicit error types -// TODO(options): Cleanup error messages +// Validate validates scorecard configuration options. +// TODO(options): Cleanup error messages. func (o *Options) Validate() []error { var errs []error @@ -73,7 +97,7 @@ func (o *Options) Validate() []error { o.Local != "") != 1 { errs = append( errs, - fmt.Errorf("Exactly one of `--repo`, `--npm`, `--pypi`, `--rubygems` or `--local` must be set"), + errRepoOptionMustBeSet, ) } @@ -82,13 +106,13 @@ func (o *Options) Validate() []error { if o.Format == FormatSarif { errs = append( errs, - fmt.Errorf("sarif format not supported yet"), + errSARIFNotSupported, ) } if o.PolicyFile != "" { errs = append( errs, - fmt.Errorf("policy file not supported yet"), + errPolicyFileNotSupported, ) } } @@ -98,13 +122,13 @@ func (o *Options) Validate() []error { if o.Format == FormatRaw { errs = append( errs, - fmt.Errorf("raw option not supported yet"), + errRawOptionNotSupported, ) } if o.Commit != clients.HeadSHA { errs = append( errs, - fmt.Errorf("--commit option not supported yet"), + errCommitOptionNotSupported, ) } } @@ -113,7 +137,7 @@ func (o *Options) Validate() []error { if !validateFormat(o.Format) { errs = append( errs, - fmt.Errorf("unsupported format '%s'", o.Format), + errFormatNotSupported, ) } @@ -121,7 +145,7 @@ func (o *Options) Validate() []error { if o.Commit == "" { errs = append( errs, - fmt.Errorf("commit should be non-empty"), + errCommitIsEmpty, ) } @@ -138,7 +162,8 @@ func boolSum(bools ...bool) int { return sum } -// TODO(options): This probably doesn't need to be exported +// IsSarifEnabled returns true if `EnvVarEnableSarif` is specified. +// TODO(options): This probably doesn't need to be exported. func IsSarifEnabled() bool { // UPGRADEv4: remove. var sarifEnabled bool diff --git a/policy/policy.go b/policy/policy.go index 20ee0a70d1e6..2ab7da02dfb0 100644 --- a/policy/policy.go +++ b/policy/policy.go @@ -66,6 +66,7 @@ func modeToProto(m string) CheckPolicy_Mode { } } +// ParseFromFile takes a policy file and returns a `ScorecardPolicy`. func ParseFromFile(policyFile string) (*ScorecardPolicy, error) { if policyFile != "" { data, err := os.ReadFile(policyFile) @@ -74,7 +75,7 @@ func ParseFromFile(policyFile string) (*ScorecardPolicy, error) { fmt.Sprintf("os.ReadFile: %v", err)) } - sp, err := ParseFromYAML(data) + sp, err := parseFromYAML(data) if err != nil { return nil, sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("spol.ParseFromYAML: %v", err)) @@ -86,9 +87,8 @@ func ParseFromFile(policyFile string) (*ScorecardPolicy, error) { return nil, nil } -// ParseFromYAML parses a policy file and returns -// a scorecardPolicy. -func ParseFromYAML(b []byte) (*ScorecardPolicy, error) { +// parseFromYAML parses a policy file and returns a `ScorecardPolicy`. +func parseFromYAML(b []byte) (*ScorecardPolicy, error) { // Internal golang for unmarshalling the policy file. sp := scorecardPolicy{} // Protobuf-defined policy (policy.proto and policy.pb.go). @@ -137,14 +137,19 @@ func ParseFromYAML(b []byte) (*ScorecardPolicy, error) { return &retPolicy, nil } +// GetAll returns the full list of checks, given any environment variable +// constraints. func GetAll() checker.CheckNameToFnMap { - // Returns the full list of checks, given any environment variable constraints. possibleChecks := checks.AllChecks return possibleChecks } -func GetEnabled(sp *ScorecardPolicy, argsChecks []string, - requiredRequestTypes []checker.RequestType) (checker.CheckNameToFnMap, error) { +// GetEnabled returns the list of enabled checks. +func GetEnabled( + sp *ScorecardPolicy, + argsChecks []string, + requiredRequestTypes []checker.RequestType, +) (checker.CheckNameToFnMap, error) { enabledChecks := checker.CheckNameToFnMap{} switch { diff --git a/policy/policy_test.go b/policy/policy_test.go index 14dcfbef2820..306519b51ea7 100644 --- a/policy/policy_test.go +++ b/policy/policy_test.go @@ -114,7 +114,7 @@ func TestPolicyRead(t *testing.T) { t.Fatalf("cannot read file: %v", err) } - p, err := ParseFromYAML(content) + p, err := parseFromYAML(content) if !errors.Is(err, tt.err) { t.Fatalf("%s: expected %v, got %v", tt.name, tt.err, err)