Skip to content

Commit 48cbeab

Browse files
josephdt12apeabody
andauthored
chore: Switch GH clients to use utils package (#375)
Co-authored-by: Andrew Peabody <[email protected]>
1 parent 0fb002e commit 48cbeab

File tree

3 files changed

+14
-148
lines changed

3 files changed

+14
-148
lines changed

test/integration/cloudbuild_repo_connection_github/cloudbuild_repo_connection_github_test.go

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,26 @@ import (
2424
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
2525
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
2626
cftutils "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
27-
"github.com/google/go-github/v72/github"
2827
"github.com/stretchr/testify/assert"
28+
"github.com/terraform-google-modules/terraform-google-bootstrap/test/integration/utils"
2929
)
3030

3131
const (
32-
repoName = "cb-repo-conn-gh"
33-
owner = "im-goose"
34-
githubAppID = "47590865"
32+
repoName = "cb-repo-conn-gh"
3533
)
3634

37-
type GitHubClient struct {
38-
t *testing.T
39-
client *github.Client
40-
owner string
41-
repoName string
42-
repository *github.Repository
43-
}
44-
45-
func NewGitHubClient(t *testing.T, token string) *GitHubClient {
46-
t.Helper()
47-
client := github.NewClient(nil).WithAuthToken(token)
48-
return &GitHubClient{
49-
t: t,
50-
client: client,
51-
owner: owner,
52-
repoName: repoName,
53-
}
54-
}
55-
56-
func (gh *GitHubClient) GetRepository(ctx context.Context) *github.Repository {
57-
repo, resp, err := gh.client.Repositories.Get(ctx, gh.owner, gh.repoName)
58-
if resp.StatusCode != 404 && err != nil {
59-
gh.t.Fatal(err.Error())
60-
}
61-
gh.repository = repo
62-
return repo
63-
}
64-
6535
func TestCloudBuildRepoConnectionGithub(t *testing.T) {
6636
ctx := context.Background()
6737
githubPAT := cftutils.ValFromEnv(t, "IM_GITHUB_PAT")
68-
client := NewGitHubClient(t, githubPAT)
38+
client := utils.NewGitHubClient(t, githubPAT, repoName)
6939

7040
client.GetRepository(ctx)
71-
repoURL := client.repository.GetCloneURL()
41+
repoURL := client.Repository.GetCloneURL()
7242

7343
resourcesLocation := "us-central1"
7444
vars := map[string]interface{}{
7545
"github_pat": githubPAT,
76-
"github_app_id": githubAppID,
46+
"github_app_id": utils.GitHubAppID,
7747
"repository_name": repoName,
7848
"repository_url": repoURL,
7949
}

test/integration/im_cloudbuild_workspace_github/im_cloudbuild_workspace_github_test.go

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -32,92 +32,18 @@ import (
3232
)
3333

3434
const (
35-
githubOwner = "im-goose"
36-
githubRepo = "im-cloudbuild-workspace-github"
35+
githubRepo = "im-cloudbuild-workspace-github"
3736
)
3837

39-
type GitHubClient struct {
40-
t *testing.T
41-
client *github.Client
42-
owner string
43-
repoName string
44-
repository *github.Repository
45-
}
46-
47-
func NewGitHubClient(t *testing.T, token, owner, repo string) *GitHubClient {
48-
t.Helper()
49-
client := github.NewClient(nil).WithAuthToken(token)
50-
return &GitHubClient{
51-
t: t,
52-
client: client,
53-
owner: owner,
54-
repoName: repo,
55-
}
56-
}
57-
58-
// GetOpenPullRequest gets an open pull request for a given branch if it exists.
59-
func (gh *GitHubClient) GetOpenPullRequest(ctx context.Context, branch string) *github.PullRequest {
60-
opts := &github.PullRequestListOptions{
61-
State: "open",
62-
Head: branch,
63-
}
64-
prs, resp, err := gh.client.PullRequests.List(ctx, gh.owner, gh.repoName, opts)
65-
if resp.StatusCode != 422 && err != nil {
66-
gh.t.Fatal(err.Error())
67-
}
68-
if len(prs) == 0 {
69-
return nil
70-
}
71-
return prs[0]
72-
}
73-
74-
func (gh *GitHubClient) CreatePullRequest(ctx context.Context, title, branch, base string) *github.PullRequest {
75-
newPR := &github.NewPullRequest{
76-
Title: github.String(title),
77-
Head: github.String(branch),
78-
Base: github.String(base),
79-
}
80-
pr, _, err := gh.client.PullRequests.Create(ctx, gh.owner, gh.repoName, newPR)
81-
if err != nil {
82-
gh.t.Fatal(err.Error())
83-
}
84-
return pr
85-
}
86-
87-
func (gh *GitHubClient) MergePullRequest(ctx context.Context, pr *github.PullRequest, commitTitle, commitMessage string) *github.PullRequestMergeResult {
88-
result, _, err := gh.client.PullRequests.Merge(ctx, gh.owner, gh.repoName, *pr.Number, commitMessage, nil)
89-
if err != nil {
90-
gh.t.Fatal(err.Error())
91-
}
92-
return result
93-
}
94-
95-
func (gh *GitHubClient) ClosePullRequest(ctx context.Context, pr *github.PullRequest) {
96-
pr.State = github.String("closed")
97-
_, _, err := gh.client.PullRequests.Edit(ctx, gh.owner, gh.repoName, *pr.Number, pr)
98-
if err != nil {
99-
gh.t.Fatal(err.Error())
100-
}
101-
}
102-
103-
func (gh *GitHubClient) GetRepository(ctx context.Context) *github.Repository {
104-
repo, resp, err := gh.client.Repositories.Get(ctx, gh.owner, gh.repoName)
105-
if resp.StatusCode != 404 && err != nil {
106-
gh.t.Fatal(err.Error())
107-
}
108-
gh.repository = repo
109-
return repo
110-
}
111-
11238
func TestIMCloudBuildWorkspaceGitHub(t *testing.T) {
11339
ctx := context.Background()
11440

11541
githubPAT := cftutils.ValFromEnv(t, "IM_GITHUB_PAT")
116-
client := NewGitHubClient(t, githubPAT, githubOwner, githubRepo)
42+
client := utils.NewGitHubClient(t, githubPAT, githubRepo)
11743
client.GetRepository(ctx)
11844

11945
// Testing the module's feature of appending the ".git" suffix if it's missing
120-
repoURL := strings.TrimSuffix(client.repository.GetCloneURL(), ".git")
46+
repoURL := strings.TrimSuffix(client.Repository.GetCloneURL(), ".git")
12147
vars := map[string]interface{}{
12248
"im_github_pat": githubPAT,
12349
"repository_url": repoURL,
@@ -138,7 +64,7 @@ func TestIMCloudBuildWorkspaceGitHub(t *testing.T) {
13864
projectID := bpt.GetStringOutput("project_id")
13965
secretID := bpt.GetStringOutput("github_secret_id")
14066
triggerLocation := "us-central1"
141-
repoURLSplit := strings.Split(client.repository.GetCloneURL(), "/")
67+
repoURLSplit := strings.Split(client.Repository.GetCloneURL(), "/")
14268

14369
// CB P4SA IAM
14470
projectNum := gcloud.Runf(t, "projects describe %s --format='value(projectNumber)'", projectID).Get("projectNumber")

test/integration/tf_cloudbuild_builder_simple_github/tf_cloudbuild_builder_simple_github_test.go

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,57 +26,27 @@ import (
2626
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
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/v72/github"
3029
"github.com/stretchr/testify/assert"
30+
"github.com/terraform-google-modules/terraform-google-bootstrap/test/integration/utils"
3131
)
3232

3333
const (
34-
githubOwner = "im-goose"
35-
githubRepo = "b-gh-test"
36-
githubAppID = "47590865" // Found in the URL of your Cloud Build GitHub app configuration settings
34+
githubRepo = "b-gh-test"
3735
)
3836

39-
type GitHubClient struct {
40-
t *testing.T
41-
client *github.Client
42-
owner string
43-
repoName string
44-
repository *github.Repository
45-
}
46-
47-
func NewGitHubClient(t *testing.T, token string) *GitHubClient {
48-
t.Helper()
49-
client := github.NewClient(nil).WithAuthToken(token)
50-
return &GitHubClient{
51-
t: t,
52-
client: client,
53-
owner: githubOwner,
54-
repoName: githubRepo,
55-
}
56-
}
57-
58-
func (gh *GitHubClient) GetRepository(ctx context.Context) *github.Repository {
59-
repo, resp, err := gh.client.Repositories.Get(ctx, gh.owner, gh.repoName)
60-
if resp.StatusCode != 404 && err != nil {
61-
gh.t.Fatal(err.Error())
62-
}
63-
gh.repository = repo
64-
return repo
65-
}
66-
6737
func TestTFCloudBuildBuilderGitHub(t *testing.T) {
6838
ctx := context.Background()
6939
githubPAT := cftutils.ValFromEnv(t, "IM_GITHUB_PAT")
70-
client := NewGitHubClient(t, githubPAT)
40+
client := utils.NewGitHubClient(t, githubPAT, githubRepo)
7141

7242
client.GetRepository(ctx)
7343

7444
// Testing the module's feature of appending the ".git" suffix if it's missing
75-
repoURL := strings.TrimSuffix(client.repository.GetCloneURL(), ".git")
45+
repoURL := strings.TrimSuffix(client.Repository.GetCloneURL(), ".git")
7646
vars := map[string]interface{}{
7747
"github_pat": githubPAT,
7848
"repository_uri": repoURL,
79-
"github_app_id": githubAppID,
49+
"github_app_id": utils.GitHubAppID,
8050
}
8151
bpt := tft.NewTFBlueprintTest(t, tft.WithVars(vars))
8252

0 commit comments

Comments
 (0)