Skip to content

Commit

Permalink
Use old github token if new tokens are not available (GoogleCloudPlat…
Browse files Browse the repository at this point in the history
…form#10114)

* Use old github token if new tokens are not available

* Add lookup function for github token or fallback

* Add fall back

* Add fallback
  • Loading branch information
trodge authored and balanaguharsha committed Apr 19, 2024
1 parent eeb38b4 commit 39be583
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 16 deletions.
17 changes: 15 additions & 2 deletions .ci/magician/cmd/check_cassettes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

var ccEnvironmentVariables = [...]string{
"COMMIT_SHA",
"GITHUB_TOKEN_DOWNSTREAMS",
"GOCACHE",
"GOPATH",
"GOOGLE_BILLING_ACCOUNT",
Expand Down Expand Up @@ -56,13 +55,19 @@ var checkCassettesCmd = &cobra.Command{
env[ev] = val
}

githubToken, ok := lookupGithubTokenOrFallback("GITHUB_TOKEN_DOWNSTREAMS")
if !ok {
fmt.Println("Did not provide GITHUB_TOKEN_DOWNSTREAMS or GITHUB_TOKEN environment variables")
os.Exit(1)
}

rnr, err := exec.NewRunner()
if err != nil {
fmt.Println("Error creating Runner: ", err)
os.Exit(1)
}

ctlr := source.NewController(env["GOPATH"], "modular-magician", env["GITHUB_TOKEN_DOWNSTREAMS"], rnr)
ctlr := source.NewController(env["GOPATH"], "modular-magician", githubToken, rnr)

vt, err := vcr.NewTester(env, rnr)
if err != nil {
Expand All @@ -73,6 +78,14 @@ var checkCassettesCmd = &cobra.Command{
},
}

func lookupGithubTokenOrFallback(tokenName string) (string, bool) {
val, ok := os.LookupEnv(tokenName)
if !ok {
return os.LookupEnv("GITHUB_TOKEN")
}
return val, ok
}

func listCCEnvironmentVariables() string {
var result string
for i, ev := range ccEnvironmentVariables {
Expand Down
4 changes: 2 additions & 2 deletions .ci/magician/cmd/community_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ var communityApprovalCmd = &cobra.Command{
baseBranch := args[5]
fmt.Println("Base Branch: ", baseBranch)

githubToken, ok := os.LookupEnv("GITHUB_TOKEN_MAGIC_MODULES")
githubToken, ok := lookupGithubTokenOrFallback("GITHUB_TOKEN_MAGIC_MODULES")
if !ok {
fmt.Println("Did not provide GITHUB_TOKEN_MAGIC_MODULES environment variable")
fmt.Println("Did not provide GITHUB_TOKEN_MAGIC_MODULES or GITHUB_TOKEN environment variables")
os.Exit(1)
}
gh := github.NewClient(githubToken)
Expand Down
10 changes: 8 additions & 2 deletions .ci/magician/cmd/generate_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ var gcEnvironmentVariables = [...]string{
"BUILD_ID",
"BUILD_STEP",
"COMMIT_SHA",
"GITHUB_TOKEN_DOWNSTREAMS",
"GITHUB_TOKEN_MAGIC_MODULES",
"GOPATH",
"HOME",
"PATH",
Expand Down Expand Up @@ -71,6 +69,14 @@ var generateCommentCmd = &cobra.Command{
env[ev] = val
}

for _, tokenName := range []string{"GITHUB_TOKEN_DOWNSTREAMS", "GITHUB_TOKEN_MAGIC_MODULES"} {
val, ok := lookupGithubTokenOrFallback(tokenName)
if !ok {
fmt.Printf("Did not provide %s or GITHUB_TOKEN environment variable\n", tokenName)
os.Exit(1)
}
env[tokenName] = val
}
gh := github.NewClient(env["GITHUB_TOKEN_MAGIC_MODULES"])
rnr, err := exec.NewRunner()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions .ci/magician/cmd/generate_downstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var gdEnvironmentVariables = [...]string{
var gdTokenEnvironmentVariables = [...]string{
"GITHUB_TOKEN_CLASSIC",
"GITHUB_TOKEN_DOWNSTREAMS",
"GITHUB_TOKEN",
}

var generateDownstreamCmd = &cobra.Command{
Expand Down
4 changes: 2 additions & 2 deletions .ci/magician/cmd/membership_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ var membershipCheckerCmd = &cobra.Command{
baseBranch := args[5]
fmt.Println("Base Branch: ", baseBranch)

githubToken, ok := os.LookupEnv("GITHUB_TOKEN_MAGIC_MODULES")
githubToken, ok := lookupGithubTokenOrFallback("GITHUB_TOKEN_MAGIC_MODULES")
if !ok {
fmt.Println("Did not provide GITHUB_TOKEN_MAGIC_MODULES environment variable")
fmt.Println("Did not provide GITHUB_TOKEN_MAGIC_MODULES or GITHUB_TOKEN environment variables")
os.Exit(1)
}
gh := github.NewClient(githubToken)
Expand Down
4 changes: 2 additions & 2 deletions .ci/magician/cmd/request_service_reviewers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ var requestServiceReviewersCmd = &cobra.Command{
prNumber := args[0]
fmt.Println("PR Number: ", prNumber)

githubToken, ok := os.LookupEnv("GITHUB_TOKEN_MAGIC_MODULES")
githubToken, ok := lookupGithubTokenOrFallback("GITHUB_TOKEN_MAGIC_MODULES")
if !ok {
fmt.Println("Did not provide GITHUB_TOKEN_MAGIC_MODULES environment variable")
fmt.Println("Did not provide GITHUB_TOKEN_MAGIC_MODULES or GITHUB_TOKEN environment variable")
os.Exit(1)
}
gh := github.NewClient(githubToken)
Expand Down
11 changes: 9 additions & 2 deletions .ci/magician/cmd/test_terraform_vcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
)

var ttvEnvironmentVariables = [...]string{
"GITHUB_TOKEN_DOWNSTREAMS",
"GITHUB_TOKEN_MAGIC_MODULES",
"GOCACHE",
"GOPATH",
"GOOGLE_BILLING_ACCOUNT",
Expand Down Expand Up @@ -55,6 +53,15 @@ var testTerraformVCRCmd = &cobra.Command{
env[ev] = val
}

for _, tokenName := range []string{"GITHUB_TOKEN_DOWNSTREAMS", "GITHUB_TOKEN_MAGIC_MODULES"} {
val, ok := lookupGithubTokenOrFallback(tokenName)
if !ok {
fmt.Printf("Did not provide %s or GITHUB_TOKEN environment variable\n", tokenName)
os.Exit(1)
}
env[tokenName] = val
}

baseBranch := os.Getenv("BASE_BRANCH")
if baseBranch == "" {
baseBranch = "main"
Expand Down
4 changes: 2 additions & 2 deletions .ci/magician/cmd/test_tgc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ var testTGCCmd = &cobra.Command{
commit := os.Getenv("COMMIT_SHA")
pr := os.Getenv("PR_NUMBER")

githubToken, ok := os.LookupEnv("GITHUB_TOKEN_MAGIC_MODULES")
githubToken, ok := lookupGithubTokenOrFallback("GITHUB_TOKEN_MAGIC_MODULES")
if !ok {
fmt.Println("Did not provide GITHUB_TOKEN_MAGIC_MODULES environment variable")
fmt.Println("Did not provide GITHUB_TOKEN_MAGIC_MODULES or GITHUB_TOKEN environment variables")
os.Exit(1)
}
gh := github.NewClient(githubToken)
Expand Down
4 changes: 2 additions & 2 deletions .ci/magician/cmd/test_tpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ var testTPGCmd = &cobra.Command{
commit := os.Getenv("COMMIT_SHA")
pr := os.Getenv("PR_NUMBER")

githubToken, ok := os.LookupEnv("GITHUB_TOKEN_MAGIC_MODULES")
githubToken, ok := lookupGithubTokenOrFallback("GITHUB_TOKEN_MAGIC_MODULES")
if !ok {
fmt.Println("Did not provide GITHUB_TOKEN_MAGIC_MODULES environment variable")
fmt.Println("Did not provide GITHUB_TOKEN_MAGIC_MODULES or GITHUB_TOKEN environment variables")
os.Exit(1)
}
gh := github.NewClient(githubToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ post_body=$( jq -n \
--arg state "pending" \
'{context: $context, target_url: $target_url, state: $state}')

# Fall back to old github token if new token is unavailable.
if [[ -z $GITHUB_TOKEN_MAGIC_MODULES ]]; then
GITHUB_TOKEN_MAGIC_MODULES=$GITHUB_TOKEN
fi

curl \
-X POST \
-u "$github_username:$GITHUB_TOKEN_MAGIC_MODULES" \
Expand Down

0 comments on commit 39be583

Please sign in to comment.