Skip to content

Commit 4d85fba

Browse files
authoredand pushed
Merge pull request #681 from jamesmontemagno:copilot/migrate-native-hooks
Migrate repository hooks to native Copilot format
2 parents 2bda235 + 498489f commit 4d85fba

6 files changed

Lines changed: 160 additions & 72 deletions

‎.github/hooks/require-build-validation.json‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
{
2+
"version": 1,
23
"hooks": {
3-
"PostToolUse": [
4+
"postToolUse": [
45
{
56
"type": "command",
6-
"command": "/usr/bin/env python3 .github/hooks/require_build_validation.py",
7-
"timeout": 10
7+
"bash": "/usr/bin/env python3 .github/hooks/require_build_validation.py postToolUse",
8+
"powershell": "python .github/hooks/require_build_validation.py postToolUse",
9+
"timeoutSec": 10
810
}
911
],
10-
"Stop": [
12+
"agentStop": [
1113
{
1214
"type": "command",
13-
"command": "/usr/bin/env python3 .github/hooks/require_build_validation.py",
14-
"timeout": 10
15+
"bash": "/usr/bin/env python3 .github/hooks/require_build_validation.py agentStop",
16+
"powershell": "python .github/hooks/require_build_validation.py agentStop",
17+
"timeoutSec": 10
1518
}
1619
]
1720
}

‎.github/hooks/require-changelog-update.json‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
{
2+
"version": 1,
23
"hooks": {
3-
"PostToolUse": [
4+
"postToolUse": [
45
{
56
"type": "command",
6-
"command": "/usr/bin/env python3 .github/hooks/require_changelog_update.py",
7-
"timeout": 10
7+
"bash": "/usr/bin/env python3 .github/hooks/require_changelog_update.py postToolUse",
8+
"powershell": "python .github/hooks/require_changelog_update.py postToolUse",
9+
"timeoutSec": 10
810
}
911
],
10-
"Stop": [
12+
"agentStop": [
1113
{
1214
"type": "command",
13-
"command": "/usr/bin/env python3 .github/hooks/require_changelog_update.py",
14-
"timeout": 10
15+
"bash": "/usr/bin/env python3 .github/hooks/require_changelog_update.py agentStop",
16+
"powershell": "python .github/hooks/require_changelog_update.py agentStop",
17+
"timeoutSec": 10
1518
}
1619
]
1720
}

‎.github/hooks/require_build_validation.py‎

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@
1414
)
1515
EDIT_TOOL_NAMES = {
1616
"apply_patch",
17+
"create",
1718
"create_file",
19+
"edit",
1820
"multi_replace_string_in_file",
1921
"replace_string_in_file",
22+
"str_replace_editor",
2023
"vscode_renameSymbol",
2124
}
2225
BUILD_TOOL_NAMES = {
26+
"bash",
2327
"create_and_run_task",
2428
"execution_subagent",
29+
"powershell",
2530
"run_in_terminal",
31+
"runTask",
32+
"task",
2633
}
2734
TARGET_SCHEMES = ("TinyClips", "TinyClipsMAS")
2835
PATCH_FILE_PATTERN = re.compile(
@@ -40,6 +47,10 @@ def emit(payload):
4047
sys.stdout.write(json.dumps(payload))
4148

4249

50+
def warn(message):
51+
sys.stderr.write(f"{message}\n")
52+
53+
4354
def normalize_path(value, repo_root):
4455
if not isinstance(value, str) or not value:
4556
return None
@@ -79,6 +90,8 @@ def extract_paths_from_patch(patch_text, repo_root):
7990

8091
def extract_paths_from_payload(payload, repo_root):
8192
paths = set()
93+
if isinstance(payload, str):
94+
return extract_paths_from_patch(payload, repo_root)
8295

8396
def visit(value, parent_key=None):
8497
if isinstance(value, dict):
@@ -94,7 +107,7 @@ def visit(value, parent_key=None):
94107
if not isinstance(value, str):
95108
return
96109

97-
if parent_key == "input":
110+
if parent_key in {"input", "patch"}:
98111
paths.update(extract_paths_from_patch(value, repo_root))
99112

100113
if parent_key in {"filePath", "filePaths", "path", "uri", "oldPath", "newPath", "old_path", "new_path"}:
@@ -163,8 +176,8 @@ def is_source_path(path):
163176
return any(path.startswith(prefix) for prefix in SOURCE_PREFIXES)
164177

165178

166-
def mark_edits(payload, repo_root, state):
167-
edited_paths = sorted(extract_paths_from_payload(payload.get("tool_input", {}), repo_root))
179+
def mark_edits(tool_args, repo_root, state):
180+
edited_paths = sorted(extract_paths_from_payload(tool_args, repo_root))
168181
if not edited_paths:
169182
return state
170183

@@ -181,7 +194,14 @@ def mark_edits(payload, repo_root, state):
181194

182195

183196
def payload_mentions_scheme(text, scheme):
184-
return f"-scheme {scheme}" in text or f"scheme {scheme}" in text
197+
return (
198+
re.search(
199+
rf"(?:^|\s)-?scheme\s+{re.escape(scheme)}(?=[\s;&|()<>]|$)",
200+
text,
201+
re.IGNORECASE,
202+
)
203+
is not None
204+
)
185205

186206

187207
def payload_has_success(text):
@@ -209,33 +229,33 @@ def mark_builds(payload, state):
209229

210230

211231
def handle_post_tool_use(payload, repo_root):
212-
tool_name = payload.get("tool_name")
232+
tool_name = payload.get("toolName")
213233
if tool_name not in EDIT_TOOL_NAMES and tool_name not in BUILD_TOOL_NAMES:
214-
emit({"continue": True})
234+
emit({})
215235
return
216236

217237
state_path = state_file(repo_root, payload["sessionId"])
218238
state = load_state(state_path)
219239

220240
if tool_name in EDIT_TOOL_NAMES:
221-
state = mark_edits(payload, repo_root, state)
241+
state = mark_edits(payload.get("toolArgs", {}), repo_root, state)
222242

223243
if tool_name in BUILD_TOOL_NAMES:
224244
state = mark_builds(payload, state)
225245

226246
save_state(state_path, state)
227-
emit({"continue": True})
247+
emit({})
228248

229249

230250
def handle_stop(payload, repo_root):
231251
if payload.get("stop_hook_active"):
232-
emit({"continue": True})
252+
emit({})
233253
return
234254

235255
state = load_state(state_file(repo_root, payload["sessionId"]))
236256
source_revision = state.get("sourceRevision", 0)
237257
if source_revision == 0:
238-
emit({"continue": True})
258+
emit({})
239259
return
240260

241261
missing_schemes = [
@@ -244,7 +264,7 @@ def handle_stop(payload, repo_root):
244264
if validated_revision < source_revision
245265
]
246266
if not missing_schemes:
247-
emit({"continue": True})
267+
emit({})
248268
return
249269

250270
reason = (
@@ -255,46 +275,40 @@ def handle_stop(payload, repo_root):
255275
"mac/TinyClips.xcodeproj -scheme TinyClipsMAS -configuration Debug CODE_SIGN_IDENTITY=\"\" CODE_SIGNING_REQUIRED=NO "
256276
"CODE_SIGNING_ALLOWED=NO."
257277
)
258-
emit(
259-
{
260-
"hookSpecificOutput": {
261-
"hookEventName": "Stop",
262-
"decision": "block",
263-
"reason": reason,
264-
}
265-
}
266-
)
278+
emit({"decision": "block", "reason": reason})
267279

268280

269281
def main():
270282
try:
271283
payload = json.load(sys.stdin)
272284
except json.JSONDecodeError:
273-
emit({"continue": True, "systemMessage": "require_build_validation hook received invalid JSON input."})
285+
warn("require_build_validation hook received invalid JSON input.")
286+
emit({})
274287
return
275288

276289
cwd = payload.get("cwd")
277290
session_id = payload.get("sessionId")
278-
event_name = payload.get("hookEventName")
291+
event_name = sys.argv[1] if len(sys.argv) > 1 else None
279292
if not cwd or not session_id or not event_name:
280-
emit({"continue": True})
293+
emit({})
281294
return
282295

283296
repo_root = Path(cwd).resolve()
284297

285298
try:
286-
if event_name == "PostToolUse":
299+
if event_name == "postToolUse":
287300
handle_post_tool_use(payload, repo_root)
288301
return
289302

290-
if event_name == "Stop":
303+
if event_name == "agentStop":
291304
handle_stop(payload, repo_root)
292305
return
293306
except OSError as error:
294-
emit({"continue": True, "systemMessage": f"require_build_validation hook warning: {error}"})
307+
warn(f"require_build_validation hook warning: {error}")
308+
emit({})
295309
return
296310

297-
emit({"continue": True})
311+
emit({})
298312

299313

300314
if __name__ == "__main__":

‎.github/hooks/require_changelog_update.py‎

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
)
2525
EDIT_TOOL_NAMES = {
2626
"apply_patch",
27+
"create",
2728
"create_file",
29+
"edit",
2830
"multi_replace_string_in_file",
2931
"replace_string_in_file",
32+
"str_replace_editor",
3033
"vscode_renameSymbol",
3134
}
3235
PATCH_FILE_PATTERN = re.compile(
@@ -39,6 +42,10 @@ def emit(payload):
3942
sys.stdout.write(json.dumps(payload))
4043

4144

45+
def warn(message):
46+
sys.stderr.write(f"{message}\n")
47+
48+
4249
def normalize_path(value, repo_root):
4350
if not isinstance(value, str) or not value:
4451
return None
@@ -78,6 +85,8 @@ def extract_paths_from_patch(patch_text, repo_root):
7885

7986
def extract_paths_from_payload(payload, repo_root):
8087
paths = set()
88+
if isinstance(payload, str):
89+
return extract_paths_from_patch(payload, repo_root)
8190

8291
def visit(value, parent_key=None):
8392
if isinstance(value, dict):
@@ -93,7 +102,7 @@ def visit(value, parent_key=None):
93102
if not isinstance(value, str):
94103
return
95104

96-
if parent_key == "input":
105+
if parent_key in {"input", "patch"}:
97106
paths.update(extract_paths_from_patch(value, repo_root))
98107

99108
if parent_key in {"filePath", "filePaths", "path", "uri", "oldPath", "newPath", "old_path", "new_path"}:
@@ -133,26 +142,26 @@ def is_agent_source_path(path):
133142

134143

135144
def handle_post_tool_use(payload, repo_root):
136-
tool_name = payload.get("tool_name")
145+
tool_name = payload.get("toolName")
137146
if tool_name not in EDIT_TOOL_NAMES:
138-
emit({"continue": True})
147+
emit({})
139148
return
140149

141-
edited_paths = sorted(extract_paths_from_payload(payload.get("tool_input", {}), repo_root))
150+
edited_paths = sorted(extract_paths_from_payload(payload.get("toolArgs", {}), repo_root))
142151
if not edited_paths:
143-
emit({"continue": True})
152+
emit({})
144153
return
145154

146155
state_path = state_file(repo_root, payload["sessionId"])
147156
existing = load_state(state_path)
148157
merged_paths = sorted(set(existing.get("editedPaths", [])) | set(edited_paths))
149158
save_state(state_path, {"editedPaths": merged_paths})
150-
emit({"continue": True})
159+
emit({})
151160

152161

153162
def handle_stop(payload, repo_root):
154163
if payload.get("stop_hook_active"):
155-
emit({"continue": True})
164+
emit({})
156165
return
157166

158167
state_path = state_file(repo_root, payload["sessionId"])
@@ -171,15 +180,7 @@ def handle_stop(payload, repo_root):
171180
"Please update CHANGELOG.md with an appropriate entry describing what was added or fixed, "
172181
"then finish."
173182
)
174-
emit(
175-
{
176-
"hookSpecificOutput": {
177-
"hookEventName": "Stop",
178-
"decision": "block",
179-
"reason": reason,
180-
}
181-
}
182-
)
183+
emit({"decision": "block", "reason": reason})
183184
return
184185

185186
if agent_source_paths and not agent_changelog_touched:
@@ -191,49 +192,43 @@ def handle_stop(payload, repo_root):
191192
"Please evaluate whether Agent API coverage is warranted, update agent/CHANGELOG.md "
192193
"for the user-visible Agent CLI/plugin/MCP/contract change, then finish."
193194
)
194-
emit(
195-
{
196-
"hookSpecificOutput": {
197-
"hookEventName": "Stop",
198-
"decision": "block",
199-
"reason": reason,
200-
}
201-
}
202-
)
195+
emit({"decision": "block", "reason": reason})
203196
return
204197

205-
emit({"continue": True})
198+
emit({})
206199

207200

208201
def main():
209202
try:
210203
payload = json.load(sys.stdin)
211204
except json.JSONDecodeError:
212-
emit({"continue": True, "systemMessage": "require_changelog_update hook received invalid JSON input."})
205+
warn("require_changelog_update hook received invalid JSON input.")
206+
emit({})
213207
return
214208

215209
cwd = payload.get("cwd")
216210
session_id = payload.get("sessionId")
217-
event_name = payload.get("hookEventName")
211+
event_name = sys.argv[1] if len(sys.argv) > 1 else None
218212
if not cwd or not session_id or not event_name:
219-
emit({"continue": True})
213+
emit({})
220214
return
221215

222216
repo_root = Path(cwd).resolve()
223217

224218
try:
225-
if event_name == "PostToolUse":
219+
if event_name == "postToolUse":
226220
handle_post_tool_use(payload, repo_root)
227221
return
228222

229-
if event_name == "Stop":
223+
if event_name == "agentStop":
230224
handle_stop(payload, repo_root)
231225
return
232226
except OSError as error:
233-
emit({"continue": True, "systemMessage": f"require_changelog_update hook warning: {error}"})
227+
warn(f"require_changelog_update hook warning: {error}")
228+
emit({})
234229
return
235230

236-
emit({"continue": True})
231+
emit({})
237232

238233

239234
if __name__ == "__main__":

0 commit comments

Comments
 (0)