@@ -30,6 +30,12 @@ import (
30
30
"github.com/xanzy/go-gitlab"
31
31
)
32
32
33
+ const (
34
+ gitlabGroup = "infrastructure-manager"
35
+ gitlabGroupID = 84326276
36
+ gitlabProjectName = "im-cloudbuild-workspace-gitlab"
37
+ )
38
+
33
39
type GitLabClient struct {
34
40
t * testing.T
35
41
client * gitlab.Client
@@ -39,7 +45,7 @@ type GitLabClient struct {
39
45
project * gitlab.Project
40
46
}
41
47
42
- func NewGitLabClient (t * testing.T , token , owner , repo string ) * GitLabClient {
48
+ func NewGitLabClient (t * testing.T , token string ) * GitLabClient {
43
49
t .Helper ()
44
50
client , err := gitlab .NewClient (token )
45
51
if err != nil {
@@ -48,9 +54,9 @@ func NewGitLabClient(t *testing.T, token, owner, repo string) *GitLabClient {
48
54
return & GitLabClient {
49
55
t : t ,
50
56
client : client ,
51
- group : "infrastructure-manager" ,
52
- namespace : 84326276 ,
53
- repo : repo ,
57
+ group : gitlabGroup ,
58
+ namespace : gitlabGroupID ,
59
+ repo : gitlabProjectName ,
54
60
}
55
61
}
56
62
@@ -67,41 +73,6 @@ func (gl *GitLabClient) GetProject() *gitlab.Project {
67
73
return proj
68
74
}
69
75
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 (84326276 ),
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 ) AddFileToProject (file []byte ) {
86
- opts := & gitlab.CreateFileOptions {
87
- Branch : gitlab .Ptr ("main" ),
88
- CommitMessage : gitlab .Ptr ("Initial config commit" ),
89
- Content : gitlab .Ptr (string (file )),
90
- }
91
- _ , _ , err := gl .client .RepositoryFiles .CreateFile (gl .ProjectName (), "main.tf" , opts )
92
- if err != nil {
93
- gl .t .Fatal (err .Error ())
94
- }
95
- }
96
-
97
- func (gl * GitLabClient ) DeleteProject () {
98
- resp , err := gl .client .Projects .DeleteProject (gl .ProjectName (), utils .GetDeleteProjectOptions ())
99
- if err != nil {
100
- gl .t .Errorf ("error deleting project with status %s and error %s" , resp .Status , err .Error ())
101
- }
102
- gl .project = nil
103
- }
104
-
105
76
// GetOpenMergeRequest gets the last opened merge request for a given branch if it exists.
106
77
func (gl * GitLabClient ) GetOpenMergeRequest (branch string ) * gitlab.MergeRequest {
107
78
opts := gitlab.ListProjectMergeRequestsOptions {
@@ -154,13 +125,8 @@ func (gl *GitLabClient) AcceptMergeRequest(mr *gitlab.MergeRequest, commitMessag
154
125
155
126
func TestIMCloudBuildWorkspaceGitLab (t * testing.T ) {
156
127
gitlabPAT := cftutils .ValFromEnv (t , "IM_GITLAB_PAT" )
157
- client := NewGitLabClient (t , gitlabPAT , "infrastructure-manager" , fmt .Sprintf ("blueprint-test-%s" , utils .GetRandomStringFromSetup (t )))
158
-
159
- proj := client .GetProject ()
160
- if proj == nil {
161
- client .CreateProject ()
162
- client .AddFileToProject (utils .GetFileContents (t , "files/main.tf" ))
163
- }
128
+ client := NewGitLabClient (t , gitlabPAT )
129
+ client .GetProject ()
164
130
165
131
vars := map [string ]interface {}{
166
132
"im_gitlab_pat" : gitlabPAT ,
@@ -177,10 +143,6 @@ func TestIMCloudBuildWorkspaceGitLab(t *testing.T) {
177
143
if mr != nil {
178
144
client .CloseMergeRequest (mr )
179
145
}
180
- // Delete the repository if we hit a failed state
181
- if t .Failed () {
182
- client .DeleteProject ()
183
- }
184
146
})
185
147
186
148
projectID := bpt .GetStringOutput ("project_id" )
@@ -278,7 +240,7 @@ func TestIMCloudBuildWorkspaceGitLab(t *testing.T) {
278
240
return true , nil
279
241
}
280
242
}
281
- cftutils .Poll (t , pollCloudBuild (buildListCmd ), 20 , 15 * time .Second )
243
+ cftutils .Poll (t , pollCloudBuild (buildListCmd ), 40 , 15 * time .Second )
282
244
build := gcloud .Run (t , buildListCmd , gcloud .WithLogger (logger .Discard )).Array ()[0 ]
283
245
284
246
switch branch {
@@ -293,7 +255,6 @@ func TestIMCloudBuildWorkspaceGitLab(t *testing.T) {
293
255
bpt .DefineTeardown (func (assert * assert.Assertions ) {
294
256
// Guarantee clean up even if the normal gcloud/teardown run into errors
295
257
t .Cleanup (func () {
296
- client .DeleteProject ()
297
258
bpt .DefaultTeardown (assert )
298
259
})
299
260
projectID := bpt .GetStringOutput ("project_id" )
0 commit comments