Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
})
}
4 changes: 4 additions & 0 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down