Skip to content

Commit e2f29c9

Browse files
authored
chore(CI): Use single existing repository (#368)
1 parent a8e8b4b commit e2f29c9

File tree

1 file changed

+11
-45
lines changed

1 file changed

+11
-45
lines changed

test/integration/cloudbuild_repo_connection_github/cloudbuild_repo_connection_github_test.go

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ import (
2626
cftutils "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
2727
"github.com/google/go-github/v71/github"
2828
"github.com/stretchr/testify/assert"
29-
"github.com/terraform-google-modules/terraform-google-bootstrap/test/integration/utils"
29+
)
30+
31+
const (
32+
repoName = "cb-repo-conn-gh"
33+
owner = "im-goose"
34+
githubAppID = "47590865"
3035
)
3136

3237
type GitHubClient struct {
@@ -37,14 +42,14 @@ type GitHubClient struct {
3742
repository *github.Repository
3843
}
3944

40-
func NewGitHubClient(t *testing.T, token, owner, repo string) *GitHubClient {
45+
func NewGitHubClient(t *testing.T, token string) *GitHubClient {
4146
t.Helper()
4247
client := github.NewClient(nil).WithAuthToken(token)
4348
return &GitHubClient{
4449
t: t,
4550
client: client,
4651
owner: owner,
47-
repoName: repo,
52+
repoName: repoName,
4853
}
4954
}
5055

@@ -57,49 +62,18 @@ func (gh *GitHubClient) GetRepository(ctx context.Context) *github.Repository {
5762
return repo
5863
}
5964

60-
func (gh *GitHubClient) CreateRepository(ctx context.Context, org, repoName string) *github.Repository {
61-
newRepo := &github.Repository{
62-
Name: github.String(repoName),
63-
AutoInit: github.Bool(true),
64-
Private: github.Bool(true),
65-
Visibility: github.String("private"),
66-
}
67-
repo, _, err := gh.client.Repositories.Create(ctx, org, newRepo)
68-
if err != nil {
69-
gh.t.Fatal(err.Error())
70-
}
71-
gh.repository = repo
72-
return repo
73-
}
74-
75-
func (gh *GitHubClient) DeleteRepository(ctx context.Context) {
76-
resp, err := gh.client.Repositories.Delete(ctx, gh.owner, *gh.repository.Name)
77-
if resp.StatusCode != 404 && err != nil {
78-
gh.t.Fatal(err.Error())
79-
}
80-
}
81-
8265
func TestCloudBuildRepoConnectionGithub(t *testing.T) {
8366
ctx := context.Background()
84-
85-
repoName := fmt.Sprintf("conn-gh-%s", utils.GetRandomStringFromSetup(t))
86-
8767
githubPAT := cftutils.ValFromEnv(t, "IM_GITHUB_PAT")
68+
client := NewGitHubClient(t, githubPAT)
8869

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-
}
96-
70+
client.GetRepository(ctx)
9771
repoURL := client.repository.GetCloneURL()
9872

9973
resourcesLocation := "us-central1"
10074
vars := map[string]interface{}{
10175
"github_pat": githubPAT,
102-
"github_app_id": "47590865",
76+
"github_app_id": githubAppID,
10377
"repository_name": repoName,
10478
"repository_url": repoURL,
10579
}
@@ -109,13 +83,6 @@ func TestCloudBuildRepoConnectionGithub(t *testing.T) {
10983
bpt.DefineVerify(func(assert *assert.Assertions) {
11084
bpt.DefaultVerify(assert)
11185

112-
t.Cleanup(func() {
113-
// Delete the repository if we hit a failed state
114-
if t.Failed() {
115-
client.DeleteRepository(ctx)
116-
}
117-
})
118-
11986
// validate if repository was created using the connection
12087
projectId := bpt.GetTFSetupStringOutput("project_id")
12188
connectionId := bpt.GetStringOutput("cloud_build_repositories_2nd_gen_connection")
@@ -142,7 +109,6 @@ func TestCloudBuildRepoConnectionGithub(t *testing.T) {
142109
bpt.DefineTeardown(func(assert *assert.Assertions) {
143110
// Guarantee clean up even if the normal gcloud/teardown run into errors
144111
t.Cleanup(func() {
145-
client.DeleteRepository(ctx)
146112
bpt.DefaultTeardown(assert)
147113
})
148114
})

0 commit comments

Comments
 (0)