Skip to content

Commit ca8a3f7

Browse files
committed
PR feedback and improvements to the new gitlab_project import_url attribute
Fix import_url not being set in properties Use StateChangeConf instead of for loop Make the importURL test less brittle by importing a local repository Add import_url to docs Make import_url force new
1 parent 3291f33 commit ca8a3f7

File tree

3 files changed

+79
-55
lines changed

3 files changed

+79
-55
lines changed

gitlab/resource_gitlab_project.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
6262
return old == new
6363
},
6464
},
65+
"import_url": {
66+
Type: schema.TypeString,
67+
Optional: true,
68+
ForceNew: true,
69+
},
6570
"request_access_enabled": {
6671
Type: schema.TypeBool,
6772
Optional: true,
@@ -113,10 +118,6 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
113118
ValidateFunc: validation.StringInSlice([]string{"private", "internal", "public"}, true),
114119
Default: "private",
115120
},
116-
"import_url": {
117-
Type: schema.TypeString,
118-
Optional: true,
119-
},
120121
"merge_method": {
121122
Type: schema.TypeString,
122123
Optional: true,
@@ -315,6 +316,7 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
315316

316317
if v, ok := d.GetOk("import_url"); ok {
317318
options.ImportURL = gitlab.String(v.(string))
319+
setProperties = append(setProperties, "import_url")
318320
}
319321

320322
log.Printf("[DEBUG] create gitlab project %q", *options.Name)
@@ -333,6 +335,28 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
333335
// is committed to state since we set its ID
334336
d.SetId(fmt.Sprintf("%d", project.ID))
335337

338+
if _, ok := d.GetOk("import_url"); ok {
339+
log.Printf("[DEBUG] waiting for project %q import to finish", *options.Name)
340+
341+
stateConf := &resource.StateChangeConf{
342+
Pending: []string{"scheduled", "started"},
343+
Target: []string{"finished"},
344+
Timeout: time.Minute,
345+
Refresh: func() (interface{}, string, error) {
346+
status, _, err := client.ProjectImportExport.ImportStatus(d.Id())
347+
if err != nil {
348+
return nil, "", err
349+
}
350+
351+
return status, status.ImportStatus, nil
352+
},
353+
}
354+
355+
if _, err := stateConf.WaitForState(); err != nil {
356+
return fmt.Errorf("error while waiting for project %q import to finish: %w", *options.Name, err)
357+
}
358+
}
359+
336360
if v, ok := d.GetOk("shared_with_groups"); ok {
337361
for _, option := range expandSharedWithGroupsOptions(v) {
338362
if _, err := client.Projects.ShareProjectWithGroup(project.ID, option); err != nil {
@@ -360,7 +384,6 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
360384
}
361385

362386
func resourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error {
363-
var project *gitlab.Project
364387
client := meta.(*gitlab.Client)
365388
log.Printf("[DEBUG] read gitlab project %s", d.Id())
366389

@@ -374,13 +397,6 @@ func resourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error {
374397
return nil
375398
}
376399

377-
for project.ImportStatus == "started" {
378-
project, _, err = client.Projects.GetProject(d.Id(), nil)
379-
if err != nil {
380-
return err
381-
}
382-
}
383-
384400
resourceGitlabProjectSetToState(d, project)
385401
return nil
386402
}

gitlab/resource_gitlab_project_test.go

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitlab
22

33
import (
44
"fmt"
5+
"os"
56
"regexp"
67
"testing"
78

@@ -352,18 +353,55 @@ func TestAccGitlabProject_transfer(t *testing.T) {
352353
})
353354
}
354355

355-
func TestAccImportURL(t *testing.T) {
356-
var received gitlab.Project
356+
func TestAccGitlabProject_importURL(t *testing.T) {
357+
// Since we do some manual setup in this test, we need to handle the test skip first.
358+
if os.Getenv(resource.TestEnvVar) == "" {
359+
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", resource.TestEnvVar))
360+
}
361+
362+
client := testAccProvider.Meta().(*gitlab.Client)
363+
rInt := acctest.RandInt()
364+
365+
// Create a base project for importing.
366+
baseProject, _, err := client.Projects.CreateProject(&gitlab.CreateProjectOptions{
367+
Name: gitlab.String(fmt.Sprintf("base-%d", rInt)),
368+
Visibility: gitlab.Visibility(gitlab.PublicVisibility),
369+
})
370+
if err != nil {
371+
t.Fatalf("failed to create base project: %v", err)
372+
}
373+
374+
defer client.Projects.DeleteProject(baseProject.ID)
375+
376+
// Add a file to the base project, for later verifying the import.
377+
_, _, err = client.RepositoryFiles.CreateFile(baseProject.ID, "foo.txt", &gitlab.CreateFileOptions{
378+
Branch: gitlab.String("master"),
379+
CommitMessage: gitlab.String("add file"),
380+
Content: gitlab.String(""),
381+
})
382+
if err != nil {
383+
t.Fatalf("failed to commit file to base project: %v", err)
384+
}
385+
357386
resource.Test(t, resource.TestCase{
358387
PreCheck: func() { testAccPreCheck(t) },
359388
Providers: testAccProviders,
360389
CheckDestroy: testAccCheckGitlabProjectDestroy,
361390
Steps: []resource.TestStep{
362391
{
363-
Config: testImportURLOptions(),
392+
Config: testAccGitlabProjectConfigImportURL(rInt, baseProject.HTTPURLToRepo),
364393
Check: resource.ComposeTestCheckFunc(
365-
testAccCheckGitlabProjectExists("gitlab_project.import_url", &received),
366-
testAccCheckImportURL("gitlab/resource_gitlab_project.go", &received),
394+
resource.TestCheckResourceAttr("gitlab_project.imported", "import_url", baseProject.HTTPURLToRepo),
395+
func(state *terraform.State) error {
396+
projectID := state.RootModule().Resources["gitlab_project.imported"].Primary.ID
397+
398+
_, _, err := client.RepositoryFiles.GetFile(projectID, "foo.txt", &gitlab.GetFileOptions{Ref: gitlab.String("master")}, nil)
399+
if err != nil {
400+
return fmt.Errorf("failed to get file from imported project: %w", err)
401+
}
402+
403+
return nil
404+
},
367405
),
368406
},
369407
},
@@ -686,48 +724,16 @@ resource "gitlab_project" "foo" {
686724
`, rInt, rInt)
687725
}
688726

689-
func testImportURLOptions() string {
727+
func testAccGitlabProjectConfigImportURL(rInt int, importURL string) string {
690728
return fmt.Sprintf(`
691-
resource "gitlab_project" "import_url" {
692-
name = "import"
693-
path = "import"
694-
description = "Terraform Import URL Acceptance test"
729+
resource "gitlab_project" "imported" {
730+
name = "imported-%d"
731+
default_branch = "master"
732+
import_url = "%s"
695733
696734
# So that acceptance tests can be run in a gitlab organization
697735
# with no billing
698736
visibility_level = "public"
699-
merge_method = "ff"
700-
only_allow_merge_if_pipeline_succeeds = true
701-
only_allow_merge_if_all_discussions_are_resolved = true
702-
703-
issues_enabled = false
704-
merge_requests_enabled = false
705-
approvals_before_merge = 0
706-
wiki_enabled = false
707-
snippets_enabled = false
708-
container_registry_enabled = false
709-
shared_runners_enabled = false
710-
archived = false
711-
import_url = "https://github.com/terraform-providers/terraform-provider-gitlab.git"
712-
default_branch = "master"
713737
}
714-
`)
715-
}
716-
717-
func testAccCheckImportURL(fp string, project *gitlab.Project) resource.TestCheckFunc {
718-
return func(s *terraform.State) error {
719-
conn := testAccProvider.Meta().(*gitlab.Client)
720-
ref := &gitlab.GetFileOptions{
721-
Ref: gitlab.String("master"),
722-
}
723-
f, _, err := conn.RepositoryFiles.GetFile(project.ID, fp, ref, nil)
724-
if err != nil {
725-
return fmt.Errorf("Cannot find file %s, error %s\n", fp, err)
726-
}
727-
if f == nil {
728-
return fmt.Errorf("Did not find file\n")
729-
}
730-
731-
return nil
732-
}
738+
`, rInt, importURL)
733739
}

website/docs/r/project.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ The following arguments are supported:
4040

4141
* `default_branch` - (Optional) The default branch for the project.
4242

43+
* `import_url` - (Optional) Git URL to a repository to be imported.
44+
4345
* `request_access_enabled` - Allow users to request member access.
4446

4547
* `issues_enabled` - (Optional) Enable issue tracking for the project.

0 commit comments

Comments
 (0)