Skip to content

Commit

Permalink
Default Branch Checking Bugfix (#171)
Browse files Browse the repository at this point in the history
* test action

* fixed Dockerfile

* / before policy filepath

* default branch checking + log

* revert logging

* remove lookupenv

* Dockerfile use golang entrypoint

* fixed test githubRef env

* revert dockerfile

* revert dockerfile
  • Loading branch information
rohankh532 committed Apr 21, 2022
1 parent c8f6027 commit 559d544
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ COPY policies/template.yml /policy.yml
# Note: the file is executable in the repo
# and permission carry over to the image.
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
ENTRYPOINT ["/entrypoint.sh"]
2 changes: 1 addition & 1 deletion options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (o *Options) Validate() error {
}

if strings.Contains(o.GithubEventName, "pull_request") &&
o.GithubRef == o.DefaultBranch {
o.GithubRef != o.DefaultBranch {
fmt.Printf("%s not supported with %s event.\n", o.GithubRef, o.GithubEventName)
fmt.Printf("Only the default branch %s is supported.\n", o.DefaultBranch)

Expand Down
63 changes: 44 additions & 19 deletions options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func TestNew(t *testing.T) {
tests := []struct {
name string
githubEventPath string
githubEventName string
githubRef string
repo string
resultsFile string
resultsFormat string
Expand All @@ -59,6 +61,8 @@ func TestNew(t *testing.T) {
{
name: "SuccessFormatSARIF",
githubEventPath: githubEventPathNonFork,
githubEventName: "pull_request",
githubRef: "main",
repo: testRepo,
resultsFormat: "sarif",
resultsFile: testResultsFile,
Expand All @@ -75,6 +79,8 @@ func TestNew(t *testing.T) {
{
name: "SuccessFormatJSON",
githubEventPath: githubEventPathNonFork,
githubEventName: "pull_request",
githubRef: "main",
repo: testRepo,
resultsFormat: "json",
resultsFile: testResultsFile,
Expand All @@ -90,6 +96,8 @@ func TestNew(t *testing.T) {
{
name: "FailureTokenIsNotSet",
githubEventPath: githubEventPathNonFork,
githubEventName: "pull_request",
githubRef: "main",
repo: testRepo,
resultsFormat: "sarif",
resultsFile: testResultsFile,
Expand All @@ -107,6 +115,8 @@ func TestNew(t *testing.T) {
{
name: "FailureResultsPathNotSet",
githubEventPath: githubEventPathNonFork,
githubEventName: "pull_request",
githubRef: "main",
want: fields{
EnableSarif: true,
Format: formatSarif,
Expand All @@ -120,6 +130,8 @@ func TestNew(t *testing.T) {
{
name: "FailureResultsPathEmpty",
githubEventPath: githubEventPathNonFork,
githubEventName: "pull_request",
githubRef: "main",
resultsFile: "",
want: fields{
EnableSarif: true,
Expand All @@ -131,33 +143,46 @@ func TestNew(t *testing.T) {
},
wantErr: true,
},
{
name: "FailureBranchIsntMain",
githubEventPath: githubEventPathNonFork,
githubEventName: "pull_request",
githubRef: "other-branch",
repo: testRepo,
resultsFormat: "sarif",
resultsFile: testResultsFile,
want: fields{
EnableSarif: true,
Format: formatSarif,
PolicyFile: defaultScorecardPolicyFile,
ResultsFile: testResultsFile,
Commit: options.DefaultCommit,
LogLevel: options.DefaultLogLevel,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, tokenEnvExists := os.LookupEnv(EnvGithubAuthToken)
if !tokenEnvExists {
os.Setenv(EnvGithubAuthToken, testToken)
defer os.Unsetenv(EnvGithubAuthToken)
}

os.Setenv(EnvGithubAuthToken, testToken)
defer os.Unsetenv(EnvGithubAuthToken)

if tt.unsetToken {
os.Unsetenv(EnvGithubAuthToken)
}

_, pathEnvExists := os.LookupEnv(EnvGithubEventPath)
if !pathEnvExists {
if tt.githubEventPath != "" {
os.Setenv(EnvGithubEventPath, tt.githubEventPath)
defer os.Unsetenv(EnvGithubEventPath)
}
}
os.Setenv(EnvGithubEventPath, tt.githubEventPath)
defer os.Unsetenv(EnvGithubEventPath)

_, repoEnvExists := os.LookupEnv(EnvGithubRepository)
if !repoEnvExists {
if tt.repo != "" {
os.Setenv(EnvGithubRepository, tt.repo)
defer os.Unsetenv(EnvGithubRepository)
}
}
os.Setenv(EnvGithubEventName, tt.githubEventName)
defer os.Unsetenv(EnvGithubEventName)

os.Setenv(EnvGithubRef, tt.githubRef)
defer os.Unsetenv(EnvGithubRef)

os.Setenv(EnvGithubRepository, tt.repo)
defer os.Unsetenv(EnvGithubRepository)

os.Setenv(EnvInputResultsFormat, tt.resultsFormat)
defer os.Unsetenv(EnvInputResultsFormat)
Expand Down

0 comments on commit 559d544

Please sign in to comment.