@@ -26,93 +26,32 @@ import (
2626 "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/git"
2727 "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
2828 cftutils "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
29- "github.com/google/go-github/v71/github"
3029 "github.com/stretchr/testify/assert"
3130 "github.com/terraform-google-modules/terraform-google-bootstrap/test/integration/utils"
3231)
3332
34- type GitHubClient struct {
35- t * testing.T
36- client * github.Client
37- owner string
38- repoName string
39- repository * github.Repository
40- }
41-
42- func NewGitHubClient (t * testing.T , token , owner , repo string ) * GitHubClient {
43- t .Helper ()
44- client := github .NewClient (nil ).WithAuthToken (token )
45- return & GitHubClient {
46- t : t ,
47- client : client ,
48- owner : owner ,
49- repoName : repo ,
50- }
51- }
52-
53- func (gh * GitHubClient ) GetRepository (ctx context.Context ) * github.Repository {
54- repo , resp , err := gh .client .Repositories .Get (ctx , gh .owner , gh .repoName )
55- if resp .StatusCode != 404 && err != nil {
56- gh .t .Fatal (err .Error ())
57- }
58- gh .repository = repo
59- return repo
60- }
61-
62- func (gh * GitHubClient ) CreateRepository (ctx context.Context , org , repoName string ) * github.Repository {
63- newRepo := & github.Repository {
64- Name : github .String (repoName ),
65- AutoInit : github .Bool (true ),
66- Private : github .Bool (true ),
67- Visibility : github .String ("private" ),
68- }
69- repo , _ , err := gh .client .Repositories .Create (ctx , org , newRepo )
70- if err != nil {
71- gh .t .Fatal (err .Error ())
72- }
73- gh .repository = repo
74- return repo
75- }
76-
77- func (gh * GitHubClient ) DeleteRepository (ctx context.Context ) {
78- resp , err := gh .client .Repositories .Delete (ctx , gh .owner , * gh .repository .Name )
79- if resp .StatusCode != 404 && err != nil {
80- gh .t .Fatal (err .Error ())
81- }
82- }
33+ const (
34+ githubRepo = "cb-bp-gh"
35+ )
8336
8437func TestCloudBuildWorkspaceSimpleGitHub (t * testing.T ) {
8538 ctx := context .Background ()
86-
87- repoName := fmt .Sprintf ("cb-bp-gh-%s" , utils .GetRandomStringFromSetup (t ))
8839 githubPAT := cftutils .ValFromEnv (t , "IM_GITHUB_PAT" )
89- owner := "im-goose"
90- client := NewGitHubClient (t , githubPAT , owner , repoName )
91-
92- repo := client .GetRepository (ctx )
93- if repo == nil {
94- client .CreateRepository (ctx , client .owner , client .repoName )
95- }
40+ client := utils .NewGitHubClient (t , githubPAT , githubRepo )
41+ client .GetRepository (ctx )
9642
9743 // Testing the module's feature of appending the ".git" suffix if it's missing
98- repoURL := strings .TrimSuffix (client .repository .GetCloneURL (), ".git" )
44+ repoURL := strings .TrimSuffix (client .Repository .GetCloneURL (), ".git" )
9945 vars := map [string ]interface {}{
10046 "github_pat" : githubPAT ,
10147 "repository_uri" : repoURL ,
102- "github_app_id" : "47590865" , // Found in the URL of your Cloud Build GitHub app configuration settings
48+ "github_app_id" : utils . GitHubAppID ,
10349 }
10450 bpt := tft .NewTFBlueprintTest (t , tft .WithVars (vars ))
10551
10652 bpt .DefineVerify (func (assert * assert.Assertions ) {
10753 bpt .DefaultVerify (assert )
10854
109- t .Cleanup (func () {
110- // Delete the repository if we hit a failed state
111- if t .Failed () {
112- client .DeleteRepository (ctx )
113- }
114- })
115-
11655 location := bpt .GetStringOutput ("location" )
11756 projectID := bpt .GetStringOutput ("project_id" )
11857
@@ -121,8 +60,8 @@ func TestCloudBuildWorkspaceSimpleGitHub(t *testing.T) {
12160 for _ , trigger := range triggers {
12261 triggerOP := utils .LastElement (bpt .GetStringOutput (fmt .Sprintf ("cloudbuild_%s_trigger_id" , trigger )), "/" )
12362 cloudBuildOP := gcloud .Runf (t , "beta builds triggers describe %s --region %s --project %s" , triggerOP , location , projectID )
124- assert .Equal (fmt .Sprintf ("%s-%s" , repoName , trigger ), cloudBuildOP .Get ("name" ).String (), "should have the correct name" )
125- assert .Equal (fmt .Sprintf ("projects/%s/serviceAccounts/tf-gh-%s@%s.iam.gserviceaccount.com" , projectID , repoName , projectID ), cloudBuildOP .Get ("serviceAccount" ).String (), "uses expected SA" )
63+ assert .Equal (fmt .Sprintf ("%s-%s" , githubRepo , trigger ), cloudBuildOP .Get ("name" ).String (), "should have the correct name" )
64+ assert .Equal (fmt .Sprintf ("projects/%s/serviceAccounts/tf-gh-%s@%s.iam.gserviceaccount.com" , projectID , githubRepo , projectID ), cloudBuildOP .Get ("serviceAccount" ).String (), "uses expected SA" )
12665 }
12766
12867 // artifacts, state and log buckets
@@ -157,7 +96,7 @@ func TestCloudBuildWorkspaceSimpleGitHub(t *testing.T) {
15796 }
15897 }
15998
160- gitRun (
"clone" ,
fmt .
Sprintf (
"https://%[email protected] /%s/%s" ,
githubPAT ,
owner , repoName ),
tmpDir )
99+ gitRun (
"clone" ,
fmt .
Sprintf (
"https://%[email protected] /%s/%s" ,
githubPAT ,
utils . GitHubOwner , githubRepo ),
tmpDir )
161100 gitRun (
"config" ,
"user.email" ,
"[email protected] " )
162101 gitRun ("config" , "user.name" , "TF Robot" )
163102
@@ -209,7 +148,6 @@ func TestCloudBuildWorkspaceSimpleGitHub(t *testing.T) {
209148 bpt .DefineTeardown (func (assert * assert.Assertions ) {
210149 // Guarantee clean up even if the normal gcloud/teardown run into errors
211150 t .Cleanup (func () {
212- client .DeleteRepository (ctx )
213151 bpt .DefaultTeardown (assert )
214152 })
215153 })
0 commit comments