@@ -725,7 +725,16 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
725
725
if d .HasChange ("pages" ) && ! d .IsNewResource () {
726
726
opts := expandPagesUpdate (d .Get ("pages" ).([]interface {}))
727
727
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
+ }
729
738
if err != nil {
730
739
return err
731
740
}
@@ -827,17 +836,26 @@ func expandPages(input []interface{}) *github.Pages {
827
836
return nil
828
837
}
829
838
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
+ }
838
851
}
839
852
}
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 }
841
859
}
842
860
843
861
func expandPagesUpdate (input []interface {}) * github.PagesUpdate {
@@ -854,21 +872,24 @@ func expandPagesUpdate(input []interface{}) *github.PagesUpdate {
854
872
update .CNAME = github .String (v )
855
873
}
856
874
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
+
857
880
// To update the GitHub Pages source, the github.PagesUpdate Source field
858
881
// must include the branch name and optionally the subdirectory /docs.
859
882
// 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 != "" {
865
889
sourcePath = v
866
890
}
891
+ update .Source = & github.PagesSource {Branch : & sourceBranch , Path : & sourcePath }
867
892
}
868
- update .Source = & github.PagesSource {Branch : & sourceBranch , Path : & sourcePath }
869
-
870
- pagesBuildType := pages ["build_type" ].(string )
871
- update .BuildType = & pagesBuildType
872
893
873
894
return update
874
895
}
0 commit comments