refactor(hive-forge): share latest-per-reviewer logic between pr-status and pr-merge

pr-status and pr-merge both computed 'latest non-comment review verdict per
reviewer' independently (identical oldest-first, COMMENT/PENDING-skipping,
supersede-by-later loop). Extract it to a shared verbs::latest_reviews helper
so the verdict semantics live in one place and can't drift between the
health view and the pre-merge changes-requested gate. Pure dedup, no
behaviour change.
This commit is contained in:
atlas 2026-06-15 11:39:19 +02:00 committed by mara
commit f633abbdc4
3 changed files with 42 additions and 53 deletions

View file

@ -36,6 +36,8 @@ pub mod view;
use anyhow::Result;
use serde_json::Value;
use crate::client::Client;
/// Pretty-print a `serde_json` value to stdout with a trailing newline,
/// matching the bash script's `| jq` output shape.
pub(crate) fn print_json(v: &Value) -> Result<()> {
@ -43,3 +45,38 @@ pub(crate) fn print_json(v: &Value) -> Result<()> {
println!("{s}");
Ok(())
}
/// Latest non-comment review verdict per reviewer on a PR, as
/// `(login, state)`. Reviews come oldest-first, so a later verdict from
/// the same user supersedes an earlier one; `COMMENT` / `PENDING`
/// reviews carry no verdict and are skipped. Shared by `pr-status` (for
/// its health view + readiness verdict) and `pr-merge` (for its
/// pre-merge changes-requested gate) so the verdict semantics stay in
/// one place.
pub(crate) fn latest_reviews(
client: &Client,
repo: &str,
pr: u64,
) -> Result<Vec<(String, String)>> {
let reviews = client.get_json_all(&format!("/repos/{repo}/pulls/{pr}/reviews"), 10)?;
let mut latest: Vec<(String, String)> = Vec::new();
for r in &reviews {
let Some(login) = r
.get("user")
.and_then(|u| u.get("login"))
.and_then(Value::as_str)
else {
continue;
};
let st = r.get("state").and_then(Value::as_str).unwrap_or("");
if st == "COMMENT" || st == "PENDING" || st.is_empty() {
continue;
}
if let Some(slot) = latest.iter_mut().find(|(l, _)| l == login) {
st.clone_into(&mut slot.1);
} else {
latest.push((login.to_owned(), st.to_owned()));
}
}
Ok(latest)
}