Skip to content

Commit 735fe86

Browse files
committed
fix: have an option to set org level forking policy
1 parent 8359c39 commit 735fe86

2 files changed

Lines changed: 67 additions & 4 deletions

File tree

github/resource_github_repository.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ func resourceGithubRepository() *schema.Resource {
259259
Computed: true,
260260
Description: "Set to 'true' to allow private forking on the repository; this is only relevant if the repository is owned by an organization and is private or internal.",
261261
},
262+
"org_allow_forking": {
263+
Type: schema.TypeBool,
264+
Optional: true,
265+
Default: true,
266+
Description: "Set to 'true' if the org allows forking.",
267+
},
262268
"squash_merge_commit_title": {
263269
Type: schema.TypeString,
264270
Optional: true,
@@ -634,9 +640,13 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
634640
}
635641

636642
// only configure allow forking if repository is not public
637-
allowForking, ok := d.Get("allow_forking").(bool)
638-
if ok && visibility != "public" {
639-
repository.AllowForking = github.Ptr(allowForking)
643+
// skip this when the org doesn't allow forking
644+
orgAllowForking, ok := d.Get("org_allow_forking").(bool)
645+
if ok && orgAllowForking {
646+
allowForking, forkingOk := d.Get("allow_forking").(bool)
647+
if forkingOk && visibility != "public" {
648+
repository.AllowForking = github.Ptr(allowForking)
649+
}
640650
}
641651

642652
return repository
@@ -845,7 +855,11 @@ func resourceGithubRepositoryRead(ctx context.Context, d *schema.ResourceData, m
845855
_ = d.Set("allow_rebase_merge", repo.GetAllowRebaseMerge())
846856
_ = d.Set("allow_squash_merge", repo.GetAllowSquashMerge())
847857
_ = d.Set("allow_update_branch", repo.GetAllowUpdateBranch())
848-
_ = d.Set("allow_forking", repo.GetAllowForking())
858+
if orgAllowForking, ok := d.Get("org_allow_forking").(bool); ok && orgAllowForking {
859+
_ = d.Set("allow_forking", repo.GetAllowForking())
860+
} else {
861+
_ = d.Set("allow_forking", false)
862+
}
849863
_ = d.Set("delete_branch_on_merge", repo.GetDeleteBranchOnMerge())
850864
_ = d.Set("web_commit_signoff_required", repo.GetWebCommitSignoffRequired())
851865
_ = d.Set("has_downloads", repo.GetHasDownloads())

github/resource_github_repository_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,55 @@ func TestAccGithubRepository(t *testing.T) {
597597
})
598598
})
599599

600+
t.Run("create_private_with_org_allow_forking", func(t *testing.T) {
601+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
602+
repoName := fmt.Sprintf("%sorg-forking-%s", testResourcePrefix, randomID)
603+
604+
// Test with org_allow_forking = true, allow_forking should be applied
605+
configOrgAllowsForking := fmt.Sprintf(`
606+
resource "github_repository" "test" {
607+
name = "%s"
608+
visibility = "private"
609+
org_allow_forking = true
610+
allow_forking = true
611+
}
612+
`, repoName)
613+
614+
// Test with org_allow_forking = false, allow_forking should not cause errors
615+
configOrgDisallowsForking := fmt.Sprintf(`
616+
resource "github_repository" "test" {
617+
name = "%s"
618+
visibility = "private"
619+
org_allow_forking = false
620+
allow_forking = true
621+
}
622+
`, repoName)
623+
624+
resource.Test(t, resource.TestCase{
625+
PreCheck: func() { skipUnlessHasOrgs(t) },
626+
ProviderFactories: providerFactories,
627+
Steps: []resource.TestStep{
628+
{
629+
Config: configOrgAllowsForking,
630+
Check: resource.ComposeTestCheckFunc(
631+
resource.TestCheckResourceAttr("github_repository.test", "visibility", "private"),
632+
resource.TestCheckResourceAttr("github_repository.test", "org_allow_forking", "true"),
633+
resource.TestCheckResourceAttr("github_repository.test", "allow_forking", "true"),
634+
),
635+
},
636+
{
637+
Config: configOrgDisallowsForking,
638+
Check: resource.ComposeTestCheckFunc(
639+
resource.TestCheckResourceAttr("github_repository.test", "visibility", "private"),
640+
resource.TestCheckResourceAttr("github_repository.test", "org_allow_forking", "false"),
641+
// allow_forking should be false in state when org doesn't allow forking
642+
resource.TestCheckResourceAttr("github_repository.test", "allow_forking", "false"),
643+
),
644+
},
645+
},
646+
})
647+
})
648+
600649
t.Run("configures vulnerability alerts for a private repository", func(t *testing.T) {
601650
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
602651
repoName := fmt.Sprintf("%sprv-vuln-%s", testResourcePrefix, randomID)

0 commit comments

Comments
 (0)