Skip to content

Commit 64db034

Browse files
committed
Remove unnecessary hasChanges checks and explain the ones that are unnecessary
Signed-off-by: Timo Sand <[email protected]>
1 parent eb4caae commit 64db034

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

github/resource_github_repository_pages.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func resourceGithubRepositoryPagesCreate(ctx context.Context, d *schema.Resource
162162

163163
// Sending a null value will remove the custom domain in the API, so we make sure to only send the value if it's set.
164164
cname, cnameOK := d.GetOk("cname")
165-
// `public` can only be set for GHEC. Hence we make sure to only send the value if it's set.
165+
// // Sending the `public` value will return an error if the repository doesn't have public pages enabled.
166166
public, publicOKExists := d.GetOkExists("public") //nolint:staticcheck // SA1019: d.GetOkExists is deprecated but necessary for bool fields
167167
// `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".
168168
httpsEnforced, httpsEnforcedExists := d.GetOkExists("https_enforced") //nolint:staticcheck // SA1019: d.GetOkExists is deprecated but necessary for bool fields
@@ -289,45 +289,43 @@ func resourceGithubRepositoryPagesUpdate(ctx context.Context, d *schema.Resource
289289

290290
update := &github.PagesUpdate{}
291291

292+
// Sending a null value for `cname` will remove the custom domain in the API, so we make sure to only send the value if it's changed.
292293
if d.HasChange("cname") {
293294
cname := d.Get("cname").(string)
294-
if cname != "" {
295-
update.CNAME = new(cname)
296-
}
295+
update.CNAME = new(cname)
297296
}
298297

298+
// Sending the `public` value on updates will return an error if the repository doesn't have public pages enabled.
299+
// Hence we make sure to only send the value if it's changed.
300+
// Error: "400 Private pages is not enabled for this repository. All Pages will be public."
299301
if d.HasChange("public") {
300302
public, ok := d.Get("public").(bool)
301303
if ok {
302304
update.Public = new(public)
303305
}
304306
}
305307

308+
// `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".
306309
if d.HasChange("https_enforced") {
307310
httpsEnforced, ok := d.Get("https_enforced").(bool)
308311
if ok {
309312
update.HTTPSEnforced = new(httpsEnforced)
310313
}
311314
}
312315

313-
if d.HasChange("build_type") {
314-
buildType := d.Get("build_type").(string)
315-
update.BuildType = new(buildType)
316-
}
317-
318-
if d.HasChange("source") || d.HasChange("build_type") {
319-
buildType := d.Get("build_type").(string)
320-
if buildType == "legacy" {
321-
if source, ok := d.GetOk("source"); ok {
322-
sourceList := source.([]any)
323-
if len(sourceList) > 0 {
324-
sourceMap := sourceList[0].(map[string]any)
325-
branch := sourceMap["branch"].(string)
326-
path := sourceMap["path"].(string)
327-
update.Source = &github.PagesSource{
328-
Branch: &branch,
329-
Path: &path,
330-
}
316+
buildType := d.Get("build_type").(string)
317+
update.BuildType = new(buildType)
318+
319+
if buildType == "legacy" {
320+
if source, ok := d.GetOk("source"); ok {
321+
sourceList := source.([]any)
322+
if len(sourceList) > 0 {
323+
sourceMap := sourceList[0].(map[string]any)
324+
branch := sourceMap["branch"].(string)
325+
path := sourceMap["path"].(string)
326+
update.Source = &github.PagesSource{
327+
Branch: &branch,
328+
Path: &path,
331329
}
332330
}
333331
}

0 commit comments

Comments
 (0)