bohay 0.7.1

Next-Gen Agents multiplexer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! GitHub data for the git tab via the **`gh` CLI** (docs/17, GIT-2). No HTTP/
//! auth dependency — we shell out and parse JSON, and degrade gracefully when
//! `gh` is missing or unauthenticated.

use std::path::Path;
use std::process::Command;

use serde_json::Value;

use super::model::{Check, Checks, Issue, PrDetail, PullRequest, Review};
use super::Scope;

/// Availability of the `gh` CLI for this session.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum GhState {
    Ready,
    NotAuthed,
    Missing,
}

impl GhState {
    pub fn note(self) -> Option<&'static str> {
        match self {
            GhState::Ready => None,
            GhState::NotAuthed => Some("run `gh auth login` for PRs & issues"),
            GhState::Missing => Some("install GitHub CLI (`gh`) for PRs & issues"),
        }
    }
}

/// Probe `gh` once (installed? authenticated?).
pub fn detect() -> GhState {
    match Command::new("gh").arg("--version").output() {
        Ok(o) if o.status.success() => match Command::new("gh").args(["auth", "status"]).output() {
            Ok(a) if a.status.success() => GhState::Ready,
            Ok(_) => GhState::NotAuthed,
            Err(_) => GhState::Missing,
        },
        _ => GhState::Missing,
    }
}

fn run_gh(cwd: &Path, args: &[&str]) -> Result<String, String> {
    let out = Command::new("gh")
        .args(args)
        .current_dir(cwd)
        .output()
        .map_err(|e| format!("gh: {e}"))?;
    if !out.status.success() {
        let err = String::from_utf8_lossy(&out.stderr);
        return Err(err.lines().next().unwrap_or("gh failed").trim().to_string());
    }
    Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}

const PR_FIELDS: &str = "number,title,author,isDraft,state,reviewDecision,reviewRequests,headRefName,additions,deletions,statusCheckRollup";
// `gh search` exposes fewer fields than `gh pr list` (no checks/+-/review).
const SEARCH_PR_FIELDS: &str = "number,title,author,isDraft,state,repository";
const SEARCH_ISSUE_FIELDS: &str = "number,title,author,labels,repository";

/// Open pull requests for `scope` (this repo, or everything you're involved in).
pub fn pull_requests(cwd: &Path, scope: Scope) -> Result<Vec<PullRequest>, String> {
    let raw = match scope {
        Scope::ThisRepo => run_gh(cwd, &["pr", "list", "--json", PR_FIELDS, "--limit", "50"])?,
        Scope::MyWork => run_gh(
            cwd,
            &[
                "search",
                "prs",
                "--involves=@me",
                "--state=open",
                "--json",
                SEARCH_PR_FIELDS,
                "--limit",
                "50",
            ],
        )?,
    };
    let v: Value = serde_json::from_str(&raw).map_err(|e| format!("parse gh: {e}"))?;
    Ok(v.as_array()
        .map(|a| a.iter().map(parse_pr).collect())
        .unwrap_or_default())
}

const ISSUE_FIELDS: &str = "number,title,author,labels,assignees";

/// Open issues for `scope`.
pub fn issues(cwd: &Path, scope: Scope) -> Result<Vec<Issue>, String> {
    let raw = match scope {
        Scope::ThisRepo => run_gh(
            cwd,
            &["issue", "list", "--json", ISSUE_FIELDS, "--limit", "50"],
        )?,
        Scope::MyWork => run_gh(
            cwd,
            &[
                "search",
                "issues",
                "--involves=@me",
                "--state=open",
                "--json",
                SEARCH_ISSUE_FIELDS,
                "--limit",
                "50",
            ],
        )?,
    };
    let v: Value = serde_json::from_str(&raw).map_err(|e| format!("parse gh: {e}"))?;
    Ok(v.as_array()
        .map(|a| a.iter().map(parse_issue).collect())
        .unwrap_or_default())
}

/// `{ "nameWithOwner": "o/r" }` (search results) → "o/r".
fn repo_of(v: &Value) -> String {
    v.get("repository")
        .and_then(|r| r.get("nameWithOwner").or_else(|| r.get("name")))
        .and_then(Value::as_str)
        .unwrap_or("")
        .to_string()
}

fn parse_pr(v: &Value) -> PullRequest {
    PullRequest {
        number: v.get("number").and_then(Value::as_u64).unwrap_or(0),
        title: str_at(v, "title"),
        author: login(v.get("author")),
        state: str_at(v, "state"),
        is_draft: v.get("isDraft").and_then(Value::as_bool).unwrap_or(false),
        review_decision: str_at(v, "reviewDecision"),
        reviewers: logins(v.get("reviewRequests")),
        head: str_at(v, "headRefName"),
        additions: v.get("additions").and_then(Value::as_u64).unwrap_or(0),
        deletions: v.get("deletions").and_then(Value::as_u64).unwrap_or(0),
        checks: rollup(v.get("statusCheckRollup")),
        repo: repo_of(v),
    }
}

