Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(config state): honor [forge].platform in ci-status JSON (#3120)
Worktrunk Bote3b354c217fa
diff --git a/src/commands/config/state.rs b/src/commands/config/state.rsindex 535237a6b..1e0f87876 100644--- a/src/commands/config/state.rs+++ b/src/commands/config/state.rs@@ -775,9 +775,13 @@ pub fn handle_state_get(.and_then(|ci_branch| PrStatus::detect(&repo, &ci_branch, &branch_ref.commit_sha));if format == SwitchFormat::Json {- let output = pr_status- .as_ref()- .map(super::super::list::json_output::JsonCi::from);+ let ci_provider_override = repo.forge_platform_override();+ let output = pr_status.as_ref().map(|pr| {+ super::super::list::json_output::JsonCi::from_pr_status(+ pr,+ ci_provider_override.as_deref(),+ )+ });println!("{}", serde_json::to_string_pretty(&output)?);} else {let ci_status = pr_statusdiff --git a/src/commands/list/json_output.rs b/src/commands/list/json_output.rsindex 90685e24e..9845f2c94 100644--- a/src/commands/list/json_output.rs+++ b/src/commands/list/json_output.rs@@ -464,14 +464,13 @@ fn worktree_state_to_json((None, None)}-impl From<&PrStatus> for JsonCi {- fn from(pr: &PrStatus) -> Self {- Self::from_pr_status(pr, None)- }-}-impl JsonCi {- fn from_pr_status(pr: &PrStatus, provider_override: Option<&str>) -> Self {+ /// Build a `JsonCi` from a [`PrStatus`], deriving the target-repo metadata+ /// from the PR/MR URL. `provider_override` is the configured+ /// `[forge].platform` (see [`Repository::forge_platform_override`]); it lets+ /// a self-hosted instance whose host can't be auto-detected still report the+ /// right provider.+ pub(crate) fn from_pr_status(pr: &PrStatus, provider_override: Option<&str>) -> Self {let repo = pr.url.as_deref().and_then(|url| {worktrunk::git::remote_ref::repo_info_from_ref_url_with_provider(url, provider_override)});@@ -548,11 +547,7 @@ fn format_raw_symbols(symbols: &super::model::StatusSymbols) -> String {pub fn to_json_items(items: &[ListItem], repo: &Repository) -> Vec<JsonItem> {let mut all_vars = repo.all_vars_entries();let repo_metadata = repo.repo_info();- let ci_provider_override = repo- .project_config()- .ok()- .flatten()- .and_then(|config| config.forge_platform().map(str::to_string));+ let ci_provider_override = repo.forge_platform_override();items.iter().map(|item| {@@ -600,20 +595,23 @@ mod tests {}// ============================================================================- // JsonCi::from Tests+ // JsonCi::from_pr_status Tests// ============================================================================#[test]fn test_json_ci_from_pr_status() {// Full field mapping with URL- let passed = JsonCi::from(&PrStatus {- ci_status: CiStatus::Passed,- source: CiSource::PullRequest,- is_stale: false,- url: Some("https://github.com/org/repo/pull/123".to_string()),- number: Some(PrRef::pr(123)),- review_state: None,- });+ let passed = JsonCi::from_pr_status(+ &PrStatus {+ ci_status: CiStatus::Passed,+ source: CiSource::PullRequest,+ is_stale: false,+ url: Some("https://github.com/org/repo/pull/123".to_string()),+ number: Some(PrRef::pr(123)),+ review_state: None,+ },+ None,+ );assert_eq!(passed.status, "passed");assert_eq!(passed.source, CiSource::PullRequest);assert!(!passed.stale);@@ -640,14 +638,17 @@ mod tests {);// Stale branch with no URL- let failed = JsonCi::from(&PrStatus {- ci_status: CiStatus::Failed,- source: CiSource::Branch,- is_stale: true,- url: None,- number: None,- review_state: None,- });+ let failed = JsonCi::from_pr_status(+ &PrStatus {+ ci_status: CiStatus::Failed,+ source: CiSource::Branch,+ is_stale: true,+ url: None,+ number: None,+ review_state: None,+ },+ None,+ );assert_eq!(failed.status, "failed");assert_eq!(failed.source, CiSource::Branch);assert!(failed.stale);@@ -664,18 +665,48 @@ mod tests {(CiStatus::Error, "error"),];for (ci_status, expected) in status_mappings {- let json = JsonCi::from(&PrStatus {- ci_status,- source: CiSource::Branch,- is_stale: false,- url: None,- number: None,- review_state: None,- });+ let json = JsonCi::from_pr_status(+ &PrStatus {+ ci_status,+ source: CiSource::Branch,+ is_stale: false,+ url: None,+ number: None,+ review_state: None,+ },+ None,+ );assert_eq!(json.status, expected);}}+ /// The configured `[forge].platform` override decides the provider when the+ /// PR URL's host can't be auto-detected. Without the override an opaque-host+ /// `pull` URL is `Unknown`; with `github` it resolves to GitHub.+ #[test]+ fn test_json_ci_from_pr_status_provider_override() {+ let pr = PrStatus {+ ci_status: CiStatus::Passed,+ source: CiSource::PullRequest,+ is_stale: false,+ url: Some("https://git.example.com/org/repo/pull/7".to_string()),+ number: Some(PrRef::pr(7)),+ review_state: None,+ };++ let without = JsonCi::from_pr_status(&pr, None);+ assert_eq!(+ without.repo.as_ref().map(|r| r.provider),+ Some(GitRepoProvider::Unknown)+ );++ let with = JsonCi::from_pr_status(&pr, Some("github"));+ assert_eq!(+ with.repo.as_ref().map(|r| r.provider),+ Some(GitRepoProvider::GitHub)+ );+ }+// ============================================================================// upstream_to_json Tests// ============================================================================diff --git a/src/commands/statusline.rs b/src/commands/statusline.rsindex 5d77a44d6..6988b2a02 100644--- a/src/commands/statusline.rs+++ b/src/commands/statusline.rs@@ -749,11 +749,7 @@ fn run_json() -> Result<()> {}}let repo_metadata = repo.repo_info();- let ci_provider_override = repo- .project_config()- .ok()- .flatten()- .and_then(|config| config.forge_platform().map(str::to_string));+ let ci_provider_override = repo.forge_platform_override();let json_item = json_output::JsonItem::from_list_item(&item,&mut all_vars,diff --git a/src/git/repository/remotes.rs b/src/git/repository/remotes.rsindex db003d080..899a3e26c 100644--- a/src/git/repository/remotes.rs+++ b/src/git/repository/remotes.rs@@ -295,6 +295,21 @@ impl Repository {.and_then(GitRemoteUrl::parse)}+ /// Configured `[forge].platform` override, if any.+ ///+ /// Local-only: reads (and may cache) project config. Every structured-output+ /// path that derives a forge provider feeds this into the URL parser so a+ /// self-hosted instance whose host can't be auto-detected reports the+ /// configured provider instead of `unknown`. Centralized here so the three+ /// call sites (`wt list`, `wt list statusline`, `wt config state ci-status`)+ /// can't drift — see `repo_info` and `json_output::JsonCi`.+ pub fn forge_platform_override(&self) -> Option<String> {+ self.project_config()+ .ok()+ .flatten()+ .and_then(|config| config.forge_platform().map(str::to_string))+ }+/// Repository metadata derived from the primary remote.////// Local-only: built from the cached primary remote URL and optional@@ -306,11 +321,7 @@ impl Repository {let remote = self.primary_remote().ok()?;let url = self.remote_url(&remote)?;let parsed = GitRemoteUrl::parse(&url)?;- let provider_override = self- .project_config()- .ok()- .flatten()- .and_then(|config| config.forge_platform().map(str::to_string));+ let provider_override = self.forge_platform_override();let mut info = parsed.repo_info(provider_override.as_deref())?;info.remote = Some(remote);Some(info)diff --git a/tests/integration_tests/config_state.rs b/tests/integration_tests/config_state.rsindex 6fb5bd9e5..1f15b993a 100644--- a/tests/integration_tests/config_state.rs+++ b/tests/integration_tests/config_state.rs@@ -434,6 +434,42 @@ fn test_state_get_ci_status_returns_cached_status(repo: TestRepo) {assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "passed");}+/// `--format=json` honors the configured `[forge].platform` when deriving the+/// repo provider from the cached PR URL, matching `wt list --format=json`.+/// Without the override an opaque-host `pull` URL resolves to `unknown`; with+/// `[forge] platform = "github"` it resolves to `github`. Regression guard for+/// the `state ci-status` JSON path that previously hard-coded no override.+#[rstest]+fn test_state_get_ci_status_json_honors_forge_platform(repo: TestRepo) {+ let head = repo.head_sha();+ write_ci_cache(+ &repo,+ "main",+ &format!(+ r#"{{"status":{{"ci_status":"passed","source":"pr","is_stale":false,"url":"https://git.example.com/org/repo/pull/7"}},"checked_at":{TEST_EPOCH},"head":"{head}","branch":"main"}}"#+ ),+ );+ repo.write_project_config("[forge]\nplatform = \"github\"\n");++ let output = wt_state_cmd(+ &repo,+ "ci-status",+ "get",+ &["--branch", "main", "--format=json"],+ )+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "command should succeed: stderr={}",+ String::from_utf8_lossy(&output.stderr)+ );++ let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();+ assert_eq!(json["repo"]["provider"].as_str(), Some("github"));+ assert_eq!(json["repo"]["host"].as_str(), Some("git.example.com"));+}+/// `wt config state ci-status --branch origin/foo` must resolve cleanly when a/// local branch literally named `origin/foo` shadows a remote-tracking ref of/// the same name. Smoke test: exercises the shadowing code path. The