Skip to content

Commit ad8edb5

Browse files
committed
Add optional mirror options
This adds support for setting mirror_overwrites_diverged_branches and only_mirror_protected_branches.
1 parent 18e9201 commit ad8edb5

File tree

3 files changed

+104
-7
lines changed

3 files changed

+104
-7
lines changed

docs/resources/project.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ consult the [gitlab documentation](https://docs.gitlab.com/ee/user/project/repos
3939

4040
* `mirror_trigger_builds` (Optional) Pull mirroring triggers builds. Default is `false`.
4141

42+
* `mirror_overwrites_diverged_branches` (Optional) Pull mirror overwrites diverged branches.
43+
44+
* `only_mirror_protected_branches` (Optional) Only mirror protected branches.
45+
4246
* `request_access_enabled` - Allow users to request member access.
4347

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

gitlab/resource_gitlab_project.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
285285
Optional: true,
286286
Default: false,
287287
},
288+
"mirror_overwrites_diverged_branches": {
289+
Type: schema.TypeBool,
290+
Optional: true,
291+
Default: false,
292+
},
293+
"only_mirror_protected_branches": {
294+
Type: schema.TypeBool,
295+
Optional: true,
296+
Default: false,
297+
},
288298
}
289299

290300
func resourceGitlabProject() *schema.Resource {
@@ -333,6 +343,8 @@ func resourceGitlabProjectSetToState(d *schema.ResourceData, project *gitlab.Pro
333343
d.Set("pages_access_level", string(project.PagesAccessLevel))
334344
d.Set("mirror", project.Mirror)
335345
d.Set("mirror_trigger_builds", project.MirrorTriggerBuilds)
346+
d.Set("mirror_overwrites_diverged_branches", project.MirrorOverwritesDivergedBranches)
347+
d.Set("only_mirror_protected_branches", project.OnlyMirrorProtectedBranches)
336348
}
337349

338350
func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error {
@@ -456,6 +468,10 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
456468
}
457469
}
458470

471+
// Some project settings can't be set in the Project Create API and have to
472+
// set in a second call after project creation.
473+
resourceGitlabProjectUpdate(d, meta)
474+
459475
return resourceGitlabProjectRead(d, meta)
460476
}
461477

@@ -602,6 +618,20 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error
602618
options.MirrorTriggerBuilds = gitlab.Bool(d.Get("mirror_trigger_builds").(bool))
603619
}
604620

