From 14eb9b89c995848085c21cb2cb78c009d91fba99 Mon Sep 17 00:00:00 2001 From: Stephen Augustus Date: Thu, 26 May 2022 01:14:51 -0400 Subject: [PATCH] options: Restore logic for publishing results Signed-off-by: Stephen Augustus --- entrypoint/entrypoint.go | 7 +++++++ options/options.go | 12 +++++------- options/options_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/entrypoint/entrypoint.go b/entrypoint/entrypoint.go index a0fe9604..a4477bdc 100755 --- a/entrypoint/entrypoint.go +++ b/entrypoint/entrypoint.go @@ -49,6 +49,13 @@ func New() (*cobra.Command, error) { "path to output results to", ) + actionCmd.Flags().BoolVar( + &opts.PublishResults, + "publish", + opts.PublishResults, + "if set, results will be published (for public repositories only)", + ) + // Adapt scorecard's PreRunE to support an output file // TODO(scorecard): Move this into scorecard var out, stdout *os.File diff --git a/options/options.go b/options/options.go index 25f8b9a0..e1517c2a 100644 --- a/options/options.go +++ b/options/options.go @@ -64,11 +64,11 @@ type Options struct { IsForkStr string `env:"SCORECARD_IS_FORK"` // TODO(options): This may be better as a bool PrivateRepoStr string `env:"SCORECARD_PRIVATE_REPOSITORY"` + PublishResults bool // Input parameters - InputResultsFile string `env:"INPUT_RESULTS_FILE"` - InputResultsFormat string `env:"INPUT_RESULTS_FORMAT"` - InputPublishResults string `env:"INPUT_PUBLISH_RESULTS"` + InputResultsFile string `env:"INPUT_RESULTS_FILE"` + InputResultsFormat string `env:"INPUT_RESULTS_FORMAT"` } const ( @@ -192,7 +192,7 @@ func (o *Options) Print() { fmt.Printf("Repository: %s\n", o.ScorecardOpts.Repo) fmt.Printf("Fork repository: %s\n", o.IsForkStr) fmt.Printf("Private repository: %s\n", o.PrivateRepoStr) - fmt.Printf("Publication enabled: %+v\n", o.ScorecardOpts.PublishResults) + fmt.Printf("Publication enabled: %+v\n", o.PublishResults) fmt.Printf("Format: %s\n", o.ScorecardOpts.Format) fmt.Printf("Policy file: %s\n", o.ScorecardOpts.PolicyFile) fmt.Printf("Default branch: %s\n", o.DefaultBranch) @@ -211,9 +211,7 @@ func (o *Options) SetPublishResults() { ) } - if privateRepo { - o.ScorecardOpts.PublishResults = false - } + o.PublishResults = o.PublishResults && !privateRepo } // SetRepoInfo gets the path to the GitHub event and sets the diff --git a/options/options_test.go b/options/options_test.go index fa0baec4..6b3067f0 100644 --- a/options/options_test.go +++ b/options/options_test.go @@ -334,3 +334,43 @@ func TestPrint(t *testing.T) { }) } } + +func TestSetPublishResults(t *testing.T) { + tests := []struct { + name string + privateRepo string + userInput bool + want bool + }{ + { + name: "DefaultNoInput", + want: false, + }, + { + name: "InputTruePrivateRepo", + privateRepo: "true", + userInput: true, + want: false, + }, + { + name: "InvalidValueForPrivateRepo", + privateRepo: "invalid-value", + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + opts := &Options{ + ScorecardOpts: options.New(), + } + opts.PrivateRepoStr = tt.privateRepo + + opts.SetPublishResults() + got := opts.PublishResults + + if !cmp.Equal(tt.want, got) { + t.Errorf("New(): -want, +got:\n%s", cmp.Diff(tt.want, got)) + } + }) + } +}