Skip to content

Commit 6aed389

Browse files
fix: GitHub Repository Update might fail when Pages enabled (#1716)
Co-authored-by: Keegan Campbell <[email protected]>
1 parent 42ebbe0 commit 6aed389

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

github/resource_github_repository.go

+40-19
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,16 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
725725
if d.HasChange("pages") && !d.IsNewResource() {
726726
opts := expandPagesUpdate(d.Get("pages").([]interface{}))
727727
if opts != nil {
728-
_, err := client.Repositories.UpdatePages(ctx, owner, repoName, opts)
728+
pages, res, err := client.Repositories.GetPagesInfo(ctx, owner, repoName)
729+
if res.StatusCode != http.StatusNotFound && err != nil {
730+
return err
731+
}
732+
733+
if pages == nil {
734+
_, _, err = client.Repositories.EnablePages(ctx, owner, repoName, &github.Pages{Source: opts.Source, BuildType: opts.BuildType})
735+
} else {
736+
_, err = client.Repositories.UpdatePages(ctx, owner, repoName, opts)
737+
}
729738
if err != nil {
730739
return err
731740
}
@@ -827,17 +836,26 @@ func expandPages(input []interface{}) *github.Pages {
827836
return nil
828837
}
829838
pages := input[0].(map[string]interface{})
830-
pagesSource := pages["source"].([]interface{})[0].(map[string]interface{})
831-
source := &github.PagesSource{
832-
Branch: github.String(pagesSource["branch"].(string)),
833-
}
834-
if v, ok := pagesSource["path"].(string); ok {
835-
// To set to the root directory "/", leave source.Path unset
836-
if v != "" && v != "/" {
837-
source.Path = github.String(v)
839+
var source *github.PagesSource
840+
if pagesSource, ok := pages["source"].([]interface{})[0].(map[string]interface{}); ok {
841+
if v, ok := pagesSource["branch"].(string); ok {
842+
if v != "" {
843+
source.Branch = github.String(v)
844+
}
845+
}
846+
if v, ok := pagesSource["path"].(string); ok {
847+
// To set to the root directory "/", leave source.Path unset
848+
if v != "" && v != "/" {
849+
source.Path = github.String(v)
850+
}
838851
}
839852
}
840-
return &github.Pages{Source: source}
853+
var buildType *string
854+
if v, ok := pages["build_type"].(string); ok {
855+
buildType = github.String(v)
856+
}
857+
858+
return &github.Pages{Source: source, BuildType: buildType}
841859
}
842860

843861
func expandPagesUpdate(input []interface{}) *github.PagesUpdate {
@@ -854,21 +872,24 @@ func expandPagesUpdate(input []interface{}) *github.PagesUpdate {
854872
update.CNAME = github.String(v)
855873
}
856874

875+
// Only set the github.PagesUpdate BuildType field if the value is a non-empty string.
876+
if v, ok := pages["build_type"].(string); ok && v != "" {
877+
update.BuildType = github.String(v)
878+
}
879+
857880
// To update the GitHub Pages source, the github.PagesUpdate Source field
858881
// must include the branch name and optionally the subdirectory /docs.
859882
// e.g. "master" or "master /docs"
860-
pagesSource := pages["source"].([]interface{})[0].(map[string]interface{})
861-
sourceBranch := pagesSource["branch"].(string)
862-
sourcePath := ""
863-
if v, ok := pagesSource["path"].(string); ok {
864-
if v != "" && v != "/" {
883+
// This is only necessary if the BuildType is "legacy".
884+
if update.BuildType == nil || *update.BuildType == "legacy" {
885+
pagesSource := pages["source"].([]interface{})[0].(map[string]interface{})
886+
sourceBranch := pagesSource["branch"].(string)
887+
sourcePath := ""
888+
if v, ok := pagesSource["path"].(string); ok && v != "" {
865889
sourcePath = v
866890
}
891+
update.Source = &github.PagesSource{Branch: &sourceBranch, Path: &sourcePath}
867892
}
868-
update.Source = &github.PagesSource{Branch: &sourceBranch, Path: &sourcePath}
869-
870-
pagesBuildType := pages["build_type"].(string)
871-
update.BuildType = &pagesBuildType
872893

873894
return update
874895
}

0 commit comments

Comments
 (0)