From 3c3e1104304c4314dd403d78677f0fa937162aa9 Mon Sep 17 00:00:00 2001 From: Timo Sand Date: Fri, 2 Jan 2026 13:40:20 +0200 Subject: [PATCH 1/2] Add SKIP to template repo test if not configured Signed-off-by: Timo Sand --- github/resource_github_repository_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 2d44eaa0e4..178a4837a2 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -439,6 +439,10 @@ func TestAccGithubRepository(t *testing.T) { }) t.Run("creates a repository using an org template", func(t *testing.T) { + if len(testAccConf.testOrgTemplateRepository) == 0 { + t.Skip("No org template repository provided") + } + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { From 379dc91782e6da482d0970d7af30be1ca02659dc Mon Sep 17 00:00:00 2001 From: Timo Sand Date: Fri, 2 Jan 2026 14:17:36 +0200 Subject: [PATCH 2/2] Add retry to repo creation as the API is eventually consistent. Resolves #2604 Signed-off-by: Timo Sand --- github/resource_github_repository.go | 35 ++++++++++++++++++++++++++++ go.mod | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index 3e27bfb824..4f6fc0f06a 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -8,10 +8,13 @@ import ( "net/http" "regexp" "strings" + "time" "github.com/google/go-github/v81/github" + "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) @@ -759,6 +762,12 @@ func resourceGithubRepositoryCreate(ctx context.Context, d *schema.ResourceData, d.SetId(repo.GetName()) } + retryErr := waitForRepositoryToBeCreated(ctx, client, owner, repoName) + + if retryErr != nil { + return diag.Errorf("error waiting for repository to be created: %s", retryErr.Error()) + } + topics := repoReq.Topics if len(topics) > 0 { _, _, err := client.Repositories.ReplaceAllTopics(ctx, owner, repoName, topics) @@ -1270,3 +1279,29 @@ func updateVulnerabilityAlerts(d *schema.ResourceData, client *github.Client, ct } return err } + +// The Repository Create API doesn't wait for the repository to be created, so we need to enable a retry mechanism to wait for it. +// Resolves https://github.com/integrations/terraform-provider-github/issues/2604 +func waitForRepositoryToBeCreated(ctx context.Context, client *github.Client, owner, repoName string) error { + timeout := 5 * time.Minute + return retry.RetryContext(ctx, timeout, func() *retry.RetryError { + tflog.Info(ctx, fmt.Sprintf("Waiting for repository to be created: %s/%s", owner, repoName)) + _, resp, err := client.Repositories.Get(ctx, owner, repoName) + if err != nil { + tflog.Debug(ctx, fmt.Sprintf("Error getting repository: %s", err)) + if resp.StatusCode == http.StatusForbidden { + return retry.NonRetryableError(fmt.Errorf("forbidden from accessing repository: %w", err)) + } + return retry.RetryableError(fmt.Errorf("expected repository to be created but was in state %s", resp.Status)) + + } + + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusMovedPermanently { + return retry.RetryableError(fmt.Errorf("expected repository to be created but was in state %s", resp.Status)) + } + + tflog.Info(ctx, fmt.Sprintf("Repository created: %s/%s", owner, repoName)) + + return nil + }) +} diff --git a/go.mod b/go.mod index f67d5e9ce1..c67609c969 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/google/go-github/v81 v81.0.0 github.com/google/uuid v1.6.0 github.com/hashicorp/go-cty v1.5.0 + github.com/hashicorp/terraform-plugin-log v0.9.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.38.1 github.com/shurcooL/githubv4 v0.0.0-20221126192849-0b5c4c7994eb github.com/stretchr/testify v1.11.1 @@ -39,7 +40,6 @@ require ( github.com/hashicorp/terraform-exec v0.23.1 // indirect github.com/hashicorp/terraform-json v0.27.1 // indirect github.com/hashicorp/terraform-plugin-go v0.29.0 // indirect - github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect github.com/hashicorp/terraform-registry-address v0.4.0 // indirect github.com/hashicorp/terraform-svchost v0.1.1 // indirect github.com/hashicorp/yamux v0.1.2 // indirect