Skip to content

Commit cd8fde6

Browse files
authored
Merge pull request #914 from jtymes/feature/529-prevent-fork-outside-group
Add prevent_forking_outside_group
2 parents 1754350 + 5b3cad3 commit cd8fde6

File tree

8 files changed

+96
-3
lines changed

8 files changed

+96
-3
lines changed

docs/data-sources/group.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ data "gitlab_group" "foo" {
4545
- **name** (String) The name of this group.
4646
- **parent_id** (Number) Integer, ID of the parent group.
4747
- **path** (String) The path of the group.
48+
- **prevent_forking_outside_group** (Boolean) When enabled, users can not fork projects from this group to external namespaces.
4849
- **request_access_enabled** (Boolean) Boolean, is request for access enabled to the group.
4950
- **runners_token** (String, Sensitive) The group level registration token to use during runner setup.
5051
- **visibility_level** (String) Visibility level of the group. Possible values are `private`, `internal`, `public`.

docs/resources/group.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ resource "gitlab_project" "example" {
4747
- **lfs_enabled** (Boolean) Boolean, defaults to true. Whether to enable LFS
4848
- **mentions_disabled** (Boolean) Boolean, defaults to false. Disable the capability
4949
- **parent_id** (Number) Integer, id of the parent group (creates a nested group).
50+
- **prevent_forking_outside_group** (Boolean) When enabled, users can not fork projects from this group to external namespaces.
5051
- **project_creation_level** (String) , defaults to Maintainer.
5152
- **request_access_enabled** (Boolean) Boolean, defaults to false. Whether to
5253
- **require_two_factor_authentication** (Boolean) Boolean, defaults to false.

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/data_source_gitlab_group.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ var _ = registerDataSource("gitlab_group", func() *schema.Resource {
9191
Type: schema.TypeInt,
9292
Computed: true,
9393
},
94+
"prevent_forking_outside_group": {
95+
Description: "When enabled, users can not fork projects from this group to external namespaces.",
96+
Type: schema.TypeBool,
97+
Computed: true,
98+
},
9499
},
95100
}
96101
})
@@ -135,6 +140,7 @@ func dataSourceGitlabGroupRead(ctx context.Context, d *schema.ResourceData, meta
135140
d.Set("parent_id", group.ParentID)
136141
d.Set("runners_token", group.RunnersToken)
137142
d.Set("default_branch_protection", group.DefaultBranchProtection)
143+
d.Set("prevent_forking_outside_group", group.PreventForkingOutsideGroup)
138144

139145
d.SetId(fmt.Sprintf("%d", group.ID))
140146

internal/provider/data_source_gitlab_group_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func testAccDataSourceGitlabGroup(src, n string) resource.TestCheckFunc {
5656
"visibility_level",
5757
"parent_id",
5858
"default_branch_protection",
59+
"prevent_forking_outside_group",
5960
}
6061

6162
for _, attribute := range testAttributes {

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 after creation %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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,35 @@ func TestAccGitlabGroup_disappears(t *testing.T) {
246246
})
247247
}
248248

249+
func TestAccGitlabGroup_PreventForkingOutsideGroup(t *testing.T) {
250+
var group gitlab.Group
251+
rInt := acctest.RandInt()
252+
253+
resource.Test(t, resource.TestCase{
254+
PreCheck: func() { testAccPreCheck(t) },
255+
ProviderFactories: providerFactories,
256+
CheckDestroy: testAccCheckGitlabGroupDestroy,
257+
Steps: []resource.TestStep{
258+
{
259+
SkipFunc: isRunningInCE,
260+
Config: testAccGitlabGroupPreventForkingOutsideGroupConfig(rInt),
261+
Check: resource.ComposeTestCheckFunc(
262+
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
263+
resource.TestCheckResourceAttr("gitlab_group.foo", "prevent_forking_outside_group", "true"),
264+
),
265+
},
266+
{
267+
SkipFunc: isRunningInCE,
268+
Config: testAccGitlabGroupPreventForkingOutsideGroupUpdateConfig(rInt),
269+
Check: resource.ComposeTestCheckFunc(
270+
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
271+
resource.TestCheckResourceAttr("gitlab_group.foo", "prevent_forking_outside_group", "false"),
272+
),
273+
},
274+
},
275+
})
276+
}
277+
249278
func testAccCheckGitlabGroupDisappears(group *gitlab.Group) resource.TestCheckFunc {
250279
return func(s *terraform.State) error {
251280
_, err := testGitlabClient.Groups.DeleteGroup(group.ID)
@@ -544,3 +573,35 @@ resource "gitlab_group" "nested_foo" {
544573
}
545574
`, rInt, rInt, rInt, rInt, rInt, rInt)
546575
}
576+
577+
func testAccGitlabGroupPreventForkingOutsideGroupConfig(rInt int) string {
578+
return fmt.Sprintf(`
579+
resource "gitlab_group" "foo" {
580+
name = "foo-name-%d"
581+
path = "foo-path-%d"
582+
description = "Terraform acceptance tests"
583+
584+
# So that acceptance tests can be run in a gitlab organization
585+
# with no billing
586+
visibility_level = "public"
587+
588+
prevent_forking_outside_group = true
589+
}
590+
`, rInt, rInt)
591+
}
592+
593+
func testAccGitlabGroupPreventForkingOutsideGroupUpdateConfig(rInt int) string {
594+
return fmt.Sprintf(`
595+
resource "gitlab_group" "foo" {
596+
name = "foo-name-%d"
597+
path = "foo-path-%d"
598+
description = "Terraform acceptance tests"
599+
600+
# So that acceptance tests can be run in a gitlab organization
601+
# with no billing
602+
visibility_level = "public"
603+
604+
prevent_forking_outside_group = false
605+
}
606+
`, rInt, rInt)
607+
}

0 commit comments

Comments
 (0)