From cae33d218ba895b0671e2f8a49579d495b8d58ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sj=C3=B6sten?= <65159+tobiassjosten@users.noreply.github.com> Date: Thu, 3 Mar 2022 21:53:06 +0100 Subject: [PATCH] Make data_github_repository work with non-existing repositories (#1031) * Make data_github_repository work with non-existing repositories * Improve 404 logging * Remove obsolete test * Update dependency --- github/data_source_github_branch.go | 2 +- github/data_source_github_repository.go | 10 +++++ github/data_source_github_repository_test.go | 45 -------------------- 3 files changed, 11 insertions(+), 46 deletions(-) diff --git a/github/data_source_github_branch.go b/github/data_source_github_branch.go index 38177fe92d..00ea312fa8 100644 --- a/github/data_source_github_branch.go +++ b/github/data_source_github_branch.go @@ -51,7 +51,7 @@ func dataSourceGithubBranchRead(d *schema.ResourceData, meta interface{}) error if err != nil { if err, ok := err.(*github.ErrorResponse); ok { if err.Response.StatusCode == http.StatusNotFound { - log.Printf("[INFO] Error reading GitHub branch reference %s/%s (%s): %s", orgName, repoName, branchRefName, err) + log.Printf("[DEBUG] Missing GitHub branch %s/%s (%s)", orgName, repoName, branchRefName) d.SetId("") return nil } diff --git a/github/data_source_github_repository.go b/github/data_source_github_repository.go index 9a4d1b8896..ff592c0ec9 100644 --- a/github/data_source_github_repository.go +++ b/github/data_source_github_repository.go @@ -3,8 +3,11 @@ package github import ( "context" "fmt" + "log" + "net/http" "strings" + "github.com/google/go-github/v42/github" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) @@ -203,6 +206,13 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) er repo, _, err := client.Repositories.Get(context.TODO(), owner, repoName) if err != nil { + if err, ok := err.(*github.ErrorResponse); ok { + if err.Response.StatusCode == http.StatusNotFound { + log.Printf("[DEBUG] Missing GitHub repository %s/%s", owner, repoName) + d.SetId("") + return nil + } + } return err } diff --git a/github/data_source_github_repository_test.go b/github/data_source_github_repository_test.go index 7350084293..fb684647b8 100644 --- a/github/data_source_github_repository_test.go +++ b/github/data_source_github_repository_test.go @@ -51,51 +51,6 @@ func TestAccGithubRepositoryDataSource(t *testing.T) { }) - t.Run("raises expected errors when querying for a repository", func(t *testing.T) { - - config := map[string]string{ - "no_match_name": ` - data "github_repository" "no_match_name" { - name = "owner/repo" - } - `, - "no_match_fullname": ` - data "github_repository" "no_match_fullname" { - full_name = "owner/repo" - } - `, - } - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config["no_match_name"], - ExpectError: regexp.MustCompile(`Not Found`), - }, - { - Config: config["no_match_fullname"], - ExpectError: regexp.MustCompile(`Not Found`), - }, - }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) - }) - - 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) - }) - }) - t.Run("queries a repository with pages configured", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)