Skip to content

Commit 379dc91

Browse files
committed
Add retry to repo creation as the API is eventually consistent.
Resolves #2604 Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 3c3e110 commit 379dc91

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

github/resource_github_repository.go

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

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/google/go-github/v81 v81.0.0
88
github.com/google/uuid v1.6.0
99
github.com/hashicorp/go-cty v1.5.0
10+
github.com/hashicorp/terraform-plugin-log v0.9.0
1011
github.com/hashicorp/terraform-plugin-sdk/v2 v2.38.1
1112
github.com/shurcooL/githubv4 v0.0.0-20221126192849-0b5c4c7994eb
1213
github.com/stretchr/testify v1.11.1
@@ -39,7 +40,6 @@ require (
3940
github.com/hashicorp/terraform-exec v0.23.1 // indirect
4041
github.com/hashicorp/terraform-json v0.27.1 // indirect
4142
github.com/hashicorp/terraform-plugin-go v0.29.0 // indirect
42-
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
4343
github.com/hashicorp/terraform-registry-address v0.4.0 // indirect
4444
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
4545
github.com/hashicorp/yamux v0.1.2 // indirect

0 commit comments

Comments
 (0)