69da5df254
Adds 'review-bot validate-docmap' for CI hard-fail on docmap coverage gaps.
Usage:
git diff --name-only origin/main HEAD | \
review-bot validate-docmap --docmap .review-bot/doc-map.yml --repo-root .
Flags:
--docmap (required) path to doc-map YAML file
--repo-root (optional, default '.') root for resolving docs: paths
Two checks, both always run:
1. Coverage: every stdin file must match at least one paths: glob.
2. Stale docs: every docs: entry must exist on disk under --repo-root.
Exit codes: 0=clean, 1=failures found, 2=usage/parse error.
Tests cover: clean pass, uncovered file, stale doc, both failures,
empty stdin, blank-line stdin, and duplicate docs: deduplication.
140 lines
3.8 KiB
Go
140 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gitea.weiker.me/rodin/review-bot/review"
|
|
)
|
|
|
|
// runValidateDocmap implements the `review-bot validate-docmap` subcommand.
|
|
//
|
|
// It reads changed file paths from stdin (one per line, as produced by
|
|
// `git diff --name-only`), parses a doc-map YAML file, and performs two checks:
|
|
//
|
|
// 1. Coverage check: every changed file must be matched by at least one
|
|
// paths: glob in the docmap. Fails if any file is uncovered.
|
|
//
|
|
// 2. Stale-docs check: every docs: entry in the docmap must exist on disk
|
|
// (relative to --repo-root). Fails if any path is missing.
|
|
//
|
|
// Both checks always run — all failures are reported before exiting.
|
|
//
|
|
// Exit codes:
|
|
//
|
|
// 0 — clean (all files covered, all docs exist)
|
|
// 1 — one or more coverage or stale-doc failures
|
|
// 2 — usage error, missing flag, or YAML parse error
|
|
func runValidateDocmap(args []string) int {
|
|
fs := flag.NewFlagSet("validate-docmap", flag.ContinueOnError)
|
|
fs.SetOutput(errWriter)
|
|
|
|
docmapFlag := fs.String("docmap", "", "Path to doc-map YAML file (required)")
|
|
repoRootFlag := fs.String("repo-root", ".", "Repo root for resolving docs: paths (default: cwd)")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
// flag.ContinueOnError already wrote the error to errWriter.
|
|
return 2
|
|
}
|
|
|
|
if *docmapFlag == "" {
|
|
fmt.Fprintln(errWriter, "Error: --docmap is required")
|
|
fmt.Fprintln(errWriter, "")
|
|
fmt.Fprintln(errWriter, "usage: review-bot validate-docmap --docmap <path> [--repo-root <dir>]")
|
|
fmt.Fprintln(errWriter, " Changed files are read from stdin, one per line.")
|
|
fmt.Fprintln(errWriter, " Example: git diff --name-only origin/main HEAD | review-bot validate-docmap --docmap .review-bot/doc-map.yml")
|
|
return 2
|
|
}
|
|
|
|
// Parse docmap YAML.
|
|
cfg, err := review.ParseDocMapConfig(*docmapFlag)
|
|
if err != nil {
|
|
fmt.Fprintf(errWriter, "Error: failed to parse docmap %q: %v\n", *docmapFlag, err)
|
|
return 2
|
|
}
|
|
|
|
// Read changed files from stdin.
|
|
changedFiles, err := readLines(os.Stdin)
|
|
if err != nil {
|
|
fmt.Fprintf(errWriter, "Error: failed to read stdin: %v\n", err)
|
|
return 2
|
|
}
|
|
|
|
failed := false
|
|
|
|
// --- Check 1: Coverage ---
|
|
var uncovered []string
|
|
for _, f := range changedFiles {
|
|
if !review.FileCoveredByDocMap(cfg, f) {
|
|
uncovered = append(uncovered, f)
|
|
}
|
|
}
|
|
if len(uncovered) > 0 {
|
|
failed = true
|
|
fmt.Fprintln(errWriter, "ERROR: changed files with no docmap coverage:")
|
|
for _, f := range uncovered {
|
|
fmt.Fprintf(errWriter, " %s\n", f)
|
|
}
|
|
}
|
|
|
|
// --- Check 2: Stale docs ---
|
|
repoRoot := filepath.Clean(*repoRootFlag)
|
|
staleDocs := checkStaleDocs(cfg, repoRoot)
|
|
if len(staleDocs) > 0 {
|
|
failed = true
|
|
fmt.Fprintln(errWriter, "ERROR: stale docmap docs: entries (paths do not exist):")
|
|
for _, d := range staleDocs {
|
|
fmt.Fprintf(errWriter, " %s\n", d)
|
|
}
|
|
}
|
|
|
|
if failed {
|
|
return 1
|
|
}
|
|
|
|
fmt.Fprintln(outWriter, "OK: docmap is valid")
|
|
return 0
|
|
}
|
|
|
|
// checkStaleDocs returns deduplicated docs: entries that do not exist under repoRoot.
|
|
func checkStaleDocs(cfg *review.DocMapConfig, repoRoot string) []string {
|
|
seen := make(map[string]struct{})
|
|
var stale []string
|
|
|
|
for _, mapping := range cfg.Mappings {
|
|
for _, docPath := range mapping.Docs {
|
|
if docPath == "" {
|
|
continue
|
|
}
|
|
if _, ok := seen[docPath]; ok {
|
|
continue
|
|
}
|
|
seen[docPath] = struct{}{}
|
|
|
|
fullPath := filepath.Join(repoRoot, filepath.FromSlash(docPath))
|
|
if _, err := os.Stat(fullPath); err != nil {
|
|
stale = append(stale, docPath)
|
|
}
|
|
}
|
|
}
|
|
return stale
|
|
}
|
|
|
|
// readLines reads all non-empty trimmed lines from r.
|
|
func readLines(r io.Reader) ([]string, error) {
|
|
scanner := bufio.NewScanner(r)
|
|
var lines []string
|
|
for scanner.Scan() {
|
|
line := strings.TrimSpace(scanner.Text())
|
|
if line != "" {
|
|
lines = append(lines, line)
|
|
}
|
|
}
|
|
return lines, scanner.Err()
|
|
}
|