Skip to content

Commit 4f7109e

Browse files
authored
Return nil instead of 404 error if branch does not exist (#806)
1 parent b2f41b2 commit 4f7109e

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

github/data_source_github_branch.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package github
22

33
import (
44
"context"
5-
"fmt"
65
"log"
6+
"net/http"
77

8+
"github.com/google/go-github/v35/github"
89
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
910
)
1011

@@ -46,13 +47,17 @@ func dataSourceGithubBranchRead(d *schema.ResourceData, meta interface{}) error
4647
branchName := d.Get("branch").(string)
4748
branchRefName := "refs/heads/" + branchName
4849

49-
log.Printf("[DEBUG] Reading GitHub branch reference %s/%s (%s)",
50-
orgName, repoName, branchRefName)
51-
ref, resp, err := client.Git.GetRef(
52-
context.TODO(), orgName, repoName, branchRefName)
50+
log.Printf("[DEBUG] Reading GitHub branch reference %s/%s (%s)", orgName, repoName, branchRefName)
51+
ref, resp, err := client.Git.GetRef(context.TODO(), orgName, repoName, branchRefName)
5352
if err != nil {
54-
return fmt.Errorf("Error reading GitHub branch reference %s/%s (%s): %s",
55-
orgName, repoName, branchRefName, err)
53+
if err, ok := err.(*github.ErrorResponse); ok {
54+
if err.Response.StatusCode == http.StatusNotFound {
55+
log.Printf("Error reading GitHub branch reference %s/%s (%s): %s", orgName, repoName, branchRefName, err)
56+
d.SetId("")
57+
return nil
58+
}
59+
}
60+
return err
5661
}
5762

5863
d.SetId(buildTwoPartID(repoName, branchName))

github/data_source_github_branch_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,51 @@ func TestAccGithubBranchDataSource(t *testing.T) {
5959
})
6060

6161
})
62+
63+
t.Run("queries an invalid branch without error", func(t *testing.T) {
64+
65+
config := fmt.Sprintf(`
66+
resource "github_repository" "test" {
67+
name = "tf-acc-test-%[1]s"
68+
auto_init = true
69+
}
70+
71+
data "github_branch" "test" {
72+
repository = github_repository.test.id
73+
branch = "xxxxxx"
74+
}
75+
`, randomID)
76+
77+
check := resource.ComposeTestCheckFunc(
78+
resource.TestCheckNoResourceAttr(
79+
"data.github_branch.test", "id",
80+
),
81+
)
82+
83+
testCase := func(t *testing.T, mode string) {
84+
resource.Test(t, resource.TestCase{
85+
PreCheck: func() { skipUnlessMode(t, mode) },
86+
Providers: testAccProviders,
87+
Steps: []resource.TestStep{
88+
{
89+
Config: config,
90+
Check: check,
91+
},
92+
},
93+
})
94+
}
95+
96+
t.Run("with an anonymous account", func(t *testing.T) {
97+
t.Skip("anonymous account not supported for this operation")
98+
})
99+
100+
t.Run("with an individual account", func(t *testing.T) {
101+
testCase(t, individual)
102+
})
103+
104+
t.Run("with an organization account", func(t *testing.T) {
105+
testCase(t, organization)
106+
})
107+
108+
})
62109
}

0 commit comments

Comments
 (0)