621+
if d.HasChange("only_mirror_protected_branches") {
622+
// It appears that GitLab API requires that import_url is also set when `only_mirror_protected_branches` is updated/changed
623+
// Ref: https://github.com/gitlabhq/terraform-provider-gitlab/pull/449#discussion_r549729230
624+
options.ImportURL = gitlab.String(d.Get("import_url").(string))
625+
options.OnlyMirrorProtectedBranches = gitlab.Bool(d.Get("only_mirror_protected_branches").(bool))
626+
}
627+
628+
if d.HasChange("mirror_overwrites_diverged_branches") {
629+
// It appears that GitLab API requires that import_url is also set when `mirror_overwrites_diverged_branches` is updated/changed
630+
// Ref: https://github.com/gitlabhq/terraform-provider-gitlab/pull/449#discussion_r549729230
631+
options.ImportURL = gitlab.String(d.Get("import_url").(string))
632+
options.MirrorOverwritesDivergedBranches = gitlab.Bool(d.Get("mirror_overwrites_diverged_branches").(bool))
633+
}
634+
605635
if *options != (gitlab.EditProjectOptions{}) {
606636
log.Printf("[DEBUG] update gitlab project %s", d.Id())
607637
_, _, err := client.Projects.EditProject(d.Id(), options)

gitlab/resource_gitlab_project_test.go

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,10 @@ func TestAccGitlabProject_importURL(t *testing.T) {
540540
}
541541

542542
type testAccGitlabProjectMirroredExpectedAttributes struct {
543-
Mirror bool
544-
MirrorTriggerBuilds bool
543+
Mirror bool
544+
MirrorTriggerBuilds bool
545+
MirrorOverwritesDivergedBranches bool
546+
OnlyMirrorProtectedBranches bool
545547
}
546548

547549
func testAccCheckGitlabProjectMirroredAttributes(project *gitlab.Project, want *testAccGitlabProjectMirroredExpectedAttributes) resource.TestCheckFunc {
@@ -553,6 +555,14 @@ func testAccCheckGitlabProjectMirroredAttributes(project *gitlab.Project, want *
553555
if project.MirrorTriggerBuilds != want.MirrorTriggerBuilds {
554556
return fmt.Errorf("got mirror_trigger_builds %t; want %t", project.MirrorTriggerBuilds, want.MirrorTriggerBuilds)
555557
}
558+
559+
if project.MirrorOverwritesDivergedBranches != want.MirrorOverwritesDivergedBranches {
560+
return fmt.Errorf("got mirror_overwrites_diverged_branches %t; want %t", project.MirrorOverwritesDivergedBranches, want.MirrorOverwritesDivergedBranches)
561+
}
562+
563+
if project.OnlyMirrorProtectedBranches != want.OnlyMirrorProtectedBranches {
564+
return fmt.Errorf("got only_mirror_protected_branches %t; want %t", project.OnlyMirrorProtectedBranches, want.OnlyMirrorProtectedBranches)
565+
}
556566
return nil
557567
}
558568
}
@@ -601,10 +611,39 @@ func TestAccGitlabProject_importURLMirrored(t *testing.T) {
601611
testAccCheckGitlabProjectExists("gitlab_project.imported", &mirror),
602612
resource.TestCheckResourceAttr("gitlab_project.imported", "import_url", baseProject.HTTPURLToRepo),
603613
testAccCheckGitlabProjectMirroredAttributes(&mirror, &testAccGitlabProjectMirroredExpectedAttributes{
604-
Mirror: true,
605-
MirrorTriggerBuilds: true,
614+
Mirror: true,
615+
MirrorTriggerBuilds: true,
616+
MirrorOverwritesDivergedBranches: true,
617+
OnlyMirrorProtectedBranches: true,
618+
}),
619+
620+
func(state *terraform.State) error {
621+
projectID := state.RootModule().Resources["gitlab_project.imported"].Primary.ID
622+
623+
_, _, err := client.RepositoryFiles.GetFile(projectID, "foo.txt", &gitlab.GetFileOptions{Ref: gitlab.String("master")}, nil)
624+
if err != nil {
625+
return fmt.Errorf("failed to get file from imported project: %w", err)
626+
}
627+
628+
return nil
629+
},
630+
),
631+
},
632+
{
633+
// Second, disable all optional mirroring options
634+
Config: testAccGitlabProjectConfigImportURLMirrorDisabledOptionals(rInt, baseProject.HTTPURLToRepo),
635+
SkipFunc: isRunningInCE,
636+
Check: resource.ComposeTestCheckFunc(
637+
testAccCheckGitlabProjectExists("gitlab_project.imported", &mirror),
638+
resource.TestCheckResourceAttr("gitlab_project.imported", "import_url", baseProject.HTTPURLToRepo),
639+
testAccCheckGitlabProjectMirroredAttributes(&mirror, &testAccGitlabProjectMirroredExpectedAttributes{
640+
Mirror: true,
641+
MirrorTriggerBuilds: false,
642+
MirrorOverwritesDivergedBranches: false,
643+
OnlyMirrorProtectedBranches: false,
606644
}),
607645

646+
// Ensure the test file still is as expected
608647
func(state *terraform.State) error {
609648
projectID := state.RootModule().Resources["gitlab_project.imported"].Primary.ID
610649

@@ -618,15 +657,17 @@ func TestAccGitlabProject_importURLMirrored(t *testing.T) {
618657
),
619658
},
620659
{
621-
// Second, disable mirroring, using the original ImportURL acceptance test
660+
// Third, disable mirroring, using the original ImportURL acceptance test
622661
Config: testAccGitlabProjectConfigImportURLMirrorDisabled(rInt, baseProject.HTTPURLToRepo),
623662
SkipFunc: isRunningInCE,
624663
Check: resource.ComposeTestCheckFunc(
625664
testAccCheckGitlabProjectExists("gitlab_project.imported", &mirror),
626665
resource.TestCheckResourceAttr("gitlab_project.imported", "import_url", baseProject.HTTPURLToRepo),
627666
testAccCheckGitlabProjectMirroredAttributes(&mirror, &testAccGitlabProjectMirroredExpectedAttributes{
628-
Mirror: false,
629-
MirrorTriggerBuilds: false,
667+
Mirror: false,
668+
MirrorTriggerBuilds: false,
669+
MirrorOverwritesDivergedBranches: false,
670+
OnlyMirrorProtectedBranches: false,
630671
}),
631672

632673
// Ensure the test file still is as expected
@@ -992,6 +1033,26 @@ resource "gitlab_project" "imported" {
9921033
import_url = "%s"
9931034
mirror = true
9941035
mirror_trigger_builds = true
1036+
mirror_overwrites_diverged_branches = true
1037+
only_mirror_protected_branches = true
1038+
1039+
# So that acceptance tests can be run in a gitlab organization
1040+
# with no billing
1041+
visibility_level = "public"
1042+
}
1043+
`, rInt, importURL)
1044+
}
1045+
1046+
func testAccGitlabProjectConfigImportURLMirrorDisabledOptionals(rInt int, importURL string) string {
1047+
return fmt.Sprintf(`
1048+
resource "gitlab_project" "imported" {
1049+
name = "imported-%d"
1050+
default_branch = "master"
1051+
import_url = "%s"
1052+
mirror = true
1053+
mirror_trigger_builds = false
1054+
mirror_overwrites_diverged_branches = false
1055+
only_mirror_protected_branches = false
9951056
9961057
# So that acceptance tests can be run in a gitlab organization
9971058
# with no billing
@@ -1008,6 +1069,8 @@ resource "gitlab_project" "imported" {
10081069
import_url = "%s"
10091070
mirror = false
10101071
mirror_trigger_builds = false
1072+
mirror_overwrites_diverged_branches = false
1073+
only_mirror_protected_branches = false
10111074
10121075
# So that acceptance tests can be run in a gitlab organization
10131076
# with no billing

0 commit comments

Comments
 (0)