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

[bug]: Fix hostname used with gh CLI for github.com #1919

Merged
merged 2 commits into from
Sep 26, 2023
Merged
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
15 changes: 8 additions & 7 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,13 @@ func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
token = appToken
}

isGithubDotCom, err := regexp.MatchString("^"+regexp.QuoteMeta("https://api.github.com"), baseURL)
if err != nil {
return nil, err
}

if token == "" {
ghAuthToken, err := tokenFromGhCli(baseURL)
ghAuthToken, err := tokenFromGhCli(baseURL, isGithubDotCom)
if err != nil {
return nil, fmt.Errorf("gh auth token: %w", err)
}
Expand All @@ -359,11 +364,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
log.Printf("[DEBUG] Setting read_delay_ms to %d", readDelay)

parallelRequests := d.Get("parallel_requests").(bool)
isGithubDotCom, err := regexp.MatchString("^"+regexp.QuoteMeta("https://api.github.com"), baseURL)

if err != nil {
return nil, err
}
if parallelRequests && isGithubDotCom {
return nil, fmt.Errorf("parallel_requests cannot be true when connecting to public github")
}
Expand Down Expand Up @@ -391,13 +392,13 @@ func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
}

// See https://github.com/integrations/terraform-provider-github/issues/1822
func tokenFromGhCli(baseURL string) (string, error) {
func tokenFromGhCli(baseURL string, isGithubDotCom bool) (string, error) {
ghCliPath := os.Getenv("GH_PATH")
if ghCliPath == "" {
ghCliPath = "gh"
}
hostname := ""
if baseURL == "" {
if isGithubDotCom {
hostname = "github.com"
} else {
parsedURL, err := url.Parse(baseURL)
Expand Down