diff --git a/github/config.go b/github/config.go index a61dd541b7..868d13dcc0 100644 --- a/github/config.go +++ b/github/config.go @@ -35,6 +35,7 @@ type Owner struct { v3client *github.Client v4client *githubv4.Client StopContext context.Context + IsGHES bool IsOrganization bool } @@ -164,6 +165,7 @@ func (c *Config) Meta() (any, error) { owner.v4client = v4client owner.v3client = v3client owner.StopContext = context.Background() + owner.IsGHES = c.IsGHES _, err = c.ConfigureOwner(&owner) if err != nil { diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index 9d756ac6c9..8f0079d7d4 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -978,7 +978,12 @@ func resourceGithubRepositoryUpdate(ctx context.Context, d *schema.ResourceData, 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 meta.(*Owner).IsGHES { + ghesOpts := updatePagesWithoutCNAME(opts) + _, err = client.Repositories.UpdatePagesGHES(ctx, owner, repoName, ghesOpts) + } else { + _, err = client.Repositories.UpdatePages(ctx, owner, repoName, opts) + } } if err != nil { return diag.FromErr(err) @@ -1129,6 +1134,19 @@ func expandPagesUpdate(input []any) *github.PagesUpdate { return update } +func updatePagesWithoutCNAME(update *github.PagesUpdate) *github.PagesUpdateWithoutCNAME { + if update == nil { + return nil + } + + return &github.PagesUpdateWithoutCNAME{ + BuildType: update.BuildType, + Source: update.Source, + Public: update.Public, + HTTPSEnforced: update.HTTPSEnforced, + } +} + func flattenPages(pages *github.Pages) []any { if pages == nil { return []any{} diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 1589c84bf1..c49309f43c 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -953,6 +953,64 @@ resource "github_repository" "test" { }) }) + t.Run("updates repository pages from legacy to workflow", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + testRepoName := fmt.Sprintf("%spages-2087-%s", testResourcePrefix, randomID) + testVisibility := "private" + if testAccConf.authMode == individual { + testVisibility = "public" + } + + configs := map[string]string{ + "legacy": fmt.Sprintf(` + resource "github_repository" "test" { + name = "%s" + visibility = "%s" + auto_init = true + + pages { + build_type = "legacy" + + source { + branch = "main" + path = "/" + } + } + } + `, testRepoName, testVisibility), + "workflow": fmt.Sprintf(` + resource "github_repository" "test" { + name = "%s" + visibility = "%s" + auto_init = true + + pages { + build_type = "workflow" + } + } + `, testRepoName, testVisibility), + } + + resource.Test(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: configs["legacy"], + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_repository.test", "pages.0.source.0.branch", "main"), + resource.TestCheckResourceAttr("github_repository.test", "pages.0.source.0.path", "/"), + ), + }, + { + Config: configs["workflow"], + Check: resource.ComposeTestCheckFunc( + resource.TestCheckNoResourceAttr("github_repository.test", "pages.0.source.#"), + ), + }, + }, + }) + }) + t.Run("manages the security feature for a private repository", func(t *testing.T) { if !testAccConf.testAdvancedSecurity { t.Skip("Advanced Security is not enabled for this account")