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

🌱 Use new entrypoint for scdiff #4204

Merged
merged 2 commits into from
Jul 3, 2024
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
46 changes: 6 additions & 40 deletions cmd/internal/scdiff/app/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,22 @@ package runner
import (
"context"
"errors"
"strings"

"github.com/ossf/scorecard/v5/checker"
"github.com/ossf/scorecard/v5/checks"
"github.com/ossf/scorecard/v5/clients"
"github.com/ossf/scorecard/v5/clients/githubrepo"
"github.com/ossf/scorecard/v5/clients/gitlabrepo"
"github.com/ossf/scorecard/v5/clients/ossfuzz"
sce "github.com/ossf/scorecard/v5/errors"
"github.com/ossf/scorecard/v5/internal/packageclient"
"github.com/ossf/scorecard/v5/log"
"github.com/ossf/scorecard/v5/pkg"
)

const (
commit = clients.HeadSHA
commitDepth = 0 // default
)

// Runner holds the clients and configuration needed to run Scorecard on multiple repos.
type Runner struct {
ctx context.Context
logger *log.Logger
enabledChecks checker.CheckNameToFnMap
githubClient clients.RepoClient
gitlabClient clients.RepoClient
ossFuzz clients.RepoClient
cii clients.CIIBestPracticesClient
vuln clients.VulnerabilitiesClient
deps packageclient.ProjectPackageClient
logger *log.Logger
enabledChecks []string
}

// Creates a Runner which will run the listed checks. If no checks are provided, all will run.
Expand All @@ -62,11 +48,7 @@ func New(enabledChecks []string) Runner {
logger: logger,
githubClient: githubrepo.CreateGithubRepoClient(ctx, logger),
gitlabClient: gitlabClient,
ossFuzz: ossfuzz.CreateOSSFuzzClient(ossfuzz.StatusURL),
cii: clients.DefaultCIIBestPracticesClient(),
vuln: clients.DefaultVulnerabilitiesClient(),
deps: packageclient.CreateDepsDevClient(),
enabledChecks: parseChecks(enabledChecks),
enabledChecks: enabledChecks,
}
}

Expand All @@ -82,8 +64,9 @@ func (r *Runner) Run(repoURI string) (pkg.ScorecardResult, error) {
if err != nil {
return pkg.ScorecardResult{}, err
}
return pkg.RunScorecard(
r.ctx, repo, commit, commitDepth, r.enabledChecks, repoClient, r.ossFuzz, r.cii, r.vuln, r.deps,
return pkg.Run(r.ctx, repo,
pkg.WithRepoClient(repoClient),
pkg.WithChecks(r.enabledChecks),
)
}

Expand All @@ -93,20 +76,3 @@ 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
}
14 changes: 7 additions & 7 deletions cmd/internal/scdiff/app/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@
package runner

import (
"context"
"testing"

"github.com/golang/mock/gomock"

"github.com/ossf/scorecard/v5/checker"
"github.com/ossf/scorecard/v5/clients"
mockrepo "github.com/ossf/scorecard/v5/clients/mockclients"
"github.com/ossf/scorecard/v5/internal/checknames"
)

func TestNew(t *testing.T) {
t.Parallel()
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)
r := New(requestedChecks)
if len(r.enabledChecks) != len(requestedChecks) {
t.Errorf("requested %d checks but only got: %v", len(requestedChecks), r.enabledChecks)
}
Expand All @@ -43,11 +40,14 @@ func TestRunner_Run(t *testing.T) {
mockRepo := mockrepo.NewMockRepoClient(ctrl)
commit := []clients.Commit{{SHA: "foo"}}
mockRepo.EXPECT().ListCommits().Return(commit, nil)
mockRepo.EXPECT().ListFiles(gomock.Any()).Return(nil, nil)
mockRepo.EXPECT().InitRepo(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
mockRepo.EXPECT().GetDefaultBranchName().Return("main", nil)
mockRepo.EXPECT().Close().Return(nil)
r := Runner{
enabledChecks: checker.CheckNameToFnMap{},
ctx: context.Background(),
// use a check which works locally, but we declare no files above so no-op
enabledChecks: []string{checknames.BinaryArtifacts},
githubClient: mockRepo,
}
const repo = "github.com/foo/bar"
Expand Down
Loading