@@ -2,6 +2,7 @@ package gitlab
22
33import (
44 "fmt"
5+ "os"
56 "regexp"
67 "testing"
78
@@ -352,6 +353,61 @@ func TestAccGitlabProject_transfer(t *testing.T) {
352353 })
353354}
354355
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+
386+ resource .Test (t , resource.TestCase {
387+ PreCheck : func () { testAccPreCheck (t ) },
388+ Providers : testAccProviders ,
389+ CheckDestroy : testAccCheckGitlabProjectDestroy ,
390+ Steps : []resource.TestStep {
391+ {
392+ Config : testAccGitlabProjectConfigImportURL (rInt , baseProject .HTTPURLToRepo ),
393+ Check : resource .ComposeTestCheckFunc (
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+ },
405+ ),
406+ },
407+ },
408+ })
409+ }
410+
355411func testAccCheckGitlabProjectExists (n string , project * gitlab.Project ) resource.TestCheckFunc {
356412 return func (s * terraform.State ) error {
357413 var err error
@@ -667,3 +723,17 @@ resource "gitlab_project" "foo" {
667723}
668724 ` , rInt , rInt )
669725}
726+
727+ func testAccGitlabProjectConfigImportURL (rInt int , importURL string ) string {
728+ return fmt .Sprintf (`
729+ resource "gitlab_project" "imported" {
730+ name = "imported-%d"
731+ default_branch = "master"
732+ import_url = "%s"
733+
734+ # So that acceptance tests can be run in a gitlab organization
735+ # with no billing
736+ visibility_level = "public"
737+ }
738+ ` , rInt , importURL )
739+ }
0 commit comments