Skip to content

Commit f8886c4

Browse files
josephdt12apeabody
andauthored
chore: Update test to use existing GitLab project (#371)
Co-authored-by: Andrew Peabody <[email protected]>
1 parent 40825ef commit f8886c4

File tree

3 files changed

+131
-80
lines changed

3 files changed

+131
-80
lines changed

examples/tf_cloudbuild_builder_simple_gitlab/scripts/push-to-repo.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,14 @@ git config user.email "[email protected]"
4343
git config user.name "TF Robot"
4444
git checkout main || git checkout -b main
4545
git add Dockerfile
46-
git commit -m "init tf dockerfile"
47-
git push origin main -f
46+
47+
# The '-z' flag checks if the following string is empty.
48+
if [ -z "$(git status --porcelain)" ]; then
49+
# If the output is empty, the working directory is clean.
50+
echo "No changes to commit. Nothing to do."
51+
else
52+
# If there is output, changes exist, so we commit.
53+
echo "Changes detected. Attempting to commit..."
54+
git commit -m "init tf dockerfile"
55+
git push origin main -f
56+
fi

test/integration/tf_cloudbuild_builder_simple_gitlab/tf_cloudbuild_builder_simple_gitlab_test.go

Lines changed: 6 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -27,100 +27,29 @@ import (
2727
cftutils "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
2828
"github.com/stretchr/testify/assert"
2929
"github.com/terraform-google-modules/terraform-google-bootstrap/test/integration/utils"
30-
"github.com/xanzy/go-gitlab"
3130
)
3231

33-
type GitLabClient struct {
34-
t *testing.T
35-
client *gitlab.Client
36-
group string
37-
namespace int
38-
repo string
39-
project *gitlab.Project
40-
}
41-
42-
func NewGitLabClient(t *testing.T, token, owner, repo string) *GitLabClient {
43-
t.Helper()
44-
client, err := gitlab.NewClient(token)
45-
if err != nil {
46-
t.Fatal(err.Error())
47-
}
48-
return &GitLabClient{
49-
t: t,
50-
client: client,
51-
group: "infrastructure-manager",
52-
namespace: 84326276,
53-
repo: repo,
54-
}
55-
}
56-
57-
func (gl *GitLabClient) ProjectName() string {
58-
return fmt.Sprintf("%s/%s", gl.group, gl.repo)
59-
}
60-
61-
func (gl *GitLabClient) GetProject() *gitlab.Project {
62-
proj, resp, err := gl.client.Projects.GetProject(gl.ProjectName(), nil)
63-
if resp.StatusCode != 404 && err != nil {
64-
gl.t.Fatalf("got status code %d, error %s", resp.StatusCode, err.Error())
65-
}
66-
gl.project = proj
67-
return proj
68-
}
69-
70-
func (gl *GitLabClient) CreateProject() {
71-
opts := &gitlab.CreateProjectOptions{
72-
Name: gitlab.Ptr(gl.repo),
73-
// ID of the the Infrastructure Manager group (gitlab.com/infrastructure-manager)
74-
NamespaceID: gitlab.Ptr(gl.namespace),
75-
// Required otherwise Cloud Build errors on creating the connection
76-
InitializeWithReadme: gitlab.Ptr(true),
77-
}
78-
proj, _, err := gl.client.Projects.CreateProject(opts)
79-
if err != nil {
80-
gl.t.Fatal(err.Error())
81-
}
82-
gl.project = proj
83-
}
84-
85-
func (gl *GitLabClient) DeleteProject() {
86-
resp, err := gl.client.Projects.DeleteProject(gl.ProjectName(), utils.GetDeleteProjectOptions())
87-
if resp.StatusCode != 404 && err != nil {
88-
gl.t.Errorf("error deleting project with status %s and error %s", resp.Status, err.Error())
89-
}
90-
gl.project = nil
91-
}
32+
const (
33+
gitlabProjectName = "b-gl-test"
34+
)
9235

9336
func TestTFCloudBuildBuilderGitLab(t *testing.T) {
9437
gitlabPAT := cftutils.ValFromEnv(t, "IM_GITLAB_PAT")
95-
owner := "im-goose"
96-
repoName := fmt.Sprintf("b-gl-test-%s", utils.GetRandomStringFromSetup(t))
97-
98-
client := NewGitLabClient(t, gitlabPAT, owner, repoName)
99-
100-
proj := client.GetProject()
101-
if proj == nil {
102-
client.CreateProject()
103-
}
38+
client := utils.NewGitLabClient(t, gitlabPAT, gitlabProjectName)
39+
client.GetProject()
10440

10541
// Testing the module's feature of appending the ".git" suffix if it's missing
10642
// repoURL := strings.TrimSuffix(client.repository.GetCloneURL(), ".git")
10743
vars := map[string]interface{}{
10844
"gitlab_authorizer_credential": gitlabPAT,
10945
"gitlab_read_authorizer_credential": gitlabPAT,
110-
"repository_uri": client.project.HTTPURLToRepo,
46+
"repository_uri": client.Project.HTTPURLToRepo,
11147
}
11248
bpt := tft.NewTFBlueprintTest(t, tft.WithVars(vars))
11349

11450
bpt.DefineVerify(func(assert *assert.Assertions) {
11551
bpt.DefaultVerify(assert)
11652

117-
t.Cleanup(func() {
118-
// Delete the repository if we hit a failed state
119-
if t.Failed() {
120-
client.DeleteProject()
121-
}
122-
})
123-
12453
location := bpt.GetStringOutput("location")
12554
projectID := bpt.GetStringOutput("project_id")
12655
artifactRepo := bpt.GetStringOutput("artifact_repo")
@@ -219,7 +148,6 @@ func TestTFCloudBuildBuilderGitLab(t *testing.T) {
219148
bpt.DefineTeardown(func(assert *assert.Assertions) {
220149
// Guarantee clean up even if the normal gcloud/teardown run into errors
221150
t.Cleanup(func() {
222-
client.DeleteProject()
223151
bpt.DefaultTeardown(assert)
224152
})
225153
})
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package utils
16+
17+
import (
18+
"fmt"
19+
"testing"
20+
21+
"github.com/xanzy/go-gitlab"
22+
)
23+
24+
const (
25+
gitlabGroup = "infrastructure-manager"
26+
gitlabGroupID = 84326276
27+
)
28+
29+
type GitLabClient struct {
30+
t *testing.T
31+
client *gitlab.Client
32+
group string
33+
namespace int
34+
repo string
35+
Project *gitlab.Project
36+
}
37+
38+
func NewGitLabClient(t *testing.T, token, projectName string) *GitLabClient {
39+
t.Helper()
40+
client, err := gitlab.NewClient(token)
41+
if err != nil {
42+
t.Fatal(err.Error())
43+
}
44+
return &GitLabClient{
45+
t: t,
46+
client: client,
47+
group: gitlabGroup,
48+
namespace: gitlabGroupID,
49+
repo: projectName,
50+
}
51+
}
52+
53+
func (gl *GitLabClient) ProjectName() string {
54+
return fmt.Sprintf("%s/%s", gl.group, gl.repo)
55+
}
56+
57+
func (gl *GitLabClient) GetProject() *gitlab.Project {
58+
proj, resp, err := gl.client.Projects.GetProject(gl.ProjectName(), nil)
59+
if resp.StatusCode != 404 && err != nil {
60+
gl.t.Fatalf("got status code %d, error %s", resp.StatusCode, err.Error())
61+
}
62+
gl.Project = proj
63+
return proj
64+
}
65+
66+
// GetOpenMergeRequest gets the last opened merge request for a given branch if it exists.
67+
func (gl *GitLabClient) GetOpenMergeRequest(branch string) *gitlab.MergeRequest {
68+
opts := gitlab.ListProjectMergeRequestsOptions{
69+
State: gitlab.Ptr("opened"),
70+
SourceBranch: gitlab.Ptr(branch),
71+
}
72+
mergeRequests, _, err := gl.client.MergeRequests.ListProjectMergeRequests(gl.ProjectName(), &opts)
73+
if err != nil {
74+
gl.t.Fatal(err.Error())
75+
}
76+
if len(mergeRequests) == 0 {
77+
return nil
78+
}
79+
return mergeRequests[len(mergeRequests)-1]
80+
}
81+
82+
func (gl *GitLabClient) CreateMergeRequest(title, branch, base string) *gitlab.MergeRequest {
83+
opts := gitlab.CreateMergeRequestOptions{
84+
Title: gitlab.Ptr(title),
85+
SourceBranch: gitlab.Ptr(branch),
86+
TargetBranch: gitlab.Ptr(base),
87+
}
88+
mergeRequest, _, err := gl.client.MergeRequests.CreateMergeRequest(gl.ProjectName(), &opts)
89+
if err != nil {
90+
gl.t.Fatal(err.Error())
91+
}
92+
return mergeRequest
93+
}
94+
95+
func (gl *GitLabClient) CloseMergeRequest(mr *gitlab.MergeRequest) {
96+
_, err := gl.client.MergeRequests.DeleteMergeRequest(gl.ProjectName(), mr.IID)
97+
if err != nil {
98+
gl.t.Fatal(err.Error())
99+
}
100+
}
101+
102+
func (gl *GitLabClient) AcceptMergeRequest(mr *gitlab.MergeRequest, commitMessage string) *gitlab.MergeRequest {
103+
opts := gitlab.AcceptMergeRequestOptions{
104+
ShouldRemoveSourceBranch: gitlab.Ptr(true),
105+
}
106+
merged, resp, err := gl.client.MergeRequests.AcceptMergeRequest(gl.ProjectName(), mr.IID, &opts)
107+
if err != nil {
108+
gl.t.Fatal(err.Error())
109+
}
110+
if resp.StatusCode != 200 {
111+
gl.t.Fatalf("failed to accept merge request %v", resp)
112+
}
113+
return merged
114+
}

0 commit comments

Comments
 (0)