Skip to content

Commit 428b70a

Browse files
committed
Address review comments
Signed-off-by: Timo Sand <[email protected]>
1 parent e784625 commit 428b70a

1 file changed

Lines changed: 33 additions & 42 deletions

File tree

github/resource_github_repository_pages.go

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strconv"
88
"strings"
99

10-
"github.com/google/go-github/v84/github"
10+
"github.com/google/go-github/v85/github"
1111
"github.com/hashicorp/terraform-plugin-log/tflog"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
@@ -124,7 +124,34 @@ func resourceGithubRepositoryPagesCreate(ctx context.Context, d *schema.Resource
124124
owner := meta.name // TODO: Add owner support // d.Get("owner").(string)
125125
repoName := d.Get("repository").(string)
126126

127-
pagesReq := expandPagesForCreate(d)
127+
pagesReq := &github.Pages{}
128+
129+
buildType := d.Get("build_type").(string)
130+
pagesReq.BuildType = new(buildType)
131+
132+
if buildType == "legacy" {
133+
if source, ok := d.GetOk("source"); ok {
134+
sourceList := source.([]any)
135+
if len(sourceList) > 0 {
136+
sourceMap := sourceList[0].(map[string]any)
137+
branch := sourceMap["branch"].(string)
138+
pagesSource := &github.PagesSource{
139+
Branch: new(branch),
140+
}
141+
if path, ok := sourceMap["path"].(string); ok && path != "" && path != "/" {
142+
pagesSource.Path = new(path)
143+
}
144+
pagesReq.Source = pagesSource
145+
}
146+
}
147+
// Default to main branch if no source specified
148+
if pagesReq.Source == nil {
149+
pagesReq.Source = &github.PagesSource{
150+
Branch: new("main"),
151+
}
152+
}
153+
}
154+
128155
pages, _, err := client.Repositories.EnablePages(ctx, owner, repoName, pagesReq)
129156
if err != nil {
130157
return diag.FromErr(err)
@@ -299,18 +326,14 @@ func resourceGithubRepositoryPagesUpdate(ctx context.Context, d *schema.Resource
299326
// Hence we make sure to only send the value if it's changed.
300327
// Error: "400 Private pages is not enabled for this repository. All Pages will be public."
301328
if d.HasChange("public") {
302-
public, ok := d.Get("public").(bool)
303-
if ok {
304-
update.Public = new(public)
305-
}
329+
public := d.Get("public").(bool)
330+
update.Public = new(public)
306331
}
307332

308333
// `https_enforced` can't be sent to the API unless `cname` is set. Otherwise the API will return "404 The certificate does not exist yet".
309334
if d.HasChange("https_enforced") {
310-
httpsEnforced, ok := d.Get("https_enforced").(bool)
311-
if ok {
312-
update.HTTPSEnforced = new(httpsEnforced)
313-
}
335+
httpsEnforced := d.Get("https_enforced").(bool)
336+
update.HTTPSEnforced = new(httpsEnforced)
314337
}
315338

316339
buildType := d.Get("build_type").(string)
@@ -399,35 +422,3 @@ func resourceGithubRepositoryPagesDiff(ctx context.Context, d *schema.ResourceDi
399422

400423
return nil
401424
}
402-
403-
func expandPagesForCreate(d *schema.ResourceData) *github.Pages {
404-
pages := &github.Pages{}
405-
406-
buildType := d.Get("build_type").(string)
407-
pages.BuildType = new(buildType)
408-
409-
if buildType == "legacy" {
410-
if source, ok := d.GetOk("source"); ok {
411-
sourceList := source.([]any)
412-
if len(sourceList) > 0 {
413-
sourceMap := sourceList[0].(map[string]any)
414-
branch := sourceMap["branch"].(string)
415-
pagesSource := &github.PagesSource{
416-
Branch: new(branch),
417-
}
418-
if path, ok := sourceMap["path"].(string); ok && path != "" && path != "/" {
419-
pagesSource.Path = new(path)
420-
}
421-
pages.Source = pagesSource
422-
}
423-
}
424-
// Default to main branch if no source specified
425-
if pages.Source == nil {
426-
pages.Source = &github.PagesSource{
427-
Branch: new("main"),
428-
}
429-
}
430-
}
431-
432-
return pages
433-
}

0 commit comments

Comments
 (0)