Skip to content

Commit

Permalink
entrypoint: Init option fields
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <foo@auggie.dev>
  • Loading branch information
justaugustus committed Feb 27, 2022
1 parent 37271a9 commit 137045c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 46 deletions.
68 changes: 41 additions & 27 deletions entrypoint/entrypoint.go
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -336,57 +349,57 @@ 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
}
result.Args = []string{
"--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
}
Expand All @@ -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
}
31 changes: 12 additions & 19 deletions entrypoint/entrypoint_test.go
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -686,7 +680,7 @@ func Test_runScorecardSettings(t *testing.T) {
},
{
name: "Want error - Branch protection rule",
args: args{
args: &Options{
githubEventName: "",
scorecardResultsFormat: "",
scorecardBin: "",
Expand All @@ -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
Expand Down

0 comments on commit 137045c

Please sign in to comment.