44 "context"
55 "fmt"
66 "os"
7+ "strconv"
78 "strings"
89 "testing"
910
@@ -17,8 +18,8 @@ import (
1718)
1819
1920func Setup (ctx context.Context , viaDirectWebhook bool ) (* params.Run , options.E2E , * github.Provider , error ) {
21+ githubToken := ""
2022 githubURL := os .Getenv ("TEST_GITHUB_API_URL" )
21- githubToken := os .Getenv ("TEST_GITHUB_TOKEN" )
2223 githubRepoOwnerGithubApp := os .Getenv ("TEST_GITHUB_REPO_OWNER_GITHUBAPP" )
2324 githubRepoOwnerDirectWebhook := os .Getenv ("TEST_GITHUB_REPO_OWNER_WEBHOOK" )
2425
@@ -36,12 +37,13 @@ func Setup(ctx context.Context, viaDirectWebhook bool) (*params.Run, options.E2E
3637
3738 var splitted []string
3839 if ! viaDirectWebhook {
39- if githubURL == "" || githubToken == "" || githubRepoOwnerGithubApp == "" {
40- return nil , options.E2E {}, github .New (), fmt .Errorf ("TEST_GITHUB_API_URL TEST_GITHUB_TOKEN TEST_GITHUB_REPO_OWNER_GITHUBAPP need to be set" )
40+ if githubURL == "" || githubRepoOwnerGithubApp == "" {
41+ return nil , options.E2E {}, github .New (), fmt .Errorf ("TEST_GITHUB_API_URL TEST_GITHUB_REPO_OWNER_GITHUBAPP need to be set" )
4142 }
4243 splitted = strings .Split (githubRepoOwnerGithubApp , "/" )
4344 }
4445 if viaDirectWebhook {
46+ githubToken = os .Getenv ("TEST_GITHUB_TOKEN" )
4547 if githubURL == "" || githubToken == "" || githubRepoOwnerDirectWebhook == "" {
4648 return nil , options.E2E {}, github .New (), fmt .Errorf ("TEST_GITHUB_API_URL TEST_GITHUB_TOKEN TEST_GITHUB_REPO_OWNER_WEBHOOK need to be set" )
4749 }
@@ -52,9 +54,35 @@ func Setup(ctx context.Context, viaDirectWebhook bool) (*params.Run, options.E2E
5254 if err := run .Clients .NewClients (ctx , & run .Info ); err != nil {
5355 return nil , options.E2E {}, github .New (), err
5456 }
57+
5558 e2eoptions := options.E2E {Organization : splitted [0 ], Repo : splitted [1 ], DirectWebhook : viaDirectWebhook }
5659 gprovider := github .New ()
5760 event := info .NewEvent ()
61+
62+ if githubToken == "" && ! viaDirectWebhook {
63+ var err error
64+ // check if SYSTEM_NAMESPACE is set otherwise set it
65+ if os .Getenv ("SYSTEM_NAMESPACE" ) == "" {
66+ if err := os .Setenv ("SYSTEM_NAMESPACE" , "pipelines-as-code" ); err != nil {
67+ return & params.Run {}, options.E2E {}, & github.Provider {}, err
68+ }
69+ }
70+
71+ envGithubRepoInstallationID := os .Getenv ("TEST_GITHUB_REPO_INSTALLATION_ID" )
72+ if envGithubRepoInstallationID == "" {
73+ return nil , options.E2E {}, github .New (), fmt .Errorf ("TEST_GITHUB_REPO_INSTALLATION_ID need to be set" )
74+ }
75+ // convert to int64 githubRepoInstallationID
76+ githubRepoInstallationID , err := strconv .ParseInt (envGithubRepoInstallationID , 10 , 64 )
77+ if err != nil {
78+ return nil , options.E2E {}, github .New (), fmt .Errorf ("TEST_GITHUB_REPO_INSTALLATION_ID need to be set" )
79+ }
80+ githubToken , err = gprovider .GetAppToken (ctx , run .Clients .Kube , githubURL , githubRepoInstallationID )
81+ if err != nil {
82+ return nil , options.E2E {}, github .New (), err
83+ }
84+ }
85+
5886 event .Provider = & info.Provider {
5987 Token : githubToken ,
6088 URL : githubURL ,
0 commit comments