Skip to content

Commit

Permalink
Make data_github_repository work with non-existing repositories (#1031)
Browse files Browse the repository at this point in the history
* Make data_github_repository work with non-existing repositories

* Improve 404 logging

* Remove obsolete test

* Update dependency
  • Loading branch information
tobiassjosten authored Mar 3, 2022
1 parent 444448d commit cae33d2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 46 deletions.
2 changes: 1 addition & 1 deletion github/data_source_github_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions github/data_source_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down
45 changes: 0 additions & 45 deletions github/data_source_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cae33d2

Please sign in to comment.