diff --git a/cmd/root.go b/cmd/root.go index 8b01f216274..3ff9a6fee93 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,6 +17,7 @@ package cmd import ( "context" + "errors" "fmt" "os" "sort" @@ -27,6 +28,9 @@ import ( "github.com/ossf/scorecard/v5/checker" "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/localdir" pmc "github.com/ossf/scorecard/v5/cmd/internal/packagemanager" docs "github.com/ossf/scorecard/v5/docs/checks" sce "github.com/ossf/scorecard/v5/errors" @@ -92,16 +96,18 @@ func rootCmd(o *options.Options) error { } ctx := context.Background() - logger := sclog.NewLogger(sclog.ParseLevel(o.LogLevel)) - repoURI, repoClient, ossFuzzRepoClient, ciiClient, vulnsClient, projectClient, err := checker.GetClients( - ctx, o.Repo, o.Local, logger) // MODIFIED - if err != nil { - return fmt.Errorf("GetClients: %w", err) - } - defer repoClient.Close() - if ossFuzzRepoClient != nil { - defer ossFuzzRepoClient.Close() + var repo clients.Repo + if o.Local != "" { + repo, err = localdir.MakeLocalDirRepo(o.Local) + if err != nil { + return fmt.Errorf("making local dir: %w", err) + } + } else { + repo, err = makeRepo(o.Repo) + if err != nil { + return fmt.Errorf("making remote repo: %w", err) + } } // Read docs. @@ -117,10 +123,17 @@ func rootCmd(o *options.Options) error { if !strings.EqualFold(o.Commit, clients.HeadSHA) { requiredRequestTypes = append(requiredRequestTypes, checker.CommitBased) } + // this call to policy is different from the one in pkg.Run + // this one is concerned with a policy file, while the pkg.Run call is + // more concerned with the supported request types enabledChecks, err := policy.GetEnabled(pol, o.Checks(), requiredRequestTypes) if err != nil { return fmt.Errorf("GetEnabled: %w", err) } + checks := make([]string, 0, len(enabledChecks)) + for c := range enabledChecks { + checks = append(checks, c) + } enabledProbes := o.Probes() if o.Format == options.FormatDefault { @@ -131,18 +144,12 @@ func rootCmd(o *options.Options) error { } } - repoResult, err = pkg.ExperimentalRunProbes( - ctx, - repoURI, - o.Commit, - o.CommitDepth, - enabledChecks, - enabledProbes, - repoClient, - ossFuzzRepoClient, - ciiClient, - vulnsClient, - projectClient, + repoResult, err = pkg.Run(ctx, repo, + pkg.WithLogLevel(sclog.ParseLevel(o.LogLevel)), + pkg.WithCommitSHA(o.Commit), + pkg.WithCommitDepth(o.CommitDepth), + pkg.WithProbes(enabledProbes), + pkg.WithChecks(checks), ) if err != nil { return fmt.Errorf("RunScorecard: %w", err) @@ -206,3 +213,18 @@ func printCheckResults(enabledChecks checker.CheckNameToFnMap) { } fmt.Fprintln(os.Stderr, "\nRESULTS\n-------") } + +// makeRepo helps turn a URI into the appropriate clients.Repo. +// currently this is a decision between GitHub and GitLab, +// but may expand in the future. +func makeRepo(uri string) (clients.Repo, error) { + var repo clients.Repo + var errGitHub, errGitLab error + if repo, errGitHub = githubrepo.MakeGithubRepo(uri); errGitHub != nil { + repo, errGitLab = gitlabrepo.MakeGitlabRepo(uri) + if errGitLab != nil { + return nil, fmt.Errorf("unable to parse as github or gitlab: %w", errors.Join(errGitHub, errGitLab)) + } + } + return repo, nil +} diff --git a/pkg/scorecard.go b/pkg/scorecard.go index 5b949622824..7506817926b 100644 --- a/pkg/scorecard.go +++ b/pkg/scorecard.go @@ -268,33 +268,6 @@ func RunScorecard(ctx context.Context, ) } -// ExperimentalRunProbes is experimental. Do not depend on it, it may be removed at any point. -func ExperimentalRunProbes(ctx context.Context, - repo clients.Repo, - commitSHA string, - commitDepth int, - checksToRun checker.CheckNameToFnMap, - probesToRun []string, - repoClient clients.RepoClient, - ossFuzzRepoClient clients.RepoClient, - ciiClient clients.CIIBestPracticesClient, - vulnsClient clients.VulnerabilitiesClient, - projectClient packageclient.ProjectPackageClient, -) (ScorecardResult, error) { - return runScorecard(ctx, - repo, - commitSHA, - commitDepth, - checksToRun, - probesToRun, - repoClient, - ossFuzzRepoClient, - ciiClient, - vulnsClient, - projectClient, - ) -} - type runConfig struct { client clients.RepoClient vulnClient clients.VulnerabilitiesClient diff --git a/pkg/scorecard_test.go b/pkg/scorecard_test.go index c597656dadc..acf487ba75d 100644 --- a/pkg/scorecard_test.go +++ b/pkg/scorecard_test.go @@ -204,7 +204,7 @@ func TestRun(t *testing.T) { } } -func TestExperimentalRunProbes(t *testing.T) { +func TestRun_WithProbes(t *testing.T) { t.Parallel() // These values depend on the environment, // so don't encode particular expectations @@ -285,7 +285,7 @@ func TestExperimentalRunProbes(t *testing.T) { repo.EXPECT().Host().Return("github.com").AnyTimes() mockRepoClient.EXPECT().InitRepo(repo, tt.args.commitSHA, 0).Return(nil) - + mockRepoClient.EXPECT().URI().Return(repo.URI()).AnyTimes() mockRepoClient.EXPECT().Close().DoAndReturn(func() error { return nil }) @@ -322,17 +322,13 @@ func TestExperimentalRunProbes(t *testing.T) { mockRepoClient.EXPECT().ListProgrammingLanguages().Return(progLanguages, nil).AnyTimes() mockRepoClient.EXPECT().GetDefaultBranchName().Return("main", nil).AnyTimes() - got, err := ExperimentalRunProbes(context.Background(), - repo, - tt.args.commitSHA, - 0, - nil, - tt.args.probes, - mockRepoClient, - nil, - nil, - nil, - nil, + mockOSSFuzzClient := mockrepo.NewMockRepoClient(ctrl) + mockOSSFuzzClient.EXPECT().Search(gomock.Any()).Return(clients.SearchResponse{}, nil).AnyTimes() + got, err := Run(context.Background(), repo, + WithRepoClient(mockRepoClient), + WithOSSFuzzClient(mockOSSFuzzClient), + WithCommitSHA(tt.args.commitSHA), + WithProbes(tt.args.probes), ) if (err != nil) != tt.wantErr { t.Errorf("RunScorecard() error = %v, wantErr %v", err, tt.wantErr)