@@ -26,93 +26,32 @@ import (
26
26
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/git"
27
27
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
28
28
cftutils "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
29
- "github.com/google/go-github/v71/github"
30
29
"github.com/stretchr/testify/assert"
31
30
"github.com/terraform-google-modules/terraform-google-bootstrap/test/integration/utils"
32
31
)
33
32
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
+ )
83
36
84
37
func TestCloudBuildWorkspaceSimpleGitHub (t * testing.T ) {
85
38
ctx := context .Background ()
86
-
87
- repoName := fmt .Sprintf ("cb-bp-gh-%s" , utils .GetRandomStringFromSetup (t ))
88
39
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 )
96
42
97
43
// 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" )
99
45
vars := map [string ]interface {}{
100
46
"github_pat" : githubPAT ,
101
47
"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 ,
103
49
}
104
50
bpt := tft .NewTFBlueprintTest (t , tft .WithVars (vars ))
105
51
106
52
bpt .DefineVerify (func (assert * assert.Assertions ) {
107
53
bpt .DefaultVerify (assert )
108
54
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
-
116
55
location := bpt .GetStringOutput ("location" )
117
56
projectID := bpt .GetStringOutput ("project_id" )
118
57
@@ -121,8 +60,8 @@ func TestCloudBuildWorkspaceSimpleGitHub(t *testing.T) {
121
60
for _ , trigger := range triggers {
122
61
triggerOP := utils .LastElement (bpt .GetStringOutput (fmt .Sprintf ("cloudbuild_%s_trigger_id" , trigger )), "/" )
123
62
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" )
126
65
}
127
66
128
67
// artifacts, state and log buckets
@@ -157,7 +96,7 @@ func TestCloudBuildWorkspaceSimpleGitHub(t *testing.T) {
157
96
}
158
97
}
159
98
160
- gitRun ("clone" , fmt .Sprintf ("https://%s@github.com/%s/%s" , githubPAT , owner , repoName ), tmpDir )
99
+ gitRun ("clone" , fmt .Sprintf ("https://%s@github.com/%s/%s" , githubPAT , utils . GitHubOwner , githubRepo ), tmpDir )
161
100
gitRun ("config" , "user.email" , "tf-robot@example.com" )
162
101
gitRun ("config" , "user.name" , "TF Robot" )
163
102
@@ -209,7 +148,6 @@ func TestCloudBuildWorkspaceSimpleGitHub(t *testing.T) {
209
148
bpt .DefineTeardown (func (assert * assert.Assertions ) {
210
149
// Guarantee clean up even if the normal gcloud/teardown run into errors
211
150
t .Cleanup (func () {
212
- client .DeleteRepository (ctx )
213
151
bpt .DefaultTeardown (assert )
214
152
})
215
153
})
0 commit comments