From 137045c7f239b45d52b07f23ebb8408e406682ab Mon Sep 17 00:00:00 2001 From: Stephen Augustus Date: Sun, 27 Feb 2022 13:26:54 -0500 Subject: [PATCH] entrypoint: Init option fields Signed-off-by: Stephen Augustus --- entrypoint/entrypoint.go | 68 +++++++++++++++++++++-------------- entrypoint/entrypoint_test.go | 31 +++++++--------- 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/entrypoint/entrypoint.go b/entrypoint/entrypoint.go index 30928eec..f766e23c 100644 --- a/entrypoint/entrypoint.go +++ b/entrypoint/entrypoint.go @@ -26,7 +26,14 @@ import ( "strings" ) -type Options struct{} +type Options struct { + githubEventName string + scorecardPolicyFile string + scorecardResultsFormat string + scorecardBin string + scorecardResultsFile string + githubRepository string +} var ( errInputResultFileNotSet = errors.New("INPUT_RESULTS_FILE is not set") @@ -43,12 +50,14 @@ var ( errEmptyGitHubAuthToken = errors.New("repo_token variable is empty") errOnlyDefaultBranchSupported = errors.New("only default branch is supported") errEmptyScorecardBin = errors.New("scorecard_bin variable is empty") - enabledChecks = "" - scorecardPrivateRepository = "" - scorecardDefaultBranch = "" - scorecardPublishResults = "" - scorecardResultsFormat = "" - scorecardResultsFile = "" + + // TODO: Consider passing these as options instead + enabledChecks = "" + scorecardPrivateRepository = "" + scorecardDefaultBranch = "" + scorecardPublishResults = "" + scorecardResultsFormat = "" + scorecardResultsFile = "" ) type repositoryInformation struct { @@ -110,9 +119,13 @@ func Run(o *Options) error { } // gets the cmd run settings - cmd, err := runScorecardSettings(os.Getenv(githubEventName), - scorecardPolicyFile, scorecardResultsFormat, - scorecardBin, scorecardResultsFile, os.Getenv(githubRepository)) + // TODO: Remove this once the getenv logic exists + /* + cmd, err := runScorecardSettings(os.Getenv(githubEventName), + scorecardPolicyFile, scorecardResultsFormat, + scorecardBin, scorecardResultsFile, os.Getenv(githubRepository)) + */ + cmd, err := runScorecardSettings(o) if err != nil { return err } @@ -336,25 +349,25 @@ func validate(writer io.Writer) error { return nil } -func runScorecardSettings(githubEventName, scorecardPolicyFile, scorecardResultsFormat, scorecardBin, - scorecardResultsFile, githubRepository string) (*exec.Cmd, error) { - if scorecardBin == "" { +// TODO(entrypoint): Pass as options +func runScorecardSettings(o *Options) (*exec.Cmd, error) { + if o.scorecardBin == "" { return nil, errEmptyScorecardBin } var result exec.Cmd - result.Path = scorecardBin + result.Path = o.scorecardBin // if pull_request - if strings.Contains(githubEventName, "pull_request") { + if strings.Contains(o.githubEventName, "pull_request") { // empty policy file - if scorecardPolicyFile == "" { + if o.scorecardPolicyFile == "" { result.Args = []string{ "--local", ".", "--format", - scorecardResultsFormat, + o.scorecardResultsFormat, "--show-details", ">", - scorecardResultsFile, + o.scorecardResultsFile, } return &result, nil } @@ -362,31 +375,31 @@ func runScorecardSettings(githubEventName, scorecardPolicyFile, scorecardResults "--local", ".", "--format", - scorecardResultsFormat, + o.scorecardResultsFormat, "--policy", - scorecardPolicyFile, + o.scorecardPolicyFile, "--show-details", ">", - scorecardResultsFile, + o.scorecardResultsFile, } return &result, nil } enabledChecks = "" - if githubEventName == "branch_protection_rule" { + if o.githubEventName == "branch_protection_rule" { enabledChecks = "--checks Branch-Protection" } - if scorecardPolicyFile == "" { + if o.scorecardPolicyFile == "" { result.Args = []string{ "--repo", githubRepository, "--format", enabledChecks, - scorecardResultsFormat, + o.scorecardResultsFormat, "--show-details", ">", - scorecardResultsFile, + o.scorecardResultsFile, } return &result, nil } @@ -395,12 +408,13 @@ func runScorecardSettings(githubEventName, scorecardPolicyFile, scorecardResults githubRepository, "--format", enabledChecks, - scorecardResultsFormat, + o.scorecardResultsFormat, "--policy", scorecardPolicyFile, "--show-details", ">", - scorecardResultsFile, + o.scorecardResultsFile, } + return &result, nil } diff --git a/entrypoint/entrypoint_test.go b/entrypoint/entrypoint_test.go index 4f6728a3..00a5a8c4 100644 --- a/entrypoint/entrypoint_test.go +++ b/entrypoint/entrypoint_test.go @@ -482,14 +482,8 @@ func Test_validate(t *testing.T) { func Test_runScorecardSettings(t *testing.T) { t.Parallel() - type args struct { - githubEventName string - scorecardPolicyFile string - scorecardResultsFormat string - scorecardBin string - scorecardResultsFile string - githubRepository string - } + type args *Options + //nolint tests := []struct { wantErr bool @@ -499,7 +493,7 @@ func Test_runScorecardSettings(t *testing.T) { }{ { name: "Success - scorecardFork set", - args: args{ + args: &Options{ githubEventName: "pull_request", scorecardPolicyFile: "./testdata/scorecard.yaml", scorecardResultsFormat: "json", @@ -524,7 +518,7 @@ func Test_runScorecardSettings(t *testing.T) { }, { name: "Success - scorecardFork set", - args: args{ + args: &Options{ githubEventName: "pull_request", scorecardPolicyFile: "./testdata/scorecard.yaml", scorecardResultsFormat: "json", @@ -549,7 +543,7 @@ func Test_runScorecardSettings(t *testing.T) { }, { name: "Success - scorecardFork set", - args: args{ + args: &Options{ githubEventName: "pull_request", scorecardPolicyFile: "./testdata/scorecard.yaml", scorecardResultsFormat: "json", @@ -574,7 +568,7 @@ func Test_runScorecardSettings(t *testing.T) { }, { name: "Success - scorecardFork set", - args: args{ + args: &Options{ githubEventName: "pull_request", scorecardResultsFormat: "json", scorecardBin: "scorecard", @@ -596,7 +590,7 @@ func Test_runScorecardSettings(t *testing.T) { }, { name: "Success - scorecardFork set", - args: args{ + args: &Options{ githubEventName: "pull_request", scorecardResultsFormat: "json", scorecardBin: "scorecard", @@ -618,7 +612,7 @@ func Test_runScorecardSettings(t *testing.T) { }, { name: "Success - scorecardFork set", - args: args{ + args: &Options{ scorecardResultsFormat: "json", scorecardBin: "scorecard", scorecardResultsFile: "./testdata/scorecard.json", @@ -639,7 +633,7 @@ func Test_runScorecardSettings(t *testing.T) { }, { name: "Success - Branch protection rule", - args: args{ + args: &Options{ githubEventName: "branch_protection_rule", scorecardResultsFormat: "json", scorecardBin: "scorecard", @@ -661,7 +655,7 @@ func Test_runScorecardSettings(t *testing.T) { }, { name: "Success - Branch protection rule", - args: args{ + args: &Options{ scorecardPolicyFile: "./testdata/scorecard.yaml", githubEventName: "branch_protection_rule", scorecardResultsFormat: "json", @@ -686,7 +680,7 @@ func Test_runScorecardSettings(t *testing.T) { }, { name: "Want error - Branch protection rule", - args: args{ + args: &Options{ githubEventName: "", scorecardResultsFormat: "", scorecardBin: "", @@ -701,8 +695,7 @@ func Test_runScorecardSettings(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() - got, err := runScorecardSettings(tt.args.githubEventName, tt.args.scorecardPolicyFile, - tt.args.scorecardResultsFormat, tt.args.scorecardBin, tt.args.scorecardResultsFile, tt.args.githubRepository) + got, err := runScorecardSettings(tt.args) if (err != nil) != tt.wantErr { t.Errorf("runScorecardSettings() error = %v, wantErr %v", err, tt.wantErr) return