refactor(#1141): extract submit_review, list_reviews, fetch_inline_comments helpers

This commit is contained in:
damocles 2026-06-03 16:00:35 +02:00
commit b39967eb7a

View file

@ -32,8 +32,6 @@ pub struct Args {
} }
pub fn run(client: &Client, args: Args) -> Result<()> { pub fn run(client: &Client, args: Args) -> Result<()> {
let repo = client.repo();
let event = if args.approve { let event = if args.approve {
Some("APPROVED") Some("APPROVED")
} else if args.request_changes { } else if args.request_changes {
@ -45,114 +43,119 @@ pub fn run(client: &Client, args: Args) -> Result<()> {
}; };
if let Some(ev) = event { if let Some(ev) = event {
// Submit a review. submit_review(client, args.number, ev, args.body)
let payload = json!({
"event": ev,
"body": args.body.unwrap_or_default(),
});
let v = client.post_json(
&format!("/repos/{repo}/pulls/{}/reviews", args.number),
&payload,
)?;
// Print a compact summary rather than the full review blob.
let summary = json!({
"id": v.get("id"),
"state": v.get("state"),
"user": v.get("user").and_then(|u| u.get("login")),
});
print_json(&summary)
} else { } else {
if args.body.is_some() { if args.body.is_some() {
bail!("--body requires one of --approve / --request-changes / --comment"); bail!("--body requires one of --approve / --request-changes / --comment");
} }
// List mode: fetch reviews, then fetch inline comments for each review list_reviews(client, args.number)
// so the full review content is visible without curl fallbacks. }
let v = }
client.get_json(&format!("/repos/{repo}/pulls/{}/reviews", args.number))?;
let reviews = v.as_array().cloned().unwrap_or_default();
if client.json_mode() { /// Submit a review event (APPROVED / REQUEST_CHANGES / COMMENT) and print
let trimmed: Vec<Value> = reviews /// a compact summary of the created review.
.iter() fn submit_review(
.map(|r| { client: &Client,
let id = r.get("id").and_then(Value::as_u64).unwrap_or(0); number: u64,
let inline = if id > 0 { event: &str,
client body: Option<String>,
.get_json(&format!( ) -> Result<()> {
"/repos/{repo}/pulls/{}/reviews/{id}/comments", let repo = client.repo();
args.number let payload = json!({
)) "event": event,
.ok() "body": body.unwrap_or_default(),
.and_then(|v| v.as_array().cloned()) });
.map(|comments| { let v = client.post_json(
comments &format!("/repos/{repo}/pulls/{number}/reviews"),
.iter() &payload,
.map(|c| { )?;
json!({ print_json(&json!({
"id": c.get("id"), "id": v.get("id"),
"path": c.get("path"), "state": v.get("state"),
"line": c.get("line"), "user": v.get("user").and_then(|u| u.get("login")),
"body": c.get("body"), }))
}) }
})
.collect::<Vec<_>>() /// 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, including inline diff comments per review.
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() {
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"),
}) })
.unwrap_or_default() })
} else { .collect()
vec![] } else {
}; vec![]
json!({ };
"id": r.get("id"), json!({
"state": r.get("state"), "id": r.get("id"),
"user": r.get("user").and_then(|u| u.get("login")), "state": r.get("state"),
"body": r.get("body"), "user": r.get("user").and_then(|u| u.get("login")),
"comments_count": r.get("comments_count"), "body": r.get("body"),
"comments": inline, "comments_count": r.get("comments_count"),
}) "comments": inline,
}) })
.collect(); })
print_json(&Value::Array(trimmed)) .collect();
} else { print_json(&Value::Array(trimmed))
if reviews.is_empty() { } else {
println!("(no reviews)"); if reviews.is_empty() {
return Ok(()); println!("(no reviews)");
return Ok(());
}
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}");
} }
for r in &reviews { if id > 0 {
let id = r.get("id").and_then(Value::as_u64).unwrap_or(0); for c in &fetch_inline_comments(client, repo, number, id) {
let user = r let path = c.get("path").and_then(Value::as_str).unwrap_or("?");
.get("user") let line = c
.and_then(|u| u.get("login")) .get("line")
.and_then(Value::as_str) .and_then(Value::as_u64)
.unwrap_or("?"); .map(|n| n.to_string())
let state = r.get("state").and_then(Value::as_str).unwrap_or("?"); .unwrap_or_else(|| "?".to_string());
let body = r.get("body").and_then(Value::as_str).unwrap_or("").trim(); let cbody = c.get("body").and_then(Value::as_str).unwrap_or("").trim();
println!("### review by {user} ({state})"); println!(" [{path}:{line}] {cbody}");
if !body.is_empty() {
println!("{body}");
}
// Fetch and print inline comments for this review.
if id > 0 {
if let Ok(ic) = client.get_json(&format!(
"/repos/{repo}/pulls/{}/reviews/{id}/comments",
args.number
)) {
let inline = ic.as_array().cloned().unwrap_or_default();
for c in &inline {
let path = c.get("path").and_then(Value::as_str).unwrap_or("?");
let line = c
.get("line")
.and_then(Value::as_u64)
.map(|n| n.to_string())
.unwrap_or_else(|| "?".to_string());
let cbody =
c.get("body").and_then(Value::as_str).unwrap_or("").trim();
println!(" [{path}:{line}] {cbody}");
}
}
} }
println!();
} }
Ok(()) println!();
} }
Ok(())
} }
} }