Skip to content

Commit f2686f5

Browse files
authored
[log] Add debug logging to GitHub API URL derivation (#6043)
## Summary Adds meaningful debug logging to `internal/envutil/github.go` for the `deriveAPIFromServerURL` function, which previously had **no logging** despite containing important control-flow decisions. ## Changes **File:** `internal/envutil/github.go` ### New logging in `deriveAPIFromServerURL` - Logs entry with the raw server URL being parsed - Logs when URL parsing fails or returns an empty host - Logs when an unsupported scheme (not `http`/`https`) is detected - Logs which resolution path was taken for each case: - `github.com` → default API URL - `*.ghe.com` tenant → `copilot-api.<tenant>.ghe.com` - GHES instance → `<host>/api/v3` ### New logging in `DeriveGitHubAPIURL` - Logs when falling back to the caller-provided `defaultURL` ## Why this matters When debugging "why is the gateway using the wrong GitHub API URL?" or "why isn't my GHEC tenant resolving correctly?", operators previously had no visibility into which resolution branch was taken. The new `DEBUG=envutil:*` log lines make this transparent. ## Testing - Pre-existing test failure in `internal/config` (`TestFetchAndFixSchema_NetworkError` — HTTP 403 network issue) confirmed to be unrelated and present on the base branch. - All other unit and integration tests pass. > [!WARNING] > <details> > <summary>Firewall blocked 1 domain</summary> > > The following domain was blocked by the firewall during workflow execution: > > - `invalidhostthatdoesnotexist12345.com` >> To allow these domains, add them to the `network.allowed` list in your workflow frontmatter: > > ```yaml > network: > allowed: > - defaults > - "invalidhostthatdoesnotexist12345.com" > ``` > > See [Network Configuration](https://github.github.com/gh-aw/reference/network/) for more information. > > </details> > Generated by [Go Logger Enhancement](https://github.com/github/gh-aw-mcpg/actions/runs/26132068575/agentic_workflow) · ● 5M · [◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+go-logger%22&type=pullrequests) <!-- gh-aw-agentic-workflow: Go Logger Enhancement, engine: copilot, version: 1.0.40, model: claude-sonnet-4.6, id: 26132068575, workflow_id: go-logger, run: https://github.com/github/gh-aw-mcpg/actions/runs/26132068575 --> <!-- gh-aw-workflow-id: go-logger -->
2 parents a2aac30 + d5a93a8 commit f2686f5

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

internal/envutil/github.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/github/gh-aw-mcpg/internal/logger"
10+
"github.com/github/gh-aw-mcpg/internal/logger/sanitize"
1011
)
1112

1213
var logGitHub = logger.New("envutil:github")
@@ -66,37 +67,50 @@ func DeriveGitHubAPIURL(defaultURL string) string {
6667
if serverURL := strings.TrimSpace(os.Getenv("GITHUB_SERVER_URL")); serverURL != "" {
6768
derived := deriveAPIFromServerURL(serverURL)
6869
if derived != "" {
69-
logGitHub.Printf("GitHub API URL derived from GITHUB_SERVER_URL=%s: %s", serverURL, derived)
70+
logGitHub.Printf("GitHub API URL derived from GITHUB_SERVER_URL=%s: %s", sanitize.RedactURL(serverURL), sanitize.RedactURL(derived))
7071
return derived
7172
}
7273
}
73-
return strings.TrimRight(strings.TrimSpace(defaultURL), "/")
74+
result := strings.TrimRight(strings.TrimSpace(defaultURL), "/")
75+
logGitHub.Printf("GitHub API URL falling back to provided default: %s", sanitize.RedactURL(result))
76+
return result
7477
}
7578

7679
// deriveAPIFromServerURL converts a GITHUB_SERVER_URL to the corresponding API endpoint.
7780
// GHEC tenants (*.ghe.com): https://tenant.ghe.com → https://copilot-api.tenant.ghe.com
7881
// GitHub.com: https://github.com → https://api.github.com
7982
// GHES (all others): https://github.example.com → https://github.example.com/api/v3
8083
func deriveAPIFromServerURL(serverURL string) string {
84+
logGitHub.Printf("Deriving API URL from server URL: %s", sanitize.RedactURL(serverURL))
85+
8186
parsed, err := url.Parse(strings.TrimRight(serverURL, "/"))
8287
if err != nil || parsed.Host == "" {
88+
logGitHub.Printf("Failed to parse server URL or empty host: serverURL=%s, err=%v", sanitize.RedactURL(serverURL), err)
8389
return ""
8490
}
8591
if parsed.Scheme != "http" && parsed.Scheme != "https" {
92+
logGitHub.Printf("Unsupported scheme in server URL: scheme=%s, serverURL=%s", parsed.Scheme, sanitize.RedactURL(serverURL))
8693
return ""
8794
}
8895

8996
hostname := strings.ToLower(parsed.Hostname())
9097

9198
switch {
9299
case hostname == "github.com" || hostname == "www.github.com":
100+
logGitHub.Printf("GitHub.com detected, using default API URL: %s", DefaultGitHubAPIBaseURL)
93101
return DefaultGitHubAPIBaseURL
94102
case strings.HasSuffix(hostname, ".ghe.com"):
103+
var apiURL string
95104
if port := parsed.Port(); port != "" {
96-
return fmt.Sprintf("%s://copilot-api.%s:%s", parsed.Scheme, hostname, port)
105+
apiURL = fmt.Sprintf("%s://copilot-api.%s:%s", parsed.Scheme, hostname, port)
106+
} else {
107+
apiURL = fmt.Sprintf("%s://copilot-api.%s", parsed.Scheme, hostname)
97108
}
98-
return fmt.Sprintf("%s://copilot-api.%s", parsed.Scheme, hostname)
109+
logGitHub.Printf("GHEC tenant detected, using copilot-api subdomain: hostname=%s, apiURL=%s", hostname, apiURL)
110+
return apiURL
99111
default:
100-
return fmt.Sprintf("%s://%s/api/v3", parsed.Scheme, parsed.Host)
112+
apiURL := fmt.Sprintf("%s://%s/api/v3", parsed.Scheme, parsed.Host)
113+
logGitHub.Printf("GHES instance detected, using /api/v3 path: host=%s, apiURL=%s", parsed.Host, apiURL)
114+
return apiURL
101115
}
102116
}

0 commit comments

Comments
 (0)