Skip to content
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
21 changes: 11 additions & 10 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func Provider() *schema.Provider {
p := &schema.Provider{
Schema: map[string]*schema.Schema{
"token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GITHUB_TOKEN", nil),
Description: descriptions["token"],
ExactlyOneOf: []string{"app_auth"},
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GITHUB_TOKEN", nil),
Description: descriptions["token"],
ConflictsWith: []string{"app_auth"},
},
"owner": {
Type: schema.TypeString,
Expand Down Expand Up @@ -94,11 +94,11 @@ func Provider() *schema.Provider {
Description: descriptions["parallel_requests"],
},
"app_auth": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: descriptions["app_auth"],
ExactlyOneOf: []string{"token"},
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: descriptions["app_auth"],
ConflictsWith: []string{"token"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Expand Down Expand Up @@ -505,6 +505,7 @@ func tokenFromGHCLI(u *url.URL) string {

out, err := exec.Command(ghCliPath, "auth", "token", "--hostname", host).Output()
if err != nil {
log.Printf("[DEBUG] Error getting token from GitHub CLI: %s", err.Error())
// GH CLI is either not installed or there was no `gh auth login` command issued,
// which is fine. don't return the error to keep the flow going
return ""
Expand Down
20 changes: 11 additions & 9 deletions github/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func TestAccProviderConfigure(t *testing.T) {
t.Run("can be configured to run anonymously", func(t *testing.T) {
config := `
provider "github" {
token = ""
}
data "github_ip_ranges" "test" {}
`

resource.Test(t, resource.TestCase{
PreCheck: func() { t.Setenv("GITHUB_TOKEN", ""); t.Setenv("GH_PATH", "none-existent-path") },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -161,10 +161,15 @@ func TestAccProviderConfigure(t *testing.T) {
provider "github" {
token = "%s"
base_url = "%s"
}`, testAccConf.token, testAccConf.owner)
}`, testAccConf.token, testAccConf.baseURL)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, individual) },
PreCheck: func() {
skipUnlessMode(t, enterprise)
if testAccConf.baseURL.Host != "api.github.com" {
t.Skip("Skipping as test mode is not GHES")
}
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand All @@ -187,7 +192,6 @@ func TestAccProviderConfigure(t *testing.T) {
`, testAccConf.owner, testMaxRetries)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand All @@ -211,7 +215,6 @@ func TestAccProviderConfigure(t *testing.T) {
`, testAccConf.owner, testMaxPerPage)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -243,12 +246,11 @@ func TestAccProviderConfigure(t *testing.T) {
`, testAccConf.owner, testAccConf.token)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("only one of `app_auth,token` can be specified"),
ExpectError: regexp.MustCompile(`"app_auth": conflicts with token`),
},
},
})
Expand All @@ -268,12 +270,12 @@ func TestAccProviderConfigure(t *testing.T) {
`, testAccConf.owner)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t); t.Setenv("GITHUB_TOKEN", "1234567890") },
PreCheck: func() { t.Setenv("GITHUB_TOKEN", "1234567890") },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("only one of `app_auth,token` can be specified"),
ExpectError: regexp.MustCompile(`"token": conflicts with app_auth`),
},
},
})
Expand Down