Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make data_github_repository work with non-existing repositories #1031

Merged
merged 4 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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