Skip to content

Commit d1de3e9

Browse files
feat: support internal visibility for repositories created by a template (#3123)
* feat: support internal visibility for repositories created by a template * fix: update handling of internal visibility for repositories created from templates * fix: update test to reflect internal repository visibility as private
1 parent 2deb0df commit d1de3e9

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

github/resource_github_repository.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,6 @@ func resourceGithubRepositoryCreate(ctx context.Context, d *schema.ResourceData,
650650
owner := meta.(*Owner).name
651651
repoName := repoReq.GetName()
652652

653-
isPrivate := repoReq.GetVisibility() == "private"
654-
repoReq.Private = github.Ptr(isPrivate)
655653
if template, ok := d.GetOk("template"); ok {
656654
templateConfigBlocks := template.([]any)
657655

@@ -665,11 +663,14 @@ func resourceGithubRepositoryCreate(ctx context.Context, d *schema.ResourceData,
665663
templateRepoOwner := templateConfigMap["owner"].(string)
666664
includeAllBranches := templateConfigMap["include_all_branches"].(bool)
667665

666+
// Template API only supports Private boolean, so treat "internal" as private, then update via PATCH.
667+
private := repoReq.GetVisibility() != "public"
668+
668669
templateRepoReq := github.TemplateRepoRequest{
669670
Name: &repoName,
670671
Owner: &owner,
671672
Description: github.Ptr(d.Get("description").(string)),
672-
Private: github.Ptr(isPrivate),
673+
Private: github.Ptr(private),
673674
IncludeAllBranches: github.Ptr(includeAllBranches),
674675
}
675676

github/resource_github_repository_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,15 +1202,15 @@ resource "github_repository" "test" {
12021202
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
12031203
testRepoName := fmt.Sprintf("%svisibility-internal-%s", testResourcePrefix, randomID)
12041204
config := fmt.Sprintf(`
1205-
resource "github_repository" "internal" {
1205+
resource "github_repository" "test" {
12061206
name = "%s"
12071207
visibility = "internal"
12081208
}
12091209
`, testRepoName)
12101210

12111211
check := resource.ComposeTestCheckFunc(
12121212
resource.TestCheckResourceAttr(
1213-
"github_repository.internal", "visibility",
1213+
"github_repository.test", "visibility",
12141214
"internal",
12151215
),
12161216
)
@@ -1399,6 +1399,35 @@ resource "github_repository" "test" {
13991399
},
14001400
})
14011401
})
1402+
1403+
t.Run("create_internal_repo_from_template", func(t *testing.T) {
1404+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
1405+
testRepoName := fmt.Sprintf("%s%s", testResourcePrefix, randomID)
1406+
config := fmt.Sprintf(`
1407+
resource "github_repository" "test" {
1408+
name = "%s"
1409+
visibility = "internal"
1410+
template {
1411+
owner = "%s"
1412+
repository = "%s"
1413+
}
1414+
}
1415+
`, testRepoName, testAccConf.testPublicTemplateRepositoryOwner, testAccConf.testPublicTemplateRepository)
1416+
1417+
resource.Test(t, resource.TestCase{
1418+
PreCheck: func() { skipUnlessMode(t, enterprise) },
1419+
ProviderFactories: providerFactories,
1420+
Steps: []resource.TestStep{
1421+
{
1422+
Config: config,
1423+
Check: resource.ComposeTestCheckFunc(
1424+
resource.TestCheckResourceAttr("github_repository.test", "visibility", "internal"),
1425+
resource.TestCheckResourceAttr("github_repository.test", "private", "true"),
1426+
),
1427+
},
1428+
},
1429+
})
1430+
})
14021431
}
14031432

14041433
func Test_expandPages(t *testing.T) {

website/docs/r/repository.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ The `advanced_security` block supports the following:
214214
* `repository`: The name of the template repository.
215215
* `include_all_branches`: Whether the new repository should include all the branches from the template repository (defaults to false, which includes only the default branch from the template).
216216

217+
~> **Note on `internal` visibility with templates**: When creating a repository from a template with `visibility = "internal"`, the provider uses a two-step process due to GitHub API limitations. The template creation API only supports a `private` boolean parameter. Therefore, repositories with `visibility = "internal"` are initially created as private and then immediately updated to internal visibility. This ensures internal repositories are never exposed publicly during creation.
218+
217219
## Attributes Reference
218220

219221
The following additional attributes are exported:

0 commit comments

Comments
 (0)