|
1 | | -# Copilot Agent Hooks Scripts |
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +check_api_changes.py - Detects changes to the public API and enforces documentation updates. |
2 | 4 |
|
3 | | -This directory contains Python scripts used by agent hooks to automate workflow validation. |
| 5 | +This script is invoked by the pre-commit hook to ensure that any modifications |
| 6 | +to public API files are accompanied by corresponding documentation changes. |
| 7 | +""" |
4 | 8 |
|
5 | | -## Scripts |
| 9 | +import subprocess |
| 10 | +import sys |
6 | 11 |
|
7 | | -### session_start.py |
8 | 12 |
|
9 | | -Runs at session start to inject project context: |
| 13 | +# Files that define the public API surface |
| 14 | +PUBLIC_API_FILES = [ |
| 15 | + "src/extension.ts", |
| 16 | + "src/api.ts", |
| 17 | +] |
10 | 18 |
|
11 | | -- Current git branch and commit |
12 | | -- Uncommitted changes count |
13 | | -- Open issues (if gh CLI available) |
14 | | -- Snapshot health summary (if available) |
15 | | -- Available skills reminder |
| 19 | +# Documentation files that must be updated when the API changes |
| 20 | +DOCS_FILES = [ |
| 21 | + "README.md", |
| 22 | + "docs/api.md", |
| 23 | + "CHANGELOG.md", |
| 24 | +] |
16 | 25 |
|
17 | | -### post_tool_use.py |
18 | 26 |
|
19 | | -Runs after file edit tools to provide immediate feedback: |
| 27 | +def get_staged_files(): |
| 28 | + """Return list of files staged for the current commit.""" |
| 29 | + result = subprocess.run( |
| 30 | + ["git", "diff", "--cached", "--name-only"], |
| 31 | + capture_output=True, |
| 32 | + text=True, |
| 33 | + ) |
| 34 | + if result.returncode != 0: |
| 35 | + return [] |
| 36 | + return [f for f in result.stdout.strip().split("\n") if f] |
20 | 37 |
|
21 | | -- Runs ESLint on changed TypeScript files |
22 | | -- Reports lint errors back to the model |
23 | 38 |
|
24 | | -### stop_hook.py |
| 39 | +def has_api_changes(staged_files): |
| 40 | + """Check whether any public API files are staged.""" |
| 41 | + return any(f in PUBLIC_API_FILES for f in staged_files) |
25 | 42 |
|
26 | | -Runs before session ends to enforce workflow: |
27 | 43 |
|
28 | | -- Checks for uncommitted TypeScript changes |
29 | | -- Reminds about pre-commit checks |
30 | | -- Blocks completion if staged changes aren't committed |
| 44 | +def has_doc_changes(staged_files): |
| 45 | + """Check whether any documentation files are staged.""" |
| 46 | + return any(f in DOCS_FILES for f in staged_files) |
31 | 47 |
|
32 | | -### subagent_stop.py |
33 | 48 |
|
34 | | -Runs when subagents complete: |
| 49 | +def main(): |
| 50 | + """Enforce that API changes are documented.""" |
| 51 | + staged_files = get_staged_files() |
35 | 52 |
|
36 | | -- Currently a passthrough for logging |
37 | | -- Can be extended to validate reviewer output |
| 53 | + if not has_api_changes(staged_files): |
| 54 | + sys.exit(0) |
38 | 55 |
|
39 | | -## Requirements |
| 56 | + if has_doc_changes(staged_files): |
| 57 | + print("API changes detected with documentation updates.") |
| 58 | + sys.exit(0) |
40 | 59 |
|
41 | | -These scripts use Python 3.9+ with no external dependencies (beyond what's already in the repo). |
| 60 | + print("WARNING: Public API changes detected without documentation updates.") |
| 61 | + print("Modified API files:") |
| 62 | + for f in PUBLIC_API_FILES: |
| 63 | + if f in staged_files: |
| 64 | + print(f" - {f}") |
| 65 | + print("Please update one or more of these documentation files:") |
| 66 | + for f in DOCS_FILES: |
| 67 | + print(f" - {f}") |
| 68 | + sys.exit(1) |
42 | 69 |
|
43 | | -They expect: |
44 | 70 |
|
45 | | -- `git` CLI available |
46 | | -- `gh` CLI available (optional, for issue context) |
47 | | -- `npx` available for running ESLint |
48 | | - |
49 | | -## Hook Configuration |
50 | | - |
51 | | -See `.github/hooks/maintainer-hooks.json` for the hook configuration that loads these scripts. |
| 71 | +if __name__ == "__main__": |
| 72 | + main() |
0 commit comments