Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public field to repository pages #2540

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions github/data_source_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ func dataSourceGithubRepository() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"public": {
Type: schema.TypeBool,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
Expand Down
20 changes: 18 additions & 2 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,14 @@ func resourceGithubRepository() *schema.Resource {
Computed: true,
Description: "URL to the repository on the web.",
},
"public": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether the rendered GitHub Pages site is publicly availabe.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The GitHub Pages site's build status e.g. building or built.",
},
"url": {
Expand Down Expand Up @@ -913,7 +918,12 @@ func expandPages(input []interface{}) *github.Pages {
buildType = github.String(v)
}

return &github.Pages{Source: source, BuildType: buildType}
var public *bool
// Only set the github.PagesUpdate public field if the value is a valid boolean.
if v, ok := pages["public"].(bool); ok {
public = github.Bool(v)
}
return &github.Pages{Source: source, BuildType: buildType, Public: public}
}

func expandPagesUpdate(input []interface{}) *github.PagesUpdate {
Expand Down Expand Up @@ -948,6 +958,11 @@ func expandPagesUpdate(input []interface{}) *github.PagesUpdate {
}
update.Source = &github.PagesSource{Branch: &sourceBranch, Path: &sourcePath}
}

// Only set the github.PagesUpdate public field if the value is a valid boolean.
if v, ok := pages["public"].(bool); ok {
update.Public = github.Bool(v)
}

return update
}
Expand All @@ -966,6 +981,7 @@ func flattenPages(pages *github.Pages) []interface{} {
pagesMap["build_type"] = pages.GetBuildType()
pagesMap["url"] = pages.GetURL()
pagesMap["status"] = pages.GetStatus()
pagesMap["public"] = pages.GetPublic()
pagesMap["cname"] = pages.GetCNAME()
pagesMap["custom_404"] = pages.GetCustom404()
pagesMap["html_url"] = pages.GetHTMLURL()
Expand Down
47 changes: 47 additions & 0 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,53 @@ func TestAccGithubRepositoryPages(t *testing.T) {
t.Errorf("got %q; want %q", pages.GetSource().GetPath(), "/docs")
}
})

t.Run("manages private pages feature for a repository", func(t *testing.T) {
config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-%s"
auto_init = true
pages {
source {
branch = "main"
}
public = false
}
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "pages.0.public",
"false",
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this feature")
})

t.Run("with an individual account", func(t *testing.T) {
t.Skip("individual account not supported for this feature")
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})
}

func TestAccGithubRepositorySecurity(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ The `pages` block supports the following:

* `cname` - (Optional) The custom domain for the repository. This can only be set after the repository has been created.

* `public` - (Optional) Whether the rendered GitHub Pages site is publicly available.

#### GitHub Pages Source ####

The `source` block supports the following:
Expand Down