Skip to content

Commit c4a070b

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

File tree

2 files changed

+7
-135
lines changed

2 files changed

+7
-135
lines changed

test/integration/cloudbuild_repo_connection_gitlab/cloudbuild_repo_connection_gitlab_test.go

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,63 +24,24 @@ import (
2424
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
2525
cftutils "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
2626
"github.com/stretchr/testify/assert"
27-
"github.com/xanzy/go-gitlab"
27+
"github.com/terraform-google-modules/terraform-google-bootstrap/test/integration/utils"
2828
)
2929

3030
const (
3131
gitlabProjectName = "cb-repo-conn-gl"
32-
gitlabGroup = "infrastructure-manager"
33-
gitlabGroupID = 84326276
3432
)
3533

36-
type GitLabClient struct {
37-
t *testing.T
38-
client *gitlab.Client
39-
group string
40-
namespace int
41-
repo string
42-
project *gitlab.Project
43-
}
44-
45-
func NewGitLabClient(t *testing.T, token string) *GitLabClient {
46-
t.Helper()
47-
client, err := gitlab.NewClient(token)
48-
if err != nil {
49-
t.Fatal(err.Error())
50-
}
51-
return &GitLabClient{
52-
t: t,
53-
client: client,
54-
group: gitlabGroup,
55-
namespace: gitlabGroupID,
56-
repo: gitlabProjectName,
57-
}
58-
}
59-
60-
func (gl *GitLabClient) ProjectName() string {
61-
return fmt.Sprintf("%s/%s", gl.group, gl.repo)
62-
}
63-
64-
func (gl *GitLabClient) GetProject() *gitlab.Project {
65-
proj, resp, err := gl.client.Projects.GetProject(gl.ProjectName(), nil)
66-
if resp.StatusCode != 404 && err != nil {
67-
gl.t.Fatalf("got status code %d, error %s", resp.StatusCode, err.Error())
68-
}
69-
gl.project = proj
70-
return proj
71-
}
72-
7334
func TestCloudBuildRepoConnectionGitLab(t *testing.T) {
7435
gitlabPAT := cftutils.ValFromEnv(t, "IM_GITLAB_PAT")
75-
client := NewGitLabClient(t, gitlabPAT)
36+
client := utils.NewGitLabClient(t, gitlabPAT, gitlabProjectName)
7637
client.GetProject()
7738

7839
resourcesLocation := "us-central1"
7940
vars := map[string]interface{}{
8041
"gitlab_read_authorizer_credential": gitlabPAT,
8142
"gitlab_authorizer_credential": gitlabPAT,
8243
"repository_name": gitlabProjectName,
83-
"repository_url": client.project.HTTPURLToRepo,
44+
"repository_url": client.Project.HTTPURLToRepo,
8445
}
8546
bpt := tft.NewTFBlueprintTest(t, tft.WithVars(vars))
8647

@@ -107,7 +68,7 @@ func TestCloudBuildRepoConnectionGitLab(t *testing.T) {
10768

10869
repository := gcloud.Runf(t, "builds repositories describe %s --project %s --region %s --connection %s", gitlabProjectName, projectId, resourcesLocation, connectionName)
10970

110-
assert.Equal(client.project.HTTPURLToRepo, repository.Get("remoteUri").String(), "Git clone URL must be the same on the created resource.")
71+
assert.Equal(client.Project.HTTPURLToRepo, repository.Get("remoteUri").String(), "Git clone URL must be the same on the created resource.")
11172
})
11273