fn parse_issue(v: &Value) -> Issue {
    Issue {
        number: v.get("number").and_then(Value::as_u64).unwrap_or(0),
        title: str_at(v, "title"),
        author: login(v.get("author")),
        labels: names(v.get("labels"), "name"),
        assignees: logins(v.get("assignees")),
        repo: repo_of(v),
    }
}

fn str_at(v: &Value, key: &str) -> String {
    v.get(key).and_then(Value::as_str).unwrap_or("").to_string()
}

/// `{ "login": "x" }` → "x".
fn login(v: Option<&Value>) -> String {
    v.and_then(|o| o.get("login"))
        .and_then(Value::as_str)
        .unwrap_or("")
        .to_string()
}

/// `[ { "login": "a" }, … ]` → ["a", …] (users) / `name` (teams).
fn logins(v: Option<&Value>) -> Vec<String> {
    v.and_then(Value::as_array)
        .map(|a| {
            a.iter()
                .filter_map(|o| {
                    o.get("login")
                        .or_else(|| o.get("name"))
                        .and_then(Value::as_str)
                        .map(str::to_string)
                })
                .collect()
        })
        .unwrap_or_default()
}

fn names(v: Option<&Value>, key: &str) -> Vec<String> {
    v.and_then(Value::as_array)
        .map(|a| {
            a.iter()
                .filter_map(|o| o.get(key).and_then(Value::as_str).map(str::to_string))
                .collect()
        })
        .unwrap_or_default()
}

/// Bucket one check item. Status contexts use `state`; check runs use `status` +
/// `conclusion`.
fn bucket_one(c: &Value) -> Checks {
    let state = c.get("state").and_then(Value::as_str).unwrap_or("");
    let status = c.get("status").and_then(Value::as_str).unwrap_or("");
    let concl = c.get("conclusion").and_then(Value::as_str).unwrap_or("");
    let s = if !state.is_empty() {
        state
    } else if !concl.is_empty() {
        concl
    } else {
        status
    };
    match s {
        "FAILURE" | "ERROR" | "TIMED_OUT" | "CANCELLED" | "ACTION_REQUIRED" | "STARTUP_FAILURE" => {
            Checks::Failing
        }
        "PENDING" | "IN_PROGRESS" | "QUEUED" | "EXPECTED" | "REQUESTED" | "WAITING" => {
            Checks::Pending
        }
        "" => Checks::None,
        _ => Checks::Passing,
    }
}

/// Collapse `statusCheckRollup` into one state. A failure wins, then pending.
fn rollup(v: Option<&Value>) -> Checks {
    let Some(arr) = v.and_then(Value::as_array) else {
        return Checks::None;
    };
    if arr.is_empty() {
        return Checks::None;
    }
    let mut pending = false;
    for c in arr {
        match bucket_one(c) {
            Checks::Failing => return Checks::Failing,
            Checks::Pending => pending = true,
            _ => {}
        }
    }
    if pending {
        Checks::Pending
    } else {
        Checks::Passing
    }
}

const PR_DETAIL_FIELDS: &str = "number,title,state,isDraft,author,baseRefName,headRefName,body,additions,deletions,changedFiles,commits,comments,mergeable,reviewDecision,reviews,statusCheckRollup,labels,updatedAt";

/// Full detail for one PR — the detail panel (`gh pr view <n> --json …`).
pub fn pr_detail(cwd: &Path, number: u64) -> Result<PrDetail, String> {
    let raw = run_gh(
        cwd,
        &[
            "pr",
            "view",
            &number.to_string(),
            "--json",
            PR_DETAIL_FIELDS,
        ],
    )?;
    let v: Value = serde_json::from_str(&raw).map_err(|e| format!("parse gh: {e}"))?;
    Ok(parse_pr_detail(&v))
}

fn parse_pr_detail(v: &Value) -> PrDetail {
    let count = |key: &str| {
        v.get(key)
            .and_then(Value::as_array)
            .map(|a| a.len() as u64)
            .unwrap_or(0)
    };
    PrDetail {
        number: v.get("number").and_then(Value::as_u64).unwrap_or(0),
        title: str_at(v, "title"),
        state: str_at(v, "state"),
        is_draft: v.get("isDraft").and_then(Value::as_bool).unwrap_or(false),
        author: login(v.get("author")),
        base: str_at(v, "baseRefName"),
        head: str_at(v, "headRefName"),
        body: str_at(v, "body"),
        additions: v.get("additions").and_then(Value::as_u64).unwrap_or(0),
        deletions: v.get("deletions").and_then(Value::as_u64).unwrap_or(0),
        changed_files: v.get("changedFiles").and_then(Value::as_u64).unwrap_or(0),
        commits: count("commits"),
        comments: count("comments"),
        mergeable: str_at(v, "mergeable"),
        review_decision: str_at(v, "reviewDecision"),
        reviews: parse_reviews(v.get("reviews")),
        check_runs: parse_checks(v.get("statusCheckRollup")),
        labels: names(v.get("labels"), "name"),
        updated_at: str_at(v, "updatedAt"),
    }
}

