Skip to content

Commit ce330fd

Browse files
authored
✨ use GITHUB_TOKEN when repo_token is empty on PRs (#335)
* update * update * update * update * update * update * update * update * update * update * update * update * update * update * update * update * update * test * updates * updates
1 parent 2e062bc commit ce330fd

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

action.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ inputs:
3737
required: false
3838
default: false
3939

40+
internal_default_token:
41+
description: "INPUT: Default GitHub token. (Internal purpose only, not intended for developers to set. Used for pull requests configured with a PAT)."
42+
required: false
43+
default: ${{ github.token }}
44+
4045
branding:
4146
icon: "mic"
4247
color: "white"

entrypoint.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ set -euo pipefail
2222
# GITHUB_EVENT_NAME contains the event name.
2323
# GITHUB_ACTIONS is true in GitHub env.
2424

25+
if [[ -z "$INPUT_REPO_TOKEN" ]]; then
26+
INPUT_REPO_TOKEN="$INPUT_INTERNAL_DEFAULT_TOKEN"
27+
if [[ -z "$INPUT_REPO_TOKEN" ]]; then
28+
exit 2
29+
fi
30+
echo "The repo_token was empty so GITHUB_TOKEN is used instead"
31+
fi
32+
2533
export GITHUB_AUTH_TOKEN="$INPUT_REPO_TOKEN"
2634
export ENABLE_SARIF=1
2735
export ENABLE_LICENSE=1

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/sigstore/cosign v1.9.0
1111
github.com/sirupsen/logrus v1.8.1
1212
github.com/spf13/cobra v1.5.0
13+
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2
1314
sigs.k8s.io/release-sdk v0.8.0
1415
sigs.k8s.io/release-utils v0.6.1-0.20220405215325-d4a2a2f0e8fd
1516
)
@@ -241,7 +242,6 @@ require (
241242
gocloud.dev v0.25.0 // indirect
242243
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
243244
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
244-
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
245245
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
246246
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
247247
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect

options/env.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ const (
3838

3939
// TODO(input): INPUT_ constants should be removed in a future release once
4040
// they have replacements in upstream scorecard.
41-
EnvInputRepoToken = "INPUT_REPO_TOKEN" //nolint:gosec
42-
EnvInputResultsFile = "INPUT_RESULTS_FILE"
43-
EnvInputResultsFormat = "INPUT_RESULTS_FORMAT"
44-
EnvInputPublishResults = "INPUT_PUBLISH_RESULTS"
41+
EnvInputRepoToken = "INPUT_REPO_TOKEN" //nolint:gosec
42+
EnvInputInternalRepoToken = "INPUT_INTERNAL_DEFAULT_TOKEN" //nolint:gosec
43+
EnvInputResultsFile = "INPUT_RESULTS_FILE"
44+
EnvInputResultsFormat = "INPUT_RESULTS_FORMAT"
45+
EnvInputPublishResults = "INPUT_PUBLISH_RESULTS"
4546
)
4647

4748
// Errors

options/options.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ func New() (*Options, error) {
105105

106106
// Validate validates the scorecard configuration.
107107
func (o *Options) Validate() error {
108+
fmt.Println("EnvGithubAuthToken:", EnvGithubAuthToken, os.Getenv(EnvGithubAuthToken))
108109
if os.Getenv(EnvGithubAuthToken) == "" {
109-
fmt.Printf("The 'repo_token' variable is empty.\n")
110+
fmt.Printf("%s variable is empty.\n", EnvGithubAuthToken)
110111
if o.IsForkStr == trueStr {
111112
fmt.Printf("We have detected you are running on a fork.\n")
112113
}
@@ -151,6 +152,14 @@ func (o *Options) Print() {
151152

152153
func (o *Options) setScorecardOpts() {
153154
o.ScorecardOpts = scopts.New()
155+
// Set GITHUB_AUTH_TOKEN
156+
inputToken := os.Getenv(EnvInputRepoToken)
157+
if inputToken == "" {
158+
fmt.Printf("The 'repo_token' variable is empty.\n")
159+
fmt.Printf("Using the '%s' variable instead.\n", EnvInputInternalRepoToken)
160+
inputToken := os.Getenv(EnvInputInternalRepoToken)
161+
os.Setenv(EnvGithubAuthToken, inputToken)
162+
}
154163

155164
// --repo= | --local
156165
// This section restores functionality that was removed in

options/options_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,12 @@ func TestNew(t *testing.T) {
222222
os.Setenv(EnvGithubAuthToken, testToken)
223223
defer os.Unsetenv(EnvGithubAuthToken)
224224

225+
os.Setenv(EnvInputRepoToken, "token-value-123456")
226+
defer os.Unsetenv(EnvInputRepoToken)
227+
225228
if tt.unsetToken {
226229
os.Unsetenv(EnvGithubAuthToken)
230+
os.Unsetenv(EnvInputRepoToken)
227231
}
228232

229233
os.Setenv(EnvGithubEventPath, tt.githubEventPath)

0 commit comments

Comments
 (0)