@@ -8,10 +8,13 @@ import (
88 "net/http"
99 "regexp"
1010 "strings"
11+ "time"
1112
1213 "github.com/google/go-github/v81/github"
14+ "github.com/hashicorp/terraform-plugin-log/tflog"
1315 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1416 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
17+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1518 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1619 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1720)
@@ -759,6 +762,12 @@ func resourceGithubRepositoryCreate(ctx context.Context, d *schema.ResourceData,
759762 d .SetId (repo .GetName ())
760763 }
761764
765+ retryErr := waitForRepositoryToBeCreated (ctx , client , owner , repoName )
766+
767+ if retryErr != nil {
768+ return diag .Errorf ("error waiting for repository to be created: %s" , retryErr .Error ())
769+ }
770+
762771 topics := repoReq .Topics
763772 if len (topics ) > 0 {
764773 _ , _ , err := client .Repositories .ReplaceAllTopics (ctx , owner , repoName , topics )
@@ -1270,3 +1279,29 @@ func updateVulnerabilityAlerts(d *schema.ResourceData, client *github.Client, ct
12701279 }
12711280 return err
12721281}
1282+
1283+ // 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.
1284+ // Resolves https://github.com/integrations/terraform-provider-github/issues/2604
1285+ func waitForRepositoryToBeCreated (ctx context.Context , client * github.Client , owner , repoName string ) error {
1286+ timeout := 5 * time .Minute
1287+ return retry .RetryContext (ctx , timeout , func () * retry.RetryError {
1288+ tflog .Info (ctx , fmt .Sprintf ("Waiting for repository to be created: %s/%s" , owner , repoName ))
1289+ _ , resp , err := client .Repositories .Get (ctx , owner , repoName )
1290+ if err != nil {
1291+ tflog .Debug (ctx , fmt .Sprintf ("Error getting repository: %s" , err ))
1292+ if resp .StatusCode == http .StatusForbidden {
1293+ return retry .NonRetryableError (fmt .Errorf ("forbidden from accessing repository: %w" , err ))
1294+ }
1295+ return retry .RetryableError (fmt .Errorf ("expected repository to be created but was in state %s" , resp .Status ))
1296+
1297+ }
1298+
1299+ if resp .StatusCode != http .StatusOK && resp .StatusCode != http .StatusMovedPermanently {
1300+ return retry .RetryableError (fmt .Errorf ("expected repository to be created but was in state %s" , resp .Status ))
1301+ }
1302+
1303+ tflog .Info (ctx , fmt .Sprintf ("Repository created: %s/%s" , owner , repoName ))
1304+
1305+ return nil
1306+ })
1307+ }
0 commit comments