fix(workflow): post review findings outside the diff in the review body instead of inline
This commit is contained in:
1 parent
7973feceb0
commit
6d7f341de5
3 files changed
+68
-8
No files matched your search
@@ -673,3 +673,21 @@ var languages = map[string]string{
|
||||
".html": "html",
|
||||
".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 {
|
||||
for _, file := range files {
|
||||
if file.Path != path {
|
||||
continue
|
||||
}
|
||||
for _, hunk := range file.Hunks {
|
||||
for _, placed := range hunk.positions() {
|
||||
if placed.Line == line {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package workflow
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/prreview"
|
||||
)
|
||||
|
||||
func TestForgeReviewPlacesOnlyDiffLinesInline(t *testing.T) {
|
||||
files := []prreview.FileChange{{
|
||||
Path: "calc/calc.go",
|
||||
Hunks: []prreview.Hunk{{NewStart: 10, Lines: []string{" ctx", "+added", "-gone", " ctx2"}}},
|
||||
}}
|
||||
tests := []struct {
|
||||
name string
|
||||
file string
|
||||
line int
|
||||
inline bool
|
||||
}{
|
||||
{"added line", "calc/calc.go", 11, true},
|
||||
{"context line", "calc/calc.go", 12, true},
|
||||
{"outside the hunk", "calc/calc.go", 40, false},
|
||||
{"file not in the diff", "README.md", 1, false},
|
||||
{"no line", "calc/calc.go", 0, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
review := forgeReview([]prreview.ScoredFinding{{File: tt.file, LineStart: tt.line, Body: "b"}}, files)
|
||||
if got := len(review.Comments) == 1; got != tt.inline {
|
||||
t.Fatalf("inline = %v, want %v (body %q)", got, tt.inline, review.Body)
|
||||
}
|
||||
if !tt.inline && review.Body == "" {
|
||||
t.Fatal("finding dropped: neither inline nor in the body")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -688,27 +688,32 @@ func postApprovedReview(ctx context.Context, tc *TaskContext, gate task.ReviewGa
|
||||
if len(posted) == 0 {
|
||||
return "operator approved no findings; nothing posted", nil
|
||||
}
|
||||
if err := tc.Forge.CreateReviewComments(ctx, gate.Owner, gate.Repo, gate.PRNumber, gate.HeadSHA, forgeReview(posted)); err != nil {
|
||||
if err := tc.Forge.CreateReviewComments(ctx, gate.Owner, gate.Repo, gate.PRNumber, gate.HeadSHA, forgeReview(posted, tc.prReview.files)); err != nil {
|
||||
return "", fmt.Errorf("post approved review: %w", err)
|
||||
}
|
||||
return fmt.Sprintf("posted %d finding(s) approved by the operator as %s", len(posted), prreview.ReviewEventFor(posted)), nil
|
||||
}
|
||||
|
||||
// forgeReview renders findings as one forge review: line-anchored findings
|
||||
// as inline comments, whole-file findings as a list in the review body, and
|
||||
// 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.
|
||||
func forgeReview(findings []prreview.ScoredFinding) Review {
|
||||
func forgeReview(findings []prreview.ScoredFinding, files []prreview.FileChange) Review {
|
||||
review := Review{Event: string(prreview.ReviewEventFor(findings))}
|
||||
var unanchored []string
|
||||
for _, f := range findings {
|
||||
if f.LineStart <= 0 {
|
||||
unanchored = append(unanchored, fmt.Sprintf("- `%s`: %s", f.File, f.Body))
|
||||
if f.LineStart <= 0 || !prreview.Commentable(files, f.File, f.LineStart) {
|
||||
where := f.File
|
||||
if f.LineStart > 0 {
|
||||
where = fmt.Sprintf("%s:%d", f.File, f.LineStart)
|
||||
}
|
||||
unanchored = append(unanchored, fmt.Sprintf("- `%s`: %s", where, f.Body))
|
||||
continue
|
||||
}
|
||||
review.Comments = append(review.Comments, ReviewComment{Path: f.File, Line: f.LineStart, Body: f.Body})
|
||||
}
|
||||
if len(unanchored) > 0 {
|
||||
review.Body = "Findings without a line anchor:\n\n" + strings.Join(unanchored, "\n")
|
||||
review.Body = "Findings outside the diff:\n\n" + strings.Join(unanchored, "\n")
|
||||
}
|
||||
return review
|
||||
}
|
||||
@@ -719,7 +724,7 @@ func postPRReview(ctx context.Context, tc *TaskContext) error {
|
||||
return nil
|
||||
}
|
||||
return tc.Forge.CreateReviewComments(
|
||||
ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.PRNumber, tc.prReview.headSHA, forgeReview(tc.prReview.scored),
|
||||
ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.PRNumber, tc.prReview.headSHA, forgeReview(tc.prReview.scored, tc.prReview.files),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user