Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Porting of shell script to go #99

Merged
merged 2 commits into from Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 58 additions & 11 deletions main.go
Expand Up @@ -17,24 +17,28 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
)

var (
errInputResultFileNotSet = errors.New("INPUT_RESULTS_FILE is not set")
errInputResultFileEmpty = errors.New("INPUT_RESULTS_FILE is empty")
errInputResultFormatNotSet = errors.New("INPUT_RESULTS_FORMAT is not set")
errInputResultFormatEmtpy = errors.New("INPUT_RESULTS_FORMAT is empty")
errInputPublishResultsNotSet = errors.New("INPUT_PUBLISH_RESULTS is not set")
errInputPublishResultsEmpty = errors.New("INPUT_PUBLISH_RESULTS is empty")
errRequiredENVNotSet = errors.New("required environment variables are not set")
errGitHubEventPath = errors.New("error getting GITHUB_EVENT_PATH")
errGitHubEventPathEmpty = errors.New("GITHUB_EVENT_PATH is empty")
errGitHubEventPathNotSet = errors.New("GITHUB_EVENT_PATH is not set")
errEmptyDefaultBranch = errors.New("default branch is empty")
errInputResultFileNotSet = errors.New("INPUT_RESULTS_FILE is not set")
errInputResultFileEmpty = errors.New("INPUT_RESULTS_FILE is empty")
errInputResultFormatNotSet = errors.New("INPUT_RESULTS_FORMAT is not set")
errInputResultFormatEmtpy = errors.New("INPUT_RESULTS_FORMAT is empty")
errInputPublishResultsNotSet = errors.New("INPUT_PUBLISH_RESULTS is not set")
errInputPublishResultsEmpty = errors.New("INPUT_PUBLISH_RESULTS is empty")
errRequiredENVNotSet = errors.New("required environment variables are not set")
errGitHubEventPath = errors.New("error getting GITHUB_EVENT_PATH")
errGitHubEventPathEmpty = errors.New("GITHUB_EVENT_PATH is empty")
errGitHubEventPathNotSet = errors.New("GITHUB_EVENT_PATH is not set")
errEmptyDefaultBranch = errors.New("default branch is empty")
errEmptyGitHubAuthToken = errors.New("repo_token variable is empty")
errOnlyDefaultBranchSupported = errors.New("only default branch is supported")
)

