Skip to content

Commit 9e4b14c

Browse files
committed
Add retry to repo creation as the API is eventually consistent.
Resolves #2604 Signed-off-by: Timo Sand <[email protected]>
1 parent d7cda77 commit 9e4b14c

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

github/resource_github_repository.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/google/go-github/v67 v67.0.0
1010
github.com/google/uuid v1.6.0
1111
github.com/hashicorp/go-cty v1.5.0
12+
github.com/hashicorp/terraform-plugin-log v0.9.0
1213
github.com/hashicorp/terraform-plugin-sdk/v2 v2.38.1
1314
github.com/shurcooL/githubv4 v0.0.0-20221126192849-0b5c4c7994eb
1415
github.com/stretchr/testify v1.11.1
@@ -129,7 +130,6 @@ require (
129130
github.com/hashicorp/terraform-exec v0.23.1 // indirect
130131
github.com/hashicorp/terraform-json v0.27.1 // indirect
131132
github.com/hashicorp/terraform-plugin-go v0.29.0 // indirect
132-
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
133133
github.com/hashicorp/terraform-registry-address v0.4.0 // indirect
134134
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
135135
github.com/hashicorp/yamux v0.1.2 // indirect

0 commit comments

Comments
 (0)