Fixes all clippy -D warnings errors in the crates iris owns: hive-sh4re: - doc_lazy_continuation: add blank /// separator in priv_proto.rs - doc_markdown: backtick PRIVATE_NETWORK=0 / PRIVATE_NETWORK=1 hive-matrix-mcp: - map_unwrap_or: map().unwrap_or_else() -> map_or_else() in paths.rs - collapsible_if: if-let chains in wake.rs - doc_markdown: backtick M_UNKNOWN_TOKEN in main.rs - cast_possible_truncation: usize/u64 -> u32::try_from in handlers.rs - map_unwrap_or: map_or_else() in handlers.rs - manual_let_else: match Ok(r) => r, Err => return -> let Ok in handlers.rs - unused_async: remove async from list_invites; update socket.rs call site hive-forge: - doc_markdown: backtick REQUEST_CHANGES / APPROVED / COMMENT in pr_reviews.rs - unnecessary_wraps: list_reviews_text returns () not Result<()> - doc_markdown: backtick start_page / last_page in comments.rs - cast_possible_truncation: PAGE_SIZE u64 -> usize; remove as usize casts hive-ag3nt: - collapsible_if: if-let chains in events.rs and mcp.rs - single_match_else: match -> if let in events.rs and mcp.rs - items_after_statements: hoist STATUS_MAX_CHARS const in mcp.rs - map_unwrap_or: map_or_else() in mcp.rs and mcp_loose_ends.rs - cast_possible_truncation: usize -> u32::try_from in mcp.rs - doc_markdown: backtick snake_case in mcp.rs, needs_update/deployed_sha in web_ui.rs, HISTORY_CAPACITY in web_ui.rs - identical_match_arms: combine manage_root_agent | query_agent_state - redundant_closure: |s| s.to_string() -> ToString::to_string in web_ui.rs - duration_suboptimal_units: from_secs(3600) -> from_hours(1) in turn.rs Remaining failures in hive-c0re (39), hive-priv (8), hive-bash-mcp (11) are owned by damocles.
164 lines
5.6 KiB
Rust
164 lines
5.6 KiB
Rust
//! `pr-reviews <number>` — list reviews, or submit one via `--approve` /
|
|
//! `--request-changes` / `--comment`.
|
|
|
|
use anyhow::{Result, bail};
|
|
use clap::Args as ClapArgs;
|
|
use serde_json::{Value, json};
|
|
|
|
use crate::client::Client;
|
|
use crate::verbs::print_json;
|
|
|
|
#[derive(ClapArgs)]
|
|
pub struct Args {
|
|
/// PR number.
|
|
number: u64,
|
|
|
|
/// Approve the PR (submit an APPROVED review).
|
|
#[arg(long, conflicts_with_all = ["request_changes", "comment"])]
|
|
approve: bool,
|
|
|
|
/// Request changes on the PR (submit a `REQUEST_CHANGES` review).
|
|
#[arg(long, conflicts_with_all = ["approve", "comment"])]
|
|
request_changes: bool,
|
|
|
|
/// Leave a comment review (submit a COMMENT review).
|
|
#[arg(long, conflicts_with_all = ["approve", "request_changes"])]
|
|
comment: bool,
|
|
|
|
/// Optional body / message for the review (used with --approve,
|
|
/// --request-changes, or --comment).
|
|
#[arg(long, short = 'm')]
|
|
body: Option<String>,
|
|
}
|
|
|
|
pub fn run(client: &Client, args: Args) -> Result<()> {
|
|
let event = if args.approve {
|
|
Some("APPROVED")
|
|
} else if args.request_changes {
|
|
Some("REQUEST_CHANGES")
|
|
} else if args.comment {
|
|
Some("COMMENT")
|
|
} else {
|
|
None
|
|
};
|
|
|
|
if let Some(ev) = event {
|
|
submit_review(client, args.number, ev, args.body)
|
|
} else {
|
|
if args.body.is_some() {
|
|
bail!("--body requires one of --approve / --request-changes / --comment");
|
|
}
|
|
list_reviews(client, args.number)
|
|
}
|
|
}
|
|
|
|
/// Submit a review event (`APPROVED` / `REQUEST_CHANGES` / `COMMENT`) and print
|
|
/// a compact summary of the created review.
|
|
fn submit_review(client: &Client, number: u64, event: &str, body: Option<String>) -> Result<()> {
|
|
let repo = client.repo();
|
|
let payload = json!({
|
|
"event": event,
|
|
"body": body.unwrap_or_default(),
|
|
});
|
|
let v = client.post_json(&format!("/repos/{repo}/pulls/{number}/reviews"), &payload)?;
|
|
print_json(&json!({
|
|
"id": v.get("id"),
|
|
"state": v.get("state"),
|
|
"user": v.get("user").and_then(|u| u.get("login")),
|
|
}))
|
|
}
|
|
|
|
/// Fetch inline diff comments for a single review. Returns an empty vec on
|
|
/// any error (missing review, network failure) so callers can degrade
|
|
/// gracefully.
|
|
fn fetch_inline_comments(client: &Client, repo: &str, pr: u64, review_id: u64) -> Vec<Value> {
|
|
client
|
|
.get_json(&format!(
|
|
"/repos/{repo}/pulls/{pr}/reviews/{review_id}/comments"
|
|
))
|
|
.ok()
|
|
.and_then(|v| v.as_array().cloned())
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
/// List all reviews for a PR, dispatching to the appropriate output mode.
|
|
fn list_reviews(client: &Client, number: u64) -> Result<()> {
|
|
let repo = client.repo();
|
|
let v = client.get_json(&format!("/repos/{repo}/pulls/{number}/reviews"))?;
|
|
let reviews = v.as_array().cloned().unwrap_or_default();
|
|
if client.json_mode() {
|
|
list_reviews_json(client, repo, number, &reviews)
|
|
} else {
|
|
list_reviews_text(client, repo, number, &reviews);
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
/// JSON output: one object per review, with an inline `comments` array.
|
|
fn list_reviews_json(client: &Client, repo: &str, number: u64, reviews: &[Value]) -> Result<()> {
|
|
let trimmed: Vec<Value> = reviews
|
|
.iter()
|
|
.map(|r| {
|
|
let id = r.get("id").and_then(Value::as_u64).unwrap_or(0);
|
|
let inline: Vec<Value> = if id > 0 {
|
|
fetch_inline_comments(client, repo, number, id)
|
|
.iter()
|
|
.map(|c| {
|
|
json!({
|
|
"id": c.get("id"),
|
|
"path": c.get("path"),
|
|
"line": c.get("line"),
|
|
"body": c.get("body"),
|
|
})
|
|
})
|
|
.collect()
|
|
} else {
|
|
vec![]
|
|
};
|
|
json!({
|
|
"id": r.get("id"),
|
|
"state": r.get("state"),
|
|
"user": r.get("user").and_then(|u| u.get("login")),
|
|
"body": r.get("body"),
|
|
"comments_count": r.get("comments_count"),
|
|
"comments": inline,
|
|
})
|
|
})
|
|
.collect();
|
|
print_json(&Value::Array(trimmed))
|
|
}
|
|
|
|
/// Human-readable output: Markdown-style heading per review, inline
|
|
/// comments as `[path:line] body` (line omitted for PR-level comments).
|
|
fn list_reviews_text(client: &Client, repo: &str, number: u64, reviews: &[Value]) {
|
|
if reviews.is_empty() {
|
|
println!("(no reviews)");
|
|
return;
|
|
}
|
|
for r in reviews {
|
|
let id = r.get("id").and_then(Value::as_u64).unwrap_or(0);
|
|
let user = r
|
|
.get("user")
|
|
.and_then(|u| u.get("login"))
|
|
.and_then(Value::as_str)
|
|
.unwrap_or("?");
|
|
let state = r.get("state").and_then(Value::as_str).unwrap_or("?");
|
|
let body = r.get("body").and_then(Value::as_str).unwrap_or("").trim();
|
|
println!("### review by {user} ({state})");
|
|
if !body.is_empty() {
|
|
println!("{body}");
|
|
}
|
|
if id > 0 {
|
|
for c in &fetch_inline_comments(client, repo, number, id) {
|
|
let path = c.get("path").and_then(Value::as_str).unwrap_or("?");
|
|
let cbody = c.get("body").and_then(Value::as_str).unwrap_or("").trim();
|
|
// PR-level comments have no line; omit `:line` when absent.
|
|
match c.get("line").and_then(Value::as_u64) {
|
|
Some(line) => println!(" [{path}:{line}] {cbody}"),
|
|
None => println!(" [{path}] {cbody}"),
|
|
}
|
|
}
|
|
}
|
|
println!();
|
|
}
|
|
}
|