From c240bccda92652f74cffc98983aa3b0709b7b404 Mon Sep 17 00:00:00 2001 From: Naden Date: Thu, 3 Jun 2021 14:53:58 +1000 Subject: [PATCH] Return nil instead of 404 error if branch does not exist --- github/data_source_github_branch.go | 19 ++++++---- github/data_source_github_branch_test.go | 47 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/github/data_source_github_branch.go b/github/data_source_github_branch.go index 3fd9845722..8d01225a74 100644 --- a/github/data_source_github_branch.go +++ b/github/data_source_github_branch.go @@ -2,9 +2,10 @@ package github import ( "context" - "fmt" "log" + "net/http" + "github.com/google/go-github/v35/github" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) @@ -46,13 +47,17 @@ func dataSourceGithubBranchRead(d *schema.ResourceData, meta interface{}) error branchName := d.Get("branch").(string) branchRefName := "refs/heads/" + branchName - log.Printf("[DEBUG] Reading GitHub branch reference %s/%s (%s)", - orgName, repoName, branchRefName) - ref, resp, err := client.Git.GetRef( - context.TODO(), orgName, repoName, branchRefName) + log.Printf("[DEBUG] Reading GitHub branch reference %s/%s (%s)", orgName, repoName, branchRefName) + ref, resp, err := client.Git.GetRef(context.TODO(), orgName, repoName, branchRefName) if err != nil { - return fmt.Errorf("Error reading GitHub branch reference %s/%s (%s): %s", - orgName, repoName, branchRefName, err) + if err, ok := err.(*github.ErrorResponse); ok { + if err.Response.StatusCode == http.StatusNotFound { + log.Printf("Error reading GitHub branch reference %s/%s (%s): %s", orgName, repoName, branchRefName, err) + d.SetId("") + return nil + } + } + return err } d.SetId(buildTwoPartID(repoName, branchName)) diff --git a/github/data_source_github_branch_test.go b/github/data_source_github_branch_test.go index a958edba5f..a90dda10bb 100644 --- a/github/data_source_github_branch_test.go +++ b/github/data_source_github_branch_test.go @@ -59,4 +59,51 @@ func TestAccGithubBranchDataSource(t *testing.T) { }) }) + + t.Run("queries an invalid branch without error", func(t *testing.T) { + + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-%[1]s" + auto_init = true + } + + data "github_branch" "test" { + repository = github_repository.test.id + branch = "xxxxxx" + } + `, randomID) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckNoResourceAttr( + "data.github_branch.test", "id", + ), + ) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + + }) }