From e1d3abc7fd2bdfe8819ac19b5c82815ea20890e6 Mon Sep 17 00:00:00 2001 From: Spencer Schrock Date: Thu, 5 Oct 2023 11:58:53 -0700 Subject: [PATCH] :sparkles: scdiff: Limit generating results to specific checks (#3535) * accept checks arg when generating golden. Signed-off-by: Spencer Schrock * dont shadow import Signed-off-by: Spencer Schrock --------- Signed-off-by: Spencer Schrock --- cmd/internal/scdiff/app/generate.go | 6 ++++- cmd/internal/scdiff/app/runner/runner.go | 23 +++++++++++++++++-- cmd/internal/scdiff/app/runner/runner_test.go | 7 +++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/cmd/internal/scdiff/app/generate.go b/cmd/internal/scdiff/app/generate.go index 3cf29acd8ac..d3a6399a3fa 100644 --- a/cmd/internal/scdiff/app/generate.go +++ b/cmd/internal/scdiff/app/generate.go @@ -19,6 +19,7 @@ import ( "fmt" "io" "os" + "strings" "github.com/spf13/cobra" @@ -32,11 +33,13 @@ func init() { rootCmd.AddCommand(generateCmd) generateCmd.PersistentFlags().StringVarP(&repoFile, "repos", "r", "", "path to newline-delimited repo file") generateCmd.PersistentFlags().StringVarP(&outputFile, "output", "o", "", "write to file instead of stdout") + generateCmd.PersistentFlags().StringVar(&checksArg, "checks", "", "Comma separated list of checks to run") } var ( repoFile string outputFile string + checksArg string generateCmd = &cobra.Command{ Use: "generate [flags] repofile", @@ -57,7 +60,8 @@ var ( defer outputF.Close() output = outputF } - r := runner.New() + checks := strings.Split(checksArg, ",") + r := runner.New(checks) return generate(&r, input, output) }, } diff --git a/cmd/internal/scdiff/app/runner/runner.go b/cmd/internal/scdiff/app/runner/runner.go index 4a65e1f7a51..535bc7a84f2 100644 --- a/cmd/internal/scdiff/app/runner/runner.go +++ b/cmd/internal/scdiff/app/runner/runner.go @@ -16,6 +16,7 @@ package runner import ( "context" + "strings" "github.com/ossf/scorecard/v4/checker" "github.com/ossf/scorecard/v4/checks" @@ -42,7 +43,8 @@ type Runner struct { vuln clients.VulnerabilitiesClient } -func New() Runner { +// Creates a Runner which will run the listed checks. If no checks are provided, all will run. +func New(enabledChecks []string) Runner { ctx := context.Background() logger := log.NewLogger(log.DefaultLevel) return Runner{ @@ -52,7 +54,7 @@ func New() Runner { ossFuzz: ossfuzz.CreateOSSFuzzClient(ossfuzz.StatusURL), cii: clients.DefaultCIIBestPracticesClient(), vuln: clients.DefaultVulnerabilitiesClient(), - enabledChecks: checks.GetAll(), + enabledChecks: parseChecks(enabledChecks), } } @@ -73,3 +75,20 @@ func (r *Runner) log(msg string) { r.logger.Info(msg) } } + +func parseChecks(c []string) checker.CheckNameToFnMap { + all := checks.GetAll() + if len(c) == 0 { + return all + } + + ret := checker.CheckNameToFnMap{} + for _, requested := range c { + for key, fn := range all { + if strings.EqualFold(key, requested) { + ret[key] = fn + } + } + } + return ret +} diff --git a/cmd/internal/scdiff/app/runner/runner_test.go b/cmd/internal/scdiff/app/runner/runner_test.go index 8d44ce10345..1b4c05e3c4a 100644 --- a/cmd/internal/scdiff/app/runner/runner_test.go +++ b/cmd/internal/scdiff/app/runner/runner_test.go @@ -25,10 +25,15 @@ import ( ) func TestNew(t *testing.T) { - r := New() + r := New(nil) if len(r.enabledChecks) == 0 { t.Errorf("runner has no checks to run: %v", r.enabledChecks) } + requestedChecks := []string{"Code-Review"} + r = New(requestedChecks) + if len(r.enabledChecks) != len(requestedChecks) { + t.Errorf("requested %d checks but only got: %v", len(requestedChecks), r.enabledChecks) + } } func TestRunner_Run(t *testing.T) {