fix: align comment numbering with review sequence

This commit is contained in:
2026-04-08 23:44:43 +08:00
parent eb7dd1125d
commit 56f436592d
7 changed files with 44 additions and 10 deletions
+2 -2
View File
@@ -71,14 +71,14 @@ total: <files_changed> files, +<additions>, -<deletions>
## Review 1 (<review-state>) ## Review 1 (<review-state>)
> <reviewer> > <reviewer>
### Comment <pr-index>.<comment-seq> ### Comment <pr-index>.<review-seq>.<comment-seq>
<file>:<line> <file>:<line>
<user>: <user>:
```md ```md
<original comment body> <original comment body>
``` ```
### Reply <pr-index>.<comment-seq>.<reply-seq> ### Reply <pr-index>.<review-seq>.<comment-seq>.<reply-seq>
<file>:<line> <file>:<line>
<user>: <user>:
```md ```md
+2 -2
View File
@@ -71,14 +71,14 @@ total: <files_changed> files, +<additions>, -<deletions>
## Review 1 (<review-state>) ## Review 1 (<review-state>)
> <reviewer> > <reviewer>
### Comment <pr-index>.<comment-seq> ### Comment <pr-index>.<review-seq>.<comment-seq>
<file>:<line> <file>:<line>
<user>: <user>:
```md ```md
<原始评论正文> <原始评论正文>
``` ```
### Reply <pr-index>.<comment-seq>.<reply-seq> ### Reply <pr-index>.<review-seq>.<comment-seq>.<reply-seq>
<file>:<line> <file>:<line>
<user>: <user>:
```md ```md
+1
View File
@@ -67,6 +67,7 @@ pub struct CommentThread {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommentItem { pub struct CommentItem {
pub id: i64, pub id: i64,
pub review_id: Option<i64>,
pub user: String, pub user: String,
pub created_at: String, pub created_at: String,
pub path: Option<String>, pub path: Option<String>,
+1
View File
@@ -214,6 +214,7 @@ fn to_comment_item(comment: ReviewCommentDto) -> CommentItem {
CommentItem { CommentItem {
id: comment.id, id: comment.id,
review_id: comment.pull_request_review_id,
user: comment.user.login, user: comment.user.login,
created_at: comment.created_at, created_at: comment.created_at,
path: comment.path, path: comment.path,
+28 -4
View File
@@ -29,9 +29,8 @@ fn render_comment_location(path: Option<&str>, line: Option<i64>) -> Option<Stri
}) })
} }
fn render_thread(pr_index: i64, comment_seq: usize, thread: &CommentThread) -> String { fn render_thread(comment_number: &str, thread: &CommentThread) -> String {
let mut out = String::new(); let mut out = String::new();
let comment_number = format!("{pr_index}.{comment_seq}");
out.push_str(&format!("### Comment {comment_number}\n")); out.push_str(&format!("### Comment {comment_number}\n"));
let root_location = let root_location =
render_comment_location(thread.root_comment.path.as_deref(), thread.root_comment.line) render_comment_location(thread.root_comment.path.as_deref(), thread.root_comment.line)
@@ -57,6 +56,7 @@ fn render_thread(pr_index: i64, comment_seq: usize, thread: &CommentThread) -> S
pub fn render_markdown(doc: &PrReviewDocument) -> String { pub fn render_markdown(doc: &PrReviewDocument) -> String {
let mut out = String::new(); let mut out = String::new();
let mut rendered = vec![false; doc.threads.len()];
out.push_str(&format!( out.push_str(&format!(
"# {} `#{}` {}\n\n", "# {} `#{}` {}\n\n",
@@ -108,11 +108,35 @@ pub fn render_markdown(doc: &PrReviewDocument) -> String {
if let Some(submitted_at) = &review.submitted_at { if let Some(submitted_at) = &review.submitted_at {
out.push_str(&format!("- submitted at: {}\n\n", submitted_at)); out.push_str(&format!("- submitted at: {}\n\n", submitted_at));
} }
let mut review_comment_seq = 1usize;
for (thread_index, thread) in doc.threads.iter().enumerate() {
if thread.root_comment.review_id == Some(review.id) {
let comment_number =
format!("{}.{}.{}", doc.meta.pr_index, review_index + 1, review_comment_seq);
out.push_str(&render_thread(&comment_number, thread));
out.push('\n');
rendered[thread_index] = true;
review_comment_seq += 1;
}
}
out.push('\n');
} }
let mut has_unassigned = false;
let mut unassigned_seq = 1usize;
for (thread_index, thread) in doc.threads.iter().enumerate() { for (thread_index, thread) in doc.threads.iter().enumerate() {
out.push_str(&render_thread(doc.meta.pr_index, thread_index + 1, thread)); if !rendered[thread_index] {
out.push('\n'); if !has_unassigned {
out.push_str("## Comments (No Review)\n\n");
has_unassigned = true;
}
let comment_number = format!("{}.0.{}", doc.meta.pr_index, unassigned_seq);
out.push_str(&render_thread(&comment_number, thread));
out.push('\n');
unassigned_seq += 1;
}
} }
out.trim_end().to_string() out.trim_end().to_string()
+1
View File
@@ -35,6 +35,7 @@ fn model_json_roundtrip() {
line: Some(10), line: Some(10),
root_comment: CommentItem { root_comment: CommentItem {
id: 100, id: 100,
review_id: Some(1),
user: "reviewer".into(), user: "reviewer".into(),
created_at: "2026-04-08T12:00:00Z".into(), created_at: "2026-04-08T12:00:00Z".into(),
path: Some("src/main.rs".into()), path: Some("src/main.rs".into()),
+9 -2
View File
@@ -48,6 +48,7 @@ fn render_markdown_includes_expected_sections_and_preserves_comment_markdown() {
line: Some(9), line: Some(9),
root_comment: CommentItem { root_comment: CommentItem {
id: 11, id: 11,
review_id: Some(1),
user: "bob".into(), user: "bob".into(),
created_at: "2026-04-08T11:01:00Z".into(), created_at: "2026-04-08T11:01:00Z".into(),
path: Some("src/main.rs".into()), path: Some("src/main.rs".into()),
@@ -56,6 +57,7 @@ fn render_markdown_includes_expected_sections_and_preserves_comment_markdown() {
}, },
replies: vec![CommentItem { replies: vec![CommentItem {
id: 12, id: 12,
review_id: Some(1),
user: "alice".into(), user: "alice".into(),
created_at: "2026-04-08T11:02:00Z".into(), created_at: "2026-04-08T11:02:00Z".into(),
path: Some("src/main.rs".into()), path: Some("src/main.rs".into()),
@@ -72,10 +74,14 @@ fn render_markdown_includes_expected_sections_and_preserves_comment_markdown() {
assert!(md.contains("## Commits")); assert!(md.contains("## Commits"));
assert!(md.contains("## Diff Stat")); assert!(md.contains("## Diff Stat"));
assert!(md.contains("## Review 1 (COMMENT)")); assert!(md.contains("## Review 1 (COMMENT)"));
assert!(md.contains("### Comment 7.1")); assert!(md.contains("### Comment 7.1.1"));
assert!(md.contains("src/main.rs:9")); assert!(md.contains("src/main.rs:9"));
assert!(md.contains("### Reply 7.1.1")); assert!(md.contains("### Reply 7.1.1.1"));
assert!(md.contains("src/main.rs:12")); assert!(md.contains("src/main.rs:12"));
let review_pos = md.find("## Review 1 (COMMENT)").unwrap();
let comment_pos = md.find("### Comment 7.1.1").unwrap();
assert!(comment_pos > review_pos);
assert!(!md.contains("## Comments (No Review)"));
assert!(md.contains("````md")); assert!(md.contains("````md"));
assert!(md.contains("```rs\nlet x = 1;\n```")); assert!(md.contains("```rs\nlet x = 1;\n```"));
assert!(md.contains("looks good")); assert!(md.contains("looks good"));
@@ -111,6 +117,7 @@ fn render_markdown_uses_minimal_fence_for_plain_text() {
line: None, line: None,
root_comment: CommentItem { root_comment: CommentItem {
id: 1, id: 1,
review_id: None,
user: "bob".into(), user: "bob".into(),
created_at: "2026-04-08T11:00:00Z".into(), created_at: "2026-04-08T11:00:00Z".into(),
path: None, path: None,