/// Latest decision per reviewer (gh returns reviews chronologically, so a later
/// review supersedes an earlier one).
fn parse_reviews(v: Option<&Value>) -> Vec<Review> {
    let Some(arr) = v.and_then(Value::as_array) else {
        return Vec::new();
    };
    let mut out: Vec<Review> = Vec::new();
    for r in arr {
        let author = login(r.get("author"));
        if author.is_empty() {
            continue;
        }
        let state = str_at(r, "state");
        if let Some(prev) = out.iter_mut().find(|x| x.author == author) {
            prev.state = state;
        } else {
            out.push(Review { author, state });
        }
    }
    out
}

fn parse_checks(v: Option<&Value>) -> Vec<Check> {
    v.and_then(Value::as_array)
        .map(|a| {
            a.iter()
                .map(|c| Check {
                    name: c
                        .get("name")
                        .or_else(|| c.get("context"))
                        .and_then(Value::as_str)
                        .unwrap_or("check")
                        .to_string(),
                    bucket: bucket_one(c),
                })
                .collect()
        })
        .unwrap_or_default()
}

/// Open a PR (or issue) in the browser.
pub fn view_web(cwd: &Path, kind: &str, number: u64) -> Result<(), String> {
    run_gh(cwd, &[kind, "view", &number.to_string(), "--web"]).map(|_| ())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_a_pr() {
        let v: Value = serde_json::from_str(
            r#"{"number":78,"title":"Add dark mode","author":{"login":"sam"},
                "isDraft":false,"state":"OPEN","reviewDecision":"REVIEW_REQUIRED",
                "reviewRequests":[{"login":"taylor"}],"headRefName":"feature/dark-mode",
                "additions":567,"deletions":234,"url":"http://x",
                "statusCheckRollup":[{"state":"SUCCESS"}]}"#,
        )
        .unwrap();
        let pr = parse_pr(&v);
        assert_eq!(pr.number, 78);
        assert_eq!(pr.author, "sam");
        assert_eq!(pr.reviewers, vec!["taylor"]);
        assert_eq!(pr.additions, 567);
        assert!(matches!(pr.checks, Checks::Passing));
    }

    #[test]
    fn rollup_failure_wins() {
        let v: Value =
            serde_json::from_str(r#"[{"conclusion":"SUCCESS"},{"state":"FAILURE"}]"#).unwrap();
        assert!(matches!(rollup(Some(&v)), Checks::Failing));
        let v: Value = serde_json::from_str(r#"[{"status":"IN_PROGRESS"}]"#).unwrap();
        assert!(matches!(rollup(Some(&v)), Checks::Pending));
        let v: Value = serde_json::from_str("[]").unwrap();
        assert!(matches!(rollup(Some(&v)), Checks::None));
    }

    #[test]
    fn parses_pr_detail_with_per_check_and_deduped_reviews() {
        let v: Value = serde_json::from_str(
            r#"{
                "number":42,"title":"Wire up auth","state":"OPEN","isDraft":false,
                "author":{"login":"alice"},"baseRefName":"main","headRefName":"feat/auth",
                "body":"Adds login.\n\nSee the RFC.","additions":120,"deletions":8,
                "changedFiles":5,"commits":[{},{},{}],"comments":[{}],
                "mergeable":"MERGEABLE","reviewDecision":"CHANGES_REQUESTED",
                "reviews":[
                    {"author":{"login":"bob"},"state":"COMMENTED"},
                    {"author":{"login":"bob"},"state":"APPROVED"},
                    {"author":{"login":"carol"},"state":"CHANGES_REQUESTED"}
                ],
                "statusCheckRollup":[
                    {"name":"build","status":"COMPLETED","conclusion":"SUCCESS"},
                    {"name":"e2e","status":"COMPLETED","conclusion":"FAILURE"},
                    {"context":"ci/lint","state":"PENDING"}
                ],
                "labels":[{"name":"auth"}],"updatedAt":"2026-06-25T10:30:00Z"
            }"#,
        )
        .unwrap();
        let d = parse_pr_detail(&v);
        assert_eq!(d.number, 42);
        assert_eq!(d.base, "main");
        assert_eq!(d.head, "feat/auth");
        assert_eq!(d.commits, 3);
        assert_eq!(d.comments, 1);
        assert_eq!(d.changed_files, 5);
        // Reviews collapse to the latest decision per author (bob's APPROVED wins).
        assert_eq!(d.reviews.len(), 2);
        let bob = d.reviews.iter().find(|r| r.author == "bob").unwrap();
        assert_eq!(bob.state, "APPROVED");
        // Per-check buckets are individual, not a rollup.
        assert_eq!(d.check_runs.len(), 3);
        assert!(matches!(d.check_runs[0].bucket, Checks::Passing));
        assert!(matches!(d.check_runs[1].bucket, Checks::Failing));
        assert!(matches!(d.check_runs[2].bucket, Checks::Pending));
        assert_eq!(d.check_runs[2].name, "ci/lint");
    }
}