1use anyhow::{bail, Context, Result};
2use std::process::Command;
3
4pub fn is_gh_installed() -> bool {
5 Command::new("gh")
6 .arg("--version")
7 .output()
8 .map(|o| o.status.success())
9 .unwrap_or(false)
10}
11
12pub fn is_gh_authenticated() -> bool {
13 Command::new("gh")
14 .args(["auth", "status"])
15 .output()
16 .map(|o| o.status.success())
17 .unwrap_or(false)
18}
19
20pub fn checkout_pr(worktree_path: &std::path::Path, pr_number: u32) -> Result<()> {
21 let output = Command::new("gh")
22 .current_dir(worktree_path)
23 .args(["pr", "checkout", &pr_number.to_string()])
24 .output()
25 .context("Failed to execute gh pr checkout")?;
26
27 if !output.status.success() {
28 let stderr = String::from_utf8_lossy(&output.stderr);
29 bail!("gh pr checkout failed: {}", stderr.trim());
30 }
31
32 Ok(())
33}
34
35pub fn get_pr_branch(pr_number: u32) -> Result<String> {
36 let output = Command::new("gh")
37 .args([
38 "pr",
39 "view",
40 &pr_number.to_string(),
41 "--json",
42 "headRefName",
43 "-q",
44 ".headRefName",
45 ])
46 .output()
47 .context("Failed to get PR branch name")?;
48
49 if !output.status.success() {
50 let stderr = String::from_utf8_lossy(&output.stderr);
51 bail!("Failed to get PR info: {}", stderr.trim());
52 }
53
54 Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
55}