Skip to content

Commit 56b7d70

Browse files
committed
Restructure UpdatePages section
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 4f2a335 commit 56b7d70

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

github/resource_github_repository_pages.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/google/go-github/v83/github"
11+
"github.com/hashicorp/terraform-plugin-log/tflog"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -158,36 +159,46 @@ func resourceGithubRepositoryPagesCreate(ctx context.Context, d *schema.Resource
158159
return diag.FromErr(err)
159160
}
160161

161-
// Determine if we need to update the page with CNAME or public flag
162-
shouldUpdatePage := false
163-
update := &github.PagesUpdate{}
164-
cname, cnameExists := d.GetOk("cname")
165-
if cnameExists && cname.(string) != "" {
166-
shouldUpdatePage = true
167-
update.CNAME = github.Ptr(cname.(string))
168-
}
169-
public, publicExists := d.GetOkExists("public") // nolint:staticcheck // SA1019: There is no better alternative for checking if boolean value is set
170-
if publicExists && public != nil {
171-
shouldUpdatePage = true
172-
update.Public = github.Ptr(public.(bool))
173-
} else {
174-
if err := d.Set("public", pages.GetPublic()); err != nil {
162+
// Check if we have values set that can't be configured as part of the create logic
163+
cname, cnameOK := d.GetOk("cname")
164+
public, publicOKExists := d.GetOkExists("public") //nolint:staticcheck // SA1019: d.GetOkExists is deprecated but necessary for bool fields
165+
httpsEnforced, httpsEnforcedExists := d.GetOkExists("https_enforced") //nolint:staticcheck // SA1019: d.GetOkExists is deprecated but necessary for bool fields
166+
tflog.Debug(ctx, "Do we have values set that need the update logic?", map[string]any{
167+
"public": public,
168+
"public_ok_exists": publicOKExists,
169+
"https_enforced": httpsEnforced,
170+
"https_enforced_exists": httpsEnforcedExists,
171+
"cname": cname,
172+
"cname_ok": cnameOK,
173+
})
174+
175+
if cnameOK || publicOKExists || httpsEnforcedExists {
176+
update := &github.PagesUpdate{}
177+
178+
if cnameOK {
179+
update.CNAME = github.Ptr(cname.(string))
180+
}
181+
182+
if publicOKExists {
183+
update.Public = github.Ptr(public.(bool))
184+
}
185+
186+
if httpsEnforcedExists {
187+
update.HTTPSEnforced = github.Ptr(httpsEnforced.(bool))
188+
}
189+
190+
_, err = client.Repositories.UpdatePages(ctx, owner, repoName, update)
191+
if err != nil {
175192
return diag.FromErr(err)
176193
}
177194
}
178-
httpsEnforced, httpsEnforcedExists := d.GetOkExists("https_enforced") // nolint:staticcheck // SA1019: There is no better alternative for checking if boolean value is set
179-
if httpsEnforcedExists && httpsEnforced != nil {
180-
shouldUpdatePage = true
181-
update.HTTPSEnforced = github.Ptr(httpsEnforced.(bool))
182-
} else {
183-
if err := d.Set("https_enforced", pages.GetHTTPSEnforced()); err != nil {
195+
if !publicOKExists {
196+
if err := d.Set("public", pages.GetPublic()); err != nil {
184197
return diag.FromErr(err)
185198
}
186199
}
187-
188-
if shouldUpdatePage {
189-
_, err = client.Repositories.UpdatePages(ctx, owner, repoName, update)
190-
if err != nil {
200+
if !httpsEnforcedExists {
201+
if err := d.Set("https_enforced", pages.GetHTTPSEnforced()); err != nil {
191202
return diag.FromErr(err)
192203
}
193204
}

0 commit comments

Comments
 (0)