type repositoryInformation struct {
Expand All @@ -48,7 +52,9 @@ const (
enableDangerousWorkflow = "ENABLE_DANGEROUS_WORKFLOW"
enabledChecks = "ENABLED_CHECKS"
githubEventPath = "GITHUB_EVENT_PATH"
githubEventName = "GITHUB_EVENT_NAME"
githubRepository = "GITHUB_REPOSITORY"
githubRef = "GITHUB_REF"
//nolint:gosec
githubAuthToken = "GITHUB_AUTH_TOKEN"
inputresultsfile = "INPUT_RESULTS_FILE"
Expand Down Expand Up @@ -91,6 +97,12 @@ func main() {
if err := updateEnvVariables(); err != nil {
panic(err)
}

printEnvVariables(os.Stdout)

if err := validate(os.Stderr); err != nil {
panic(err)
}
}

// initalizeENVVariables is a function to initialize the environment variables required for the action.
Expand Down Expand Up @@ -287,3 +299,38 @@ func updateEnvVariables() error {
}
return nil
}

// printEnvVariables is a function to print the ENV variables.
func printEnvVariables(writer io.Writer) {
fmt.Fprintf(writer, "GITHUB_EVENT_PATH=%s\n", os.Getenv(githubEventPath))
fmt.Fprintf(writer, "GITHUB_EVENT_NAME=%s\n", os.Getenv(githubEventName))
fmt.Fprintf(writer, "GITHUB_REPOSITORY=%s\n", os.Getenv(githubRepository))
fmt.Fprintf(writer, "SCORECARD_IS_FORK=%s\n", os.Getenv(scorecardFork))
fmt.Fprintf(writer, "Ref=%s\n", os.Getenv(githubRef))
fmt.Fprintf(writer, "SCORECARD_PRIVATE_REPOSITORY=%s\n", os.Getenv(scorecardPrivateRepository))
fmt.Fprintf(writer, "SCORECARD_PUBLISH_RESULTS=%s\n", os.Getenv(scorecardPublishResults))
fmt.Fprintf(writer, "Format=%s\n", os.Getenv(scorecardResultsFormat))
fmt.Fprintf(writer, "Policy file=%s\n", os.Getenv(scorecardPolicyFile))
fmt.Fprintf(writer, "Default branch=%s\n", os.Getenv(scorecardDefaultBranch))
}

// validate is a function to validate the scorecard configuration based on the environment variables.
func validate(writer io.Writer) error {
if os.Getenv(githubAuthToken) == "" {
fmt.Fprintf(writer, "The 'repo_token' variable is empty.\n")
if os.Getenv(scorecardFork) == "true" {
fmt.Fprintf(writer, "We have detected you are running on a fork.\n")
}
//nolint:lll
fmt.Fprintf(writer,
"Please follow the instructions at https://github.com/ossf/scorecard-action#authentication to create the read-only PAT token.\n")
return errEmptyGitHubAuthToken
}
if strings.Contains(os.Getenv(githubEventName), "pull_request") &&
os.Getenv(githubRef) == os.Getenv(scorecardDefaultBranch) {
fmt.Fprintf(writer, "%s not supported with %s event.\n", os.Getenv(githubRef), os.Getenv(githubEventName))
fmt.Fprintf(writer, "Only the default branch %s is supported.\n", os.Getenv(scorecardDefaultBranch))
return errOnlyDefaultBranchSupported
}
return nil
}
83 changes: 83 additions & 0 deletions main_test.go
Expand Up @@ -14,6 +14,7 @@
package main

import (
"bytes"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -399,3 +400,85 @@ func Test_gitHubEventPath(t *testing.T) {
})
}
}

// The reason we are not using t.Parallel() here is because we are mutating the env variables
//nolint
func Test_validate(t *testing.T) {
//nolint
tests := []struct {
name string
wantWriter string
wantErr bool
authToken string
scorecardFork bool
gitHubEventName string
ref string
scorecardDefaultBranch string
}{

{
name: "scorecardFork set and failure",
wantErr: true,
authToken: "",
scorecardFork: true,
gitHubEventName: "",
ref: "",
scorecardDefaultBranch: "",
},
{
name: "Success - scorecardFork set",
wantErr: false,
authToken: "token",
scorecardFork: false,
gitHubEventName: "",
ref: "",
scorecardDefaultBranch: "",
},
{
name: "Success - scorecardFork set",
wantErr: true,
authToken: "token",
scorecardFork: true,
gitHubEventName: "pull_request",
ref: "main",
scorecardDefaultBranch: "main",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
writer := &bytes.Buffer{}
if err := os.Setenv(scorecardFork, strconv.FormatBool(tt.scorecardFork)); err != nil {
t.Errorf("failed to set env var %s", scorecardFork)
}
defer os.Unsetenv(scorecardFork)
if tt.gitHubEventName != "" {
if err := os.Setenv(githubEventName, tt.gitHubEventName); err != nil {
t.Errorf("failed to set env var %s", githubEventName)
}
defer os.Unsetenv(githubEventName)
}
if tt.ref != "" {
if err := os.Setenv(githubRef, tt.ref); err != nil {
t.Errorf("failed to set env var %s", githubRef)
}
defer os.Unsetenv(githubRef)
}
if tt.scorecardDefaultBranch != "" {
if err := os.Setenv(scorecardDefaultBranch, tt.scorecardDefaultBranch); err != nil {
t.Errorf("failed to set env var %s", scorecardDefaultBranch)
}
defer os.Unsetenv(scorecardDefaultBranch)
}
if tt.authToken != "" {
if err := os.Setenv(githubAuthToken, tt.authToken); err != nil {
t.Errorf("failed to set env var %s", githubAuthToken)
}
defer os.Unsetenv(githubAuthToken)
}
if err := validate(writer); (err != nil) != tt.wantErr {
t.Errorf("validate() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}