From eb7dd1125df964cec11ac3a9f2b849f8259f2881 Mon Sep 17 00:00:00 2001 From: Origami404 Date: Wed, 8 Apr 2026 23:37:20 +0800 Subject: [PATCH] fix: prefer valid position for comment line fallback --- src/normalize.rs | 17 ++++++++++++- tests/normalize_tests.rs | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/normalize.rs b/src/normalize.rs index 65ce9b0..445c420 100644 --- a/src/normalize.rs +++ b/src/normalize.rs @@ -197,12 +197,27 @@ fn to_commit_item(commit: CommitDto) -> CommitItem { } fn to_comment_item(comment: ReviewCommentDto) -> CommentItem { + let line = if let Some(value) = comment.line.filter(|value| *value > 0) { + Some(value) + } else if let Some(value) = comment + .position + .filter(|value| *value > 0) + .and_then(|value| i64::try_from(value).ok()) + { + Some(value) + } else { + comment + .original_position + .filter(|value| *value > 0) + .and_then(|value| i64::try_from(value).ok()) + }; + CommentItem { id: comment.id, user: comment.user.login, created_at: comment.created_at, path: comment.path, - line: comment.line, + line, body: comment.body, } } diff --git a/tests/normalize_tests.rs b/tests/normalize_tests.rs index 4981539..0808f12 100644 --- a/tests/normalize_tests.rs +++ b/tests/normalize_tests.rs @@ -249,3 +249,56 @@ fn normalize_does_not_merge_unrelated_comments_on_same_file() { assert_eq!(doc.threads[1].root_comment.id, 11); assert_eq!(doc.threads[1].replies.len(), 0); } + +#[test] +fn normalize_uses_non_zero_position_when_line_is_missing() { + let bundle = PullBundleDto { + pull: PullDto { + number: 101, + title: "T".into(), + state: "open".into(), + body: None, + user: UserDto { + login: "alice".into(), + }, + base: PullBranchDto { + ref_name: "main".into(), + }, + head: PullBranchDto { + ref_name: "feature/z".into(), + }, + created_at: "2026-04-08T10:00:00Z".into(), + updated_at: "2026-04-08T10:00:00Z".into(), + merged_at: None, + additions: None, + deletions: None, + changed_files: None, + }, + reviews: vec![], + comments: vec![ReviewCommentDto { + id: 1001, + body: "position based comment".into(), + created_at: "2026-04-08T12:00:00Z".into(), + updated_at: None, + user: UserDto { + login: "bob".into(), + }, + path: Some("src/main.rs".into()), + line: None, + pull_request_review_id: Some(1), + in_reply_to: None, + original_position: Some(42), + position: Some(41), + commit_id: Some("abc123".into()), + original_commit_id: Some("abc123".into()), + diff_hunk: None, + }], + commits: vec![], + files: vec![], + }; + + let doc = normalize_bundle("org/repo", bundle); + assert_eq!(doc.threads.len(), 1); + assert_eq!(doc.threads[0].root_comment.path.as_deref(), Some("src/main.rs")); + assert_eq!(doc.threads[0].root_comment.line, Some(41)); +}