Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ scdiff: Limit generating results to specific checks #3535

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"fmt"
"io"
"os"
"strings"

"github.com/spf13/cobra"

Expand All @@ -32,11 +33,13 @@
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 @@
defer outputF.Close()
output = outputF
}
r := runner.New()
checks := strings.Split(checksArg, ",")
r := runner.New(checks)

Check warning on line 64 in cmd/internal/scdiff/app/generate.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/scdiff/app/generate.go#L63-L64

Added lines #L63 - L64 were not covered by tests
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
Loading