Skip to content

Commit

Permalink
options: Prefer SCORECARD_ env vars over INPUT_
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <[email protected]>
  • Loading branch information
justaugustus committed Mar 16, 2022
1 parent a05fa17 commit 33d5f13
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
11 changes: 7 additions & 4 deletions options/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ const (
EnvGithubRef = "GITHUB_REF"
EnvGithubWorkspace = "GITHUB_WORKSPACE"
EnvGithubAuthToken = "GITHUB_AUTH_TOKEN" //nolint:gosec
EnvInputRepoToken = "INPUT_REPO_TOKEN" //nolint:gosec
EnvInputResultsFile = "INPUT_RESULTS_FILE"
EnvInputResultsFormat = "INPUT_RESULTS_FORMAT"
EnvInputPublishResults = "INPUT_PUBLISH_RESULTS"
EnvScorecardFork = "SCORECARD_IS_FORK"
EnvScorecardPrivateRepo = "SCORECARD_PRIVATE_REPOSITORY"

// TODO(input): INPUT_ constants should be removed in a future release once
// they have replacements in upstream scorecard.
EnvInputRepoToken = "INPUT_REPO_TOKEN" //nolint:gosec
EnvInputResultsFile = "INPUT_RESULTS_FILE"
EnvInputResultsFormat = "INPUT_RESULTS_FORMAT"
EnvInputPublishResults = "INPUT_PUBLISH_RESULTS"
)

// Errors
Expand Down
13 changes: 7 additions & 6 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func New() (*Options, error) {
}

// TODO(options): Move this set-or-default logic to its own function.
opts.ScorecardOpts.Format = opts.EnvInputResultsFormat
if opts.ScorecardOpts.Format == "" {
opts.ScorecardOpts.Format = formatSarif
opts.ScorecardOpts.Format = formatSarif
opts.ScorecardOpts.EnableSarif = true
if opts.InputResultsFormat != "" {
opts.ScorecardOpts.Format = opts.InputResultsFormat
}

opts.ScorecardOpts.EnableSarif = true
if opts.ScorecardOpts.Format == formatSarif {
if opts.ScorecardOpts.PolicyFile == "" {
// TODO(policy): Should we default or error here?
Expand All @@ -123,11 +123,12 @@ func New() (*Options, error) {

opts.SetPublishResults()

if opts.EnvInputResultsFile != "" {
opts.ScorecardOpts.ResultsFile = opts.EnvInputResultsFile
if opts.ScorecardOpts.ResultsFile == "" {
opts.ScorecardOpts.ResultsFile = opts.InputResultsFile
}

if opts.ScorecardOpts.ResultsFile == "" {
// TODO(test): Reassess test case for this code path
return opts, errResultsPathEmpty
}

Expand Down
56 changes: 42 additions & 14 deletions options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ func TestNew(t *testing.T) {
LogLevel string
}
tests := []struct {
name string
githubEventPath string
repo string
resultsFile string
resultsFormat string
publishResults string
want fields
unsetToken bool
wantErr bool
name string
githubEventPath string
repo string
resultsFile string
resultsFormat string
publishResults string
want fields
unsetResultsPath bool
unsetToken bool
wantErr bool
}{
{
name: "SuccessFormatSARIF",
Expand Down Expand Up @@ -103,6 +104,33 @@ func TestNew(t *testing.T) {
unsetToken: true,
wantErr: true,
},
{
name: "FailureResultsPathNotSet",
githubEventPath: githubEventPathNonFork,
want: fields{
EnableSarif: true,
Format: formatSarif,
PolicyFile: defaultScorecardPolicyFile,
Commit: options.DefaultCommit,
LogLevel: options.DefaultLogLevel,
},
unsetResultsPath: true,
wantErr: true,
},
{
name: "FailureResultsPathEmpty",
githubEventPath: githubEventPathNonFork,
resultsFile: "",
want: fields{
EnableSarif: true,
Format: formatSarif,
PolicyFile: defaultScorecardPolicyFile,
ResultsFile: "",
Commit: options.DefaultCommit,
LogLevel: options.DefaultLogLevel,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -131,12 +159,12 @@ func TestNew(t *testing.T) {
}
}

if tt.repo != "" {
os.Setenv(EnvInputResultsFormat, tt.resultsFormat)
defer os.Unsetenv(EnvInputResultsFormat)
}
os.Setenv(EnvInputResultsFormat, tt.resultsFormat)
defer os.Unsetenv(EnvInputResultsFormat)

if tt.repo != "" {
if tt.unsetResultsPath {
os.Unsetenv(EnvInputResultsFile)
} else {
os.Setenv(EnvInputResultsFile, tt.resultsFile)
defer os.Unsetenv(EnvInputResultsFile)
}
Expand Down

0 comments on commit 33d5f13

Please sign in to comment.