Skip to content

Commit 035265f

Browse files
committed
Add prevent_forking_outside_group to group resource
1 parent 278f2b3 commit 035265f

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
1212
github.com/mitchellh/hashstructure v1.1.0
1313
github.com/onsi/gomega v1.18.1
14-
github.com/xanzy/go-gitlab v0.55.1
14+
github.com/xanzy/go-gitlab v0.56.0
1515
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
1616
google.golang.org/api v0.34.0 // indirect
1717
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaU
344344
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
345345
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
346346
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
347-
github.com/xanzy/go-gitlab v0.55.1 h1:IgX/DS9buV0AUz8fuJPQkdl0fQGfBiAsAHxpun8sNhg=
348-
github.com/xanzy/go-gitlab v0.55.1/go.mod h1:F0QEXwmqiBUxCgJm8fE9S+1veX4XC9Z4cfaAbqwk4YM=
347+
github.com/xanzy/go-gitlab v0.56.0 h1:/QHBvk3IKVNwvXB/UOWVb5J6VCN6r2bg9/WxjUbFY/0=
348+
github.com/xanzy/go-gitlab v0.56.0/go.mod h1:F0QEXwmqiBUxCgJm8fE9S+1veX4XC9Z4cfaAbqwk4YM=
349349
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
350350
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
351351
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

internal/provider/resource_gitlab_group.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ var _ = registerResource("gitlab_group", func() *schema.Resource {
148148
Computed: true,
149149
Sensitive: true,
150150
},
151+
"prevent_forking_outside_group": {
152+
Description: "When enabled, users can not fork projects from this group to external namespaces",
153+
Type: schema.TypeBool,
154+
Optional: true,
155+
Default: false,
156+
},
151157
},
152158
}
153159
})
@@ -223,6 +229,18 @@ func resourceGitlabGroupCreate(ctx context.Context, d *schema.ResourceData, meta
223229

224230
d.SetId(fmt.Sprintf("%d", group.ID))
225231

232+
var updateOptions gitlab.UpdateGroupOptions
233+
234+
if v, ok := d.GetOk("prevent_forking_outside_group"); ok {
235+
updateOptions.PreventForkingOutsideGroup = gitlab.Bool(v.(bool))
236+
}
237+
238+
if (updateOptions != gitlab.UpdateGroupOptions{}) {
239+
if _, _, err = client.Groups.UpdateGroup(d.Id(), &updateOptions, gitlab.WithContext(ctx)); err != nil {
240+
return diag.Errorf("could not update group %q: %s", d.Id(), err)
241+
}
242+
}
243+
226244
return resourceGitlabGroupRead(ctx, d, meta)
227245
}
228246

@@ -266,6 +284,7 @@ func resourceGitlabGroupRead(ctx context.Context, d *schema.ResourceData, meta i
266284
d.Set("runners_token", group.RunnersToken)
267285
d.Set("share_with_group_lock", group.ShareWithGroupLock)
268286
d.Set("default_branch_protection", group.DefaultBranchProtection)
287+
d.Set("prevent_forking_outside_group", group.PreventForkingOutsideGroup)
269288

270289
return nil
271290
}
@@ -337,6 +356,10 @@ func resourceGitlabGroupUpdate(ctx context.Context, d *schema.ResourceData, meta
337356
options.DefaultBranchProtection = gitlab.Int(d.Get("default_branch_protection").(int))
338357
}
339358

359+
if d.HasChange("prevent_forking_outside_group") {
360+
options.PreventForkingOutsideGroup = gitlab.Bool(d.Get("prevent_forking_outside_group").(bool))
361+
}
362+
340363
log.Printf("[DEBUG] update gitlab group %s", d.Id())
341364

342365
_, _, err := client.Groups.UpdateGroup(d.Id(), options, gitlab.WithContext(ctx))

internal/provider/resource_gitlab_group_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,33 @@ func testAccCheckGitlabGroupDestroy(s *terraform.State) error {
408408
return nil
409409
}
410410

411+
func TestAccGitlabGroup_PreventForkingOutsideGroup(t *testing.T) {
412+
var group gitlab.Group
413+
rInt := acctest.RandInt()
414+
415+
resource.Test(t, resource.TestCase{
416+
PreCheck: func() { testAccPreCheck(t) },
417+
ProviderFactories: providerFactories,
418+
CheckDestroy: testAccCheckGitlabGroupDestroy,
419+
Steps: []resource.TestStep{
420+
{
421+
SkipFunc: isRunningInCE,
422+
Config: testAccGitlabGroupPreventForkingOutsideGroupConfig(rInt),
423+
Check: resource.ComposeTestCheckFunc(
424+
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
425+
func(s *terraform.State) error {
426+
if group.PreventForkingOutsideGroup != true {
427+
return fmt.Errorf("expected forking outside the group to be disabled")
428+
}
429+
430+
return nil
431+
},
432+
),
433+
},
434+
},
435+
})
436+
}
437+
411438
func testAccGitlabGroupConfig(rInt int) string {
412439
return fmt.Sprintf(`
413440
resource "gitlab_group" "foo" {
@@ -544,3 +571,19 @@ resource "gitlab_group" "nested_foo" {
544571
}
545572
`, rInt, rInt, rInt, rInt, rInt, rInt)
546573
}
574+
575+
func testAccGitlabGroupPreventForkingOutsideGroupConfig(rInt int) string {
576+
return fmt.Sprintf(`
577+
resource "gitlab_group" "foo" {
578+
name = "foo-name-%d"
579+
path = "foo-path-%d"
580+
description = "Terraform acceptance tests"
581+
582+
# So that acceptance tests can be run in a gitlab organization
583+
# with no billing
584+
visibility_level = "public"
585+
586+
prevent_forking_outside_group = true
587+
}
588+
`, rInt, rInt)
589+
}

0 commit comments

Comments
 (0)