@@ -8,9 +8,12 @@ import (
88 "net/http"
99 "regexp"
1010 "strings"
11+ "time"
1112
1213 "github.com/google/go-github/v67/github"
14+ "github.com/hashicorp/terraform-plugin-log/tflog"
1315 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
16+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1417 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1518 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1619)
@@ -752,6 +755,12 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta any) error {
752755 d .SetId (repo .GetName ())
753756 }
754757
758+ retryErr := waitForRepositoryToBeCreated (ctx , client , owner , repoName )
759+
760+ if retryErr != nil {
761+ return fmt .Errorf ("error waiting for repository to be created: %w" , retryErr )
762+ }
763+
755764 topics := repoReq .Topics
756765 if len (topics ) > 0 {
757766 _ , _ , err := client .Repositories .ReplaceAllTopics (ctx , owner , repoName , topics )
@@ -1251,3 +1260,29 @@ func updateVulnerabilityAlerts(d *schema.ResourceData, client *github.Client, ct
12511260 _ , err := updateVulnerabilityAlerts (ctx , owner , repoName )
12521261 return err
12531262}
1263+
1264+ // 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.
1265+ // Resolves https://github.com/integrations/terraform-provider-github/issues/2604
1266+ func waitForRepositoryToBeCreated (ctx context.Context , client * github.Client , owner , repoName string ) error {
1267+ timeout := 2 * time .Minute
1268+ return retry .RetryContext (ctx , timeout , func () * retry.RetryError {
1269+ tflog .Info (ctx , fmt .Sprintf ("Waiting for repository to be created: %s/%s" , owner , repoName ))
1270+ _ , resp , err := client .Repositories .Get (ctx , owner , repoName )
1271+ if err != nil {
1272+ tflog .Debug (ctx , fmt .Sprintf ("Error getting repository: %s" , err ))
1273+ if resp .StatusCode == http .StatusForbidden {
1274+ return retry .NonRetryableError (fmt .Errorf ("forbidden from accessing repository: %w" , err ))
1275+ }
1276+ return retry .RetryableError (fmt .Errorf ("expected repository to be created but was in state %s" , resp .Status ))
1277+
1278+ }
1279+
1280+ if resp .StatusCode != http .StatusOK && resp .StatusCode != http .StatusMovedPermanently {
1281+ return retry .RetryableError (fmt .Errorf ("expected repository to be created but was in state %s" , resp .Status ))
1282+ }
1283+
1284+ tflog .Info (ctx , fmt .Sprintf ("Repository created: %s/%s" , owner , repoName ))
1285+
1286+ return nil
1287+ })
1288+ }
0 commit comments