@@ -509,6 +509,113 @@ func TestAccGitlabProject_importURL(t *testing.T) {
509509 })
510510}
511511
512+ type testAccGitlabProjectMirroredExpectedAttributes struct {
513+ Mirror bool
514+ MirrorTriggerBuilds bool
515+ }
516+
517+ func testAccCheckGitlabProjectMirroredAttributes (project * gitlab.Project , want * testAccGitlabProjectMirroredExpectedAttributes ) resource.TestCheckFunc {
518+ return func (s * terraform.State ) error {
519+ if project .Mirror != want .Mirror {
520+ return fmt .Errorf ("got mirror %t; want %t" , project .Mirror , want .Mirror )
521+ }
522+
523+ if project .MirrorTriggerBuilds != want .MirrorTriggerBuilds {
524+ return fmt .Errorf ("got mirror_trigger_builds %t; want %t" , project .MirrorTriggerBuilds , want .MirrorTriggerBuilds )
525+ }
526+ return nil
527+ }
528+ }
529+
530+ func TestAccGitlabProject_importURLMirrored (t * testing.T ) {
531+ // Since we do some manual setup in this test, we need to handle the test skip first.
532+ if os .Getenv (resource .TestEnvVar ) == "" {
533+ t .Skip (fmt .Sprintf ("Acceptance tests skipped unless env '%s' set" , resource .TestEnvVar ))
534+ }
535+
536+ client := testAccProvider .Meta ().(* gitlab.Client )
537+ var mirror gitlab.Project
538+ rInt := acctest .RandInt ()
539+
540+ // Create a base project for importing.
541+ baseProject , _ , err := client .Projects .CreateProject (& gitlab.CreateProjectOptions {
542+ Name : gitlab .String (fmt .Sprintf ("base-%d" , rInt )),
543+ Visibility : gitlab .Visibility (gitlab .PublicVisibility ),
544+ })
545+ if err != nil {
546+ t .Fatalf ("failed to create base project: %v" , err )
547+ }
548+
549+ defer client .Projects .DeleteProject (baseProject .ID )
550+
551+ // Add a file to the base project, for later verifying the import.
552+ _ , _ , err = client .RepositoryFiles .CreateFile (baseProject .ID , "foo.txt" , & gitlab.CreateFileOptions {
553+ Branch : gitlab .String ("master" ),
554+ CommitMessage : gitlab .String ("add file" ),
555+ Content : gitlab .String ("" ),
556+ })
557+ if err != nil {
558+ t .Fatalf ("failed to commit file to base project: %v" , err )
559+ }
560+
561+ resource .Test (t , resource.TestCase {
562+ PreCheck : func () { testAccPreCheck (t ) },
563+ Providers : testAccProviders ,
564+ CheckDestroy : testAccCheckGitlabProjectDestroy ,
565+ Steps : []resource.TestStep {
566+ {
567+ // First, import, as mirrored
568+ Config : testAccGitlabProjectConfigImportURLMirror (rInt , baseProject .HTTPURLToRepo ),
569+ SkipFunc : isRunningInCE ,
570+ Check : resource .ComposeTestCheckFunc (
571+ testAccCheckGitlabProjectExists ("gitlab_project.imported" , & mirror ),
572+ resource .TestCheckResourceAttr ("gitlab_project.imported" , "import_url" , baseProject .HTTPURLToRepo ),
573+ testAccCheckGitlabProjectMirroredAttributes (& mirror , & testAccGitlabProjectMirroredExpectedAttributes {
574+ Mirror : true ,
575+ MirrorTriggerBuilds : true ,
576+ }),
577+
578+ func (state * terraform.State ) error {
579+ projectID := state .RootModule ().Resources ["gitlab_project.imported" ].Primary .ID
580+
581+ _ , _ , err := client .RepositoryFiles .GetFile (projectID , "foo.txt" , & gitlab.GetFileOptions {Ref : gitlab .String ("master" )}, nil )
582+ if err != nil {
583+ return fmt .Errorf ("failed to get file from imported project: %w" , err )
584+ }
585+
586+ return nil
587+ },
588+ ),
589+ },
590+ {
591+ // Second, disable mirroring, using the original ImportURL acceptance test
592+ Config : testAccGitlabProjectConfigImportURLMirrorDisabled (rInt , baseProject .HTTPURLToRepo ),
593+ SkipFunc : isRunningInCE ,
594+ Check : resource .ComposeTestCheckFunc (
595+ testAccCheckGitlabProjectExists ("gitlab_project.imported" , & mirror ),
596+ resource .TestCheckResourceAttr ("gitlab_project.imported" , "import_url" , baseProject .HTTPURLToRepo ),
597+ testAccCheckGitlabProjectMirroredAttributes (& mirror , & testAccGitlabProjectMirroredExpectedAttributes {
598+ Mirror : false ,
599+ MirrorTriggerBuilds : false ,
600+ }),
601+
602+ // Ensure the test file still is as expected
603+ func (state * terraform.State ) error {
604+ projectID := state .RootModule ().Resources ["gitlab_project.imported" ].Primary .ID
605+
606+ _ , _ , err := client .RepositoryFiles .GetFile (projectID , "foo.txt" , & gitlab.GetFileOptions {Ref : gitlab .String ("master" )}, nil )
607+ if err != nil {
608+ return fmt .Errorf ("failed to get file from imported project: %w" , err )
609+ }
610+
611+ return nil
612+ },
613+ ),
614+ },
615+ },
616+ })
617+ }
618+
512619func TestAccGitlabProjec_templateMutualExclusiveNameAndID (t * testing.T ) {
513620 rInt := acctest .RandInt ()
514621
@@ -838,6 +945,38 @@ resource "gitlab_project" "imported" {
838945` , rInt , importURL )
839946}
840947
948+ func testAccGitlabProjectConfigImportURLMirror (rInt int , importURL string ) string {
949+ return fmt .Sprintf (`
950+ resource "gitlab_project" "imported" {
951+ name = "imported-%d"
952+ default_branch = "master"
953+ import_url = "%s"
954+ mirror = true
955+ mirror_trigger_builds = true
956+
957+ # So that acceptance tests can be run in a gitlab organization
958+ # with no billing
959+ visibility_level = "public"
960+ }
961+ ` , rInt , importURL )
962+ }
963+
964+ func testAccGitlabProjectConfigImportURLMirrorDisabled (rInt int , importURL string ) string {
965+ return fmt .Sprintf (`
966+ resource "gitlab_project" "imported" {
967+ name = "imported-%d"
968+ default_branch = "master"
969+ import_url = "%s"
970+ mirror = false
971+ mirror_trigger_builds = false
972+
973+ # So that acceptance tests can be run in a gitlab organization
974+ # with no billing
975+ visibility_level = "public"
976+ }
977+ ` , rInt , importURL )
978+ }
979+
841980func testAccGitlabProjectConfigPushRules (rInt int , pushRules string ) string {
842981 return fmt .Sprintf (`
843982resource "gitlab_project" "foo" {
0 commit comments