Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(switch): pass a GitHub Enterprise remote's host to gh api when gh is logged in to it (#4710)
Worktrunk Bot79f19f6407b6
diff --git a/src/git/remote_ref/github.rs b/src/git/remote_ref/github.rsindex db36780d4..82aba428c 100644--- a/src/git/remote_ref/github.rs+++ b/src/git/remote_ref/github.rs@@ -107,8 +107,17 @@ pub(super) fn fetch_pr_info(pr_number: u32, repo: &Repository) -> anyhow::Resultlet api_path = format!("repos/{}/{}/pulls/{}", owner, repo_name, pr_number);- // Only pass --hostname when explicitly configured (for GHE / self-hosted).- let hostname = repo.forge_hostname();+ // `gh api` doesn't infer the host from the repository: without+ // `--hostname` it asks gh's default host, which is github.com once several+ // hosts are logged in. A configured `forge.hostname` wins; otherwise a+ // GitHub Enterprise remote's host is the API host when gh holds a login+ // for it. An SSH alias (`git@github-personal:…`) has no login, so it keeps+ // gh's default (#1790).+ let hostname = repo.forge_hostname().or_else(|| {+ repo.forge_remote_parsed_url(|u| u.is_github())+ .map(|u| u.host().to_string())+ .filter(|host| !host.eq_ignore_ascii_case("github.com") && is_authed_for(host))+ });let mut args = vec!["api", api_path.as_str()];if let Some(h) = &hostname {diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rsindex ce430a128..aff674e0b 100644--- a/tests/integration_tests/switch.rs+++ b/tests/integration_tests/switch.rs@@ -6575,6 +6575,85 @@ fn test_switch_pr_self_hosted_defaults_to_github(}}+/// A GitHub Enterprise remote with no `forge.hostname`: `gh api` sends a+/// request without `--hostname` to gh's default host, so the PR lookup must+/// name the remote's host when gh holds a login for it, and leave gh's default+/// alone when it doesn't (an SSH alias such as `github-personal`, #1790).+#[rstest]+#[case::authed_host(true)]+#[case::unauthed_host(false)]+fn test_switch_pr_enterprise_remote_passes_hostname(+ #[from(repo_with_remote)] mut repo: TestRepo,+ #[case] gh_authed: bool,+) {+ repo.add_worktree("feature-auth");+ repo.run_git(&["push", "origin", "feature-auth"]);++ let bare_url = String::from_utf8_lossy(+ &repo+ .git_command()+ .args(["config", "remote.origin.url"])+ .run()+ .unwrap()+ .stdout,+ )+ .trim()+ .to_string();+ let ghe_url = "https://github.example.com/owner/test-repo.git";+ repo.run_git(&["remote", "set-url", "origin", ghe_url]);+ repo.run_git(&["config", &format!("url.{bare_url}.insteadOf"), ghe_url]);++ let gh_response = r#"{+ "title": "Enterprise PR",+ "user": {"login": "alice"},+ "state": "open",+ "draft": false,+ "head": {+ "ref": "feature-auth",+ "repo": {"name": "test-repo", "owner": {"login": "owner"}}+ },+ "base": {+ "ref": "main",+ "repo": {"name": "test-repo", "owner": {"login": "owner"}}+ },+ "html_url": "https://github.example.com/owner/test-repo/pull/101"+ }"#;+ let mock_bin = repo.root_path().join("mock-bin");+ fs::create_dir_all(&mock_bin).unwrap();+ fs::write(mock_bin.join("pr_response.json"), gh_response).unwrap();+ let mut gh = MockConfig::new("gh").command("api", MockResponse::file("pr_response.json"));+ if gh_authed {+ gh = gh.command("auth token --hostname", MockResponse::output("gho_mock"));+ }+ gh.command("_default", MockResponse::exit(1))+ .write(&mock_bin);+ let call_log = worktrunk::testing::tempdir().unwrap();++ let mut cmd = repo.wt_command();+ configure_mock_cli_env(&mut cmd, &mock_bin);+ let output = cmd+ .args(["switch", "pr:101"])+ .env("WORKTRUNK_TEST_MOCK_CALL_LOG_DIR", call_log.path())+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "{}",+ String::from_utf8_lossy(&output.stderr)+ );++ let calls = fs::read_to_string(call_log.path().join("gh.calls")).unwrap();+ let api_call = calls.lines().find(|line| line.starts_with("api ")).unwrap();+ if gh_authed {+ assert_eq!(+ api_call,+ "api repos/owner/test-repo/pulls/101 --hostname github.example.com"+ );+ } else {+ assert_eq!(api_call, "api repos/owner/test-repo/pulls/101");+ }+}+/// A configured SSH hostname can differ from the web hostname. tea's login/// list supplies both, so an SSH remote must route to the same Gitea account.#[rstest]