forge cli: show comment ids and resolved state on inline review comments, --only unresolved, and list them in pr timeline
This commit is contained in:
parent
e334513244
commit
219d85a15d
1 changed files with 335 additions and 44 deletions
|
|
@ -32,6 +32,7 @@ use serde_json::{Value, json};
|
|||
use time::format_description::well_known::Rfc3339;
|
||||
|
||||
use crate::client::{Client, index};
|
||||
use crate::verbs::pr_reviews::{fetch_inline_comments, render_inline_comment};
|
||||
use crate::verbs::{MAX_LIMIT, NullableVec, PAGE_SIZE, clamp_limit, parse_rfc3339, print_json};
|
||||
|
||||
#[derive(ClapArgs)]
|
||||
|
|
@ -85,7 +86,10 @@ pub fn run(client: &Client, args: Args) -> Result<()> {
|
|||
};
|
||||
// Serialize back to the API's JSON shape so the per-type render
|
||||
// arms (and their tests) keep working on plain `Value`s.
|
||||
let v = serde_json::to_value(&events)?;
|
||||
let mut v = serde_json::to_value(&events)?;
|
||||
if let Some(arr) = v.as_array_mut() {
|
||||
attach_inline_comments(client, args.number, arr);
|
||||
}
|
||||
if client.json_mode() {
|
||||
return print_json(&json!({
|
||||
"events": v,
|
||||
|
|
@ -285,6 +289,52 @@ fn fetch_since(
|
|||
Ok((events, more))
|
||||
}
|
||||
|
||||
/// Attach the inline (line) comments to the review events of a PR timeline.
|
||||
///
|
||||
/// The timeline row for a line comment carries no file or line, and the
|
||||
/// `review` row none of its comments, so both are looked up per review: each
|
||||
/// `code` row gets its comment under `inline` (path, position, resolver), and
|
||||
/// a `review` row with no `code` row of its own in this window gets all its
|
||||
/// comments under `inline_comments`, so they show up whichever way the forge
|
||||
/// lists them, and never twice. One fetch per distinct review; a failed one
|
||||
/// leaves the rows as they were.
|
||||
fn attach_inline_comments(client: &Client, number: u64, events: &mut [Value]) {
|
||||
let review_id = |ev: &Value| {
|
||||
ev.get("review_id")
|
||||
.and_then(Value::as_i64)
|
||||
.filter(|&id| id > 0)
|
||||
};
|
||||
let kind = |ev: &Value| ev.get("type").and_then(Value::as_str).map(str::to_owned);
|
||||
let with_code: std::collections::HashSet<i64> = events
|
||||
.iter()
|
||||
.filter(|ev| kind(ev).as_deref() == Some("code"))
|
||||
.filter_map(review_id)
|
||||
.collect();
|
||||
let mut fetched: std::collections::HashMap<i64, Vec<Value>> = std::collections::HashMap::new();
|
||||
for ev in events.iter_mut() {
|
||||
let (Some(rid), Some(k)) = (review_id(ev), kind(ev)) else {
|
||||
continue;
|
||||
};
|
||||
if k != "code" && k != "review" {
|
||||
continue;
|
||||
}
|
||||
let comments = fetched
|
||||
.entry(rid)
|
||||
.or_insert_with(|| fetch_inline_comments(client, number, rid));
|
||||
if k == "code" {
|
||||
let id = ev.get("id").and_then(Value::as_i64);
|
||||
if let Some(c) = comments
|
||||
.iter()
|
||||
.find(|c| c.get("id").and_then(Value::as_i64) == id)
|
||||
{
|
||||
ev["inline"] = c.clone();
|
||||
}
|
||||
} else if !with_code.contains(&rid) && !comments.is_empty() {
|
||||
ev["inline_comments"] = Value::Array(comments.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Render one timeline event as a single `**actor @ ts**: summary`
|
||||
/// line. Comment rows inline their full body; structured event types
|
||||
/// (label, assignees, close, etc.) get a one-line human summary
|
||||
|
|
@ -439,7 +489,24 @@ fn format_event(ev: &Value) -> String {
|
|||
format!("{verb} #{number} ({title})")
|
||||
}
|
||||
"changed_target_branch" => "changed target branch".to_owned(),
|
||||
"review" => "submitted a review".to_owned(),
|
||||
"review" => {
|
||||
let mut out = "submitted a review".to_owned();
|
||||
let inline = ev.get("inline_comments").and_then(Value::as_array);
|
||||
for c in inline.into_iter().flatten() {
|
||||
out.push_str("\n ");
|
||||
out.push_str(&render_inline_comment(c));
|
||||
}
|
||||
out
|
||||
}
|
||||
// A line comment: the comment text, led by where it sits and its
|
||||
// id when `attach_inline_comments` could look that up.
|
||||
"code" => {
|
||||
let body = ev.get("body").and_then(Value::as_str).unwrap_or("");
|
||||
match ev.get("inline") {
|
||||
Some(c) => format!("commented on a line\n {}", render_inline_comment(c)),
|
||||
None => format!("commented on a line: {body}"),
|
||||
}
|
||||
}
|
||||
"lock" => "locked the conversation".to_owned(),
|
||||
"unlock" => "unlocked the conversation".to_owned(),
|
||||
// Future / unknown types: surface the raw label so we don't
|
||||
|
|
@ -699,4 +766,50 @@ mod tests {
|
|||
});
|
||||
assert_eq!(format_event(&ev), "**? @ 2026-05-31T12:00:00Z**: closed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn code_event_without_lookup_shows_the_bare_body() {
|
||||
let ev = serde_json::json!({
|
||||
"type": "code", "user": {"login": "argus"},
|
||||
"created_at": "2026-11-01T01:00:00Z", "body": "off by one",
|
||||
});
|
||||
assert_eq!(
|
||||
format_event(&ev),
|
||||
"**argus @ 2026-11-01T01:00:00Z**: commented on a line: off by one"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn code_event_with_lookup_shows_id_location_and_state() {
|
||||
let ev = serde_json::json!({
|
||||
"type": "code", "user": {"login": "argus"},
|
||||
"created_at": "2026-11-01T01:00:00Z", "body": "off by one",
|
||||
"inline": {"id": 9, "path": "src/a.rs", "position": 42, "body": "off by one",
|
||||
"resolver": {"login": "mara"}},
|
||||
});
|
||||
assert_eq!(
|
||||
format_event(&ev),
|
||||
"**argus @ 2026-11-01T01:00:00Z**: commented on a line\n [#9 src/a.rs:42] off by one (resolved by mara)"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn review_event_lists_its_inline_comments() {
|
||||
let ev = serde_json::json!({
|
||||
"type": "review", "user": {"login": "argus"},
|
||||
"created_at": "2026-11-01T01:00:00Z",
|
||||
"inline_comments": [{"id": 9, "path": "src/a.rs", "position": 42, "body": "x"}],
|
||||
});
|
||||
assert_eq!(
|
||||
format_event(&ev),
|
||||
"**argus @ 2026-11-01T01:00:00Z**: submitted a review\n [#9 src/a.rs:42] x"
|
||||
);
|
||||
// Without the lookup it stays the one-line summary.
|
||||
let bare = serde_json::json!({"type": "review", "user": {"login": "argus"},
|
||||
"created_at": "2026-11-01T01:00:00Z"});
|
||||
assert_eq!(
|
||||
format_event(&bare),
|
||||
"**argus @ 2026-11-01T01:00:00Z**: submitted a review"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue