fix(prreview): classify comment lines against the diff before posting

This commit is contained in:
sam committed 2026-10-30 00:35:36 +11:00
1 parent 7c8114bc55
commit 0a1aa8b3c2
3 files changed
+51 -10

No files matched your search

+11 -5
View File
@@ -674,9 +674,15 @@ var languages = map[string]string{
".css": "css",
}
// Commentable reports whether line in path is a line the change's diff shows
// (added or context), the only lines a forge accepts an inline comment on.
func Commentable(files []FileChange, path string, line int) bool {
// MapLine classifies one line of one file as added, context or outside the
// diff. Added and context lines are lines the diff shows, the only lines a
// forge accepts an inline review comment on; an outside line -- one no hunk
// contains, any line of a file the diff does not touch, or no line at all --
// is where a comment belongs in the review body rather than inline.
func MapLine(files []FileChange, path string, line int) LineKind {
if line <= 0 {
return LineOutside
}
for _, file := range files {
if file.Path != path {
continue
@@ -684,10 +690,10 @@ func Commentable(files []FileChange, path string, line int) bool {
for _, hunk := range file.Hunks {
for _, placed := range hunk.positions() {
if placed.Line == line {
return true
return placed.Kind
}
}
}
}
return false
return LineOutside
}
@@ -0,0 +1,35 @@
package prreview
import "testing"
func TestMapLine(t *testing.T) {
files := []FileChange{{
Path: "calc/calc.go",
Hunks: []Hunk{
{NewStart: 10, Lines: []string{" ctx", "+added", "-gone"}},
{NewStart: 30, Lines: []string{"+later"}},
},
}}
tests := []struct {
name string
path string
line int
want LineKind
}{
{"added line", "calc/calc.go", 11, LineAdded},
{"context line", "calc/calc.go", 10, LineContext},
{"added line in a later hunk", "calc/calc.go", 30, LineAdded},
{"line the hunk does not contain", "calc/calc.go", 12, LineOutside},
{"line between hunks", "calc/calc.go", 20, LineOutside},
{"line before the hunk", "calc/calc.go", 5, LineOutside},
{"file not in the diff", "README.md", 1, LineOutside},
{"no line", "calc/calc.go", 0, LineOutside},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MapLine(files, tt.path, tt.line); got != tt.want {
t.Fatalf("MapLine(%q, %d) = %q, want %q", tt.path, tt.line, got, tt.want)
}
})
}
}
+5 -5
View File
@@ -699,15 +699,15 @@ func postApprovedReview(ctx context.Context, tc *TaskContext, gate task.ReviewGa
}
// forgeReview renders findings as one forge review: findings on a line the
// diff shows as inline comments, the rest as a list in the review body (a
// forge rejects the whole review over one comment it cannot place), and
// REQUEST_CHANGES when any finding blocks, unless the task opened the pull
// request itself.
// diff shows (added or context) as inline comments, findings on a line
// outside the diff as a list in the review body (a forge rejects the whole
// review over one comment it cannot place), and REQUEST_CHANGES when any
// finding blocks, unless the task opened the pull request itself.
func forgeReview(findings []prreview.ScoredFinding, files []prreview.FileChange, ownPR bool) Review {
review := Review{Event: string(reviewEvent(findings, ownPR))}
var unanchored []string
for _, f := range findings {
if f.LineStart <= 0 || !prreview.Commentable(files, f.File, f.LineStart) {
if prreview.MapLine(files, f.File, f.LineStart) == prreview.LineOutside {
where := f.File
if f.LineStart > 0 {
where = fmt.Sprintf("%s:%d", f.File, f.LineStart)