diff --git a/NOTICE b/NOTICE new file mode 100644 index 000000000..c9d0c8b6b --- /dev/null +++ b/NOTICE @@ -0,0 +1,13 @@ +NOTICE + +This product includes software derived from PR-AF +(https://github.com/Agent-Field/pr-af), Copyright AgentField, +licensed under the Apache License, Version 2.0. + +Derived portions: internal/domain/workflow/prreview -- the unified diff +parser, change clustering, blast radius, evidence extraction, scoring, +deduplication, line mapping and review-event selection. + +PR-AF declares the Apache License, Version 2.0 in its pyproject.toml and +ships no license file. A copy of the License is available at +https://www.apache.org/licenses/LICENSE-2.0 diff --git a/internal/domain/workflow/diff_rules.go b/internal/domain/workflow/diff_rules.go index 2c3a39a19..d14ff3c09 100644 --- a/internal/domain/workflow/diff_rules.go +++ b/internal/domain/workflow/diff_rules.go @@ -6,10 +6,11 @@ import ( "errors" "fmt" "regexp" - "strconv" "strings" "gopkg.in/yaml.v3" + + "github.com/samcharles93/archie-core/internal/domain/workflow/prreview" ) // DiffRulesStepName is the registered workflow step type that applies a @@ -377,83 +378,19 @@ type diffLine struct { Text string } -// addedDiffLines walks a unified diff and returns the lines it ADDS, each with -// the path of the file it lands in and its line number in the new file. +// addedDiffLines returns the lines a unified diff ADDS, each with the path of +// the file it lands in and its line number in the file the change produces. // -// Only added lines are returned: a removed line has no position in the file the -// change produces, and a context line is not something the change did, so -// neither can be something a repository rule should report against. +// The diff is parsed by internal/domain/workflow/prreview, which owns the one +// parser this repository reads diffs with -- the review pipeline positions its +// findings by it. This maps that parser's result into the shape the rules +// matcher reads, so a rule and a review comment can never disagree about which +// line a change introduced. func addedDiffLines(diff string) []diffLine { - var ( - lines []diffLine - path string - newLn int - inHunk bool - ) - for raw := range strings.SplitSeq(diff, "\n") { - switch { - case strings.HasPrefix(raw, "diff --git "): - // A new file's patch starts: nothing may be attributed to the - // previous file until its own +++ header is read. - path, inHunk = "", false - case strings.HasPrefix(raw, "@@"): - start, ok := hunkNewStart(raw) - inHunk = ok - newLn = start - case !inHunk: - // Outside a hunk only the file headers say anything; index lines, - // permissions and the like are noise here. This is also what keeps - // a body line that begins with the header marker ("+++ x", the - // added line "++ x") from being read as a header. - if after, ok := strings.CutPrefix(raw, "+++ "); ok { - path = diffTargetPath(after) - } - case raw == "": - // The trailing empty element of the split, at most. - case raw[0] == '\\': - // git's "\ No newline at end of file": a marker, not a line. - case raw[0] == '-': - // A removed line: no position in the new file. - case raw[0] == '+': - lines = append(lines, diffLine{Path: path, Line: newLn, Text: raw[1:]}) - newLn++ - default: - // A context line: it advances the new-file position without - // itself being part of the change. - newLn++ - } + added := prreview.AddedLines(prreview.ParseDiff(diff)) + lines := make([]diffLine, 0, len(added)) + for _, line := range added { + lines = append(lines, diffLine{Path: line.Path, Line: line.Line, Text: line.Text}) } return lines } - -// hunkNewStart reads the new-file start line out of a hunk header such as -// "@@ -1,4 +1,5 @@ func main() {". Both counts are optional in the format, and -// the trailing section heading may contain anything. -func hunkNewStart(header string) (int, bool) { - _, after, ok := strings.Cut(header, " +") - if !ok { - return 0, false - } - rest := after - end := strings.IndexAny(rest, ", @") - if end < 0 { - return 0, false - } - start, err := strconv.Atoi(rest[:end]) - if err != nil { - return 0, false - } - return start, true -} - -// diffTargetPath reads the path out of a "+++ " header: "b/main.go" in git's -// own output, quoted when the path carries something git has to escape, and -// tab-separated from a timestamp in a plain unified diff. -func diffTargetPath(header string) string { - path := strings.TrimSpace(header) - if tab := strings.IndexByte(path, '\t'); tab >= 0 { - path = path[:tab] - } - path = strings.Trim(path, `"`) - return strings.TrimPrefix(strings.TrimPrefix(path, "b/"), "a/") -} diff --git a/internal/domain/workflow/prreview/blast.go b/internal/domain/workflow/prreview/blast.go new file mode 100644 index 000000000..944d26228 --- /dev/null +++ b/internal/domain/workflow/prreview/blast.go @@ -0,0 +1,278 @@ +package prreview + +import ( + "errors" + "fmt" + "go/parser" + "go/token" + "io/fs" + "path" + "regexp" + "sort" + "strconv" + "strings" +) + +// BlastRadius returns the files inside fsys that a change reaches without +// touching: the files that import a changed file, and the files a changed file +// imports. It is the review's other half -- a line that breaks its callers +// still compiles, so the callers have to be read even though the diff never +// mentions them. +// +// Paths are relative to fsys's root and are matched as the diff wrote them. An +// import that cannot be resolved to a file inside fsys -- a standard library +// package, a node module, a file this snapshot does not carry -- contributes +// nothing: a guessed path would put a file nobody can open into the review's +// scope. +func BlastRadius(fsys fs.FS, changed []string) ([]string, error) { + graph, err := buildImportGraph(fsys) + if err != nil { + return nil, err + } + + reached := map[string]bool{} + for _, file := range changed { + for _, imported := range graph.imports[file] { + reached[imported] = true + } + for _, dependent := range graph.dependents[file] { + reached[dependent] = true + } + } + for _, file := range changed { + delete(reached, file) + } + + radius := make([]string, 0, len(reached)) + for file := range reached { + radius = append(radius, file) + } + sort.Strings(radius) + return radius, nil +} + +// importGraph is a snapshot's file-level dependency graph, in both directions: +// an edge exists when one file's imports resolve to another file. +type importGraph struct { + imports map[string][]string + dependents map[string][]string +} + +// buildImportGraph reads every source file in the snapshot once and resolves +// the imports it declares. A file contributes no edges of its own when it +// declares none, or when it cannot be parsed: an unparsable file's imports are +// not dependable, and a guessed edge is worse than a missing one. +func buildImportGraph(fsys fs.FS) (*importGraph, error) { + paths, err := walkPaths(fsys, isSourcePath) + if err != nil { + return nil, err + } + module, err := modulePath(fsys) + if err != nil { + return nil, err + } + + goPackages := goPackageFiles(paths, module) + pythonModules := pythonModuleFiles(paths) + graph := &importGraph{imports: map[string][]string{}, dependents: map[string][]string{}} + for _, file := range paths { + imported, err := importSpecs(fsys, file, goPackages, pythonModules) + if err != nil { + return nil, err + } + for _, target := range imported { + graph.imports[file] = appendUnique(graph.imports[file], target) + graph.dependents[target] = appendUnique(graph.dependents[target], file) + } + } + return graph, nil +} + +// isSourcePath reports whether a file's imports are resolved. +func isSourcePath(file string) bool { return sourceExtensions[path.Ext(file)] } + +// sourceExtensions are the files whose imports this package resolves. +var sourceExtensions = map[string]bool{ + ".go": true, + ".py": true, + ".js": true, + ".jsx": true, + ".ts": true, + ".tsx": true, + ".mjs": true, + ".cjs": true, +} + +// modulePath reads the module path out of the snapshot's go.mod, empty when +// the snapshot has none. Without it an import path cannot be turned back into +// a directory, so no Go edge is claimed at all. +func modulePath(fsys fs.FS) (string, error) { + data, err := fs.ReadFile(fsys, "go.mod") + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + return "", nil + } + return "", fmt.Errorf("read go.mod: %w", err) + } + for line := range strings.SplitSeq(string(data), "\n") { + if module, found := strings.CutPrefix(line, "module "); found { + return strings.TrimSpace(module), nil + } + } + return "", nil +} + +// goPackageFiles maps every Go import path this snapshot owns to the files +// that package contains. The module path is what turns a directory into an +// import path, so an import resolves only when a directory of the snapshot +// really is the package that path names. +// +// A _test.go file is left out: it is never part of what an importer compiles, +// so no import path resolves to one. A test file's own imports are still edges +// of its own -- a test that exercises the change is read with it. +func goPackageFiles(paths []string, module string) map[string][]string { + if module == "" { + return nil + } + packages := map[string][]string{} + for _, file := range paths { + if path.Ext(file) != ".go" || strings.HasSuffix(file, "_test.go") { + continue + } + importPath := module + if directory := path.Dir(file); directory != "." { + importPath = module + "/" + directory + } + packages[importPath] = append(packages[importPath], file) + } + return packages +} + +// pythonModuleFiles maps every Python module name this snapshot owns to the +// files that claim it, so `from pkg.util import helper` resolves to pkg/util.py +// and `import pkg` to pkg/__init__.py. +func pythonModuleFiles(paths []string) map[string][]string { + modules := map[string][]string{} + for _, file := range paths { + if path.Ext(file) != ".py" { + continue + } + modules[pythonModule(file)] = append(modules[pythonModule(file)], file) + } + return modules +} + +// pythonModule is the module name Python imports a file by: directories become +// dots and the .py suffix, or a package's __init__, goes. +func pythonModule(file string) string { + module := strings.TrimSuffix(strings.TrimSuffix(file, ".py"), "/__init__") + return strings.ReplaceAll(module, "/", ".") +} + +// importSpecs resolves the files one source file imports. +func importSpecs(fsys fs.FS, file string, goPackages, pythonModules map[string][]string) ([]string, error) { + data, err := fs.ReadFile(fsys, file) + if err != nil { + return nil, fmt.Errorf("read %s: %w", file, err) + } + switch path.Ext(file) { + case ".go": + return goImports(data, goPackages), nil + case ".py": + return pythonImports(data, pythonModules), nil + default: + return jsImports(fsys, file, data), nil + } +} + +// goImports resolves a Go file's imports to the files of the packages they +// name. +func goImports(data []byte, packages map[string][]string) []string { + if packages == nil { + return nil + } + parsed, err := parser.ParseFile(token.NewFileSet(), "", data, parser.ImportsOnly) + if err != nil { + return nil + } + var imported []string + for _, spec := range parsed.Imports { + importPath, err := strconv.Unquote(spec.Path.Value) + if err != nil { + continue + } + imported = append(imported, packages[importPath]...) + } + return imported +} + +// pythonImports resolves a Python file's imports to the modules this snapshot +// owns. A relative import (from .util import helper) names its package only +// relative to a file the parser is not given, so it stays unresolved rather +// than guessed at. +var pythonImportsRE = regexp.MustCompile(`(?m)^\s*(?:from|import)\s+([\w.]+)`) + +func pythonImports(data []byte, modules map[string][]string) []string { + var imported []string + for _, match := range pythonImportsRE.FindAllStringSubmatch(string(data), -1) { + imported = append(imported, modules[match[1]]...) + } + return imported +} + +// jsImports resolves a JavaScript or TypeScript file's relative specifiers to +// files inside the snapshot. A bare specifier names a package, and a package +// is not a file this snapshot owns, so only "./" and "../" are resolved. +func jsImports(fsys fs.FS, file string, data []byte) []string { + source := string(data) + var imported []string + for _, pattern := range []*regexp.Regexp{jsFromRE, jsSideRE, jsRequireRE} { + for _, match := range pattern.FindAllStringSubmatch(source, -1) { + specifier := match[1] + if !strings.HasPrefix(specifier, "./") && !strings.HasPrefix(specifier, "../") { + continue + } + if resolved := resolveJS(fsys, file, specifier); resolved != "" { + imported = appendUnique(imported, resolved) + } + } + } + return imported +} + +var ( + jsFromRE = regexp.MustCompile(`(?m)\bfrom\s+['"]([^'"]+)['"]`) + jsSideRE = regexp.MustCompile(`(?m)\bimport\s+['"]([^'"]+)['"]`) + jsRequireRE = regexp.MustCompile(`(?m)\b(?:require|import)\s*\(\s*['"]([^'"]+)['"]\s*\)`) +) + +// jsExtensions are the extensions a specifier is probed with when it names no +// file of its own. +var jsExtensions = []string{".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"} + +// resolveJS turns one relative specifier into a file of the snapshot. An empty +// answer means the specifier names nothing here: a probe that fails is a +// missing file, not a failure of the review. +func resolveJS(fsys fs.FS, from, specifier string) string { + base := path.Clean(path.Join(path.Dir(from), specifier)) + for _, candidate := range jsCandidates(base) { + if info, err := fs.Stat(fsys, candidate); err == nil && !info.IsDir() { + return candidate + } + } + return "" +} + +// jsCandidates are the paths one specifier may name: the file itself, the file +// with each extension the language allows, and a directory's index file. +func jsCandidates(base string) []string { + candidates := make([]string, 0, 1+2*len(jsExtensions)) + candidates = append(candidates, base) + for _, extension := range jsExtensions { + candidates = append(candidates, base+extension) + } + for _, extension := range jsExtensions { + candidates = append(candidates, path.Join(base, "index"+extension)) + } + return candidates +} diff --git a/internal/domain/workflow/prreview/blast_test.go b/internal/domain/workflow/prreview/blast_test.go new file mode 100644 index 000000000..1f7d76451 --- /dev/null +++ b/internal/domain/workflow/prreview/blast_test.go @@ -0,0 +1,176 @@ +package prreview + +import ( + "testing" + "testing/fstest" +) + +// wantStrings compares a sorted string slice against an expectation, treating +// nil and empty as the same answer. +func wantStrings(t *testing.T, got, want []string) { + t.Helper() + + if len(got) != len(want) { + t.Fatalf("got %v, want %v", got, want) + } + for i := range got { + if got[i] != want[i] { + t.Errorf("[%d] = %q, want %q", i, got[i], want[i]) + } + } +} + +func TestBlastRadiusGo(t *testing.T) { + t.Parallel() + + fsys := fstest.MapFS{ + "go.mod": {Data: []byte("module example.com/m\n\ngo 1.27.0\n")}, + "internal/store/store.go": {Data: []byte("package store\n\nimport \"example.com/m/internal/db\"\n\nfunc Get() int { return db.Value() }\n")}, + "internal/db/db.go": {Data: []byte("package db\n\nfunc Value() int { return 1 }\n")}, + "internal/app/app.go": {Data: []byte("package app\n\nimport \"example.com/m/internal/store\"\n\nfunc Run() int { return store.Get() }\n")}, + "internal/store/store_test.go": {Data: []byte("package store\n\nimport \"testing\"\n\nfunc TestGet(t *testing.T) { _ = Get() }\n")}, + "internal/broken/broken.go": {Data: []byte("package broken\n\nimport (\n")}, + "docs/notes.md": {Data: []byte("# notes\n")}, + } + + tests := []struct { + name string + changed []string + want []string + }{ + { + name: "the dependent and the dependency are both reached", + changed: []string{"internal/store/store.go"}, + want: []string{"internal/app/app.go", "internal/db/db.go"}, + }, + { + name: "a changed file is never in its own blast radius", + changed: []string{"internal/store/store.go", "internal/app/app.go", "internal/db/db.go"}, + want: nil, + }, + { + name: "an unchanged file that imports nothing internal has no radius", + changed: []string{"internal/db/db.go"}, + want: []string{"internal/store/store.go"}, + }, + { + name: "a file nothing imports and that imports nothing has no radius", + changed: []string{"docs/notes.md"}, + want: nil, + }, + { + name: "a file outside the snapshot contributes nothing", + changed: []string{"internal/gone/gone.go"}, + want: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + got, err := BlastRadius(fsys, test.changed) + if err != nil { + t.Fatalf("BlastRadius() error = %v", err) + } + wantStrings(t, got, test.want) + }) + } +} + +func TestBlastRadiusPython(t *testing.T) { + t.Parallel() + + fsys := fstest.MapFS{ + "pkg/__init__.py": {Data: []byte("from pkg.util import helper\n")}, + "pkg/util.py": {Data: []byte("import os\n\n\ndef helper():\n return os.getcwd()\n")}, + "app.py": {Data: []byte("from pkg.util import helper\n\nhelper()\n")}, + "cli.py": {Data: []byte("import pkg\n")}, + "unrelated.py": {Data: []byte("from missing.module import thing\n")}, + } + + tests := []struct { + name string + changed []string + want []string + }{ + { + name: "a module import reaches the module's file", + changed: []string{"pkg/util.py"}, + want: []string{"app.py", "pkg/__init__.py"}, + }, + { + name: "a package import reaches the package, and the package's own imports", + changed: []string{"pkg/__init__.py"}, + want: []string{"cli.py", "pkg/util.py"}, + }, + { + name: "an import this snapshot cannot resolve reaches nothing", + changed: []string{"missing/module.py"}, + want: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + got, err := BlastRadius(fsys, test.changed) + if err != nil { + t.Fatalf("BlastRadius() error = %v", err) + } + wantStrings(t, got, test.want) + }) + } +} + +func TestBlastRadiusJavaScriptAndTypeScript(t *testing.T) { + t.Parallel() + + fsys := fstest.MapFS{ + "src/a.ts": {Data: []byte("export const a = 1\n")}, + "src/b.ts": {Data: []byte("import { a } from './a'\n")}, + "src/reexport.ts": {Data: []byte("export { a } from './a'\n")}, + "src/nested/c.ts": {Data: []byte("import a from '../a'\n")}, + "src/side.js": {Data: []byte("require('./a')\n")}, + "src/dyn.ts": {Data: []byte("export async function load() {\n\treturn import('./a')\n}\n")}, + "src/other.ts": {Data: []byte("import fs from 'node:fs'\nimport './missing'\n")}, + "src/lib/index.ts": {Data: []byte("export const lib = 1\n")}, + "src/uses-lib.ts": {Data: []byte("import './lib'\n")}, + "node_modules/pkg/index.ts": {Data: []byte("import '../../src/a'\n")}, + } + + tests := []struct { + name string + changed []string + want []string + }{ + { + name: "every relative import form reaches the file it names", + changed: []string{"src/a.ts"}, + want: []string{"src/b.ts", "src/dyn.ts", "src/nested/c.ts", "src/reexport.ts", "src/side.js"}, + }, + { + name: "a directory specifier resolves to its index", + changed: []string{"src/lib/index.ts"}, + want: []string{"src/uses-lib.ts"}, + }, + { + name: "a bare specifier reaches nothing", + changed: []string{"src/other.ts"}, + want: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + got, err := BlastRadius(fsys, test.changed) + if err != nil { + t.Fatalf("BlastRadius() error = %v", err) + } + wantStrings(t, got, test.want) + }) + } +} diff --git a/internal/domain/workflow/prreview/diff.go b/internal/domain/workflow/prreview/diff.go new file mode 100644 index 000000000..750be4dea --- /dev/null +++ b/internal/domain/workflow/prreview/diff.go @@ -0,0 +1,504 @@ +// Package prreview is the deterministic core of the PR review pipeline: diff +// parsing, change clustering, blast radius, evidence extraction, scoring, +// deduplication, line mapping and the review event. Everything the pipeline +// can compute is computed here, so identical findings always produce identical +// scores, order and verdicts, and the agents around them only reason. +// +// It imports the standard library only, and the workflow package imports it +// rather than the reverse: code that cannot reach the store, the forge or the +// worktree is code whose data boundary stays visible. +package prreview + +import ( + "fmt" + "path/filepath" + "regexp" + "sort" + "strconv" + "strings" +) + +// FileStatus is what a change did to one file. The values are git's own +// vocabulary for a change, which is also what a forge reports. +type FileStatus string + +const ( + FileAdded FileStatus = "added" + FileModified FileStatus = "modified" + FileRemoved FileStatus = "removed" + FileRenamed FileStatus = "renamed" +) + +// LineKind says where a line sits relative to a diff. +type LineKind string + +const ( + // LineAdded is a line the change introduces. + LineAdded LineKind = "added" + // LineContext is an unchanged line the hunk quotes. It is inside the diff, + // so a review comment can be anchored to it. + LineContext LineKind = "context" + // LineOutside is a line no hunk of the diff contains. Most of a file is + // this, and it is the difference between a comment a forge accepts and one + // it rejects. + LineOutside LineKind = "outside" +) + +// Hunk is one unified-diff hunk: the header that positions it in both files +// and the body lines that follow it. +type Hunk struct { + OldStart int + OldCount int + NewStart int + NewCount int + // Header is the "@@ ... @@" line as the diff wrote it, section heading + // included: it is what a reader needs when a comment's position is wrong. + Header string + // Lines are the body lines with their diff prefix intact (' ', '+' or + // '-'). The marker git writes for a file with no trailing newline is not a + // line and is not kept. + Lines []string +} + +// FileChange is one file of a diff: where it went, what happened to it, and +// the hunks that carry the change. +type FileChange struct { + // Path is the file's path in the change's own tree -- the path a reviewer + // opens. A deletion keeps the path the file had: a finding anchored to "" + // is a finding nobody can act on. + Path string + // PreviousPath is the pre-change path of a rename, empty otherwise. + PreviousPath string + Status FileStatus + // Language is the file's language by extension, empty when the extension + // is not one this package knows. + Language string + LinesAdded int + LinesRemoved int + Hunks []Hunk +} + +// AddedLine is one line a change adds, positioned in the file the change +// produces: the number a reviewer opens, not the diff's own offset. +type AddedLine struct { + Path string + Line int + Text string +} + +// DiffStats is the size and shape of a change, from which intake picks a +// review depth. +type DiffStats struct { + TotalFiles int + TotalAdditions int + TotalDeletions int + FilesAdded int + FilesModified int + FilesRemoved int + FilesRenamed int + TestFilesChanged int + // TestToCodeRatio is changed test files per changed non-test file. A + // change that touches no non-test file reports its own test count rather + // than dividing by zero. + TestToCodeRatio float64 +} + +// Cluster is a group of changed files that belong together. +type Cluster struct { + ID string + Name string + Files []string + PrimaryLanguage string +} + +// rootClusterName is the cluster of the files at the repository root, which +// have no directory to be named after. +const rootClusterName = "root" + +const ( + gitFilePrefix = "diff --git " + devNull = "/dev/null" +) + +// gitHeaderPaths reads both paths out of a "diff --git" line. git C-quotes a +// path that needs escaping, and a quoted line is left to the ---/+++ headers: +// those name the file in every section that has content, and this is only the +// fallback for the sections that have none (a pure rename, a mode change). +func gitHeaderPaths(line string) (oldPath, newPath string) { + rest := strings.TrimPrefix(line, gitFilePrefix) + if strings.HasPrefix(rest, `"`) { + return "", "" + } + split := strings.LastIndex(rest, " b/") + if split < 0 { + return "", "" + } + return strings.TrimPrefix(rest[:split], "a/"), rest[split+len(" b/"):] +} + +// headerPath reads the path out of a "--- "/"+++ " header: "b/main.go" in +// git's own output, quoted when the path carries something git has to escape, +// and tab-separated from a timestamp in a plain unified diff. +func headerPath(field string) string { + filePath := strings.TrimSpace(field) + if tab := strings.IndexByte(filePath, '\t'); tab >= 0 { + filePath = filePath[:tab] + } + filePath = strings.Trim(filePath, `"`) + return strings.TrimPrefix(strings.TrimPrefix(filePath, "b/"), "a/") +} + +// hunkHeaderRE reads the position a hunk header gives it in both files. Both +// counts are optional in the format, and the section heading after the second +// "@@" may contain anything, so nothing after it is matched. +var hunkHeaderRE = regexp.MustCompile(`^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@`) + +// fileInProgress is the parser's state for the file section it is inside. +type fileInProgress struct { + change FileChange + oldPath string + // hunkAt is the index of the hunk body lines are read into, or -1 outside + // a hunk. It is an index rather than a pointer because appending to the + // hunks slice moves every element. + hunkAt int +} + +// newFileInProgress starts the file section a "diff --git" line opens. The +// header's own paths seed it, so a section with no content still has a path. +func newFileInProgress(line string) *fileInProgress { + oldPath, newPath := gitHeaderPaths(line) + return &fileInProgress{ + change: FileChange{Path: newPath, Status: FileModified}, + oldPath: oldPath, + hunkAt: -1, + } +} + +// note reads one of the file's own header lines: the old and new paths, the +// /dev/null side that makes a file an addition or a deletion, and the rename +// pair. Anything else the section says -- index, mode, similarity -- says +// nothing about where the change is, which is all findings are positioned by. +func (f *fileInProgress) note(line string) { + switch { + case strings.HasPrefix(line, "--- "): + f.oldPath = headerPath(strings.TrimPrefix(line, "--- ")) + if f.oldPath == devNull { + f.change.Status = FileAdded + f.oldPath = "" + } + case strings.HasPrefix(line, "+++ "): + target := headerPath(strings.TrimPrefix(line, "+++ ")) + if target == devNull { + f.change.Status = FileRemoved + return + } + f.change.Path = target + case strings.HasPrefix(line, "rename from "): + f.oldPath = strings.TrimPrefix(line, "rename from ") + f.change.Status = FileRenamed + case strings.HasPrefix(line, "rename to "): + f.change.Path = strings.TrimPrefix(line, "rename to ") + f.change.Status = FileRenamed + } +} + +// startHunk opens the hunk a header line describes. A header this package +// cannot position lines by starts no hunk, so the body after it is read as +// nothing rather than anchored to a guessed position. +func (f *fileInProgress) startHunk(line string) { + match := hunkHeaderRE.FindStringSubmatch(line) + if match == nil { + f.hunkAt = -1 + return + } + f.change.Hunks = append(f.change.Hunks, Hunk{ + OldStart: headerNumber(match[1]), + OldCount: headerCount(match[2]), + NewStart: headerNumber(match[3]), + NewCount: headerCount(match[4]), + Header: line, + }) + f.hunkAt = len(f.change.Hunks) - 1 +} + +// body folds one line of hunk body into the change: its count and the raw +// line. Outside a hunk the same bytes are a file header, so the caller decides +// which of the two it is; this is only ever called inside one. +func (f *fileInProgress) body(line string) { + switch { + case line == "": + // The empty element a trailing newline leaves behind: every real line + // of a hunk carries its prefix, so an unprefixed empty line is the end + // of the text, not a line. + case strings.HasPrefix(line, `\`): + // git's "\ No newline at end of file" marker describes the line before + // it and holds no position of its own. + case strings.HasPrefix(line, "+"): + f.change.LinesAdded++ + f.hunk().Lines = append(f.hunk().Lines, line) + case strings.HasPrefix(line, "-"): + f.change.LinesRemoved++ + f.hunk().Lines = append(f.hunk().Lines, line) + default: + // A context line advances the position without being the change. + f.hunk().Lines = append(f.hunk().Lines, line) + } +} + +// hunk is the hunk body() is reading into. Every caller has checked hunkAt. +func (f *fileInProgress) hunk() *Hunk { return &f.change.Hunks[f.hunkAt] } + +// close finishes the section. A section that never named a new path keeps the +// old one, so a deletion is still anchored to the file it deleted; the old +// path survives as PreviousPath only when the change moved the file. +func (f *fileInProgress) close() FileChange { + change := f.change + if change.Path == "" { + change.Path = f.oldPath + } + if f.oldPath != "" && f.oldPath != change.Path { + change.PreviousPath = f.oldPath + } + change.Language = language(change.Path) + return change +} + +// ParseDiff parses git's unified diff into per-file changes: the file sections +// "diff --git" opens, their rename and /dev/null headers, and the hunks that +// carry the change. It is the one parser: every position, count and path this +// package reports is read here. +func ParseDiff(diff string) []FileChange { + var ( + files []FileChange + file *fileInProgress + ) + for line := range strings.SplitSeq(diff, "\n") { + switch { + case strings.HasPrefix(line, gitFilePrefix): + if file != nil { + files = append(files, file.close()) + } + file = newFileInProgress(line) + case file == nil: + // Text before the first "diff --git" belongs to no file, so there + // is nothing it could position a finding against. + case strings.HasPrefix(line, "@@"): + file.startHunk(line) + case file.hunkAt < 0: + file.note(line) + default: + file.body(line) + } + } + if file != nil { + files = append(files, file.close()) + } + return files +} + +// headerNumber reads a start line out of a hunk header. The regular expression +// that matched it guarantees digits. +func headerNumber(digits string) int { + number, _ := strconv.Atoi(digits) + return number +} + +// headerCount reads a line count out of a hunk header, defaulting to the one +// line the format implies when the count is omitted. +func headerCount(digits string) int { + if digits == "" { + return 1 + } + return headerNumber(digits) +} + +// position is one line of a hunk placed in the file the change produces. +type position struct { + Line int + Kind LineKind + Text string +} + +// positions walks a hunk's body, numbering the lines that exist in the file +// the change produces. A removed line is not in that file, so it holds no +// position here: its coordinates belong to the old file. +func (h Hunk) positions() []position { + positions := make([]position, 0, len(h.Lines)) + line := h.NewStart + for _, raw := range h.Lines { + switch { + case strings.HasPrefix(raw, "-"): + // Removed: no position in the file the change produces. + case strings.HasPrefix(raw, "+"): + positions = append(positions, position{Line: line, Kind: LineAdded, Text: strings.TrimPrefix(raw, "+")}) + line++ + default: + positions = append(positions, position{Line: line, Kind: LineContext, Text: strings.TrimPrefix(raw, " ")}) + line++ + } + } + return positions +} + +// AddedLines returns every line the change adds, in file and hunk order. Only +// added lines: a removed line has no position in the file the change produces, +// and a context line is not something the change did. +func AddedLines(files []FileChange) []AddedLine { + var added []AddedLine + for _, file := range files { + for _, hunk := range file.Hunks { + for _, placed := range hunk.positions() { + if placed.Kind != LineAdded { + continue + } + added = append(added, AddedLine{Path: file.Path, Line: placed.Line, Text: placed.Text}) + } + } + } + return added +} + +// MapLine maps a line of the file a change produces onto the diff: a line the +// change added, a line a hunk quotes as context, or a line no hunk contains. +func MapLine(files []FileChange, path string, line int) LineKind { + for _, file := range files { + if file.Path != path { + continue + } + for _, hunk := range file.Hunks { + for _, placed := range hunk.positions() { + if placed.Line == line { + return placed.Kind + } + } + } + } + return LineOutside +} + +// SummarizeFiles computes the aggregate statistics of a parsed change. +func SummarizeFiles(files []FileChange) DiffStats { + stats := DiffStats{TotalFiles: len(files)} + for _, file := range files { + stats.TotalAdditions += file.LinesAdded + stats.TotalDeletions += file.LinesRemoved + switch file.Status { + case FileAdded: + stats.FilesAdded++ + case FileRemoved: + stats.FilesRemoved++ + case FileRenamed: + stats.FilesRenamed++ + default: + stats.FilesModified++ + } + if isTestFile(file.Path) { + stats.TestFilesChanged++ + } + } + + codeFiles := stats.TotalFiles - stats.TestFilesChanged + if codeFiles < 1 { + codeFiles = 1 + } + stats.TestToCodeRatio = float64(stats.TestFilesChanged) / float64(codeFiles) + return stats +} + +// ClusterFiles groups changed files by the directory they live in, the +// cheapest grouping that puts a Go package, a Python module directory or a +// feature folder in one cluster. Files at the repository root form one cluster. +func ClusterFiles(files []FileChange) []Cluster { + byDirectory := map[string][]FileChange{} + directories := make([]string, 0, len(files)) + for _, file := range files { + directory := "" + if slash := strings.LastIndexByte(file.Path, '/'); slash >= 0 { + directory = file.Path[:slash] + } + if directory == "" { + directory = rootClusterName + } + if _, seen := byDirectory[directory]; !seen { + directories = append(directories, directory) + } + byDirectory[directory] = append(byDirectory[directory], file) + } + sort.Strings(directories) + + clusters := make([]Cluster, 0, len(directories)) + for index, directory := range directories { + clusters = append(clusters, newCluster(index, directory, byDirectory[directory])) + } + return clusters +} + +// newCluster names one directory's group and gives it the language most of its +// files are written in. Ties go to the earlier file, so the same change always +// reports the same primary language. +func newCluster(index int, directory string, files []FileChange) Cluster { + cluster := Cluster{ID: fmt.Sprintf("cluster_%d", index), Name: directory} + counts := map[string]int{} + for _, file := range files { + cluster.Files = append(cluster.Files, file.Path) + if file.Language == "" { + continue + } + counts[file.Language]++ + if counts[file.Language] > counts[cluster.PrimaryLanguage] { + cluster.PrimaryLanguage = file.Language + } + } + return cluster +} + +// testPathMarkers are the path shapes read as a test file. It is a path test, +// not a content test: a file under tests/ is the change's own evidence that +// the change is a test change, whatever it contains. +var testPathMarkers = []string{"test_", "_test.", ".test.", "tests/", "test/", "__tests__/", "spec/"} + +// isTestFile reports whether a path names a test file. +func isTestFile(filePath string) bool { + lowered := strings.ToLower(filePath) + for _, marker := range testPathMarkers { + if strings.Contains(lowered, marker) { + return true + } + } + return false +} + +// language names the language of a changed file by its extension, empty when +// the extension is not one this package knows. The name is the word a reader +// sees in the change's anatomy, not the extension. +func language(filePath string) string { + return languages[strings.ToLower(filepath.Ext(filePath))] +} + +// languages are the extensions this package names a language for. +var languages = map[string]string{ + ".py": "python", + ".js": "javascript", + ".jsx": "javascript", + ".ts": "typescript", + ".tsx": "typescript", + ".go": "go", + ".rs": "rust", + ".java": "java", + ".rb": "ruby", + ".cpp": "cpp", + ".c": "c", + ".cs": "csharp", + ".swift": "swift", + ".kt": "kotlin", + ".scala": "scala", + ".php": "php", + ".sh": "bash", + ".yaml": "yaml", + ".yml": "yaml", + ".json": "json", + ".md": "markdown", + ".sql": "sql", + ".html": "html", + ".css": "css", +} diff --git a/internal/domain/workflow/prreview/diff_test.go b/internal/domain/workflow/prreview/diff_test.go new file mode 100644 index 000000000..b4e429765 --- /dev/null +++ b/internal/domain/workflow/prreview/diff_test.go @@ -0,0 +1,348 @@ +package prreview + +import ( + "reflect" + "testing" +) + +// fileSummary is the part of a parsed FileChange these tests assert on: a +// file's identity and its geometry, without the hunk bodies other tests carry. +type fileSummary struct { + Path string + PreviousPath string + Status string + Language string + Added int + Removed int + Hunks int +} + +func summarize(files []FileChange) []fileSummary { + summaries := make([]fileSummary, 0, len(files)) + for _, file := range files { + summaries = append(summaries, fileSummary{ + Path: file.Path, + PreviousPath: file.PreviousPath, + Status: string(file.Status), + Language: file.Language, + Added: file.LinesAdded, + Removed: file.LinesRemoved, + Hunks: len(file.Hunks), + }) + } + return summaries +} + +func TestParseDiffDescribesEachFile(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + diff string + want []fileSummary + }{ + { + name: "a modified file", + diff: "diff --git a/internal/a.go b/internal/a.go\nindex 111..222 100644\n--- a/internal/a.go\n+++ b/internal/a.go\n@@ -1,2 +1,3 @@\n package a\n+var b = 1\n var c = 2\n", + want: []fileSummary{{Path: "internal/a.go", Status: "modified", Language: "go", Added: 1, Hunks: 1}}, + }, + { + name: "an added file", + diff: "diff --git a/pkg/new.py b/pkg/new.py\nnew file mode 100644\n--- /dev/null\n+++ b/pkg/new.py\n@@ -0,0 +1,2 @@\n+import os\n+x = 1\n", + want: []fileSummary{{Path: "pkg/new.py", Status: "added", Language: "python", Added: 2, Hunks: 1}}, + }, + { + name: "a deleted file keeps the path it had", + diff: "diff --git a/pkg/gone.py b/pkg/gone.py\ndeleted file mode 100644\n--- a/pkg/gone.py\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-import os\n-x = 1\n", + want: []fileSummary{{Path: "pkg/gone.py", Status: "removed", Language: "python", Removed: 2, Hunks: 1}}, + }, + { + name: "a rename with changes", + diff: "diff --git a/old.go b/new.go\nsimilarity index 90%\nrename from old.go\nrename to new.go\n--- a/old.go\n+++ b/new.go\n@@ -1,1 +1,2 @@\n package a\n+var b = 1\n", + want: []fileSummary{{Path: "new.go", PreviousPath: "old.go", Status: "renamed", Language: "go", Added: 1, Hunks: 1}}, + }, + { + name: "a rename with no content at all", + diff: "diff --git a/old.go b/new.go\nsimilarity index 100%\nrename from old.go\nrename to new.go\n", + want: []fileSummary{{Path: "new.go", PreviousPath: "old.go", Status: "renamed", Language: "go"}}, + }, + { + name: "a mode-only change still names its file", + diff: "diff --git a/tool.sh b/tool.sh\nold mode 100644\nnew mode 100755\n", + want: []fileSummary{{Path: "tool.sh", Status: "modified", Language: "bash"}}, + }, + { + name: "a multi-hunk file", + diff: "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ -1,1 +1,2 @@\n package a\n+var b = 1\n@@ -40,1 +41,2 @@\n func f() {}\n+func g() {}\n", + want: []fileSummary{{Path: "a.go", Status: "modified", Language: "go", Added: 2, Hunks: 2}}, + }, + { + name: "two files in one diff", + diff: "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ -1,1 +1,2 @@\n package a\n+var b = 1\ndiff --git a/docs/x.md b/docs/x.md\n--- a/docs/x.md\n+++ b/docs/x.md\n@@ -1,1 +1,2 @@\n # x\n+text\n", + want: []fileSummary{ + {Path: "a.go", Status: "modified", Language: "go", Added: 1, Hunks: 1}, + {Path: "docs/x.md", Status: "modified", Language: "markdown", Added: 1, Hunks: 1}, + }, + }, + { + name: "a diff of nothing", + diff: "", + want: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + got := summarize(ParseDiff(test.diff)) + if len(got) != len(test.want) { + t.Fatalf("ParseDiff() described %+v, want %+v", got, test.want) + } + for i := range got { + if got[i] != test.want[i] { + t.Errorf("file %d = %+v, want %+v", i, got[i], test.want[i]) + } + } + }) + } +} + +func TestParseDiffHunkGeometry(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + diff string + want []Hunk + }{ + { + name: "the header positions the hunk in both files", + diff: "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ -1,2 +3,4 @@ func f() {\n package a\n+var b = 1\n", + want: []Hunk{{OldStart: 1, OldCount: 2, NewStart: 3, NewCount: 4, Header: "@@ -1,2 +3,4 @@ func f() {", Lines: []string{" package a", "+var b = 1"}}}, + }, + { + name: "an omitted count is one line", + diff: "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ -5 +6 @@\n package a\n", + want: []Hunk{{OldStart: 5, OldCount: 1, NewStart: 6, NewCount: 1, Header: "@@ -5 +6 @@", Lines: []string{" package a"}}}, + }, + { + name: "an added line that looks like a file header is body text", + diff: "diff --git a/a.md b/a.md\n--- a/a.md\n+++ b/a.md\n@@ -1,1 +1,2 @@\n # a\n+++ title\n", + want: []Hunk{{OldStart: 1, OldCount: 1, NewStart: 1, NewCount: 2, Header: "@@ -1,1 +1,2 @@", Lines: []string{" # a", "+++ title"}}}, + }, + { + name: "the no-newline marker is not a line", + diff: "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ -1,1 +1,1 @@\n-package a\n+package b\n\\ No newline at end of file\n", + want: []Hunk{{OldStart: 1, OldCount: 1, NewStart: 1, NewCount: 1, Header: "@@ -1,1 +1,1 @@", Lines: []string{"-package a", "+package b"}}}, + }, + { + name: "a header that cannot be positioned starts no hunk", + diff: "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ not a hunk header @@\n+orphan\n", + want: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + files := ParseDiff(test.diff) + var got []Hunk + if len(files) > 0 { + got = files[0].Hunks + } + if !reflect.DeepEqual(got, test.want) { + t.Errorf("hunks = %+v, want %+v", got, test.want) + } + }) + } +} + +func TestAddedLinesPositionsTheChange(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + diff string + want []AddedLine + }{ + { + name: "context lines advance the position without being additions", + diff: "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ -1,2 +1,3 @@\n package a\n+var b = 1\n var c = 2\n+var d = 3\n", + want: []AddedLine{{Path: "a.go", Line: 2, Text: "var b = 1"}, {Path: "a.go", Line: 4, Text: "var d = 3"}}, + }, + { + name: "a removed line holds no position in the file the change produces", + diff: "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ -1,3 +1,2 @@\n package a\n-var gone = 1\n+var kept = 2\n var tail = 3\n", + want: []AddedLine{{Path: "a.go", Line: 2, Text: "var kept = 2"}}, + }, + { + name: "a second hunk numbers from its own header", + diff: "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ -1,1 +1,2 @@\n package a\n+var b = 1\n@@ -40,1 +41,2 @@\n func f() {}\n+func g() {}\n", + want: []AddedLine{{Path: "a.go", Line: 2, Text: "var b = 1"}, {Path: "a.go", Line: 42, Text: "func g() {}"}}, + }, + { + name: "an added line that looks like a file header is body text", + diff: "diff --git a/a.md b/a.md\n--- a/a.md\n+++ b/a.md\n@@ -1,1 +1,2 @@\n # a\n+++ title\n", + want: []AddedLine{{Path: "a.md", Line: 2, Text: "++ title"}}, + }, + { + name: "an added file numbers from line one", + diff: "diff --git a/new.go b/new.go\nnew file mode 100644\n--- /dev/null\n+++ b/new.go\n@@ -0,0 +1,2 @@\n+package new\n+var a = 1\n", + want: []AddedLine{{Path: "new.go", Line: 1, Text: "package new"}, {Path: "new.go", Line: 2, Text: "var a = 1"}}, + }, + { + name: "a deletion and a pure rename add nothing", + diff: "diff --git a/gone.go b/gone.go\n--- a/gone.go\n+++ /dev/null\n@@ -1,1 +0,0 @@\n-package gone\ndiff --git a/old.go b/new.go\nsimilarity index 100%\nrename from old.go\nrename to new.go\n", + want: nil, + }, + { + name: "the no-newline marker adds nothing", + diff: "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ -1,1 +1,2 @@\n package a\n+var b = 1\n\\ No newline at end of file\n", + want: []AddedLine{{Path: "a.go", Line: 2, Text: "var b = 1"}}, + }, + { + name: "an empty diff adds nothing", + diff: "", + want: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + got := AddedLines(ParseDiff(test.diff)) + if len(got) != len(test.want) { + t.Fatalf("AddedLines() = %+v, want %+v", got, test.want) + } + for i := range got { + if got[i] != test.want[i] { + t.Errorf("AddedLines()[%d] = %+v, want %+v", i, got[i], test.want[i]) + } + } + }) + } +} + +func TestMapLinePlacesACommentOnTheDiff(t *testing.T) { + t.Parallel() + + // The change produces four lines: 1 context, 2 added, 3 context, 4 added. + const diff = "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ -1,3 +1,4 @@\n package a\n-var gone = 1\n+var kept = 2\n var tail = 3\n+var extra = 4\n" + + tests := []struct { + name string + path string + line int + want LineKind + }{ + {name: "an added line", path: "a.go", line: 2, want: LineAdded}, + {name: "a context line inside the hunk", path: "a.go", line: 3, want: LineContext}, + {name: "the last added line", path: "a.go", line: 4, want: LineAdded}, + {name: "the line after the hunk", path: "a.go", line: 5, want: LineOutside}, + {name: "a line the hunk never quoted", path: "a.go", line: 400, want: LineOutside}, + {name: "a file the diff does not touch", path: "b.go", line: 1, want: LineOutside}, + } + + files := ParseDiff(diff) + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + if got := MapLine(files, test.path, test.line); got != test.want { + t.Errorf("MapLine(%q, %d) = %q, want %q", test.path, test.line, got, test.want) + } + }) + } + + t.Run("a deleted file has no line in the file the change produces", func(t *testing.T) { + t.Parallel() + + deleted := ParseDiff("diff --git a/gone.go b/gone.go\n--- a/gone.go\n+++ /dev/null\n@@ -1,1 +0,0 @@\n-package gone\n") + if got := MapLine(deleted, "gone.go", 1); got != LineOutside { + t.Errorf("MapLine(gone.go, 1) = %q, want %q", got, LineOutside) + } + }) +} + +func TestSummarizeFilesCountsTheChange(t *testing.T) { + t.Parallel() + + const diff = "diff --git a/pkg/a.go b/pkg/a.go\n--- a/pkg/a.go\n+++ b/pkg/a.go\n@@ -1,1 +1,3 @@\n package a\n+var b = 1\n+var c = 2\ndiff --git a/pkg/a_test.go b/pkg/a_test.go\n--- a/pkg/a_test.go\n+++ b/pkg/a_test.go\n@@ -1,1 +1,1 @@\n-package a\n+package a\n" + + "diff --git a/docs/new.md b/docs/new.md\n--- /dev/null\n+++ b/docs/new.md\n@@ -0,0 +1,1 @@\n+docs\ndiff --git a/old.go b/old.go\n--- a/old.go\n+++ /dev/null\n@@ -1,1 +0,0 @@\n-package old\ndiff --git a/from.go b/to.go\nrename from from.go\nrename to to.go\n" + + tests := []struct { + name string + diff string + want DiffStats + }{ + { + name: "every status is counted and the test ratio is per code file", + diff: diff, + want: DiffStats{ + TotalFiles: 5, TotalAdditions: 4, TotalDeletions: 2, + FilesAdded: 1, FilesModified: 2, FilesRemoved: 1, FilesRenamed: 1, + TestFilesChanged: 1, TestToCodeRatio: 0.25, + }, + }, + { + name: "a change with no test files reports a zero ratio", + diff: "diff --git a/a.go b/a.go\n--- a/a.go\n+++ b/a.go\n@@ -1,1 +1,2 @@\n package a\n+var b = 1\n", + want: DiffStats{TotalFiles: 1, TotalAdditions: 1, FilesModified: 1}, + }, + { + name: "a change of tests alone does not divide by zero", + diff: "diff --git a/a_test.go b/a_test.go\n--- a/a_test.go\n+++ b/a_test.go\n@@ -1,1 +1,2 @@\n package a\n+var b = 1\n", + want: DiffStats{TotalFiles: 1, TotalAdditions: 1, FilesModified: 1, TestFilesChanged: 1, TestToCodeRatio: 1}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + if got := SummarizeFiles(ParseDiff(test.diff)); got != test.want { + t.Errorf("SummarizeFiles() = %+v, want %+v", got, test.want) + } + }) + } +} + +func TestClusterFilesGroupsByDirectory(t *testing.T) { + t.Parallel() + + files := ParseDiff("diff --git a/main.go b/main.go\n--- a/main.go\n+++ b/main.go\n@@ -1,1 +1,2 @@\n package main\n+var a = 1\n" + + "diff --git a/docs/a.md b/docs/a.md\n--- a/docs/a.md\n+++ b/docs/a.md\n@@ -1,1 +1,2 @@\n # a\n+x\n" + + "diff --git a/docs/b.md b/docs/b.md\n--- a/docs/b.md\n+++ b/docs/b.md\n@@ -1,1 +1,2 @@\n # b\n+x\n" + + "diff --git a/internal/a.go b/internal/a.go\n--- a/internal/a.go\n+++ b/internal/a.go\n@@ -1,1 +1,2 @@\n package internal\n+var a = 1\n" + + "diff --git a/internal/README.md b/internal/README.md\n--- a/internal/README.md\n+++ b/internal/README.md\n@@ -1,1 +1,2 @@\n # a\n+x\n") + + want := []Cluster{ + {ID: "cluster_0", Name: "docs", Files: []string{"docs/a.md", "docs/b.md"}, PrimaryLanguage: "markdown"}, + {ID: "cluster_1", Name: "internal", Files: []string{"internal/a.go", "internal/README.md"}, PrimaryLanguage: "go"}, + {ID: "cluster_2", Name: "root", Files: []string{"main.go"}, PrimaryLanguage: "go"}, + } + + if got := ClusterFiles(files); !reflect.DeepEqual(got, want) { + t.Errorf("ClusterFiles() = %+v, want %+v", got, want) + } +} + +func TestClusterFilesBreaksLanguageTiesByFirstAppearance(t *testing.T) { + t.Parallel() + + files := ParseDiff("diff --git a/pkg/a.md b/pkg/a.md\n--- a/pkg/a.md\n+++ b/pkg/a.md\n@@ -1,1 +1,2 @@\n # a\n+x\n" + + "diff --git a/pkg/b.go b/pkg/b.go\n--- a/pkg/b.go\n+++ b/pkg/b.go\n@@ -1,1 +1,2 @@\n package pkg\n+var a = 1\n") + + got := ClusterFiles(files) + if len(got) != 1 { + t.Fatalf("ClusterFiles() = %+v, want one cluster", got) + } + // One Markdown file and one Go file: the first file of the directory + // decides, so the same diff always names the same language. + if got[0].PrimaryLanguage != "markdown" { + t.Errorf("PrimaryLanguage = %q, want %q", got[0].PrimaryLanguage, "markdown") + } +} diff --git a/internal/domain/workflow/prreview/evidence.go b/internal/domain/workflow/prreview/evidence.go new file mode 100644 index 000000000..751f6a991 --- /dev/null +++ b/internal/domain/workflow/prreview/evidence.go @@ -0,0 +1,202 @@ +package prreview + +import ( + "io/fs" + "regexp" + "strconv" + "strings" +) + +// EvidenceRequest is the finding an evidence package is extracted for: where it +// points and what it says. +type EvidenceRequest struct { + File string + LineStart int + LineEnd int + Title string + Body string + Evidence string +} + +// EvidencePackage is the ground truth a verifier checks a finding against: the +// code at the lines the finding cites, and the call sites of the identifiers it +// names. It is quoted code rather than a summary, because a verifier handed a +// summary is checking the reviewer's prose against itself. +type EvidencePackage struct { + FindingTitle string + File string + LineStart int + LineEnd int + // PrimaryCode is the cited lines and their context, each prefixed with its + // line number in the file. + PrimaryCode string + // CallerSnippets are call sites elsewhere in the snapshot, each headed by + // "path:line" and followed by the numbered lines around the call. + CallerSnippets []string +} + +const ( + // evidenceContextLines is how many lines each side of the cited range + // PrimaryCode quotes. + evidenceContextLines = 30 + // callerContextLines is the same for a call site: enough to read the call in + // its function, not the whole function. + callerContextLines = 5 + // maxCallerSnippets bounds what one finding quotes from its call sites. + maxCallerSnippets = 10 + // maxIdentifiers bounds how many names one finding's text sends the search + // after. + maxIdentifiers = 8 +) + +// ExtractEvidence quotes the code a finding cites and the call sites of the +// identifiers it names, both bounded: the evidence is what a verifier reads +// instead of the finding's prose, so it has to be small enough to read. +func ExtractEvidence(fsys fs.FS, req EvidenceRequest) (EvidencePackage, error) { + pkg := EvidencePackage{ + FindingTitle: req.Title, + File: req.File, + LineStart: req.LineStart, + LineEnd: req.LineEnd, + } + + lines, err := readLines(fsys, req.File) + if err != nil { + return EvidencePackage{}, err + } + pkg.PrimaryCode = quoteLines(lines, req.LineStart, req.LineEnd, evidenceContextLines) + + identifiers := mentionedIdentifiers(req.Title + "\n" + req.Body + "\n" + req.Evidence) + if len(identifiers) == 0 { + return pkg, nil + } + paths, err := walkPaths(fsys, isQuotablePath) + if err != nil { + return EvidencePackage{}, err + } + for _, identifier := range identifiers { + callers, err := callerSnippets(fsys, paths, identifier, req.File, maxCallerSnippets-len(pkg.CallerSnippets)) + if err != nil { + return EvidencePackage{}, err + } + pkg.CallerSnippets = appendNew(pkg.CallerSnippets, callers...) + if len(pkg.CallerSnippets) >= maxCallerSnippets { + break + } + } + return pkg, nil +} + +// quoteLines quotes a range of a file and its context, numbered as the file has +// it. Nothing is quoted for a file that is not there, which is a fact about the +// finding rather than a failure of the review. +func quoteLines(lines []string, lineStart, lineEnd, context int) string { + if len(lines) == 0 { + return "" + } + start := max(1, lineStart-context) + end := min(len(lines), max(lineStart, lineEnd)+context) + if start > end { + return "" + } + + var builder strings.Builder + for line := start; line <= end; line++ { + builder.WriteString(strconv.Itoa(line)) + builder.WriteString(": ") + builder.WriteString(lines[line-1]) + builder.WriteString("\n") + } + return strings.TrimSuffix(builder.String(), "\n") +} + +// callerSnippets quotes the call sites of one identifier: one snippet per +// matching line, in lexical file order, skipping the file the finding cites, +// because the finding's own line is not evidence of who calls it. budget bounds +// how many call sites are quoted in total. +func callerSnippets(fsys fs.FS, paths []string, identifier, excludeFile string, budget int) ([]string, error) { + if budget <= 0 { + return nil, nil + } + call := regexp.MustCompile(`\b` + regexp.QuoteMeta(identifier) + `\s*\(`) + + var snippets []string + for _, file := range paths { + if file == excludeFile { + continue + } + lines, err := readLines(fsys, file) + if err != nil { + return nil, err + } + for index, line := range lines { + if !call.MatchString(line) { + continue + } + snippet := file + ":" + strconv.Itoa(index+1) + "\n" + quoteLines(lines, index+1, index+1, callerContextLines) + snippets = append(snippets, snippet) + if len(snippets) >= budget { + return snippets, nil + } + } + } + return snippets, nil +} + +// appendNew appends every value not already in values, keeping the order both +// sides were built in. +func appendNew(values []string, extra ...string) []string { + for _, value := range extra { + values = appendUnique(values, value) + } + return values +} + +var ( + // backtickedRE reads the names a finding marked as code. + backtickedRE = regexp.MustCompile("`([A-Za-z_][A-Za-z0-9_]*)`") + // exportedRE reads the names a finding spelled the way an exported + // identifier is spelled. + exportedRE = regexp.MustCompile(`\b([A-Z][a-zA-Z0-9]{2,})\b`) + // calledRE reads the names a finding wrote as a call. + calledRE = regexp.MustCompile(`\b([a-z_][a-z0-9_]{2,})\s*\(`) + + // identifierRE is the shape a name must have to be searched for as one. + identifierRE = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`) +) + +// commonIdentifierWords are the words findings use to talk about code rather +// than to name it. Searching the snapshot for them finds prose. +var commonIdentifierWords = map[string]bool{ + "the": true, "this": true, "that": true, "with": true, "from": true, + "when": true, "where": true, "which": true, "there": true, "their": true, + "returns": true, "return": true, "found": true, "check": true, "line": true, + "file": true, "code": true, "issue": true, "error": true, "value": true, + "values": true, "class": true, "function": true, "method": true, + "should": true, "could": true, "would": true, "into": true, "over": true, + "under": true, "each": true, "name": true, "data": true, "test": true, + "tests": true, +} + +// mentionedIdentifiers are the names a finding's text points at, in the order +// it mentions them, capped up front: each name costs a pass over the snapshot, +// and a body that names everything names nothing worth searching. +func mentionedIdentifiers(text string) []string { + var identifiers []string + for _, pattern := range []*regexp.Regexp{backtickedRE, exportedRE, calledRE} { + for _, match := range pattern.FindAllStringSubmatch(text, -1) { + name := strings.Trim(match[1], "` ") + if len(name) < 3 || commonIdentifierWords[strings.ToLower(name)] || !identifierRE.MatchString(name) { + continue + } + identifiers = appendUnique(identifiers, name) + } + if len(identifiers) >= maxIdentifiers { + break + } + } + if len(identifiers) > maxIdentifiers { + identifiers = identifiers[:maxIdentifiers] + } + return identifiers +} diff --git a/internal/domain/workflow/prreview/evidence_test.go b/internal/domain/workflow/prreview/evidence_test.go new file mode 100644 index 000000000..ca3b6ab5f --- /dev/null +++ b/internal/domain/workflow/prreview/evidence_test.go @@ -0,0 +1,163 @@ +package prreview + +import ( + "fmt" + "strings" + "testing" + "testing/fstest" +) + +func TestExtractEvidenceQuotesTheCitedLines(t *testing.T) { + t.Parallel() + + var builder strings.Builder + for line := 1; line <= 200; line++ { + fmt.Fprintf(&builder, "line %d\n", line) + } + fsys := fstest.MapFS{ + "src/big.go": {Data: []byte(builder.String())}, + "src/small.go": {Data: []byte("package small\n")}, + "src/logo.png": {Data: []byte("\x89PNG\r\n\x1a\nbinary")}, + } + + t.Run("a cited line is quoted with its context", func(t *testing.T) { + t.Parallel() + + pkg, err := ExtractEvidence(fsys, EvidenceRequest{File: "src/big.go", LineStart: 100, LineEnd: 100, Title: "t"}) + if err != nil { + t.Fatalf("ExtractEvidence() error = %v", err) + } + for _, want := range []string{"100: line 100", "70: line 70", "130: line 130"} { + if !strings.Contains(pkg.PrimaryCode, want) { + t.Errorf("PrimaryCode lacks %q:\n%s", want, pkg.PrimaryCode) + } + } + for _, unwanted := range []string{"69: line 69", "131: line 131"} { + if strings.Contains(pkg.PrimaryCode, unwanted) { + t.Errorf("PrimaryCode quotes beyond the context window: %q\n%s", unwanted, pkg.PrimaryCode) + } + } + }) + + t.Run("a cited range is quoted whole", func(t *testing.T) { + t.Parallel() + + pkg, err := ExtractEvidence(fsys, EvidenceRequest{File: "src/big.go", LineStart: 100, LineEnd: 102, Title: "t"}) + if err != nil { + t.Fatalf("ExtractEvidence() error = %v", err) + } + if !strings.Contains(pkg.PrimaryCode, "102: line 102") || !strings.Contains(pkg.PrimaryCode, "132: line 132") { + t.Errorf("PrimaryCode does not quote the cited range:\n%s", pkg.PrimaryCode) + } + }) + + t.Run("a file the snapshot does not have is quoted as nothing", func(t *testing.T) { + t.Parallel() + + pkg, err := ExtractEvidence(fsys, EvidenceRequest{File: "src/gone.go", LineStart: 1, LineEnd: 1, Title: "t"}) + if err != nil { + t.Fatalf("ExtractEvidence() error = %v", err) + } + if pkg.PrimaryCode != "" { + t.Errorf("PrimaryCode = %q, want no code for a file that is not there", pkg.PrimaryCode) + } + }) + + t.Run("a binary file is quoted as nothing", func(t *testing.T) { + t.Parallel() + + pkg, err := ExtractEvidence(fsys, EvidenceRequest{File: "src/logo.png", LineStart: 1, LineEnd: 1, Title: "t"}) + if err != nil { + t.Fatalf("ExtractEvidence() error = %v", err) + } + if pkg.PrimaryCode != "" { + t.Errorf("PrimaryCode = %q, want no code for a binary file", pkg.PrimaryCode) + } + }) +} + +func TestExtractEvidenceQuotesCallers(t *testing.T) { + t.Parallel() + + const target = "pkg/target.go" + + fsys := fstest.MapFS{ + target: {Data: []byte("package pkg\n\nfunc Target() {}\n")}, + "pkg/caller.go": {Data: []byte("package pkg\n\nfunc Caller() {\n\tTarget()\n}\n")}, + "pkg/notes.md": {Data: []byte("Target() is called from Caller\n")}, + } + + pkg, err := ExtractEvidence(fsys, EvidenceRequest{ + File: target, + LineStart: 3, + LineEnd: 3, + Title: "Target ignores its error", + Body: "The call `Target()` at line 4 drops the returned error, and Target( is called from elsewhere", + Evidence: "Target()", + }) + if err != nil { + t.Fatalf("ExtractEvidence() error = %v", err) + } + + wantStrings(t, pkg.CallerSnippets, []string{ + "pkg/caller.go:4\n1: package pkg\n2: \n3: func Caller() {\n4: \tTarget()\n5: }", + "pkg/notes.md:1\n1: Target() is called from Caller", + }) +} + +func TestExtractEvidenceBoundsItsSearch(t *testing.T) { + t.Parallel() + + var body strings.Builder + for index := 1; index <= 10; index++ { + fmt.Fprintf(&body, "`ident_number_%d` ", index) + } + + fsys := fstest.MapFS{ + "pkg/eight.go": {Data: []byte("package pkg\n\nfunc eight() {\n\tident_number_8()\n}\n")}, + "pkg/nine.go": {Data: []byte("package pkg\n\nfunc nine() {\n\tident_number_9()\n}\n")}, + } + + pkg, err := ExtractEvidence(fsys, EvidenceRequest{File: "pkg/target.go", LineStart: 1, LineEnd: 1, Title: "t", Body: body.String()}) + if err != nil { + t.Fatalf("ExtractEvidence() error = %v", err) + } + + // Ten identifiers are named; the search stops at the eighth, so only the + // eighth's call site is quoted. + wantStrings(t, pkg.CallerSnippets, []string{"pkg/eight.go:4\n1: package pkg\n2: \n3: func eight() {\n4: \tident_number_8()\n5: }"}) +} + +func TestExtractEvidenceCapsCallerSnippets(t *testing.T) { + t.Parallel() + + var calls strings.Builder + calls.WriteString("package pkg\n\nfunc many() {\n") + for range 12 { + calls.WriteString("\tTarget()\n") + } + calls.WriteString("}\n") + + fsys := fstest.MapFS{ + "pkg/target.go": {Data: []byte("package pkg\n\nfunc Target() {}\n")}, + "pkg/many.go": {Data: []byte(calls.String())}, + } + + pkg, err := ExtractEvidence(fsys, EvidenceRequest{File: "pkg/target.go", LineStart: 3, LineEnd: 3, Title: "t", Body: "Target()"}) + if err != nil { + t.Fatalf("ExtractEvidence() error = %v", err) + } + + if len(pkg.CallerSnippets) != 10 { + t.Fatalf("quoted %d caller snippets, want 10:\n%s", len(pkg.CallerSnippets), strings.Join(pkg.CallerSnippets, "\n---\n")) + } + // The call sites are quoted one per matching line, in file order, so the + // tenth snippet is the tenth call site at line 13 and the last two are + // dropped. + if !strings.HasPrefix(pkg.CallerSnippets[0], "pkg/many.go:4\n") { + t.Errorf("first snippet = %q, want the first call site", pkg.CallerSnippets[0]) + } + if !strings.HasPrefix(pkg.CallerSnippets[9], "pkg/many.go:13\n") { + t.Errorf("last snippet = %q, want the tenth call site", pkg.CallerSnippets[9]) + } +} diff --git a/internal/domain/workflow/prreview/scoring.go b/internal/domain/workflow/prreview/scoring.go new file mode 100644 index 000000000..490c7c438 --- /dev/null +++ b/internal/domain/workflow/prreview/scoring.go @@ -0,0 +1,297 @@ +package prreview + +import ( + "cmp" + "math" + "slices" + "strings" +) + +// Severity is how bad a finding is, on the reviewer's own rubric. It drives +// weights, floors and ordering; whether the finding must be fixed before the +// change ships is a separate question, answered by the merge gate. +type Severity string + +const ( + SeverityCritical Severity = "critical" + SeverityImportant Severity = "important" + SeveritySuggestion Severity = "suggestion" + SeverityNitpick Severity = "nitpick" +) + +// AdversaryVerdict is what the adversary decided about one finding. +type AdversaryVerdict string + +const ( + // AdversaryConfirmed is a finding the adversary reproduced or agreed with. + AdversaryConfirmed AdversaryVerdict = "confirmed" + // AdversaryChallenged is a finding the adversary argued against. + AdversaryChallenged AdversaryVerdict = "challenged" +) + +// ReviewEvent is the verdict a forge review submits. It has no approving value: +// archie reviews changes it did not write and does not own the merge decision, +// so the strongest thing it can say is that a blocking finding has to be fixed. +type ReviewEvent string + +const ( + // ReviewEventComment posts the findings without gating the merge. + ReviewEventComment ReviewEvent = "COMMENT" + // ReviewEventRequestChanges asks for the blocking findings to be fixed + // before the change merges. + ReviewEventRequestChanges ReviewEvent = "REQUEST_CHANGES" +) + +// The multipliers a finding or a run activates. They compound: a compound +// finding the adversary confirmed on a machine-written change with a wide blast +// radius carries all four. +const ( + MultiplierCompound = "compound" + MultiplierAdversaryConfirmed = "adversary_confirmed" + MultiplierAdversaryChallenged = "adversary_challenged" + MultiplierAIGenerated = "ai_generated_pr" + MultiplierBlastRadiusHigh = "blast_radius_high" +) + +const ( + multiplierCompound = 1.5 + multiplierAdversaryConfirmed = 1.3 + multiplierAdversaryChallenged = 0.5 + multiplierAIGenerated = 1.2 + multiplierBlastRadiusHigh = 1.2 +) + +const ( + // BlastRadiusFileThreshold is the number of reached files above which a + // change is treated as wide-reaching. + BlastRadiusFileThreshold = 10 + // AIGeneratedThreshold is the intake confidence above which a change counts + // as machine-written. Above, not at: a coin-flip is not evidence. + AIGeneratedThreshold = 0.5 + // MaxInlineComments is how many inline comments one review posts. The cap is + // a budget on the reader's attention, so what it cuts is the tail of the + // ranked list, never a filter on what was found. + MaxInlineComments = 25 +) + +// Finding is one review finding before scoring: what the reviewer observed, +// where, and the two flags that only a verdict can set. +type Finding struct { + Dimension string + File string + LineStart int + LineEnd int + Severity Severity + Title string + Body string + Suggestion string + Evidence string + Confidence float64 + Tags []string + // Adversary is the verdict the adversary reached about this finding, empty + // when it did not reach one. It rides on the finding rather than on a + // side-list keyed by title, so a verdict cannot land on another finding + // that happens to share a title. + Adversary AdversaryVerdict + // Compound reports that this defect only exists in combination with + // others, which is what the compound multiplier is for. + Compound bool + // Blocking is the merge gate's verdict: only a broken build, a security + // hole, data loss, a contract break or a regression is blocking, and a + // failed gate call leaves this false. + Blocking bool +} + +// ScoredFinding is a finding after scoring: ranked, canonicalised, and carrying +// the multipliers that moved it. +type ScoredFinding struct { + Dimension string + File string + LineStart int + LineEnd int + Severity Severity + Title string + Body string + Suggestion string + Evidence string + Confidence float64 + Tags []string + Score float64 + Multipliers []string + Blocking bool +} + +// ScoreInputs is the run context that moves every finding's score. +type ScoreInputs struct { + // AIGenerated is intake's confidence, 0..1, that the change was written by + // a machine. + AIGenerated float64 + // BlastRadiusFiles is how many unchanged files the change reaches. + BlastRadiusFiles int +} + +// Score deduplicates exact duplicates, scores what is left, drops the findings +// under their severity's confidence floor and ranks the rest by score. The same +// findings always produce the same scores and the same order. +func Score(findings []Finding, inputs ScoreInputs) []ScoredFinding { + scored := make([]ScoredFinding, 0, len(findings)) + seen := map[dedupKey]bool{} + for _, finding := range findings { + severity := normalizeSeverity(finding.Severity) + key := dedupKey{file: finding.File, lineStart: finding.LineStart, lineEnd: finding.LineEnd, severity: severity} + if seen[key] { + continue + } + seen[key] = true + if finding.Confidence < confidenceFloor(severity) { + continue + } + score, applied := scoreOf(finding, severity, inputs) + scored = append(scored, ScoredFinding{ + Dimension: finding.Dimension, + File: finding.File, + LineStart: finding.LineStart, + LineEnd: finding.LineEnd, + Severity: severity, + Title: finding.Title, + Body: finding.Body, + Suggestion: finding.Suggestion, + Evidence: finding.Evidence, + Confidence: finding.Confidence, + Tags: finding.Tags, + Score: score, + Multipliers: applied, + Blocking: finding.Blocking, + }) + } + + slices.SortStableFunc(scored, func(a, b ScoredFinding) int { return cmp.Compare(b.Score, a.Score) }) + return scored +} + +// dedupKey is what makes two findings the same finding: one place, one +// severity. The wording is not part of it -- two reviewers describing one +// defect are one comment -- and a different severity at the same place is a +// different claim about it. +type dedupKey struct { + file string + lineStart int + lineEnd int + severity Severity +} + +// scoreOf applies the rubric: the severity's weight times the reviewer's +// confidence, then every multiplier the finding and the run activate. The +// order is fixed, because the same findings have to produce the same score. +func scoreOf(finding Finding, severity Severity, inputs ScoreInputs) (float64, []string) { + score := severityWeight(severity) * finding.Confidence + var applied []string + + if finding.Compound { + score *= multiplierCompound + applied = append(applied, MultiplierCompound) + } + switch finding.Adversary { + case AdversaryConfirmed: + score *= multiplierAdversaryConfirmed + applied = append(applied, MultiplierAdversaryConfirmed) + case AdversaryChallenged: + score *= multiplierAdversaryChallenged + applied = append(applied, MultiplierAdversaryChallenged) + } + if inputs.AIGenerated > AIGeneratedThreshold { + score *= multiplierAIGenerated + applied = append(applied, MultiplierAIGenerated) + } + if inputs.BlastRadiusFiles > BlastRadiusFileThreshold { + score *= multiplierBlastRadiusHigh + applied = append(applied, MultiplierBlastRadiusHigh) + } + return round3(score), applied +} + +// round3 rounds a score to three decimals. A score is read by humans and +// compared by the sort, and a full float64 mantissa invites ties that are not +// ties. +func round3(score float64) float64 { return math.Round(score*1000) / 1000 } + +// severityWeight is the rubric's weight for a severity. +func severityWeight(severity Severity) float64 { + switch severity { + case SeverityCritical: + return 1 + case SeverityImportant: + return 0.7 + case SeveritySuggestion: + return 0.3 + default: + return 0.1 + } +} + +// confidenceFloor is the confidence under which a finding of this severity is +// dropped: a vague crash is worth a look, a vague nitpick is not worth a +// reader's time. +func confidenceFloor(severity Severity) float64 { + switch severity { + case SeverityCritical: + return 0.2 + case SeverityImportant: + return 0.3 + default: + return 0.4 + } +} + +// severityAliases maps the words a reviewer model emits onto the rubric. +var severityAliases = map[Severity]Severity{ + Severity("high"): SeverityCritical, + Severity("blocker"): SeverityCritical, + Severity("medium"): SeverityImportant, + Severity("major"): SeverityImportant, + Severity("minor"): SeveritySuggestion, + Severity("low"): SeveritySuggestion, + Severity("info"): SeverityNitpick, + Severity("trivia"): SeverityNitpick, + Severity("trivial"): SeverityNitpick, +} + +// normalizeSeverity reads a severity as one of the rubric's four words, however +// it is spelled or capitalised. A word the rubric does not know is read as a +// suggestion: the lowest weight that keeps a plausible finding in the review +// rather than the highest, because an unrecognised word is not evidence of +// severity. +func normalizeSeverity(severity Severity) Severity { + lowered := Severity(strings.ToLower(strings.TrimSpace(string(severity)))) + switch lowered { + case SeverityCritical, SeverityImportant, SeveritySuggestion, SeverityNitpick: + return lowered + } + if canonical, known := severityAliases[lowered]; known { + return canonical + } + return SeveritySuggestion +} + +// CapInlineComments cuts a ranked list down to the comments one review posts. +// It is applied to the ranked list, so what it cuts is the lowest-scoring tail. +func CapInlineComments(findings []ScoredFinding) []ScoredFinding { + if len(findings) > MaxInlineComments { + return findings[:MaxInlineComments] + } + return findings +} + +// ReviewEventFor decides the event a review is submitted with: a blocking +// finding requests changes, and anything else -- including a review that found +// nothing -- is a comment. A pipeline that found nothing has said nothing about +// the change, and an approval from a reviewer that did not look is worse than +// silence. +func ReviewEventFor(findings []ScoredFinding) ReviewEvent { + for _, finding := range findings { + if finding.Blocking { + return ReviewEventRequestChanges + } + } + return ReviewEventComment +} diff --git a/internal/domain/workflow/prreview/scoring_test.go b/internal/domain/workflow/prreview/scoring_test.go new file mode 100644 index 000000000..02da88b4d --- /dev/null +++ b/internal/domain/workflow/prreview/scoring_test.go @@ -0,0 +1,314 @@ +package prreview + +import ( + "reflect" + "slices" + "strings" + "testing" +) + +// scored builds the finding every scoring case starts from: one advisory +// finding in one place, whose severity, confidence and flags the case varies. +func scored(severity Severity, confidence float64) Finding { + return Finding{ + Dimension: "behaviour", + File: "pkg/a.go", + LineStart: 10, + LineEnd: 12, + Severity: severity, + Title: "the defect", + Body: "what goes wrong", + Suggestion: "how to fix it", + Evidence: "the code", + Confidence: confidence, + Tags: []string{"tag"}, + } +} + +func scoreOne(t *testing.T, finding Finding, inputs ScoreInputs) ScoredFinding { + t.Helper() + + found := Score([]Finding{finding}, inputs) + if len(found) != 1 { + t.Fatalf("Score() returned %d findings, want the one it was given", len(found)) + } + return found[0] +} + +func TestScoreWeighsSeverityConfidenceAndContext(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + severity Severity + confidence float64 + adversary AdversaryVerdict + compound bool + inputs ScoreInputs + wantScore float64 + wantApplied []string + }{ + {name: "critical", severity: SeverityCritical, confidence: 1, wantScore: 1}, + {name: "important", severity: SeverityImportant, confidence: 1, wantScore: 0.7}, + {name: "suggestion", severity: SeveritySuggestion, confidence: 1, wantScore: 0.3}, + {name: "nitpick", severity: SeverityNitpick, confidence: 1, wantScore: 0.1}, + {name: "confidence scales the weight", severity: SeverityImportant, confidence: 0.5, wantScore: 0.35}, + {name: "a low confidence scales it down", severity: SeveritySuggestion, confidence: 0.5, wantScore: 0.15}, + { + name: "a compound finding compounds", severity: SeverityCritical, confidence: 0.5, compound: true, + wantScore: 0.75, wantApplied: []string{MultiplierCompound}, + }, + { + name: "an adversary confirmation raises the score", severity: SeverityImportant, confidence: 0.5, adversary: AdversaryConfirmed, + wantScore: 0.455, wantApplied: []string{MultiplierAdversaryConfirmed}, + }, + { + name: "an adversary challenge lowers it", severity: SeverityImportant, confidence: 0.5, adversary: AdversaryChallenged, + wantScore: 0.175, wantApplied: []string{MultiplierAdversaryChallenged}, + }, + { + name: "a machine-written change raises it", severity: SeveritySuggestion, confidence: 0.5, inputs: ScoreInputs{AIGenerated: 0.51}, + wantScore: 0.18, wantApplied: []string{MultiplierAIGenerated}, + }, + { + name: "a wide blast radius raises it", severity: SeveritySuggestion, confidence: 0.5, inputs: ScoreInputs{BlastRadiusFiles: 11}, + wantScore: 0.18, wantApplied: []string{MultiplierBlastRadiusHigh}, + }, + { + name: "multipliers compound", severity: SeverityCritical, confidence: 0.5, compound: true, adversary: AdversaryConfirmed, + inputs: ScoreInputs{AIGenerated: 0.51, BlastRadiusFiles: 11}, + wantScore: 1.404, wantApplied: []string{ + MultiplierCompound, MultiplierAdversaryConfirmed, MultiplierAIGenerated, MultiplierBlastRadiusHigh, + }, + }, + { + name: "an AI confidence at the threshold does not raise the score", severity: SeveritySuggestion, confidence: 1, + inputs: ScoreInputs{AIGenerated: 0.5}, wantScore: 0.3, + }, + { + name: "a blast radius at the threshold does not raise the score", severity: SeveritySuggestion, confidence: 1, + inputs: ScoreInputs{BlastRadiusFiles: 10}, wantScore: 0.3, + }, + {name: "a severity alias is read as the rubric's word", severity: Severity("high"), confidence: 0.5, wantScore: 0.5}, + {name: "a differently spelled severity is still canonical", severity: Severity("Critical"), confidence: 1, wantScore: 1}, + {name: "an unknown severity is read as a suggestion", severity: Severity("concerning"), confidence: 0.5, wantScore: 0.15}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + finding := scored(test.severity, test.confidence) + finding.Adversary = test.adversary + finding.Compound = test.compound + + got := scoreOne(t, finding, test.inputs) + if got.Score != test.wantScore { + t.Errorf("Score = %v, want %v", got.Score, test.wantScore) + } + if len(got.Multipliers) != len(test.wantApplied) { + t.Fatalf("Multipliers = %v, want %v", got.Multipliers, test.wantApplied) + } + for i := range got.Multipliers { + if got.Multipliers[i] != test.wantApplied[i] { + t.Errorf("Multipliers[%d] = %q, want %q", i, got.Multipliers[i], test.wantApplied[i]) + } + } + }) + } +} + +func TestScoreDropsFindingsUnderTheirConfidenceFloor(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + severity Severity + confidence float64 + wantKept bool + }{ + {name: "critical under its floor", severity: SeverityCritical, confidence: 0.19}, + {name: "critical at its floor", severity: SeverityCritical, confidence: 0.2, wantKept: true}, + {name: "important under its floor", severity: SeverityImportant, confidence: 0.29}, + {name: "important at its floor", severity: SeverityImportant, confidence: 0.3, wantKept: true}, + {name: "suggestion under its floor", severity: SeveritySuggestion, confidence: 0.39}, + {name: "suggestion at its floor", severity: SeveritySuggestion, confidence: 0.4, wantKept: true}, + {name: "nitpick under its floor", severity: SeverityNitpick, confidence: 0.39}, + {name: "nitpick at its floor", severity: SeverityNitpick, confidence: 0.4, wantKept: true}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + got := Score([]Finding{scored(test.severity, test.confidence)}, ScoreInputs{}) + if kept := len(got) == 1; kept != test.wantKept { + t.Errorf("Score() kept %d findings, want kept = %t", len(got), test.wantKept) + } + }) + } +} + +func TestScoreMergesExactDuplicates(t *testing.T) { + t.Parallel() + + first := scored(SeverityImportant, 0.8) + first.Title = "first" + + tests := []struct { + name string + second Finding + want []string + }{ + { + name: "the same defect in the same place is one finding", + second: func() Finding { f := first; f.Title = "second"; f.Body = "worded differently"; return f }(), + want: []string{"first"}, + }, + { + name: "the same place with another severity is another finding", + second: func() Finding { f := first; f.Title = "second"; f.Severity = SeverityCritical; return f }(), + want: []string{"first", "second"}, + }, + { + name: "the same severity elsewhere is another finding", + second: func() Finding { f := first; f.Title = "second"; f.LineStart = 40; f.LineEnd = 42; return f }(), + want: []string{"first", "second"}, + }, + { + name: "another spelling of the same severity is the same finding", + second: func() Finding { f := first; f.Title = "second"; f.Severity = Severity("major"); return f }(), + want: []string{"first"}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + got := Score([]Finding{first, test.second}, ScoreInputs{}) + titles := make([]string, 0, len(got)) + for _, finding := range got { + titles = append(titles, finding.Title) + } + // The ranking is asserted elsewhere; here the question is which + // findings survived, so the titles are compared as a set. + slices.Sort(titles) + wantStrings(t, titles, test.want) + }) + } +} + +func TestScoreRanksByScoreAndKeepsInputOrderOnTies(t *testing.T) { + t.Parallel() + + low := scored(SeveritySuggestion, 0.5) + low.Title = "low" + low.LineStart = 1 + high := scored(SeverityCritical, 0.9) + high.Title = "high" + high.LineStart = 2 + firstTie := scored(SeverityImportant, 0.5) + firstTie.Title = "tie-first" + firstTie.LineStart = 3 + secondTie := scored(SeverityImportant, 0.5) + secondTie.Title = "tie-second" + secondTie.LineStart = 4 + + findings := []Finding{low, high, firstTie, secondTie} + + got := Score(findings, ScoreInputs{}) + titles := make([]string, 0, len(got)) + for _, finding := range got { + titles = append(titles, finding.Title) + } + wantStrings(t, titles, []string{"high", "tie-first", "tie-second", "low"}) + + // The same findings always produce the same scores and the same order. + if again := Score(findings, ScoreInputs{}); !reflect.DeepEqual(got, again) { + t.Errorf("a second scoring pass differs:\n%+v\n%+v", got, again) + } +} + +func TestScoreCopiesTheFindingItScores(t *testing.T) { + t.Parallel() + + finding := scored(SeverityImportant, 0.8) + got := scoreOne(t, finding, ScoreInputs{}) + + if got.File != finding.File || got.Dimension != finding.Dimension || got.Title != finding.Title || + got.Body != finding.Body || got.Suggestion != finding.Suggestion || got.Evidence != finding.Evidence || + got.LineStart != finding.LineStart || got.LineEnd != finding.LineEnd || got.Confidence != finding.Confidence { + t.Errorf("ScoredFinding = %+v, want the finding it was scored from", got) + } + if got.Severity != SeverityImportant { + t.Errorf("Severity = %q, want %q", got.Severity, SeverityImportant) + } + if !reflect.DeepEqual(got.Tags, finding.Tags) { + t.Errorf("Tags = %v, want %v", got.Tags, finding.Tags) + } +} + +func TestCapInlineCommentsKeepsTheHighestScoring(t *testing.T) { + t.Parallel() + + findings := make([]Finding, 0, 30) + for index := range 30 { + finding := scored(SeverityCritical, 1-float64(index)/100) + finding.Title = "finding " + strings.Repeat("x", index+1) + finding.LineStart = index + 1 + finding.LineEnd = index + 1 + findings = append(findings, finding) + } + + ranked := Score(findings, ScoreInputs{}) + if len(ranked) != 30 { + t.Fatalf("Score() returned %d findings, want 30", len(ranked)) + } + + capped := CapInlineComments(ranked) + if len(capped) != 25 { + t.Fatalf("CapInlineComments() returned %d findings, want 25", len(capped)) + } + if !reflect.DeepEqual(capped, ranked[:25]) { + t.Errorf("CapInlineComments() cut from the wrong end:\n%+v", capped) + } + + short := ranked[:3] + if got := CapInlineComments(short); len(got) != 3 { + t.Errorf("CapInlineComments() returned %d findings for a list under the cap, want 3", len(got)) + } +} + +func TestReviewEventFor(t *testing.T) { + t.Parallel() + + advisory := make([]ScoredFinding, 0, 25) + for index := range 25 { + finding := scoreOne(t, scored(SeverityImportant, 0.8), ScoreInputs{}) + finding.Title = "advisory " + strings.Repeat("x", index+1) + advisory = append(advisory, finding) + } + postCap := append(append([]ScoredFinding{}, advisory...), ScoredFinding{Title: "blocking", Blocking: true}) + + tests := []struct { + name string + findings []ScoredFinding + want ReviewEvent + }{ + {name: "a review that found nothing is still a comment", want: ReviewEventComment}, + {name: "advisory findings do not gate the merge", findings: advisory, want: ReviewEventComment}, + {name: "a blocking finding asks for changes", findings: []ScoredFinding{{Blocking: true}}, want: ReviewEventRequestChanges}, + {name: "a blocking finding past the comment cap still asks for changes", findings: postCap, want: ReviewEventRequestChanges}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + if got := ReviewEventFor(test.findings); got != test.want { + t.Errorf("ReviewEventFor() = %q, want %q", got, test.want) + } + }) + } +} diff --git a/internal/domain/workflow/prreview/snapshot.go b/internal/domain/workflow/prreview/snapshot.go new file mode 100644 index 000000000..da54dd4f5 --- /dev/null +++ b/internal/domain/workflow/prreview/snapshot.go @@ -0,0 +1,127 @@ +package prreview + +import ( + "errors" + "fmt" + "io" + "io/fs" + "path/filepath" + "strings" +) + +// maxScannedFileBytes bounds how much of one file either read quotes. A +// generated bundle is not evidence, and a review's budget is not its to spend. +const maxScannedFileBytes = 1 << 20 + +// skippedDirectories are the directories a snapshot's reads never enter: +// dependencies that are not this change's to review, and caches. +var skippedDirectories = map[string]bool{ + ".git": true, + "node_modules": true, + "__pycache__": true, + ".venv": true, + "venv": true, + "vendor": true, +} + +// walkPaths lists the files of a snapshot a read wants, in lexical order. That +// order is what makes every result derived from the walk -- an import graph, a +// caller search -- the same result on every run. +func walkPaths(fsys fs.FS, wanted func(string) bool) ([]string, error) { + var paths []string + err := fs.WalkDir(fsys, ".", func(file string, entry fs.DirEntry, err error) error { + if err != nil { + return err + } + if entry.IsDir() { + if file != "." && skippedDirectories[entry.Name()] { + return fs.SkipDir + } + return nil + } + if wanted(file) { + paths = append(paths, file) + } + return nil + }) + if err != nil { + return nil, err + } + return paths, nil +} + +// isQuotablePath reports whether a file may be quoted as evidence. A file with +// no extension is decided by its content instead, so a LICENSE or a Makefile +// is still read. +func isQuotablePath(file string) bool { + extension := strings.ToLower(filepath.Ext(file)) + return extension == "" || quotableExtensions[extension] +} + +// quotableExtensions are the file types this package quotes code from. +var quotableExtensions = map[string]bool{ + ".py": true, ".js": true, ".jsx": true, ".ts": true, ".tsx": true, + ".go": true, ".rs": true, ".java": true, ".rb": true, ".php": true, + ".c": true, ".h": true, ".cpp": true, ".hpp": true, ".cs": true, + ".swift": true, ".kt": true, ".scala": true, ".sh": true, + ".yaml": true, ".yml": true, ".json": true, ".toml": true, ".ini": true, + ".cfg": true, ".md": true, ".sql": true, ".html": true, ".css": true, + ".scss": true, ".txt": true, +} + +// readLines reads the lines of a file for quoting. A file the snapshot does not +// have, one larger than the scan bound, and a binary file all read as nothing: +// none of them is code a reviewer can be shown. +func readLines(fsys fs.FS, file string) ([]string, error) { + if !isQuotablePath(file) { + return nil, nil + } + data, err := readBounded(fsys, file) + if err != nil || data == nil { + return nil, err + } + if strings.IndexByte(string(data), 0) >= 0 { + return nil, nil + } + lines := strings.Split(strings.ReplaceAll(string(data), "\r\n", "\n"), "\n") + // A file that ends with a newline ends with a line, not with an empty one: + // the trailing element the split leaves behind is not a line any position + // can be anchored to. + if last := len(lines) - 1; last >= 0 && lines[last] == "" { + lines = lines[:last] + } + return lines, nil +} + +// readBounded reads at most the scan bound of a file and answers nil for a file +// that is larger, or that the snapshot does not have. +func readBounded(fsys fs.FS, file string) ([]byte, error) { + handle, err := fsys.Open(file) + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + return nil, nil + } + return nil, fmt.Errorf("open %s: %w", file, err) + } + defer handle.Close() + + data, err := io.ReadAll(io.LimitReader(handle, maxScannedFileBytes+1)) + if err != nil { + return nil, fmt.Errorf("read %s: %w", file, err) + } + if len(data) > maxScannedFileBytes { + return nil, nil + } + return data, nil +} + +// appendUnique adds value to values unless it is already there, which keeps the +// order each caller built. +func appendUnique(values []string, value string) []string { + for _, existing := range values { + if existing == value { + return values + } + } + return append(values, value) +}