|
| 1 | +package sight |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/GrayCodeAI/sight/internal/review" |
| 9 | +) |
| 10 | + |
| 11 | +// CustomCheck represents a user-defined review check loaded from a markdown file |
| 12 | +// in the .sight/checks/ directory. Each file becomes a check whose content is |
| 13 | +// injected into the LLM prompt as an additional concern. |
| 14 | +type CustomCheck struct { |
| 15 | + // Name is derived from the filename (e.g., "no-console-log" from no-console-log.md). |
| 16 | + Name string |
| 17 | + |
| 18 | + // Prompt is the markdown body that describes the check rules and gets |
| 19 | + // injected into the LLM system prompt. |
| 20 | + Prompt string |
| 21 | + |
| 22 | + // Severity is the default severity for findings from this check. |
| 23 | + // Parsed from YAML frontmatter; defaults to "medium". |
| 24 | + Severity string |
| 25 | + |
| 26 | + // Languages restricts the check to files matching these extensions |
| 27 | + // (e.g., ["go", "py"]). Empty means all languages. |
| 28 | + Languages []string |
| 29 | + |
| 30 | + // Enabled controls whether the check is active. Defaults to true. |
| 31 | + Enabled bool |
| 32 | +} |
| 33 | + |
| 34 | +// LoadChecks reads all markdown files from the given directory (typically |
| 35 | +// ".sight/checks/") and parses them into CustomCheck values. Each .md file |
| 36 | +// becomes one check. YAML frontmatter between --- delimiters is parsed for |
| 37 | +// metadata (severity, languages, enabled); the remaining body becomes the |
| 38 | +// check prompt. |
| 39 | +// |
| 40 | +// Returns an empty slice (not an error) if the directory does not exist. |
| 41 | +func LoadChecks(dir string) ([]CustomCheck, error) { |
| 42 | + entries, err := os.ReadDir(dir) |
| 43 | + if err != nil { |
| 44 | + if os.IsNotExist(err) { |
| 45 | + return nil, nil |
| 46 | + } |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + var checks []CustomCheck |
| 51 | + for _, entry := range entries { |
| 52 | + if entry.IsDir() { |
| 53 | + continue |
| 54 | + } |
| 55 | + if !strings.HasSuffix(entry.Name(), ".md") { |
| 56 | + continue |
| 57 | + } |
| 58 | + |
| 59 | + path := filepath.Join(dir, entry.Name()) |
| 60 | + data, err := os.ReadFile(path) |
| 61 | + if err != nil { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + name := strings.TrimSuffix(entry.Name(), ".md") |
| 66 | + check := parseCheckFile(name, string(data)) |
| 67 | + checks = append(checks, check) |
| 68 | + } |
| 69 | + |
| 70 | + return checks, nil |
| 71 | +} |
| 72 | + |
| 73 | +// LoadChecksFromRepo is a convenience that looks for .sight/checks/ relative |
| 74 | +// to the given repository root directory. |
| 75 | +func LoadChecksFromRepo(repoDir string) ([]CustomCheck, error) { |
| 76 | + return LoadChecks(filepath.Join(repoDir, ".sight", "checks")) |
| 77 | +} |
| 78 | + |
| 79 | +// CustomChecksToConcerns converts loaded custom checks into internal Concern |
| 80 | +// values suitable for the review pipeline. Only enabled checks are included. |
| 81 | +// If languages is non-empty, the concern prompt notes which languages apply. |
| 82 | +func CustomChecksToConcerns(checks []CustomCheck) []review.Concern { |
| 83 | + var concerns []review.Concern |
| 84 | + for _, c := range checks { |
| 85 | + if !c.Enabled { |
| 86 | + continue |
| 87 | + } |
| 88 | + prompt := c.Prompt |
| 89 | + if len(c.Languages) > 0 { |
| 90 | + prompt += "\n\nThis check applies only to files with these extensions: " + |
| 91 | + strings.Join(c.Languages, ", ") |
| 92 | + } |
| 93 | + if c.Severity != "" { |
| 94 | + prompt += "\n\nDefault severity for issues found by this check: " + c.Severity |
| 95 | + } |
| 96 | + concerns = append(concerns, review.Concern{ |
| 97 | + Name: "custom:" + c.Name, |
| 98 | + Prompt: prompt, |
| 99 | + }) |
| 100 | + } |
| 101 | + return concerns |
| 102 | +} |
| 103 | + |
| 104 | +// WithCustomChecks loads checks from the given directory and appends them as |
| 105 | +// additional concerns to the review. This is the primary integration point: |
| 106 | +// |
| 107 | +// sight.Review(ctx, diff, sight.WithCustomChecks(".sight/checks")) |
| 108 | +func WithCustomChecks(dir string) Option { |
| 109 | + return optFunc(func(c *config) { |
| 110 | + checks, err := LoadChecks(dir) |
| 111 | + if err != nil || len(checks) == 0 { |
| 112 | + return |
| 113 | + } |
| 114 | + concerns := CustomChecksToConcerns(checks) |
| 115 | + for _, concern := range concerns { |
| 116 | + c.concerns = append(c.concerns, concern.Name) |
| 117 | + } |
| 118 | + c.customConcerns = append(c.customConcerns, concerns...) |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +// WithCustomChecksFromRepo loads checks from .sight/checks/ within the repo root. |
| 123 | +func WithCustomChecksFromRepo(repoDir string) Option { |
| 124 | + return WithCustomChecks(filepath.Join(repoDir, ".sight", "checks")) |
| 125 | +} |
| 126 | + |
| 127 | +// parseCheckFile parses a markdown file into a CustomCheck. It extracts YAML |
| 128 | +// frontmatter between --- delimiters for metadata and uses the remaining |
| 129 | +// content as the prompt. |
| 130 | +func parseCheckFile(name, content string) CustomCheck { |
| 131 | + check := CustomCheck{ |
| 132 | + Name: name, |
| 133 | + Enabled: true, |
| 134 | + Severity: "medium", |
| 135 | + } |
| 136 | + |
| 137 | + content = strings.TrimSpace(content) |
| 138 | + |
| 139 | + // Parse YAML frontmatter if present |
| 140 | + if strings.HasPrefix(content, "---") { |
| 141 | + parts := strings.SplitN(content[3:], "---", 2) |
| 142 | + if len(parts) == 2 { |
| 143 | + parseFrontmatter(&check, strings.TrimSpace(parts[0])) |
| 144 | + content = strings.TrimSpace(parts[1]) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + check.Prompt = content |
| 149 | + return check |
| 150 | +} |
| 151 | + |
| 152 | +// parseFrontmatter extracts metadata from a simplified YAML frontmatter block. |
| 153 | +// Supports: severity, languages (comma-separated), enabled (true/false). |
| 154 | +func parseFrontmatter(check *CustomCheck, fm string) { |
| 155 | + for _, line := range strings.Split(fm, "\n") { |
| 156 | + line = strings.TrimSpace(line) |
| 157 | + if line == "" || strings.HasPrefix(line, "#") { |
| 158 | + continue |
| 159 | + } |
| 160 | + |
| 161 | + parts := strings.SplitN(line, ":", 2) |
| 162 | + if len(parts) != 2 { |
| 163 | + continue |
| 164 | + } |
| 165 | + key := strings.TrimSpace(parts[0]) |
| 166 | + value := strings.TrimSpace(parts[1]) |
| 167 | + |
| 168 | + switch key { |
| 169 | + case "severity": |
| 170 | + value = strings.ToLower(value) |
| 171 | + switch value { |
| 172 | + case "info", "low", "medium", "high", "critical": |
| 173 | + check.Severity = value |
| 174 | + } |
| 175 | + case "languages": |
| 176 | + langs := strings.Split(value, ",") |
| 177 | + for _, l := range langs { |
| 178 | + l = strings.TrimSpace(l) |
| 179 | + l = strings.Trim(l, "[]\"'") |
| 180 | + if l != "" { |
| 181 | + check.Languages = append(check.Languages, l) |
| 182 | + } |
| 183 | + } |
| 184 | + case "enabled": |
| 185 | + check.Enabled = strings.ToLower(value) != "false" |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments