Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Improve GitHub PR detection for forks
Maximilian Roos50e6306d2eea
diff --git a/src/commands/list/ci_status.rs b/src/commands/list/ci_status.rsindex 76fe40a38..afab7b0db 100644--- a/src/commands/list/ci_status.rs+++ b/src/commands/list/ci_status.rs@@ -1,6 +1,100 @@use serde::{Deserialize, Serialize, de::DeserializeOwned};use std::process::Command;+/// Extract owner from a git remote URL (works for GitHub, GitLab, Bitbucket, etc.)+///+/// Supports formats:+/// - `https://<host>/<owner>/<repo>.git`+/// - `git@<host>:<owner>/<repo>.git`+fn parse_remote_owner(url: &str) -> Option<&str> {+ let url = url.trim();++ let owner = if let Some(rest) = url.strip_prefix("https://") {+ // https://github.com/owner/repo.git -> owner+ rest.split('/').nth(1)+ } else if let Some(rest) = url.strip_prefix("git@") {+ // [email protected]:owner/repo.git -> owner+ rest.split(':').nth(1)?.split('/').next()+ } else {+ None+ }?;++ if owner.is_empty() { None } else { Some(owner) }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn test_parse_remote_owner() {+ // GitHub HTTPS+ assert_eq!(+ parse_remote_owner("https://github.com/owner/repo.git"),+ Some("owner")+ );+ assert_eq!(+ parse_remote_owner(" https://github.com/owner/repo\n"),+ Some("owner")+ );++ // GitHub SSH+ assert_eq!(+ parse_remote_owner("[email protected]:owner/repo.git"),+ Some("owner")+ );++ // GitLab HTTPS+ assert_eq!(+ parse_remote_owner("https://gitlab.com/owner/repo.git"),+ Some("owner")+ );+ assert_eq!(+ parse_remote_owner("https://gitlab.example.com/owner/repo.git"),+ Some("owner")+ );++ // GitLab SSH+ assert_eq!(+ parse_remote_owner("[email protected]:owner/repo.git"),+ Some("owner")+ );++ // Bitbucket+ assert_eq!(+ parse_remote_owner("https://bitbucket.org/owner/repo.git"),+ Some("owner")+ );+ assert_eq!(+ parse_remote_owner("[email protected]:owner/repo.git"),+ Some("owner")+ );++ // Malformed URLs+ assert_eq!(parse_remote_owner("https://github.com/"), None);+ assert_eq!(parse_remote_owner("[email protected]:"), None);+ assert_eq!(parse_remote_owner(""), None);++ // Unsupported protocols+ assert_eq!(parse_remote_owner("http://github.com/owner/repo.git"), None);+ }+}++/// Get the owner of the origin remote (for fork detection)+fn get_origin_owner(repo_root: &str) -> Option<String> {+ let output = Command::new("git")+ .args(["remote", "get-url", "origin"])+ .current_dir(repo_root)+ .output()+ .ok()?;+ if output.status.success() {+ let url = String::from_utf8(output.stdout).ok()?;+ parse_remote_owner(&url).map(|s| s.to_string())+ } else {+ None+ }+}+/// Configure command to disable color outputfn disable_color_output(cmd: &mut Command) {cmd.env_remove("CLICOLOR_FORCE");@@ -101,7 +195,7 @@ impl PrStatus {}// Try GitLab MR- if let Some(status) = Self::detect_gitlab(branch, local_head) {+ if let Some(status) = Self::detect_gitlab(branch, local_head, repo_root) {return Some(status);}@@ -127,6 +221,9 @@ impl PrStatus {// Use `gh pr list --head` instead of `gh pr view` to handle numeric branch names correctly.// When branch name is all digits (e.g., "4315"), `gh pr view` interprets it as a PR number,// but `gh pr list --head` correctly treats it as a branch name.+ //+ // Use --author to filter to PRs from the origin remote owner, avoiding false matches+ // with other forks that have branches with the same name (e.g., everyone's fork has "master")let mut cmd = Command::new("gh");cmd.args(["pr",@@ -138,6 +235,9 @@ impl PrStatus {"--json","state,headRefOid,mergeStateStatus,statusCheckRollup,url",]);+ if let Some(origin_owner) = get_origin_owner(repo_root) {+ cmd.args(["--author", &origin_owner]);+ }disable_color_output(&mut cmd);cmd.current_dir(repo_root);@@ -186,20 +286,35 @@ impl PrStatus {})}- fn detect_gitlab(branch: &str, local_head: &str) -> Option<Self> {+ fn detect_gitlab(branch: &str, local_head: &str, repo_root: &str) -> Option<Self> {if !tool_available("glab", &["--version"]) {return None;}- // Get MR info for the branch- let output = match Command::new("glab")- .args(["mr", "view", branch, "--output", "json"])- .output()- {+ // Use glab mr list with --source-branch and --author to filter to MRs from the origin+ // remote owner, avoiding false matches with other forks that have branches with the+ // same name (similar to the GitHub --author fix)+ let mut cmd = Command::new("glab");+ cmd.args([+ "mr",+ "list",+ "--source-branch",+ branch,+ "--state=opened",+ "--per-page=1",+ "--output",+ "json",+ ]);+ if let Some(origin_owner) = get_origin_owner(repo_root) {+ cmd.args(["--author", &origin_owner]);+ }+ cmd.current_dir(repo_root);++ let output = match cmd.output() {Ok(output) => output,Err(e) => {log::warn!(- "glab mr view failed to execute for branch {}: {}",+ "glab mr list failed to execute for branch {}: {}",branch,e);@@ -209,16 +324,13 @@ impl PrStatus {if !output.status.success() {let stderr = String::from_utf8_lossy(&output.stderr);- log::debug!("glab mr view failed for {}: {}", branch, stderr.trim());+ log::debug!("glab mr list failed for {}: {}", branch, stderr.trim());return None;}- let mr_info: GitLabMrInfo = parse_json(&output.stdout, "glab mr view", branch)?;-- // Only process open MRs- if mr_info.state != "opened" {- return None;- }+ // glab mr list returns an array, take the first item+ let mr_list: Vec<GitLabMrInfo> = parse_json(&output.stdout, "glab mr list", branch)?;+ let mr_info = mr_list.first()?;// Determine CI status using priority: conflicts > running > failed > passed > no_cilet ci_status = if mr_info.has_conflicts@@ -429,7 +541,6 @@ impl GitHubWorkflowRun {#[derive(Debug, Deserialize)]struct GitLabMrInfo {- state: String,sha: String,has_conflicts: bool,detailed_merge_status: Option<String>,diff --git a/tests/integration_tests/.post_start_commands.rs.pending-snap b/tests/integration_tests/.post_start_commands.rs.pending-snapdeleted file mode 100644index d7fa15196..000000000--- a/tests/integration_tests/.post_start_commands.rs.pending-snap+++ /dev/null@@ -1,29 +0,0 @@-{"run_id":"1764014800-127677000","line":625,"new":{"module_name":"integration__integration_tests__post_start_commands","snapshot_name":"post_start_complex_shell_commands","metadata":{"source":"tests/integration_tests/post_start_commands.rs","assertion_line":625,"expression":"contents"},"snapshot":""},"old":{"module_name":"integration__integration_tests__post_start_commands","metadata":{},"snapshot":"line2"}}-{"run_id":"1764014800-127677000","line":672,"new":null,"old":null}-{"run_id":"1764014940-883397000","line":730,"new":null,"old":null}-{"run_id":"1764014940-883397000","line":625,"new":{"module_name":"integration__integration_tests__post_start_commands","snapshot_name":"post_start_complex_shell_commands","metadata":{"source":"tests/integration_tests/post_start_commands.rs","assertion_line":625,"expression":"contents"},"snapshot":""},"old":{"module_name":"integration__integration_tests__post_start_commands","metadata":{},"snapshot":"line2"}}-{"run_id":"1764014940-883397000","line":672,"new":null,"old":null}-{"run_id":"1764014940-883397000","line":431,"new":null,"old":null}-{"run_id":"1764014963-908385000","line":625,"new":null,"old":null}-{"run_id":"1764014980-254290000","line":730,"new":null,"old":null}-{"run_id":"1764014980-254290000","line":625,"new":{"module_name":"integration__integration_tests__post_start_commands","snapshot_name":"post_start_complex_shell_commands","metadata":{"source":"tests/integration_tests/post_start_commands.rs","assertion_line":625,"expression":"contents"},"snapshot":""},"old":{"module_name":"integration__integration_tests__post_start_commands","metadata":{},"snapshot":"line2"}}-{"run_id":"1764014980-254290000","line":672,"new":null,"old":null}-{"run_id":"1764014980-254290000","line":431,"new":{"module_name":"integration__integration_tests__post_start_commands","snapshot_name":"post_start_log_file_captures_output","metadata":{"source":"tests/integration_tests/post_start_commands.rs","assertion_line":431,"expression":"log_contents"},"snapshot":"stdout output"},"old":{"module_name":"integration__integration_tests__post_start_commands","metadata":{},"snapshot":"stdout output\nstderr output"}}-{"run_id":"1764015452-863643000","line":730,"new":null,"old":null}-{"run_id":"1764015452-863643000","line":625,"new":null,"old":null}-{"run_id":"1764015452-863643000","line":431,"new":null,"old":null}-{"run_id":"1764015452-863643000","line":672,"new":null,"old":null}-{"run_id":"1764016037-744134000","line":677,"new":null,"old":null}-{"run_id":"1764016053-461593000","line":735,"new":null,"old":null}-{"run_id":"1764016053-461593000","line":630,"new":null,"old":null}-{"run_id":"1764016053-461593000","line":431,"new":null,"old":null}-{"run_id":"1764016053-461593000","line":677,"new":null,"old":null}-{"run_id":"1764016195-594834000","line":735,"new":null,"old":null}-{"run_id":"1764016195-594834000","line":630,"new":null,"old":null}-{"run_id":"1764016195-594834000","line":431,"new":null,"old":null}-{"run_id":"1764016195-594834000","line":677,"new":{"module_name":"integration__integration_tests__post_start_commands","snapshot_name":"post_start_multiline_commands_with_newlines","metadata":{"source":"tests/integration_tests/post_start_commands.rs","assertion_line":677,"expression":"contents"},"snapshot":"first line"},"old":{"module_name":"integration__integration_tests__post_start_commands","metadata":{},"snapshot":"first line\nsecond line\nthird line"}}-{"run_id":"1764016208-474514000","line":677,"new":null,"old":null}-{"run_id":"1764016239-571456000","line":735,"new":null,"old":null}-{"run_id":"1764016239-571456000","line":630,"new":null,"old":null}-{"run_id":"1764016239-571456000","line":431,"new":null,"old":null}-{"run_id":"1764016239-571456000","line":677,"new":null,"old":null}