Skip to content

Commit 0fa92e0

Browse files
committed
Add support for public field in pages
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 898ad43 commit 0fa92e0

4 files changed

Lines changed: 189 additions & 10 deletions

github/data_source_github_repository_pages.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func dataSourceGithubRepositoryPages() *schema.Resource {
6565
Computed: true,
6666
Description: "The absolute URL (with scheme) to the rendered GitHub Pages site.",
6767
},
68-
"status": {
68+
"build_status": {
6969
Type: schema.TypeString,
7070
Computed: true,
7171
Description: "The GitHub Pages site's build status e.g. 'building' or 'built'.",
@@ -75,6 +75,11 @@ func dataSourceGithubRepositoryPages() *schema.Resource {
7575
Computed: true,
7676
Description: "The API URL of the GitHub Pages resource.",
7777
},
78+
"public": {
79+
Type: schema.TypeBool,
80+
Computed: true,
81+
Description: "Whether the GitHub Pages site is public.",
82+
},
7883
},
7984
}
8085
}
@@ -113,13 +118,15 @@ func dataSourceGithubRepositoryPagesRead(ctx context.Context, d *schema.Resource
113118
if err := d.Set("html_url", pages.GetHTMLURL()); err != nil {
114119
return diag.FromErr(err)
115120
}
116-
if err := d.Set("status", pages.GetStatus()); err != nil {
121+
if err := d.Set("build_status", pages.GetStatus()); err != nil {
117122
return diag.FromErr(err)
118123
}
119124
if err := d.Set("api_url", pages.GetURL()); err != nil {
120125
return diag.FromErr(err)
121126
}
122-
127+
if err := d.Set("public", pages.GetPublic()); err != nil {
128+
return diag.FromErr(err)
129+
}
123130
// Set source only for legacy build type
124131
if pages.GetBuildType() == "legacy" && pages.GetSource() != nil {
125132
source := []map[string]any{

github/data_source_github_repository_pages_test.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/hashicorp/terraform-plugin-testing/compare"
88
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
99
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
1011
"github.com/hashicorp/terraform-plugin-testing/statecheck"
1112
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
1213
)
@@ -45,7 +46,58 @@ func TestAccGithubRepositoryPagesDataSource(t *testing.T) {
4546
`, repoName, baseRepoVisibility)
4647

4748
resource.Test(t, resource.TestCase{
48-
PreCheck: func() { skipUnauthenticated(t) },
49+
PreCheck: func() {
50+
skipUnauthenticated(t)
51+
},
52+
ProviderFactories: providerFactories,
53+
Steps: []resource.TestStep{
54+
{
55+
Config: config,
56+
ConfigStateChecks: []statecheck.StateCheck{
57+
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("build_type"), "github_repository_pages.test", tfjsonpath.New("build_type"), compare.ValuesSame()),
58+
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("source").AtSliceIndex(0).AtMapKey("branch"), "github_repository_pages.test", tfjsonpath.New("source").AtSliceIndex(0).AtMapKey("branch"), compare.ValuesSame()),
59+
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("source").AtSliceIndex(0).AtMapKey("path"), "github_repository_pages.test", tfjsonpath.New("source").AtSliceIndex(0).AtMapKey("path"), compare.ValuesSame()),
60+
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("cname"), "github_repository_pages.test", tfjsonpath.New("cname"), compare.ValuesSame()),
61+
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("custom_404"), "github_repository_pages.test", tfjsonpath.New("custom_404"), compare.ValuesSame()),
62+
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("html_url"), "github_repository_pages.test", tfjsonpath.New("html_url"), compare.ValuesSame()),
63+
statecheck.ExpectKnownValue("data.github_repository_pages.test", tfjsonpath.New("build_status"), knownvalue.NotNull()),
64+
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("api_url"), "github_repository_pages.test", tfjsonpath.New("api_url"), compare.ValuesSame()),
65+
statecheck.ExpectKnownValue("data.github_repository_pages.test", tfjsonpath.New("public"), knownvalue.Bool(testAccConf.authMode != enterprise)),
66+
},
67+
},
68+
},
69+
})
70+
})
71+
t.Run("reads_pages_enterprise_configuration", func(t *testing.T) {
72+
randomID := acctest.RandString(5)
73+
repoName := fmt.Sprintf("%spages-ds-%s", testResourcePrefix, randomID)
74+
75+
config := fmt.Sprintf(`
76+
resource "github_repository" "test" {
77+
name = "%s"
78+
visibility = "%s"
79+
auto_init = true
80+
}
81+
82+
resource "github_repository_pages" "test" {
83+
repository = github_repository.test.name
84+
build_type = "legacy"
85+
source {
86+
branch = "main"
87+
path = "/"
88+
}
89+
public = false
90+
}
91+
92+
data "github_repository_pages" "test" {
93+
repository = github_repository.test.name
94+
95+
depends_on = [github_repository_pages.test]
96+
}
97+
`, repoName, baseRepoVisibility)
98+
99+
resource.Test(t, resource.TestCase{
100+
PreCheck: func() { skipUnlessEnterprise(t) },
49101
ProviderFactories: providerFactories,
50102
Steps: []resource.TestStep{
51103
{
@@ -54,7 +106,12 @@ func TestAccGithubRepositoryPagesDataSource(t *testing.T) {
54106
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("build_type"), "github_repository_pages.test", tfjsonpath.New("build_type"), compare.ValuesSame()),
55107
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("source").AtSliceIndex(0).AtMapKey("branch"), "github_repository_pages.test", tfjsonpath.New("source").AtSliceIndex(0).AtMapKey("branch"), compare.ValuesSame()),
56108
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("source").AtSliceIndex(0).AtMapKey("path"), "github_repository_pages.test", tfjsonpath.New("source").AtSliceIndex(0).AtMapKey("path"), compare.ValuesSame()),
109+
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("cname"), "github_repository_pages.test", tfjsonpath.New("cname"), compare.ValuesSame()),
110+
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("custom_404"), "github_repository_pages.test", tfjsonpath.New("custom_404"), compare.ValuesSame()),
111+
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("html_url"), "github_repository_pages.test", tfjsonpath.New("html_url"), compare.ValuesSame()),
112+
statecheck.ExpectKnownValue("data.github_repository_pages.test", tfjsonpath.New("build_status"), knownvalue.NotNull()),
57113
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("api_url"), "github_repository_pages.test", tfjsonpath.New("api_url"), compare.ValuesSame()),
114+
statecheck.CompareValuePairs("data.github_repository_pages.test", tfjsonpath.New("public"), "github_repository_pages.test", tfjsonpath.New("public"), compare.ValuesSame()),
58115
},
59116
},
60117
},

github/resource_github_repository_pages.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ func resourceGithubRepositoryPages() *schema.Resource {
9797
Computed: true,
9898
Description: "The API URL of the GitHub Pages resource.",
9999
},
100+
"public": {
101+
Type: schema.TypeBool,
102+
Optional: true,
103+
Computed: true,
104+
Description: "Whether the GitHub Pages site is public.",
105+
},
100106
},
101107
CustomizeDiff: customdiff.All(resourceGithubRepositoryPagesDiff, diffRepository),
102108
}
@@ -109,8 +115,8 @@ func resourceGithubRepositoryPagesCreate(ctx context.Context, d *schema.Resource
109115
owner := meta.name // TODO: Add owner support // d.Get("owner").(string)
110116
repoName := d.Get("repository").(string)
111117

112-
pages := expandPagesForCreate(d)
113-
pages, _, err := client.Repositories.EnablePages(ctx, owner, repoName, pages)
118+
pagesReq := expandPagesForCreate(d)
119+
pages, _, err := client.Repositories.EnablePages(ctx, owner, repoName, pagesReq)
114120
if err != nil {
115121
return diag.FromErr(err)
116122
}
@@ -145,11 +151,25 @@ func resourceGithubRepositoryPagesCreate(ctx context.Context, d *schema.Resource
145151
return diag.FromErr(err)
146152
}
147153

148-
// Handle CNAME update after creation if specified
149-
if cname, ok := d.GetOk("cname"); ok && cname.(string) != "" {
150-
update := &github.PagesUpdate{
151-
CNAME: github.Ptr(cname.(string)),
154+
// Determine if we need to update the page with CNAME or public flag
155+
shouldUpdatePage := false
156+
update := &github.PagesUpdate{}
157+
cname, cnameExists := d.GetOk("cname")
158+
if cnameExists && cname.(string) != "" {
159+
shouldUpdatePage = true
160+
update.CNAME = github.Ptr(cname.(string))
161+
}
162+
public, publicExists := d.GetOkExists("public") // nolint:staticcheck // SA1019: There is no better alternative for checking if boolean value is set
163+
if publicExists && public != nil {
164+
shouldUpdatePage = true
165+
update.Public = github.Ptr(public.(bool))
166+
} else {
167+
if err := d.Set("public", pages.GetPublic()); err != nil {
168+
return diag.FromErr(err)
152169
}
170+
}
171+
172+
if shouldUpdatePage {
153173
_, err = client.Repositories.UpdatePages(ctx, owner, repoName, update)
154174
if err != nil {
155175
return diag.FromErr(err)
@@ -194,6 +214,10 @@ func resourceGithubRepositoryPagesRead(ctx context.Context, d *schema.ResourceDa
194214
return diag.FromErr(err)
195215
}
196216

217+
if err := d.Set("public", pages.GetPublic()); err != nil {
218+
return diag.FromErr(err)
219+
}
220+
197221
// Set source only for legacy build type
198222
if pages.GetBuildType() == "legacy" && pages.GetSource() != nil {
199223
source := []map[string]any{
@@ -230,6 +254,13 @@ func resourceGithubRepositoryPagesUpdate(ctx context.Context, d *schema.Resource
230254
}
231255
}
232256

257+
if d.HasChange("public") {
258+
public, ok := d.Get("public").(bool)
259+
if ok {
260+
update.Public = github.Ptr(public)
261+
}
262+
}
263+
233264
if d.HasChange("build_type") {
234265
buildType := d.Get("build_type").(string)
235266
update.BuildType = github.Ptr(buildType)

github/resource_github_repository_pages_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package github
22

33
import (
44
"fmt"
5+
"os"
56
"regexp"
67
"testing"
78

9+
"github.com/hashicorp/terraform-plugin-testing/compare"
810
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
911
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1012
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
@@ -134,6 +136,88 @@ source {
134136
})
135137
})
136138

139+
t.Run("creates_pages_with_private_visibility", func(t *testing.T) {
140+
randomID := acctest.RandString(5)
141+
repoName := fmt.Sprintf("%spages-%s", testResourcePrefix, randomID)
142+
143+
config := `
144+
resource "github_repository" "test" {
145+
name = "%s"
146+
visibility = "%s"
147+
auto_init = true
148+
149+
}
150+
151+
resource "github_repository_pages" "test" {
152+
repository = github_repository.test.name
153+
build_type = "workflow"
154+
155+
public = false
156+
}
157+
`
158+
159+
resource.Test(t, resource.TestCase{
160+
PreCheck: func() {
161+
skipUnlessEnterprise(t)
162+
},
163+
ProviderFactories: providerFactories,
164+
Steps: []resource.TestStep{
165+
{
166+
Config: fmt.Sprintf(config, repoName, baseRepoVisibility),
167+
ConfigStateChecks: []statecheck.StateCheck{
168+
statecheck.ExpectKnownValue("github_repository_pages.test", tfjsonpath.New("public"), knownvalue.Bool(false)),
169+
},
170+
},
171+
},
172+
})
173+
})
174+
t.Run("updates_pages_visibility", func(t *testing.T) {
175+
randomID := acctest.RandString(5)
176+
repoName := fmt.Sprintf("%spages-%s", testResourcePrefix, randomID)
177+
178+
config := `
179+
resource "github_repository" "test" {
180+
name = "%s"
181+
visibility = "%s"
182+
auto_init = true
183+
184+
}
185+
186+
resource "github_repository_pages" "test" {
187+
repository = github_repository.test.name
188+
build_type = "workflow"
189+
190+
public = %t
191+
}
192+
`
193+
194+
publicValuesDiffer := statecheck.CompareValue(compare.ValuesDiffer())
195+
196+
resource.Test(t, resource.TestCase{
197+
PreCheck: func() {
198+
skipUnlessEnterprise(t)
199+
if os.Getenv("GH_TEST_ENTERPRISE_IS_EMU") == "true" {
200+
t.Skip("Skipping as enterprise test mode is EMU")
201+
}
202+
},
203+
ProviderFactories: providerFactories,
204+
Steps: []resource.TestStep{
205+
{
206+
Config: fmt.Sprintf(config, repoName, baseRepoVisibility, true),
207+
ConfigStateChecks: []statecheck.StateCheck{
208+
publicValuesDiffer.AddStateValue("github_repository_pages.test", tfjsonpath.New("public")),
209+
},
210+
},
211+
{
212+
Config: fmt.Sprintf(config, repoName, baseRepoVisibility, false),
213+
ConfigStateChecks: []statecheck.StateCheck{
214+
publicValuesDiffer.AddStateValue("github_repository_pages.test", tfjsonpath.New("public")),
215+
},
216+
},
217+
},
218+
})
219+
})
220+
137221
t.Run("imports_pages_configuration", func(t *testing.T) {
138222
randomID := acctest.RandString(5)
139223
repoName := fmt.Sprintf("%spages-%s", testResourcePrefix, randomID)

0 commit comments

Comments
 (0)