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 Scorecard entrypoint for CLI #4203

Merged
merged 5 commits into from
Jul 8, 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
64 changes: 43 additions & 21 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"
"sort"
Expand All @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
27 changes: 0 additions & 27 deletions pkg/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 9 additions & 13 deletions pkg/scorecard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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)
Expand Down
Loading