Skip to content

Commit 2cb5135

Browse files
authored
Give a helpful error message when trying to import a github_branch that does not exist (#734)
* Add tests for importing github_branch - check importing a branch that exists - check error message when importing a branch that does not exist * Add error message for importing github_branch There was no error message if the branch the user was trying to import did not exist on GitHub, so the user got the default terraform error: Error: nil entry in ImportState results. This is always a bug with the resource that is being imported. Please report this as a bug to Terraform. The new error message is: Error: Repository tftest does not have a branch named nosuchbranch.
1 parent 237d951 commit 2cb5135

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

github/resource_github_branch.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,15 @@ func resourceGithubBranchImport(d *schema.ResourceData, meta interface{}) ([]*sc
183183

184184
d.Set("source_branch", sourceBranch)
185185

186-
return []*schema.ResourceData{d}, resourceGithubBranchRead(d, meta)
186+
err = resourceGithubBranchRead(d, meta)
187+
if err != nil {
188+
return nil, err
189+
}
190+
191+
// resourceGithubBranchRead calls d.SetId("") if the branch does not exist
192+
if d.Id() == "" {
193+
return nil, fmt.Errorf("Repository %s does not have a branch named %s.", repoName, branchName)
194+
}
195+
196+
return []*schema.ResourceData{d}, nil
187197
}

github/resource_github_branch_test.go

+40-8
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ func TestAccGithubBranch(t *testing.T) {
1818
config := fmt.Sprintf(`
1919
resource "github_repository" "test" {
2020
name = "tf-acc-test-%[1]s"
21-
auto_init = true
21+
auto_init = true
2222
}
2323
2424
resource "github_branch" "test" {
2525
repository = github_repository.test.id
26-
branch = "tf-acc-test-%[1]s"
26+
branch = "test"
2727
}
2828
`, randomID)
2929

3030
check := resource.ComposeTestCheckFunc(
3131
resource.TestMatchResourceAttr(
32-
"github_branch.test", "id", regexp.MustCompile(randomID),
32+
"github_branch.test", "id",
33+
regexp.MustCompile(fmt.Sprintf("tf-acc-test-%s:test", randomID)),
3334
),
3435
)
3536

@@ -42,6 +43,21 @@ func TestAccGithubBranch(t *testing.T) {
4243
Config: config,
4344
Check: check,
4445
},
46+
{
47+
ResourceName: "github_branch.test",
48+
ImportState: true,
49+
ImportStateId: fmt.Sprintf("tf-acc-test-%s:test", randomID),
50+
ImportStateVerify: true,
51+
ImportStateVerifyIgnore: []string{"source_sha"},
52+
},
53+
{
54+
ResourceName: "github_branch.test",
55+
ImportState: true,
56+
ImportStateId: fmt.Sprintf("tf-acc-test-%s:nonsense", randomID),
57+
ExpectError: regexp.MustCompile(
58+
"Repository tf-acc-test-[a-z0-9]* does not have a branch named nonsense.",
59+
),
60+
},
4561
},
4662
})
4763
}
@@ -65,24 +81,25 @@ func TestAccGithubBranch(t *testing.T) {
6581
config := fmt.Sprintf(`
6682
resource "github_repository" "test" {
6783
name = "tf-acc-test-%[1]s"
68-
auto_init = true
84+
auto_init = true
6985
}
7086
7187
resource "github_branch" "source" {
7288
repository = github_repository.test.id
73-
branch = "tf-acc-test-%[1]s-source"
89+
branch = "source"
7490
}
7591
7692
resource "github_branch" "test" {
77-
repository = github_repository.test.id
93+
repository = github_repository.test.id
7894
source_branch = github_branch.source.branch
79-
branch = "tf-acc-test-%[1]s"
95+
branch = "test"
8096
}
8197
`, randomID)
8298

8399
check := resource.ComposeTestCheckFunc(
84100
resource.TestMatchResourceAttr(
85-
"github_branch.test", "id", regexp.MustCompile(randomID),
101+
"github_branch.test", "id",
102+
regexp.MustCompile(fmt.Sprintf("tf-acc-test-%s:test", randomID)),
86103
),
87104
)
88105

@@ -95,6 +112,21 @@ func TestAccGithubBranch(t *testing.T) {
95112
Config: config,
96113
Check: check,
97114
},
115+
{
116+
ResourceName: "github_branch.test",
117+
ImportState: true,
118+
ImportStateId: fmt.Sprintf("tf-acc-test-%s:test:source", randomID),
119+
ImportStateVerify: true,
120+
ImportStateVerifyIgnore: []string{"source_sha"},
121+
},
122+
{
123+
ResourceName: "github_branch.test",
124+
ImportState: true,
125+
ImportStateId: fmt.Sprintf("tf-acc-test-%s:nonsense:source", randomID),
126+
ExpectError: regexp.MustCompile(
127+
"Repository tf-acc-test-[a-z0-9]* does not have a branch named nonsense.",
128+
),
129+
},
98130
},
99131
})
100132
}

0 commit comments

Comments
 (0)