Skip to content

Commit ffc0970

Browse files
nikwottonnickfloydkfcampbell
authored
fix(resource_github_branch): creating branch and repo with gitignore doesn't 422 [1284] (#1679)
Co-authored-by: Nick Floyd <[email protected]> Co-authored-by: Keegan Campbell <[email protected]>
1 parent 907e341 commit ffc0970

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

github/resource_github_branch.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ func resourceGithubBranchCreate(d *schema.ResourceData, meta interface{}) error
9494
Ref: &branchRefName,
9595
Object: &github.GitObject{SHA: &sourceBranchSHA},
9696
})
97-
if err != nil {
97+
// If the branch already exists, rather than erroring out just continue on to importing the branch
98+
// This avoids the case where a repo with gitignore_template and branch are being created at the same time crashing terraform
99+
if err != nil && !strings.HasSuffix(err.Error(), "422 Reference already exists []") {
98100
return fmt.Errorf("error creating GitHub branch reference %s/%s (%s): %s",
99101
orgName, repoName, branchRefName, err)
100102
}

github/resource_github_branch_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,70 @@ func TestAccGithubBranch(t *testing.T) {
7676

7777
})
7878

79+
t.Run("creates a branch named main directly and a repository with a gitignore_template", func(t *testing.T) {
80+
81+
config := fmt.Sprintf(`
82+
resource "github_repository" "test" {
83+
name = "tf-acc-test-%[1]s"
84+
auto_init = true
85+
gitignore_template = "Python"
86+
}
87+
88+
resource "github_branch" "test" {
89+
repository = github_repository.test.id
90+
branch = "main"
91+
}
92+
`, randomID)
93+
94+
check := resource.ComposeTestCheckFunc(
95+
resource.TestMatchResourceAttr(
96+
"github_branch.test", "id",
97+
regexp.MustCompile(fmt.Sprintf("tf-acc-test-%s:test", randomID)),
98+
),
99+
)
100+
101+
testCase := func(t *testing.T, mode string) {
102+
resource.Test(t, resource.TestCase{
103+
PreCheck: func() { skipUnlessMode(t, mode) },
104+
Providers: testAccProviders,
105+
Steps: []resource.TestStep{
106+
{
107+
Config: config,
108+
Check: check,
109+
},
110+
{
111+
ResourceName: "github_branch.test",
112+
ImportState: true,
113+
ImportStateId: fmt.Sprintf("tf-acc-test-%s:test", randomID),
114+
ImportStateVerify: true,
115+
ImportStateVerifyIgnore: []string{"source_sha"},
116+
},
117+
{
118+
ResourceName: "github_branch.test",
119+
ImportState: true,
120+
ImportStateId: fmt.Sprintf("tf-acc-test-%s:nonsense", randomID),
121+
ExpectError: regexp.MustCompile(
122+
"Repository tf-acc-test-[a-z0-9]* does not have a branch named nonsense.",
123+
),
124+
},
125+
},
126+
})
127+
}
128+
129+
t.Run("with an anonymous account", func(t *testing.T) {
130+
t.Skip("anonymous account not supported for this operation")
131+
})
132+
133+
t.Run("with an individual account", func(t *testing.T) {
134+
testCase(t, individual)
135+
})
136+
137+
t.Run("with an organization account", func(t *testing.T) {
138+
testCase(t, organization)
139+
})
140+
141+
})
142+
79143
t.Run("creates a branch from a source branch", func(t *testing.T) {
80144

81145
config := fmt.Sprintf(`

0 commit comments

Comments
 (0)