Skip to content

Commit fdfd333

Browse files
committed
use github apps installation token to run the test
we use a generated github apps token for github to run the tests which has a much upper limit than PAT. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 39940a1 commit fdfd333

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

pkg/provider/github/github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (v *Provider) GetTaskURI(ctx context.Context, _ *params.Run, event *info.Ev
110110

111111
func (v *Provider) InitAppClient(ctx context.Context, kube kubernetes.Interface, event *info.Event) error {
112112
var err error
113-
event.Provider.Token, err = v.getAppToken(ctx, kube, event.GHEURL, event.InstallationID)
113+
event.Provider.Token, err = v.GetAppToken(ctx, kube, event.GHEURL, event.InstallationID)
114114
if err != nil {
115115
return err
116116
}

pkg/provider/github/parse_payload.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
secretName = "pipelines-as-code-secret"
2626
)
2727

28-
func (v *Provider) getAppToken(ctx context.Context, kube kubernetes.Interface, gheURL string, installationID int64) (string, error) {
28+
func (v *Provider) GetAppToken(ctx context.Context, kube kubernetes.Interface, gheURL string, installationID int64) (string, error) {
2929
// TODO: move this out of here
3030
ns := os.Getenv("SYSTEM_NAMESPACE")
3131
secret, err := kube.CoreV1().Secrets(ns).Get(ctx, secretName, v1.GetOptions{})
@@ -137,7 +137,7 @@ func (v *Provider) ParsePayload(ctx context.Context, run *params.Run, request *h
137137
installationIDFrompayload := getInstallationIDFromPayload(payload)
138138
if installationIDFrompayload != -1 {
139139
var err error
140-
if event.Provider.Token, err = v.getAppToken(ctx, run.Clients.Kube, event.Provider.URL, installationIDFrompayload); err != nil {
140+
if event.Provider.Token, err = v.GetAppToken(ctx, run.Clients.Kube, event.Provider.URL, installationIDFrompayload); err != nil {
141141
return nil, err
142142
}
143143
}
@@ -176,7 +176,7 @@ func (v *Provider) ParsePayload(ctx context.Context, run *params.Run, request *h
176176
}
177177
}
178178
var err error
179-
if processedEvent.Provider.Token, err = v.getAppToken(ctx, run.Clients.Kube, event.Provider.URL,
179+
if processedEvent.Provider.Token, err = v.GetAppToken(ctx, run.Clients.Kube, event.Provider.URL,
180180
installationIDFrompayload); err != nil {
181181
return nil, err
182182
}

test/pkg/github/setup.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"strconv"
78
"strings"
89
"testing"
910

@@ -17,8 +18,8 @@ import (
1718
)
1819

1920
func 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

Comments
 (0)