From eb53970568193441029332fea6d44d5ebde17e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sj=C3=B6sten?= Date: Wed, 12 Jan 2022 13:41:31 +0100 Subject: [PATCH 1/4] Make data_github_repository work with non-existing repositories --- github/data_source_github_repository.go | 10 ++++++++++ github/data_source_github_repository_test.go | 18 ++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/github/data_source_github_repository.go b/github/data_source_github_repository.go index 9a4d1b8896..c51908c8f4 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/v41/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("Error reading GitHub branch reference %s/%s: %s", owner, repoName, err) + 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..1db23e538c 100644 --- a/github/data_source_github_repository_test.go +++ b/github/data_source_github_repository_test.go @@ -55,29 +55,35 @@ func TestAccGithubRepositoryDataSource(t *testing.T) { config := map[string]string{ "no_match_name": ` - data "github_repository" "no_match_name" { + data "github_repository" "test" { name = "owner/repo" } `, "no_match_fullname": ` - data "github_repository" "no_match_fullname" { + data "github_repository" "test" { full_name = "owner/repo" } `, } + 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["no_match_name"], - ExpectError: regexp.MustCompile(`Not Found`), + Config: config["no_match_name"], + Check: check, }, { - Config: config["no_match_fullname"], - ExpectError: regexp.MustCompile(`Not Found`), + Config: config["no_match_fullname"], + Check: check, }, }, }) From 63f0bfcb9fc30a2ecf1d20b33820a9447ba66f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sj=C3=B6sten?= Date: Sat, 12 Feb 2022 09:35:07 +0100 Subject: [PATCH 2/4] Improve 404 logging --- github/data_source_github_branch.go | 2 +- github/data_source_github_repository.go | 2 +- 2 files changed, 2 insertions(+), 2 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 c51908c8f4..f7f53e5f42 100644 --- a/github/data_source_github_repository.go +++ b/github/data_source_github_repository.go @@ -208,7 +208,7 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) er if err != nil { if err, ok := err.(*github.ErrorResponse); ok { if err.Response.StatusCode == http.StatusNotFound { - log.Printf("Error reading GitHub branch reference %s/%s: %s", owner, repoName, err) + log.Printf("[DEBUG] Missing GitHub repository %s/%s", owner, repoName) d.SetId("") return nil } From 48b85ef49c0617b093a5ee833348944942477329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sj=C3=B6sten?= Date: Sat, 12 Feb 2022 09:52:04 +0100 Subject: [PATCH 3/4] Remove obsolete test --- github/data_source_github_repository_test.go | 51 -------------------- 1 file changed, 51 deletions(-) diff --git a/github/data_source_github_repository_test.go b/github/data_source_github_repository_test.go index 1db23e538c..fb684647b8 100644 --- a/github/data_source_github_repository_test.go +++ b/github/data_source_github_repository_test.go @@ -51,57 +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" "test" { - name = "owner/repo" - } - `, - "no_match_fullname": ` - data "github_repository" "test" { - full_name = "owner/repo" - } - `, - } - - 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["no_match_name"], - Check: check, - }, - { - Config: config["no_match_fullname"], - Check: check, - }, - }, - }) - } - - 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) From 214cc63ac33d5d6cc70d49a555d632d493f83190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sj=C3=B6sten?= Date: Wed, 2 Mar 2022 10:14:19 +0100 Subject: [PATCH 4/4] Update dependency --- github/data_source_github_repository.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/data_source_github_repository.go b/github/data_source_github_repository.go index f7f53e5f42..ff592c0ec9 100644 --- a/github/data_source_github_repository.go +++ b/github/data_source_github_repository.go @@ -7,7 +7,7 @@ import ( "net/http" "strings" - "github.com/google/go-github/v41/github" + "github.com/google/go-github/v42/github" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" )