11374
bpt.DefineTeardown(func(assert *assert.Assertions) {

test/integration/im_cloudbuild_workspace_gitlab/im_cloudbuild_workspace_gitlab_test.go

Lines changed: 3 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -31,106 +31,17 @@ import (
3131
)
3232

3333
const (
34-
gitlabGroup = "infrastructure-manager"
35-
gitlabGroupID = 84326276
3634
gitlabProjectName = "im-cloudbuild-workspace-gitlab"
3735
)
3836

39-
type GitLabClient struct {
40-
t *testing.T
41-
client *gitlab.Client
42-
group string
43-
namespace int
44-
repo string
45-
project *gitlab.Project
46-
}
47-
48-
func NewGitLabClient(t *testing.T, token string) *GitLabClient {
49-
t.Helper()
50-
client, err := gitlab.NewClient(token)
51-
if err != nil {
52-
t.Fatal(err.Error())
53-
}
54-
return &GitLabClient{
55-
t: t,
56-
client: client,
57-
group: gitlabGroup,
58-
namespace: gitlabGroupID,
59-
repo: gitlabProjectName,
60-
}
61-
}
62-
63-
func (gl *GitLabClient) ProjectName() string {
64-
return fmt.Sprintf("%s/%s", gl.group, gl.repo)
65-
}
66-
67-
func (gl *GitLabClient) GetProject() *gitlab.Project {
68-
proj, resp, err := gl.client.Projects.GetProject(gl.ProjectName(), nil)
69-
if resp.StatusCode != 404 && err != nil {
70-
gl.t.Fatalf("got status code %d, error %s", resp.StatusCode, err.Error())
71-
}
72-
gl.project = proj
73-
return proj
74-
}
75-
76-
// GetOpenMergeRequest gets the last opened merge request for a given branch if it exists.
77-
func (gl *GitLabClient) GetOpenMergeRequest(branch string) *gitlab.MergeRequest {
78-
opts := gitlab.ListProjectMergeRequestsOptions{
79-
State: gitlab.Ptr("opened"),
80-
SourceBranch: gitlab.Ptr(branch),
81-
}
82-
mergeRequests, _, err := gl.client.MergeRequests.ListProjectMergeRequests(gl.ProjectName(), &opts)
83-
if err != nil {
84-
gl.t.Fatal(err.Error())
85-
}
86-
if len(mergeRequests) == 0 {
87-
return nil
88-
}
89-
return mergeRequests[len(mergeRequests)-1]
90-
}
91-
92-
func (gl *GitLabClient) CreateMergeRequest(title, branch, base string) *gitlab.MergeRequest {
93-
opts := gitlab.CreateMergeRequestOptions{
94-
Title: gitlab.Ptr(title),
95-
SourceBranch: gitlab.Ptr(branch),
96-
TargetBranch: gitlab.Ptr(base),
97-
}
98-
mergeRequest, _, err := gl.client.MergeRequests.CreateMergeRequest(gl.ProjectName(), &opts)
99-
if err != nil {
100-
gl.t.Fatal(err.Error())
101-
}
102-
return mergeRequest
103-
}
104-
105-
func (gl *GitLabClient) CloseMergeRequest(mr *gitlab.MergeRequest) {
106-
_, err := gl.client.MergeRequests.DeleteMergeRequest(gl.ProjectName(), mr.IID)
107-
if err != nil {
108-
gl.t.Fatal(err.Error())
109-
}
110-
}
111-
112-
func (gl *GitLabClient) AcceptMergeRequest(mr *gitlab.MergeRequest, commitMessage string) *gitlab.MergeRequest {
113-
opts := gitlab.AcceptMergeRequestOptions{
114-
ShouldRemoveSourceBranch: gitlab.Ptr(true),
115-
}
116-
merged, resp, err := gl.client.MergeRequests.AcceptMergeRequest(gl.ProjectName(), mr.IID, &opts)
117-
if err != nil {
118-
gl.t.Fatal(err.Error())
119-
}
120-
if resp.StatusCode != 200 {
121-
gl.t.Fatalf("failed to accept merge request %v", resp)
122-
}
123-
return merged
124-
}
125-
12637
func TestIMCloudBuildWorkspaceGitLab(t *testing.T) {
12738
gitlabPAT := cftutils.ValFromEnv(t, "IM_GITLAB_PAT")
128-
client := NewGitLabClient(t, gitlabPAT)
39+
client := utils.NewGitLabClient(t, gitlabPAT, gitlabProjectName)
12940
client.GetProject()
13041

13142
vars := map[string]interface{}{
13243
"im_gitlab_pat": gitlabPAT,
133-
"repository_url": client.project.HTTPURLToRepo,
44+
"repository_url": client.Project.HTTPURLToRepo,
13445
}
13546
bpt := tft.NewTFBlueprintTest(t, tft.WithVars(vars))
13647

@@ -149,7 +60,7 @@ func TestIMCloudBuildWorkspaceGitLab(t *testing.T) {
14960
apiSecretID := bpt.GetStringOutput("gitlab_api_secret_id")
15061
readApiSecretID := bpt.GetStringOutput("gitlab_read_api_secret_id")
15162
triggerLocation := "us-central1"
152-
repoURLSplit := strings.Split(client.project.HTTPURLToRepo, "/")
63+
repoURLSplit := strings.Split(client.Project.HTTPURLToRepo, "/")
15364

15465
// CB P4SA IAM for the two secrets
15566
projectNum := gcloud.Runf(t, "projects describe %s --format='value(projectNumber)'", projectID).Get("projectNumber")

0 commit comments

Comments
 (0)