@@ -30,6 +30,12 @@ import (
3030 "github.com/xanzy/go-gitlab"
3131)
3232
33+ const (
34+ gitlabGroup = "infrastructure-manager"
35+ gitlabGroupID = 84326276
36+ gitlabProjectName = "im-cloudbuild-workspace-gitlab"
37+ )
38+
3339type GitLabClient struct {
3440 t * testing.T
3541 client * gitlab.Client
@@ -39,7 +45,7 @@ type GitLabClient struct {
3945 project * gitlab.Project
4046}
4147
42- func NewGitLabClient (t * testing.T , token , owner , repo string ) * GitLabClient {
48+ func NewGitLabClient (t * testing.T , token string ) * GitLabClient {
4349 t .Helper ()
4450 client , err := gitlab .NewClient (token )
4551 if err != nil {
@@ -48,9 +54,9 @@ func NewGitLabClient(t *testing.T, token, owner, repo string) *GitLabClient {
4854 return & GitLabClient {
4955 t : t ,
5056 client : client ,
51- group : "infrastructure-manager" ,
52- namespace : 84326276 ,
53- repo : repo ,
57+ group : gitlabGroup ,
58+ namespace : gitlabGroupID ,
59+ repo : gitlabProjectName ,
5460 }
5561}
5662
@@ -67,41 +73,6 @@ func (gl *GitLabClient) GetProject() *gitlab.Project {
6773 return proj
6874}
6975
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-
10576// GetOpenMergeRequest gets the last opened merge request for a given branch if it exists.
10677func (gl * GitLabClient ) GetOpenMergeRequest (branch string ) * gitlab.MergeRequest {
10778 opts := gitlab.ListProjectMergeRequestsOptions {
@@ -154,13 +125,8 @@ func (gl *GitLabClient) AcceptMergeRequest(mr *gitlab.MergeRequest, commitMessag
154125
155126func TestIMCloudBuildWorkspaceGitLab (t * testing.T ) {
156127 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 ()
164130
165131 vars := map [string ]interface {}{
166132 "im_gitlab_pat" : gitlabPAT ,
@@ -177,10 +143,6 @@ func TestIMCloudBuildWorkspaceGitLab(t *testing.T) {
177143 if mr != nil {
178144 client .CloseMergeRequest (mr )
179145 }
180- // Delete the repository if we hit a failed state
181- if t .Failed () {
182- client .DeleteProject ()
183- }
184146 })
185147
186148 projectID := bpt .GetStringOutput ("project_id" )
@@ -278,7 +240,7 @@ func TestIMCloudBuildWorkspaceGitLab(t *testing.T) {
278240 return true , nil
279241 }
280242 }
281- cftutils .Poll (t , pollCloudBuild (buildListCmd ), 20 , 15 * time .Second )
243+ cftutils .Poll (t , pollCloudBuild (buildListCmd ), 40 , 15 * time .Second )
282244 build := gcloud .Run (t , buildListCmd , gcloud .WithLogger (logger .Discard )).Array ()[0 ]
283245
284246 switch branch {
@@ -293,7 +255,6 @@ func TestIMCloudBuildWorkspaceGitLab(t *testing.T) {
293255 bpt .DefineTeardown (func (assert * assert.Assertions ) {
294256 // Guarantee clean up even if the normal gcloud/teardown run into errors
295257 t .Cleanup (func () {
296- client .DeleteProject ()
297258 bpt .DefaultTeardown (assert )
298259 })
299260 projectID := bpt .GetStringOutput ("project_id" )
0 commit comments