Skip to content

Commit 8c7b824

Browse files
fix: Repo Creation fails with enabled Pages (#1748)
Co-authored-by: Keegan Campbell <me@kfcampbell.com>
1 parent be7cfe8 commit 8c7b824

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

github/resource_github_repository.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -836,20 +836,23 @@ func expandPages(input []interface{}) *github.Pages {
836836
return nil
837837
}
838838
pages := input[0].(map[string]interface{})
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 != "" {
839+
source := &github.PagesSource{
840+
Branch: github.String("main"),
841+
}
842+
if len(pages["source"].([]interface{})) == 1 {
843+
if pagesSource, ok := pages["source"].([]interface{})[0].(map[string]interface{}); ok {
844+
if v, ok := pagesSource["branch"].(string); ok {
843845
source.Branch = github.String(v)
844846
}
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)
847+
if v, ok := pagesSource["path"].(string); ok {
848+
// To set to the root directory "/", leave source.Path unset
849+
if v != "" && v != "/" {
850+
source.Path = github.String(v)
851+
}
850852
}
851853
}
852854
}
855+
853856
var buildType *string
854857
if v, ok := pages["build_type"].(string); ok {
855858
buildType = github.String(v)

github/resource_github_repository_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,47 @@ func TestAccGithubRepositoryPages(t *testing.T) {
859859

860860
})
861861

862+
t.Run("expand Pages configuration with workflow", func(t *testing.T) {
863+
input := []interface{}{map[string]interface{}{
864+
"build_type": "workflow",
865+
"source": []interface{}{map[string]interface{}{}},
866+
}}
867+
868+
pages := expandPages(input)
869+
if pages == nil {
870+
t.Fatal("pages is nil")
871+
}
872+
if pages.GetBuildType() != "workflow" {
873+
t.Errorf("got %q; want %q", pages.GetBuildType(), "workflow")
874+
}
875+
if pages.GetSource().GetBranch() != "main" {
876+
t.Errorf("got %q; want %q", pages.GetSource().GetBranch(), "main")
877+
}
878+
})
879+
880+
t.Run("expand Pages configuration with source", func(t *testing.T) {
881+
input := []interface{}{map[string]interface{}{
882+
"build_type": "legacy",
883+
"source": []interface{}{map[string]interface{}{
884+
"branch": "main",
885+
"path": "/docs",
886+
}},
887+
}}
888+
889+
pages := expandPages(input)
890+
if pages == nil {
891+
t.Fatal("pages is nil")
892+
}
893+
if pages.GetBuildType() != "legacy" {
894+
t.Errorf("got %q; want %q", pages.GetBuildType(), "legacy")
895+
}
896+
if pages.GetSource().GetBranch() != "main" {
897+
t.Errorf("got %q; want %q", pages.GetSource().GetBranch(), "main")
898+
}
899+
if pages.GetSource().GetPath() != "/docs" {
900+
t.Errorf("got %q; want %q", pages.GetSource().GetPath(), "/docs")
901+
}
902+
})
862903
}
863904

864905
func TestAccGithubRepositorySecurity(t *testing.T) {

0 commit comments

Comments
 (0)