Skip to content

Commit

Permalink
fix: GitHub Repository Update might fail when Pages enabled (#1716)
Browse files Browse the repository at this point in the history
Co-authored-by: Keegan Campbell <[email protected]>
  • Loading branch information
0x46616c6b and kfcampbell authored Jun 16, 2023
1 parent 42ebbe0 commit 6aed389
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,16 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
if d.HasChange("pages") && !d.IsNewResource() {
opts := expandPagesUpdate(d.Get("pages").([]interface{}))
if opts != nil {
_, err := client.Repositories.UpdatePages(ctx, owner, repoName, opts)
pages, res, err := client.Repositories.GetPagesInfo(ctx, owner, repoName)
if res.StatusCode != http.StatusNotFound && err != nil {
return err
}

if pages == nil {
_, _, err = client.Repositories.EnablePages(ctx, owner, repoName, &github.Pages{Source: opts.Source, BuildType: opts.BuildType})
} else {
_, err = client.Repositories.UpdatePages(ctx, owner, repoName, opts)
}
if err != nil {
return err
}
Expand Down Expand Up @@ -827,17 +836,26 @@ func expandPages(input []interface{}) *github.Pages {
return nil
}
pages := input[0].(map[string]interface{})
pagesSource := pages["source"].([]interface{})[0].(map[string]interface{})
source := &github.PagesSource{
Branch: github.String(pagesSource["branch"].(string)),
}
if v, ok := pagesSource["path"].(string); ok {
// To set to the root directory "/", leave source.Path unset
if v != "" && v != "/" {
source.Path = github.String(v)
var source *github.PagesSource
if pagesSource, ok := pages["source"].([]interface{})[0].(map[string]interface{}); ok {
if v, ok := pagesSource["branch"].(string); ok {
if v != "" {
source.Branch = github.String(v)
}
}
if v, ok := pagesSource["path"].(string); ok {
// To set to the root directory "/", leave source.Path unset
if v != "" && v != "/" {
source.Path = github.String(v)
}
}
}
return &github.Pages{Source: source}
var buildType *string
if v, ok := pages["build_type"].(string); ok {
buildType = github.String(v)
}

return &github.Pages{Source: source, BuildType: buildType}
}

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

// Only set the github.PagesUpdate BuildType field if the value is a non-empty string.
if v, ok := pages["build_type"].(string); ok && v != "" {
update.BuildType = github.String(v)
}

// To update the GitHub Pages source, the github.PagesUpdate Source field
// must include the branch name and optionally the subdirectory /docs.
// e.g. "master" or "master /docs"
pagesSource := pages["source"].([]interface{})[0].(map[string]interface{})
sourceBranch := pagesSource["branch"].(string)
sourcePath := ""
if v, ok := pagesSource["path"].(string); ok {
if v != "" && v != "/" {
// This is only necessary if the BuildType is "legacy".
if update.BuildType == nil || *update.BuildType == "legacy" {
pagesSource := pages["source"].([]interface{})[0].(map[string]interface{})
sourceBranch := pagesSource["branch"].(string)
sourcePath := ""
if v, ok := pagesSource["path"].(string); ok && v != "" {
sourcePath = v
}
update.Source = &github.PagesSource{Branch: &sourceBranch, Path: &sourcePath}
}
update.Source = &github.PagesSource{Branch: &sourceBranch, Path: &sourcePath}

pagesBuildType := pages["build_type"].(string)
update.BuildType = &pagesBuildType

return update
}
Expand Down

0 comments on commit 6aed389

Please sign in to comment.