Skip to content

Commit

Permalink
change flag to file-mode
Browse files Browse the repository at this point in the history
Signed-off-by: Spencer Schrock <[email protected]>
  • Loading branch information
spencerschrock committed Jan 8, 2025
1 parent 0db4245 commit 08a0b7a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 30 deletions.
6 changes: 3 additions & 3 deletions clients/githubrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ type Client struct {
gitMode bool
}

// WithGitMode configures the repo client to fetch files using git.
func WithGitMode() Option {
// WithFileModeGit configures the repo client to fetch files using git.
func WithFileModeGit() Option {
return func(c *repoClientConfig) error {
c.gitMode = true
return nil
Expand Down Expand Up @@ -263,7 +263,7 @@ func (client *Client) GetOrgRepoClient(ctx context.Context) (clients.RepoClient,

options := []Option{WithRoundTripper(client.repoClient.Client().Transport)}
if client.gitMode {
options = append(options, WithGitMode())
options = append(options, WithFileModeGit())
}
c, err := NewRepoClient(ctx, options...)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ func rootCmd(o *options.Options) error {
scorecard.WithProbes(enabledProbes),
scorecard.WithChecks(checks),
}
if o.GitMode {
opts = append(opts, scorecard.WithGitMode())
if strings.EqualFold(o.FileMode, options.FileModeGit) {
opts = append(opts, scorecard.WithFileModeGit())
}

Check warning on line 159 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L156-L159

Added lines #L156 - L159 were not covered by tests

repoResult, err = scorecard.Run(ctx, repo, opts...)

Check warning on line 161 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L161

Added line #L161 was not covered by tests
Expand Down
15 changes: 8 additions & 7 deletions options/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const (
// FlagShowDetails is the flag name for outputting additional check info.
FlagShowDetails = "show-details"

// Flag FlagGitMode is the flag name for enabling git compatibility mode.
FlagGitMode = "git-mode"
// Flag FlagFileMode is the flag name for specifying how files are fetched for a repository.
FlagFileMode = "file-mode"

// FlagShowAnnotations is the flag name for outputting annotations on checks.
FlagShowAnnotations = "show-annotations"
Expand Down Expand Up @@ -226,10 +226,11 @@ func (o *Options) AddFlags(cmd *cobra.Command) {
"output file",
)

cmd.Flags().BoolVar(
&o.GitMode,
FlagGitMode,
o.GitMode,
"fetch repository files using git for maximum compatibility",
allowedModes := []string{FileModeArchive, FileModeGit}
cmd.Flags().StringVar(
&o.FileMode,
FlagFileMode,
o.FileMode,
fmt.Sprintf("mode to fetch repository files: %s", strings.Join(allowedModes, ", ")),
)
}
43 changes: 30 additions & 13 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ type Options struct {
Nuget string
PolicyFile string
ResultsFile string
FileMode string
ChecksToRun []string
ProbesToRun []string
Metadata []string
CommitDepth int
ShowDetails bool
ShowAnnotations bool
GitMode bool
// Feature flags.
EnableSarif bool `env:"ENABLE_SARIF"`
EnableScorecardV6 bool `env:"SCORECARD_V6"`
Expand All @@ -56,21 +56,15 @@ type Options struct {

// New creates a new instance of `Options`.
func New() *Options {
opts := &Options{}
opts := &Options{
Commit: DefaultCommit,
Format: FormatDefault,
LogLevel: DefaultLogLevel,
FileMode: FileModeArchive,
}

Check warning on line 64 in options/options.go

View check run for this annotation

Codecov / codecov/patch

options/options.go#L59-L64

Added lines #L59 - L64 were not covered by tests
if err := env.Parse(opts); err != nil {
log.Printf("could not parse env vars, using default options: %v", err)
}
// Defaulting.
// TODO(options): Consider moving this to a separate function/method.
if opts.Commit == "" {
opts.Commit = DefaultCommit
}
if opts.Format == "" {
opts.Format = FormatDefault
}
if opts.LogLevel == "" {
opts.LogLevel = DefaultLogLevel
}
return opts
}

Expand All @@ -90,6 +84,12 @@ const (
// FormatRaw specifies that results should be output in raw format.
FormatRaw = "raw"

// File Modes
// FileModeGit specifies that files should be fetched using git.
FileModeGit = "git"
// FileModeArchive specifies that files should be fetched using the export archive (tarball).
FileModeArchive = "archive"

// Environment variables.
// EnvVarEnableSarif is the environment variable which controls enabling
// SARIF logging.
Expand All @@ -108,6 +108,7 @@ var (

errCommitIsEmpty = errors.New("commit should be non-empty")
errFormatNotSupported = errors.New("unsupported format")
errFileModeNotSupported = errors.New("unsupported file mode")
errPolicyFileNotSupported = errors.New("policy file is not supported yet")
errRawOptionNotSupported = errors.New("raw option is not supported yet")
errRepoOptionMustBeSet = errors.New(
Expand Down Expand Up @@ -177,6 +178,13 @@ func (o *Options) Validate() error {
)
}

if !validateFileMode(o.FileMode) {
errs = append(
errs,
errFileModeNotSupported,
)
}

if len(errs) != 0 {
return fmt.Errorf(
"%w: %+v",
Expand Down Expand Up @@ -253,3 +261,12 @@ func validateFormat(format string) bool {
return false
}
}

func validateFileMode(mode string) bool {
switch strings.ToLower(mode) {
case FileModeGit, FileModeArchive:
return true
default:
return false
}
}
25 changes: 25 additions & 0 deletions options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestOptions_Validate(t *testing.T) {
Nuget string
PolicyFile string
ResultsFile string
FileMode string
ChecksToRun []string
Metadata []string
ShowDetails bool
Expand Down Expand Up @@ -84,16 +85,40 @@ func TestOptions_Validate(t *testing.T) {
},
wantErr: true,
},
{
name: "invalid filemode flagged",
fields: fields{
Repo: "github.com/oss/scorecard",
Commit: "HEAD",
Format: "default",
FileMode: "unsupported mode",
},
wantErr: true,
},
{
name: "git filemode is valid",
fields: fields{
Repo: "github.com/oss/scorecard",
Commit: "HEAD",
Format: "default",
FileMode: FileModeGit,
},
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
if tt.fields.FileMode == "" {
tt.fields.FileMode = FileModeArchive
}
t.Run(tt.name, func(t *testing.T) {
o := &Options{
Repo: tt.fields.Repo,
Local: tt.fields.Local,
Commit: tt.fields.Commit,
LogLevel: tt.fields.LogLevel,
Format: tt.fields.Format,
FileMode: tt.fields.FileMode,
NPM: tt.fields.NPM,
PyPI: tt.fields.PyPI,
RubyGems: tt.fields.RubyGems,
Expand Down
10 changes: 5 additions & 5 deletions pkg/scorecard/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,12 @@ func WithOpenSSFBestPraticesClient(client clients.CIIBestPracticesClient) Option
}
}

// WithGitMode will configure supporting repository clients to download files
// using git, instead of the archive tarball. This is useful for repositories
// which "export-ignore" files in a .gitattributes file.
// WithFileModeGit will configure supporting repository clients to download files
// using git. This is useful for repositories which "export-ignore" files in its
// .gitattributes file.
//
// Repository analysis may be slower.
func WithGitMode() Option {
func WithFileModeGit() Option {
return func(c *runConfig) error {
c.gitMode = true
return nil
Expand Down Expand Up @@ -392,7 +392,7 @@ func Run(ctx context.Context, repo clients.Repo, opts ...Option) (Result, error)
if c.client == nil {
var opts []githubrepo.Option
if c.gitMode {
opts = append(opts, githubrepo.WithGitMode())
opts = append(opts, githubrepo.WithFileModeGit())
}

Check warning on line 396 in pkg/scorecard/scorecard.go

View check run for this annotation

Codecov / codecov/patch

pkg/scorecard/scorecard.go#L395-L396

Added lines #L395 - L396 were not covered by tests
client, err := githubrepo.NewRepoClient(ctx, opts...)
if err != nil {
Expand Down

0 comments on commit 08a0b7a

Please sign in to comment.