Skip to content

Commit

Permalink
✨ scdiff: Limit generating results to specific checks (#3535)
Browse files Browse the repository at this point in the history
* accept checks arg when generating golden.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* dont shadow import

Signed-off-by: Spencer Schrock <sschrock@google.com>

---------

Signed-off-by: Spencer Schrock <sschrock@google.com>
  • Loading branch information
spencerschrock authored Oct 5, 2023
1 parent 64c491b commit e1d3abc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
6 changes: 5 additions & 1 deletion cmd/internal/scdiff/app/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/spf13/cobra"

Expand All @@ -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",
Expand All @@ -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)
},
}
Expand Down
23 changes: 21 additions & 2 deletions cmd/internal/scdiff/app/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package runner

import (
"context"
"strings"

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/checks"
Expand All @@ -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{
Expand All @@ -52,7 +54,7 @@ func New() Runner {
ossFuzz: ossfuzz.CreateOSSFuzzClient(ossfuzz.StatusURL),
cii: clients.DefaultCIIBestPracticesClient(),
vuln: clients.DefaultVulnerabilitiesClient(),
enabledChecks: checks.GetAll(),
enabledChecks: parseChecks(enabledChecks),
}
}

Expand All @@ -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
}
7 changes: 6 additions & 1 deletion cmd/internal/scdiff/app/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e1d3abc

Please sign in to comment.