diff --git a/internal/cli/postreview.go b/internal/cli/postreview.go index a2ab6c257f..c7c64a1a5d 100644 --- a/internal/cli/postreview.go +++ b/internal/cli/postreview.go @@ -35,6 +35,11 @@ var hexSHARe = regexp.MustCompile(`^[0-9a-fA-F]{40}$|^[0-9a-fA-F]{64}$`) var reasonRe = regexp.MustCompile(`^[a-zA-Z0-9_-]*$`) var hunkHeaderRe = regexp.MustCompile(`^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@`) +// findingIDRe matches the opaque id the review agent emits (f_ plus +// alphanumeric). Anything else is left off the comment so a crafted id +// cannot inject markup. The resolved-thread fetch matches this form (#7907). +var findingIDRe = regexp.MustCompile(`^f_[a-zA-Z0-9]+$`) + func newPostReviewCmd() *cobra.Command { var ( repo string @@ -200,6 +205,23 @@ type ReviewFinding struct { Description string `json:"description"` Remediation string `json:"remediation,omitempty"` Actionable bool `json:"actionable,omitempty"` + // ID is an opaque stable identifier (f_ plus alphanumeric) assigned + // upstream. This command copies it onto the inline comment; it does + // not mint ids. Empty until the agents repo starts emitting it. + ID findingID `json:"id,omitempty"` +} + +// findingID is an optional review finding id. Non-string JSON values are +// ignored so a bad id does not fail the whole review parse. +type findingID string + +func (id *findingID) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err != nil { + return nil + } + *id = findingID(s) + return nil } // reviewActionToEvent maps a ReviewResult action to a GitHub PR review event. @@ -532,9 +554,14 @@ func findingsToReviewComments(findings []ReviewFinding, diffHunks map[string][][ } // formatFindingComment renders a single review finding as a Markdown -// inline comment body. +// inline comment body. A valid id is stamped first so a later fetch of +// resolved review threads can match the comment without parsing prose. +// File-level comments use this same helper, so they carry the stamp too. func formatFindingComment(f ReviewFinding) string { var b strings.Builder + if findingIDRe.MatchString(string(f.ID)) { + fmt.Fprintf(&b, "\n", f.ID) + } fmt.Fprintf(&b, "**[%s]** %s", f.Severity, f.Category) b.WriteString("\n\n") b.WriteString(strings.TrimSpace(f.Description)) @@ -823,6 +850,12 @@ func sanitizeReviewResult(r ReviewResult, printer *ui.Printer) ReviewResult { r.Findings[i].Remediation = result.Sanitized } } + if r.Findings[i].ID != "" { + result := pipeline.Scan(string(r.Findings[i].ID)) + if len(result.Findings) > 0 || result.Sanitized != "" { + r.Findings[i].ID = "" + } + } } return r diff --git a/internal/cli/postreview_test.go b/internal/cli/postreview_test.go index 46bb6e4eaf..0dc0048ebb 100644 --- a/internal/cli/postreview_test.go +++ b/internal/cli/postreview_test.go @@ -9,6 +9,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -69,12 +70,39 @@ func TestParseReviewResult_HeadSHA(t *testing.T) { } func TestParseReviewResult_Findings(t *testing.T) { - input := `{"body":"Review","action":"approve","findings":[{"severity":"low","category":"docs","file":"README.md","line":12,"description":"Missing usage note","remediation":"Add a short note","actionable":true}]}` + input := `{"body":"Review","action":"approve","findings":[{"severity":"low","category":"docs","file":"README.md","line":12,"description":"Missing usage note","remediation":"Add a short note","actionable":true,"id":"f_abc123"}]}` result, err := parseReviewResult(input) require.NoError(t, err) require.Len(t, result.Findings, 1) assert.Equal(t, "low", result.Findings[0].Severity) assert.True(t, result.Findings[0].Actionable) + assert.EqualValues(t, "f_abc123", result.Findings[0].ID) +} + +func TestParseReviewResult_NonStringIDIgnored(t *testing.T) { + tests := []struct { + name string + id string + }{ + {"number", "123"}, + {"boolean", "true"}, + {"object", `{"x":1}`}, + {"array", `["f_abc"]`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + input := fmt.Sprintf(`{"body":"Review","action":"approve","head_sha":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","findings":[{"severity":"low","category":"docs","file":"README.md","line":12,"description":"note","id":%s}]}`, tt.id) + result, err := parseReviewResult(input) + require.NoError(t, err) + assert.Equal(t, "Review", result.Body) + assert.Equal(t, "approve", result.Action) + assert.Equal(t, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", result.HeadSHA) + require.Len(t, result.Findings, 1) + assert.Equal(t, "note", result.Findings[0].Description) + assert.Empty(t, result.Findings[0].ID) + assert.NotContains(t, formatFindingComment(result.Findings[0]), "\n")) + assert.Contains(t, body, "**[high]** logic-error") + }) + + t.Run("invalid id is not stamped", func(t *testing.T) { + f := ReviewFinding{ + ID: "", + Severity: "high", + Category: "logic-error", + Description: "Missing nil check.", + } + body := formatFindingComment(f) + assert.NotContains(t, body, "") + assert.NotContains(t, inline[1].Body, "") + assert.Contains(t, fileLevel[0].Body, "Line 50") +} + func TestResolvePostReviewClient_GitHubDefault(t *testing.T) { t.Setenv("GH_TOKEN", "ghp-test-token") t.Setenv("GITHUB_TOKEN", "") @@ -1490,6 +1564,53 @@ func TestSanitizeReviewResult_NoSecretsPassesThrough(t *testing.T) { assert.Equal(t, "style", sanitized.Findings[0].Category, "clean category should pass through unchanged") } +func TestSanitizeReviewResult_DropsSecretFindingID(t *testing.T) { + printer := ui.New(io.Discard) + // f_ plus a Google API key (AIza + 35 alnum) still matches findingIDRe. + secretID := "f_AIzaabcdefghijklmnopqrstuvwxyz012345678" + r := ReviewResult{ + Body: "Review body without secrets.", + Action: "comment", + Findings: []ReviewFinding{ + { + ID: findingID(secretID), + Severity: "high", + Category: "bug", + File: "a.go", + Line: 10, + Description: "Missing nil check.", + }, + }, + } + + sanitized := sanitizeReviewResult(r, printer) + assert.Empty(t, sanitized.Findings[0].ID) + assert.NotContains(t, formatFindingComment(sanitized.Findings[0]), "") +} + func TestSanitizeReviewResult_EmptyBody(t *testing.T) { printer := ui.New(io.Discard) r := ReviewResult{