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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,5 @@ test-script-mode.lock.yml
# Test metrics patterns workflow (temporary validation file)
.github/workflows/test-metrics-patterns.md
.github/workflows/test-metrics-patterns.lock.yml
.github/workflows/test-*.md
.github/workflows/test-*.lock.yml
43 changes: 30 additions & 13 deletions pkg/cli/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,38 @@ import (
var githubLog = logger.New("cli:github")

// getGitHubHost returns the GitHub host URL from environment variables.
// It checks GITHUB_SERVER_URL first (GitHub Actions standard),
// then falls back to GH_HOST (gh CLI standard),
// and finally defaults to https://github.com
// Environment variables are checked in priority order for GitHub Enterprise support:
// 1. GITHUB_SERVER_URL - GitHub Actions standard (e.g., https://MYORG.ghe.com)
// 2. GITHUB_ENTERPRISE_HOST - GitHub Enterprise standard (e.g., MYORG.ghe.com)
// 3. GITHUB_HOST - GitHub Enterprise standard (e.g., MYORG.ghe.com)
// 4. GH_HOST - GitHub CLI standard (e.g., MYORG.ghe.com)
// 5. Defaults to https://github.com if none are set
//
// The function normalizes the URL by adding https:// if missing and removing trailing slashes.
func getGitHubHost() string {
host := os.Getenv("GITHUB_SERVER_URL")
if host == "" {
host = os.Getenv("GH_HOST")
envVars := []string{"GITHUB_SERVER_URL", "GITHUB_ENTERPRISE_HOST", "GITHUB_HOST", "GH_HOST"}

for _, envVar := range envVars {
if value := os.Getenv(envVar); value != "" {
githubLog.Printf("Resolved GitHub host from %s: %s", envVar, value)
return normalizeGitHubHostURL(value)
}
}
if host == "" {
host = "https://github.com"
githubLog.Print("Using default GitHub host: https://github.com")
} else {
githubLog.Printf("Resolved GitHub host: %s", host)

defaultHost := "https://github.com"
githubLog.Printf("No GitHub host environment variable set, using default: %s", defaultHost)
return defaultHost
}

// normalizeGitHubHostURL ensures the host URL has https:// scheme and no trailing slashes
func normalizeGitHubHostURL(rawHostURL string) string {
// Remove all trailing slashes
normalized := strings.TrimRight(rawHostURL, "/")

// Add https:// scheme if no scheme is present
if !strings.HasPrefix(normalized, "https://") && !strings.HasPrefix(normalized, "http://") {
normalized = "https://" + normalized
}

// Remove trailing slash for consistency
return strings.TrimSuffix(host, "/")
return normalized
}
136 changes: 108 additions & 28 deletions pkg/cli/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,133 @@ import (

func TestGetGitHubHost(t *testing.T) {
tests := []struct {
name string
serverURL string
ghHost string
expectedHost string
name string
serverURL string
enterpriseHost string
githubHost string
ghHost string
expectedHost string
}{
{
name: "defaults to github.com",
serverURL: "",
ghHost: "",
expectedHost: "https://github.com",
name: "defaults to github.com",
serverURL: "",
enterpriseHost: "",
githubHost: "",
ghHost: "",
expectedHost: "https://github.com",
},
{
name: "uses GITHUB_SERVER_URL when set",
serverURL: "https://github.enterprise.com",
ghHost: "",
expectedHost: "https://github.enterprise.com",
name: "uses GITHUB_SERVER_URL when set",
serverURL: "https://github.enterprise.com",
enterpriseHost: "",
githubHost: "",
ghHost: "",
expectedHost: "https://github.enterprise.com",
},
{
name: "uses GH_HOST when GITHUB_SERVER_URL not set",
serverURL: "",
ghHost: "https://github.saobby.my.eu.orgpany.com",
expectedHost: "https://github.saobby.my.eu.orgpany.com",
name: "uses GITHUB_ENTERPRISE_HOST when GITHUB_SERVER_URL not set",
serverURL: "",
enterpriseHost: "github.enterprise.com",
githubHost: "",
ghHost: "",
expectedHost: "https://github.enterprise.com",
},
{
name: "GITHUB_SERVER_URL takes precedence over GH_HOST",
serverURL: "https://github.enterprise.com",
ghHost: "https://github.saobby.my.eu.orgpany.com",
expectedHost: "https://github.enterprise.com",
name: "uses GITHUB_HOST when GITHUB_SERVER_URL and GITHUB_ENTERPRISE_HOST not set",
serverURL: "",
enterpriseHost: "",
githubHost: "github.company.com",
ghHost: "",
expectedHost: "https://github.saobby.my.eu.orgpany.com",
},
{
name: "removes trailing slash from GITHUB_SERVER_URL",
serverURL: "https://github.enterprise.com/",
ghHost: "",
expectedHost: "https://github.enterprise.com",
name: "uses GH_HOST when other vars not set",
serverURL: "",
enterpriseHost: "",
githubHost: "",
ghHost: "https://github.saobby.my.eu.orgpany.com",
expectedHost: "https://github.saobby.my.eu.orgpany.com",
},
{
name: "removes trailing slash from GH_HOST",
serverURL: "",
ghHost: "https://github.saobby.my.eu.orgpany.com/",
expectedHost: "https://github.saobby.my.eu.orgpany.com",
name: "GITHUB_SERVER_URL takes precedence over all others",
serverURL: "https://github.enterprise.com",
enterpriseHost: "github.other.com",
githubHost: "github.another.com",
ghHost: "https://github.saobby.my.eu.orgpany.com",
expectedHost: "https://github.enterprise.com",
},
{
name: "GITHUB_ENTERPRISE_HOST takes precedence over GITHUB_HOST and GH_HOST",
serverURL: "",
enterpriseHost: "github.enterprise.com",
githubHost: "github.company.com",
ghHost: "github.other.com",
expectedHost: "https://github.enterprise.com",
},
{
name: "GITHUB_HOST takes precedence over GH_HOST",
serverURL: "",
enterpriseHost: "",
githubHost: "github.company.com",
ghHost: "github.other.com",
expectedHost: "https://github.saobby.my.eu.orgpany.com",
},
{
name: "removes trailing slash from GITHUB_SERVER_URL",
serverURL: "https://github.enterprise.com/",
enterpriseHost: "",
githubHost: "",
ghHost: "",
expectedHost: "https://github.enterprise.com",
},
{
name: "removes trailing slash from GITHUB_ENTERPRISE_HOST",
serverURL: "",
enterpriseHost: "github.enterprise.com/",
githubHost: "",
ghHost: "",
expectedHost: "https://github.enterprise.com",
},
{
name: "removes trailing slash from GH_HOST",
serverURL: "",
enterpriseHost: "",
githubHost: "",
ghHost: "https://github.saobby.my.eu.orgpany.com/",
expectedHost: "https://github.saobby.my.eu.orgpany.com",
},
{
name: "adds https:// prefix to GITHUB_ENTERPRISE_HOST",
serverURL: "",
enterpriseHost: "MYORG.ghe.com",
githubHost: "",
ghHost: "",
expectedHost: "https://MYORG.ghe.com",
},
{
name: "adds https:// prefix to GITHUB_HOST",
serverURL: "",
enterpriseHost: "",
githubHost: "MYORG.ghe.com",
ghHost: "",
expectedHost: "https://MYORG.ghe.com",
},
{
name: "adds https:// prefix to GH_HOST",
serverURL: "",
enterpriseHost: "",
githubHost: "",
ghHost: "MYORG.ghe.com",
expectedHost: "https://MYORG.ghe.com",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set test env vars (always set to ensure clean state)
t.Setenv("GITHUB_SERVER_URL", tt.serverURL)
t.Setenv("GITHUB_ENTERPRISE_HOST", tt.enterpriseHost)
t.Setenv("GITHUB_HOST", tt.githubHost)
t.Setenv("GH_HOST", tt.ghHost)

// Test
Expand Down
Loading
Loading