Skip to content

Commit 5b496ad

Browse files
🌱 More unit tests for porting to Go
1 parent 704ccbe commit 5b496ad

File tree

4 files changed

+276
-56
lines changed

4 files changed

+276
-56
lines changed

.github/workflows/tests.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ jobs:
3636
with:
3737
go-version: '1.17.x'
3838
- name: Run Go tests
39-
run: go test -race ./...
39+
# cannot run tests with race because we are mutating state (setting ENV variables)
40+
run: go test ./...

main

-6.31 MB
Binary file not shown.

main.go

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ type repositoryInformation struct {
4242
Private bool `json:"private"`
4343
}
4444

45+
const (
46+
enableSarif = "ENABLE_SARIF"
47+
enableLicense = "ENABLE_LICENSE"
48+
enableDangerousWorkflow = "ENABLE_DANGEROUS_WORKFLOW"
49+
enabledChecks = "ENABLED_CHECKS"
50+
githubEventPath = "GITHUB_EVENT_PATH"
51+
githubRepository = "GITHUB_REPOSITORY"
52+
//nolint:gosec
53+
githubAuthToken = "GITHUB_AUTH_TOKEN"
54+
inputresultsfile = "INPUT_RESULTS_FILE"
55+
inputresultsformat = "INPUT_RESULTS_FORMAT"
56+
inputpublishresults = "INPUT_PUBLISH_RESULTS"
57+
scorecardBin = "SCORECARD_BIN"
58+
scorecardResultsFormat = "SCORECARD_RESULTS_FORMAT"
59+
scorecardPublishResults = "SCORECARD_PUBLISH_RESULTS"
60+
scorecardPolicyFile = "SCORECARD_POLICY_FILE"
61+
scorecardResultsFile = "SCORECARD_RESULTS_FILE"
62+
scorecardFork = "SCORECARD_IS_FORK"
63+
scorecardDefaultBranch = "SCORECARD_DEFAULT_BRANCH"
64+
scorecardPrivateRepository = "SCORECARD_PRIVATE_REPOSITORY"
65+
sarif = "sarif"
66+
)
67+
4568
// main is the entrypoint for the action.
4669
func main() {
4770
// TODO - This is a port of the entrypoint.sh script.
@@ -53,8 +76,8 @@ func main() {
5376
panic(err)
5477
}
5578

56-
repository := os.Getenv("GITHUB_REPOSITORY")
57-
token := os.Getenv("GITHUB_AUTH_TOKEN")
79+
repository := os.Getenv(githubRepository)
80+
token := os.Getenv(githubAuthToken)
5881

5982
repo, err := getRepositoryInformation(repository, token)
6083
if err != nil {
@@ -83,49 +106,49 @@ func initalizeENVVariables() error {
83106
*/
84107

85108
envvars := make(map[string]string)
86-
envvars["ENABLE_SARIF"] = "1"
87-
envvars["ENABLE_LICENSE"] = "1"
88-
envvars["ENABLE_DANGEROUS_WORKFLOW"] = "1"
89-
envvars["SCORECARD_POLICY_FILE"] = "./policy.yml"
90-
envvars["SCORECARD_BIN"] = "/scorecard"
91-
envvars["ENABLED_CHECKS"] = ""
109+
envvars[enableSarif] = "1"
110+
envvars[enableLicense] = "1"
111+
envvars[enableDangerousWorkflow] = "1"
112+
envvars[scorecardPolicyFile] = "./policy.yml"
113+
envvars[scorecardBin] = "/scorecard"
114+
envvars[enabledChecks] = ""
92115

93116
for key, val := range envvars {
94117
if err := os.Setenv(key, val); err != nil {
95118
return fmt.Errorf("error setting %s: %w", key, err)
96119
}
97120
}
98121

99-
if result, exists := os.LookupEnv("INPUT_RESULTS_FILE"); !exists {
122+
if result, exists := os.LookupEnv(inputresultsfile); !exists {
100123
return errInputResultFileNotSet
101124
} else {
102125
if result == "" {
103126
return errInputResultFileEmpty
104127
}
105-
if err := os.Setenv("SCORECARD_RESULTS_FILE", result); err != nil {
106-
return fmt.Errorf("error setting SCORECARD_RESULTS_FILE: %w", err)
128+
if err := os.Setenv(scorecardResultsFile, result); err != nil {
129+
return fmt.Errorf("error setting %s: %w", scorecardResultsFile, err)
107130
}
108131
}
109132

110-
if result, exists := os.LookupEnv("INPUT_RESULTS_FORMAT"); !exists {
133+
if result, exists := os.LookupEnv(inputresultsformat); !exists {
111134
return errInputResultFormatNotSet
112135
} else {
113136
if result == "" {
114137
return errInputResultFormatEmtpy
115138
}
116-
if err := os.Setenv("SCORECARD_RESULTS_FORMAT", result); err != nil {
117-
return fmt.Errorf("error setting SCORECARD_RESULTS_FORMAT: %w", err)
139+
if err := os.Setenv(scorecardResultsFormat, result); err != nil {
140+
return fmt.Errorf("error setting %s: %w", scorecardResultsFormat, err)
118141
}
119142
}
120143

121-
if result, exists := os.LookupEnv("INPUT_PUBLISH_RESULTS"); !exists {
144+
if result, exists := os.LookupEnv(inputpublishresults); !exists {
122145
return errInputPublishResultsNotSet
123146
} else {
124147
if result == "" {
125148
return errInputPublishResultsEmpty
126149
}
127-
if err := os.Setenv("SCORECARD_PUBLISH_RESULTS", result); err != nil {
128-
return fmt.Errorf("error setting SCORECARD_PUBLISH_RESULTS: %w", err)
150+
if err := os.Setenv(scorecardPublishResults, result); err != nil {
151+
return fmt.Errorf("error setting %s: %w", scorecardPublishResults, err)
129152
}
130153
}
131154

@@ -138,7 +161,7 @@ func gitHubEventPath() error {
138161
var result string
139162
var exists bool
140163

141-
if result, exists = os.LookupEnv("GITHUB_EVENT_PATH"); !exists {
164+
if result, exists = os.LookupEnv(githubEventPath); !exists {
142165
return errGitHubEventPathNotSet
143166
}
144167

@@ -148,7 +171,7 @@ func gitHubEventPath() error {
148171

149172
data, err := ioutil.ReadFile(result)
150173
if err != nil {
151-
return fmt.Errorf("error reading GITHUB_EVENT_PATH: %w", err)
174+
return fmt.Errorf("error reading %s: %w", githubEventPath, err)
152175
}
153176
var isFork bool
154177

@@ -157,12 +180,12 @@ func gitHubEventPath() error {
157180
}
158181

159182
if isFork {
160-
if err := os.Setenv("SCORECARD_IS_FORK", "true"); err != nil {
161-
return fmt.Errorf("error setting SCORECARD_IS_FORK: %w", err)
183+
if err := os.Setenv(scorecardFork, "true"); err != nil {
184+
return fmt.Errorf("error setting %s: %w", scorecardFork, err)
162185
}
163186
} else {
164-
if err := os.Setenv("SCORECARD_IS_FORK", "false"); err != nil {
165-
return fmt.Errorf("error setting SCORECARD_IS_FORK: %w", err)
187+
if err := os.Setenv(scorecardFork, "false"); err != nil {
188+
return fmt.Errorf("error setting %s: %w", scorecardFork, err)
166189
}
167190
}
168191

@@ -194,8 +217,8 @@ func scorecardIsFork(ghEventPath string) (bool, error) {
194217
// checkIfRequiredENVSet is a function to check if the required environment variables are set.
195218
func checkIfRequiredENVSet() error {
196219
envVariables := make(map[string]bool)
197-
envVariables["GITHUB_REPOSITORY"] = true
198-
envVariables["GITHUB_AUTH_TOKEN"] = true
220+
envVariables[githubRepository] = true
221+
envVariables[githubAuthToken] = true
199222

200223
for key := range envVariables {
201224
if _, exists := os.LookupEnv(key); !exists {
@@ -239,25 +262,27 @@ func updateRepositoryInformation(privateRepo bool, defaultBranch string) error {
239262
return errEmptyDefaultBranch
240263
}
241264

242-
if err := os.Setenv("SCORECARD_PRIVATE_REPOSITORY", strconv.FormatBool(privateRepo)); err != nil {
243-
return fmt.Errorf("error setting SCORECARD_PRIVATE_REPOSITORY: %w", err)
265+
if err := os.Setenv(scorecardPrivateRepository, strconv.FormatBool(privateRepo)); err != nil {
266+
return fmt.Errorf("error setting %s: %w", scorecardPrivateRepository, err)
244267
}
245-
if err := os.Setenv("SCORECARD_DEFAULT_BRANCH", defaultBranch); err != nil {
246-
return fmt.Errorf("error setting SCORECARD_DEFAULT_BRANCH: %w", err)
268+
if err := os.Setenv(scorecardDefaultBranch, fmt.Sprintf("refs/heads/%s", defaultBranch)); err != nil {
269+
return fmt.Errorf("error setting %s: %w", scorecardDefaultBranch, err)
247270
}
248271
return nil
249272
}
250273

251274
// updateEnvVariables is a function to update the ENV variables based on results format and private repository.
252275
func updateEnvVariables() error {
253-
resultsFileFormat := os.Getenv("SCORECARD_RESULTS_FORMAT")
254-
if resultsFileFormat != "sarif" {
255-
os.Unsetenv("SCORECARD_POLICY_FILE")
276+
resultsFileFormat := os.Getenv(scorecardResultsFormat)
277+
if resultsFileFormat != sarif {
278+
if err := os.Unsetenv(scorecardPolicyFile); err != nil {
279+
return fmt.Errorf("error unsetting %s: %w", scorecardPolicyFile, err)
280+
}
256281
}
257-
isPrivateRepo := os.Getenv("SCORECARD_PRIVATE_REPOSITORY")
282+
isPrivateRepo := os.Getenv(scorecardPrivateRepository)
258283
if isPrivateRepo != "true" {
259-
if err := os.Setenv("SCORECARD_PUBLISH_RESULTS", "false"); err != nil {
260-
return fmt.Errorf("error setting SCORECARD_PUBLISH_RESULTS: %w", err)
284+
if err := os.Setenv(scorecardPublishResults, "false"); err != nil {
285+
return fmt.Errorf("error setting %s: %w", scorecardPublishResults, err)
261286
}
262287
}
263288
return nil

0 commit comments

Comments
 (0)