claude-dev-env 8.109.4 → 8.109.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/skills/pr-lifecycle/reference/review-loop-before-merge.md +2 -0
- package/docs/rule-guides/proof-before-pull-request.md +1 -1
- package/hooks/blocking/merge_review_gate.py +2 -1
- package/hooks/blocking/merge_review_gate_parts/open_request.py +52 -6
- package/hooks/blocking/merge_review_gate_parts/receipts.py +101 -15
- package/hooks/blocking/pull_request_proof.py +98 -14
- package/hooks/blocking/test_merge_review_gate.py +130 -3
- package/hooks/blocking/test_pull_request_proof.py +161 -10
- package/hooks/blocking/tests/test_open_request.py +195 -5
- package/hooks/blocking/tests/test_receipts.py +95 -2
- package/hooks/features/conduct-gates.md +3 -3
- package/hooks/hooks.json +1 -1
- package/hooks/hooks_constants/merge_review_gate_constants.py +24 -5
- package/hooks/hooks_constants/pull_request_proof_constants.py +11 -1
- package/package.json +1 -1
- package/scripts/merge_review_receipt.py +153 -21
- package/scripts/test_merge_review_receipt.py +168 -0
|
@@ -9,4 +9,6 @@ A pull request the agent made merges only after a code review of its head comes
|
|
|
9
9
|
5. Repeat until a pass records `CLEAN`. A pass whose every finding is ruled out records `CLEAN` with `--ruled-out <finding count> --rulings <link to the rulings comment on this pull request>`.
|
|
10
10
|
6. Merge pinned to that head: `expectedHeadSha` on the merge tool, or `--match-head-commit` on `gh pr merge`.
|
|
11
11
|
|
|
12
|
+
A pass run before the pull request opens names `open` in place of the number and the full 40 character head: `python3 ~/.claude/scripts/merge_review_receipt.py <owner>/<name> open <head sha> <finding count>`. A pass whose every finding is ruled out adds `--ruled-out <finding count> --rulings <path:line for each finding, comma separated>`. Run it in the checkout that holds the head. It checks that each `path:line` exists at that head, and each finding needs a different one. A merge pinned to that head passes on an open pass only at zero findings. Rulings for the merge go on the pull request, as in step 5.
|
|
13
|
+
|
|
12
14
|
`hooks/blocking/merge_review_gate.py` denies any other merge. In a merge-queue repository, enqueue with `gh pr merge <number> --repo <owner>/<name> --auto --match-head-commit <head sha>`; auto-merge that names no head is denied. A repository whose own CI and merge queue review and merge its pull requests is listed in `CLAUDE_MERGE_REVIEW_EXEMPT_REPOSITORIES`, comma separated as `owner/name`, and skips the gate.
|
|
@@ -40,6 +40,6 @@ When you cannot prove it, leave the pull request unopened. Tell the owner in thr
|
|
|
40
40
|
|
|
41
41
|
`hooks/blocking/pull_request_proof.py` reads the body of each new pull request before it opens. `pr_lifecycle_skill_gate.py` runs it on `gh pr create`, the `pull_request.py create` script, and each `mcp__*__create_pull_request` tool. The gate denies the call when the body has no "Proof in practice" heading, or when the text under that heading names no command in backticks. The gate also denies a section whose only commands run a test runner, such as `pytest`, `python -m unittest`, `uv run pytest`, `npm test`, `npx vitest`, `jest`, `go test` or `cargo test`. Quotes around a runner word, interpreter flags before `-m`, and `coverage run -m unittest` are still test runs. A command is an inline backticked span or a `$ ` line inside a fenced block. A span that starts with a path and line, such as `gate.py:12`, is not a command. A span that holds one path or file name, such as `src/x.py`, is not a command unless that span runs a test runner, such as `py.test` or `pytest.exe`. A test run piped into `tee`, `tail`, `head`, `grep` or `cat` is still a test run. The deny quotes the line "Unit tests are welcome as extra evidence. The live run is the proof." and asks for a run of the changed thing the way its user runs it, without the change and with it. The gate also denies a section whose prose says part of the change is not proven, not tested or not verified, with "yet", "been" or "fully" allowed after the "not", or says unproven, untested or unverified. The deny quotes that line, and asks for the run before the pull request opens, or for the line under a "Not covered" heading when the change does not target it. The same denial covers a proof line that opens with "not run", "not exercised" or "not executed" (a "yet" or "been" may follow the "not") or says "has not been run". A proof line with a merge word (merge, merged, merges or merging, so post-merge too) is denied with its own reason: run the check on the branch, and put a file or command name that contains merge in backticks. A step that needs the merged code to exist goes under a "Not covered" heading at the same level as the proof heading, as a line that opens with "not run". `UNPROVEN_ADMISSION_PATTERN` and `MERGE_WORD_PATTERN` list the shapes. Words inside backticks or a fenced block are quoted output and do not count. A section with one command that is not a test run, and no such line, passes. A `gh pr create` with no `--body` or `--body-file` is denied with a request for the body. A body edit through `update_pull_request`, `gh pr edit` or the `pull_request.py edit` script gets the same checks on its proof section that a new pull request gets, so an edit cannot swap in a proof that a new pull request would be denied. An edit with no proof section passes. A body the gate cannot read passes, such as an inline multi-line `--body`, a body from standard input, or a body written through `gh api`. Merges and pushes pass.
|
|
42
42
|
|
|
43
|
-
When the checked-out branch changes a file people see (`.html`, `.htm`, `.css`, `.scss`, `.tsx`, `.jsx`, `.vue`, `.svelte`), the gate also denies a body whose proof section holds no picture: a markdown image, an image URL, or an Artifact page link that shows the screenshots. Render the change, read the screenshot, and put it in the section, so the reader sees what you looked at. When git in the session directory cannot list the branch's changed files, the gate asks for the picture as well.
|
|
43
|
+
When the checked-out branch changes a file people see (`.html`, `.htm`, `.css`, `.scss`, `.tsx`, `.jsx`, `.vue`, `.svelte`), the gate also denies a body whose proof section holds no picture: a markdown image, an image URL, or an Artifact page link that shows the screenshots. Render the change, read the screenshot, and put it in the section, so the reader sees what you looked at. A page the branch only moves to another folder or name that ends the same way, with every byte kept (`R100` in `git diff --name-status`), changes nothing people see, so the gate leaves it out; a moved page that also changed, or a file that becomes a page by a change of name ending, still needs the picture. When git in the session directory cannot list the branch's changed files, the gate asks for the picture as well.
|
|
44
44
|
|
|
45
45
|
The gate checks that the section is there, names a command, and admits no unproven part. Whether the run proves the change stays a judgment for the author and the reviewer.
|
|
@@ -38,6 +38,7 @@ if hooks_directory not in sys.path:
|
|
|
38
38
|
from blocking.merge_review_gate_parts.open_request import OpenRequest, open_request_from
|
|
39
39
|
from blocking.merge_review_gate_parts.receipts import (
|
|
40
40
|
has_clean_open_review,
|
|
41
|
+
has_zero_finding_open_review,
|
|
41
42
|
is_clean_receipt,
|
|
42
43
|
receipt_path,
|
|
43
44
|
same_commit,
|
|
@@ -275,7 +276,7 @@ def deny_reason(request: MergeRequest) -> str | None:
|
|
|
275
276
|
if (
|
|
276
277
|
request.pinned_head
|
|
277
278
|
and not pass_on_pinned_head
|
|
278
|
-
and
|
|
279
|
+
and has_zero_finding_open_review(request.repo, request.pinned_head)
|
|
279
280
|
):
|
|
280
281
|
return None
|
|
281
282
|
fields = {"slug": request.slug, "number": request.number}
|
|
@@ -5,6 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
import os
|
|
6
6
|
import subprocess
|
|
7
7
|
import sys
|
|
8
|
+
import tempfile
|
|
8
9
|
from dataclasses import dataclass
|
|
9
10
|
from pathlib import Path
|
|
10
11
|
|
|
@@ -13,6 +14,7 @@ if hooks_directory not in sys.path:
|
|
|
13
14
|
sys.path.insert(0, hooks_directory)
|
|
14
15
|
|
|
15
16
|
from hooks_constants.merge_review_gate_constants import (
|
|
17
|
+
ALL_NON_INTERACTIVE_GIT_SETTINGS,
|
|
16
18
|
ALL_ORIGIN_URL_ARGUMENTS,
|
|
17
19
|
ALL_SHELL_TOOL_NAMES,
|
|
18
20
|
CHANGE_FOLDER_PATTERN,
|
|
@@ -21,13 +23,17 @@ from hooks_constants.merge_review_gate_constants import (
|
|
|
21
23
|
CURRENT_HEAD_REFERENCE,
|
|
22
24
|
CWD_FIELD,
|
|
23
25
|
GH_PR_CREATE_PATTERN,
|
|
26
|
+
GITHUB_REMOTE_URL_TEMPLATE,
|
|
24
27
|
GIT_TIMEOUT_SECONDS,
|
|
25
28
|
HEAD_BRANCH_FIELD,
|
|
26
29
|
HEAD_OPTION_PATTERN,
|
|
30
|
+
LS_REMOTE_FIELD_SEPARATOR,
|
|
27
31
|
ORIGIN_URL_PATTERN,
|
|
28
32
|
OWNER_FIELD,
|
|
29
33
|
PULL_REQUEST_SCRIPT_CREATE_PATTERN,
|
|
30
34
|
REMOTE_BRANCH_PREFIX,
|
|
35
|
+
REMOTE_BRANCH_REFERENCE_TEMPLATE,
|
|
36
|
+
REMOTE_LOOKUP_TIMEOUT_SECONDS,
|
|
31
37
|
REPO_FIELD,
|
|
32
38
|
REPO_OPTION_PATTERN,
|
|
33
39
|
)
|
|
@@ -62,8 +68,11 @@ def open_request_from(all_payload_fields: dict[str, object]) -> OpenRequest | No
|
|
|
62
68
|
gh pr create --repo o/r --head branch
|
|
63
69
|
python pull_request.py create --repo o/r --head branch
|
|
64
70
|
|
|
65
|
-
A
|
|
66
|
-
``
|
|
71
|
+
A tool call reads its head branch from GitHub first. A fork head
|
|
72
|
+
``user:branch`` reads the repository of ``user``. A branch GitHub does not
|
|
73
|
+
list, and every branch of a shell command, resolves in the payload's folder
|
|
74
|
+
as ``origin/<branch>`` first. A ``cd`` target names the folder. No head
|
|
75
|
+
means ``HEAD``; no repository, ``origin``.
|
|
67
76
|
|
|
68
77
|
Args:
|
|
69
78
|
all_payload_fields: The parsed PreToolUse input.
|
|
@@ -86,12 +95,15 @@ def open_request_from(all_payload_fields: dict[str, object]) -> OpenRequest | No
|
|
|
86
95
|
|
|
87
96
|
|
|
88
97
|
def _tool_open_request(all_tool_input: dict[object, object], folder: str | None) -> OpenRequest:
|
|
98
|
+
owner = str(all_tool_input.get(OWNER_FIELD, ""))
|
|
99
|
+
repo = str(all_tool_input.get(REPO_FIELD, ""))
|
|
89
100
|
branch = all_tool_input.get(HEAD_BRANCH_FIELD)
|
|
90
|
-
|
|
91
|
-
owner
|
|
92
|
-
|
|
93
|
-
|
|
101
|
+
head = (
|
|
102
|
+
_pushed_commit(owner, repo, branch) or _branch_commit(branch, folder)
|
|
103
|
+
if isinstance(branch, str) and branch
|
|
104
|
+
else None
|
|
94
105
|
)
|
|
106
|
+
return OpenRequest(owner=owner, repo=repo, head=head)
|
|
95
107
|
|
|
96
108
|
|
|
97
109
|
def _shell_open_request(command: str, folder: str | None) -> OpenRequest | None:
|
|
@@ -155,6 +167,40 @@ def _branch_commit(branch: str, folder: str | None) -> str | None:
|
|
|
155
167
|
return None
|
|
156
168
|
|
|
157
169
|
|
|
170
|
+
def _pushed_commit(owner: str, repo: str, branch: str) -> str | None:
|
|
171
|
+
head_owner, separator, head_branch = branch.partition(":")
|
|
172
|
+
if not separator:
|
|
173
|
+
head_owner, head_branch = owner, branch
|
|
174
|
+
if not head_owner or not repo or not head_branch:
|
|
175
|
+
return None
|
|
176
|
+
reference = REMOTE_BRANCH_REFERENCE_TEMPLATE.format(branch=head_branch)
|
|
177
|
+
remote_url = GITHUB_REMOTE_URL_TEMPLATE.format(owner=head_owner, repo=repo)
|
|
178
|
+
with tempfile.TemporaryDirectory() as empty_folder, tempfile.TemporaryFile() as listing:
|
|
179
|
+
try:
|
|
180
|
+
subprocess.run(
|
|
181
|
+
["git", "ls-remote", remote_url, reference],
|
|
182
|
+
cwd=empty_folder,
|
|
183
|
+
stdin=subprocess.DEVNULL,
|
|
184
|
+
stdout=listing,
|
|
185
|
+
stderr=subprocess.DEVNULL,
|
|
186
|
+
timeout=REMOTE_LOOKUP_TIMEOUT_SECONDS,
|
|
187
|
+
check=False,
|
|
188
|
+
env={**os.environ, **ALL_NON_INTERACTIVE_GIT_SETTINGS},
|
|
189
|
+
)
|
|
190
|
+
except (OSError, subprocess.SubprocessError):
|
|
191
|
+
return None
|
|
192
|
+
listing.seek(0)
|
|
193
|
+
return _listed_commit(listing.read().decode("utf-8", "replace"), reference)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _listed_commit(listing: str, reference: str) -> str | None:
|
|
197
|
+
for each_line in listing.splitlines():
|
|
198
|
+
commit, separator, listed_reference = each_line.partition(LS_REMOTE_FIELD_SEPARATOR)
|
|
199
|
+
if separator and listed_reference == reference:
|
|
200
|
+
return commit
|
|
201
|
+
return None
|
|
202
|
+
|
|
203
|
+
|
|
158
204
|
def _origin_repository(folder: str | None) -> tuple[str, str]:
|
|
159
205
|
origin_url = _run_git(list(ALL_ORIGIN_URL_ARGUMENTS), folder)
|
|
160
206
|
origin_match = ORIGIN_URL_PATTERN.search(origin_url) if origin_url else None
|
|
@@ -9,6 +9,7 @@ from __future__ import annotations
|
|
|
9
9
|
import json
|
|
10
10
|
import os
|
|
11
11
|
import sys
|
|
12
|
+
from collections.abc import Callable
|
|
12
13
|
from pathlib import Path
|
|
13
14
|
|
|
14
15
|
hooks_directory = str(Path(__file__).resolve().parent.parent.parent)
|
|
@@ -27,7 +28,9 @@ from hooks_constants.merge_review_gate_constants import (
|
|
|
27
28
|
RECEIPT_FINDINGS_KEY,
|
|
28
29
|
RECEIPT_HEAD_KEY,
|
|
29
30
|
RECEIPT_RULED_OUT_KEY,
|
|
31
|
+
RECEIPT_RULING_LINES_KEY,
|
|
30
32
|
RECEIPT_RULINGS_KEY,
|
|
33
|
+
RULING_REFERENCE_PATTERN,
|
|
31
34
|
RULINGS_LINK_PATTERN,
|
|
32
35
|
)
|
|
33
36
|
|
|
@@ -81,32 +84,71 @@ def open_receipt_path(owner: str, repo: str, head: str) -> Path:
|
|
|
81
84
|
|
|
82
85
|
|
|
83
86
|
def has_clean_open_review(repo: str, head: str) -> bool:
|
|
84
|
-
"""Return whether a pass recorded before the pull request opened
|
|
87
|
+
"""Return whether a pass recorded before the pull request opened lets it open at a head.
|
|
85
88
|
|
|
86
|
-
The
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
89
|
+
The pass is clean when it found zero findings, or when each finding has a
|
|
90
|
+
``path:line`` ruling. The head may be a full or an abbreviated commit,
|
|
91
|
+
matched against each recorded head by prefix. The owner is left out of the
|
|
92
|
+
match, since a checkout whose origin still names a transferred
|
|
93
|
+
repository's old owner opens against the new one::
|
|
90
94
|
|
|
91
95
|
open receipt {head: abc1234..., findings: 0}, head abc1234 -> True
|
|
96
|
+
open receipt {head: abc1234..., findings: 2, ruled out 2,
|
|
97
|
+
ruling_lines [a.py:1, b.py:20]}, head abc1234 -> True
|
|
92
98
|
open receipt {head: abc1234..., findings: 2}, head abc1234 -> False
|
|
93
99
|
no open receipt for the repository -> False
|
|
94
100
|
|
|
95
101
|
Args:
|
|
96
102
|
repo: Repository name.
|
|
97
|
-
head: The commit the open
|
|
103
|
+
head: The commit the open names.
|
|
98
104
|
|
|
99
105
|
Returns:
|
|
100
106
|
True when a clean open receipt covers that head.
|
|
101
107
|
"""
|
|
108
|
+
return _has_open_review(repo, head, is_clean_open_pass)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def has_zero_finding_open_review(repo: str, head: str) -> bool:
|
|
112
|
+
"""Return whether a pass recorded before the pull request opened found zero findings at a head.
|
|
113
|
+
|
|
114
|
+
A merge accepts the open pass only at zero findings. Ruled-out findings
|
|
115
|
+
need the rulings comment a numbered pass links on the pull request::
|
|
116
|
+
|
|
117
|
+
open receipt {head: abc1234..., findings: 0}, head abc1234 -> True
|
|
118
|
+
open receipt {head: abc1234..., findings: 2, ruled out 2,
|
|
119
|
+
ruling_lines [a.py:1, b.py:20]}, head abc1234 -> False
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
repo: Repository name.
|
|
123
|
+
head: The commit the merge pins.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
True when an open receipt with zero findings covers that head.
|
|
127
|
+
"""
|
|
128
|
+
return _has_open_review(repo, head, _reports_no_findings)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _has_open_review(
|
|
132
|
+
repo: str, head: str, is_acceptable_pass: Callable[[dict[str, object]], bool]
|
|
133
|
+
) -> bool:
|
|
102
134
|
if not OPEN_HEAD_PATTERN.fullmatch(head):
|
|
103
135
|
return False
|
|
104
136
|
folder = receipt_directory() / OPEN_RECEIPT_DIRECTORY_NAME
|
|
105
137
|
pattern = OPEN_RECEIPT_GLOB_TEMPLATE.format(repo=repo.casefold())
|
|
106
|
-
return any(
|
|
138
|
+
return any(
|
|
139
|
+
_is_open_receipt_for(each_path, head, is_acceptable_pass)
|
|
140
|
+
for each_path in folder.glob(pattern)
|
|
141
|
+
)
|
|
107
142
|
|
|
108
143
|
|
|
109
|
-
def
|
|
144
|
+
def _reports_no_findings(all_receipt_fields: dict[str, object]) -> bool:
|
|
145
|
+
finding_count = all_receipt_fields.get(RECEIPT_FINDINGS_KEY)
|
|
146
|
+
return _is_count(finding_count) and finding_count == 0
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _is_open_receipt_for(
|
|
150
|
+
path: Path, head: str, is_acceptable_pass: Callable[[dict[str, object]], bool]
|
|
151
|
+
) -> bool:
|
|
110
152
|
try:
|
|
111
153
|
all_receipt_fields = json.loads(path.read_text(encoding="utf-8"))
|
|
112
154
|
except (OSError, ValueError):
|
|
@@ -118,8 +160,50 @@ def _is_clean_open_receipt(path: Path, head: str) -> bool:
|
|
|
118
160
|
isinstance(recorded_head, str)
|
|
119
161
|
and OPEN_RECEIPT_HEAD_PATTERN.fullmatch(recorded_head) is not None
|
|
120
162
|
and same_commit(recorded_head, head)
|
|
121
|
-
and
|
|
122
|
-
|
|
163
|
+
and is_acceptable_pass(all_receipt_fields)
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def is_clean_open_pass(all_receipt_fields: dict[str, object]) -> bool:
|
|
168
|
+
"""Return whether a pass recorded before the pull request opened lets it open.
|
|
169
|
+
|
|
170
|
+
The pull request has no number yet, so no rulings comment exists to link.
|
|
171
|
+
A pass is clean when it reported zero findings, or when each finding it
|
|
172
|
+
reported has its own ``path:line`` ruling beside it::
|
|
173
|
+
|
|
174
|
+
findings 0 -> clean
|
|
175
|
+
findings 2, ruled out 2, ruling_lines [a.py:1, src/b.py:20] -> clean
|
|
176
|
+
findings 2, ruled out 2, ruling_lines [a.py:1] -> not clean
|
|
177
|
+
findings 2, ruled out 2, ruling_lines [a.py:1, a.py:1] -> not clean
|
|
178
|
+
findings 2, ruled out 2, ruling_lines [a.py:1, see the thread] -> not clean
|
|
179
|
+
|
|
180
|
+
Args:
|
|
181
|
+
all_receipt_fields: The parsed receipt of one review pass.
|
|
182
|
+
|
|
183
|
+
Returns:
|
|
184
|
+
True when the pass counts as clean.
|
|
185
|
+
"""
|
|
186
|
+
finding_count = all_receipt_fields.get(RECEIPT_FINDINGS_KEY)
|
|
187
|
+
return _reports_no_findings(all_receipt_fields) or (
|
|
188
|
+
_is_count(finding_count)
|
|
189
|
+
and _rules_out_every_finding_at_file_and_line(all_receipt_fields, finding_count)
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _rules_out_every_finding_at_file_and_line(
|
|
194
|
+
all_receipt_fields: dict[str, object], finding_count: object
|
|
195
|
+
) -> bool:
|
|
196
|
+
all_ruling_lines = all_receipt_fields.get(RECEIPT_RULING_LINES_KEY)
|
|
197
|
+
return (
|
|
198
|
+
_ruled_out_count_matches(all_receipt_fields, finding_count)
|
|
199
|
+
and isinstance(all_ruling_lines, list)
|
|
200
|
+
and len(all_ruling_lines) == finding_count
|
|
201
|
+
and len(set(map(str, all_ruling_lines))) == finding_count
|
|
202
|
+
and all(
|
|
203
|
+
isinstance(each_ruling, str)
|
|
204
|
+
and RULING_REFERENCE_PATTERN.fullmatch(each_ruling) is not None
|
|
205
|
+
for each_ruling in all_ruling_lines
|
|
206
|
+
)
|
|
123
207
|
)
|
|
124
208
|
|
|
125
209
|
|
|
@@ -174,14 +258,16 @@ def _is_count(candidate: object) -> bool:
|
|
|
174
258
|
def _rules_out_every_finding(
|
|
175
259
|
all_receipt_fields: dict[str, object], finding_count: object, owner: str, repo: str, number: int
|
|
176
260
|
) -> bool:
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
_is_count(ruled_out_count)
|
|
180
|
-
and ruled_out_count == finding_count
|
|
181
|
-
and _links_rulings_on(all_receipt_fields.get(RECEIPT_RULINGS_KEY), owner, repo, number)
|
|
261
|
+
return _ruled_out_count_matches(all_receipt_fields, finding_count) and _links_rulings_on(
|
|
262
|
+
all_receipt_fields.get(RECEIPT_RULINGS_KEY), owner, repo, number
|
|
182
263
|
)
|
|
183
264
|
|
|
184
265
|
|
|
266
|
+
def _ruled_out_count_matches(all_receipt_fields: dict[str, object], finding_count: object) -> bool:
|
|
267
|
+
ruled_out_count = all_receipt_fields.get(RECEIPT_RULED_OUT_KEY)
|
|
268
|
+
return _is_count(ruled_out_count) and ruled_out_count == finding_count
|
|
269
|
+
|
|
270
|
+
|
|
185
271
|
def _links_rulings_on(rulings: object, owner: str, repo: str, number: int) -> bool:
|
|
186
272
|
rulings_match = RULINGS_LINK_PATTERN.fullmatch(rulings) if isinstance(rulings, str) else None
|
|
187
273
|
return (
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
import json
|
|
5
6
|
import re
|
|
6
7
|
import subprocess
|
|
7
8
|
import sys
|
|
@@ -31,7 +32,7 @@ from hooks_constants.pull_request_proof_constants import (
|
|
|
31
32
|
BASE_FLAG_ASSIGNMENT_PREFIX,
|
|
32
33
|
COMPARE_ENDPOINT_TEMPLATE,
|
|
33
34
|
COMPARE_FILE_LIST_LIMIT,
|
|
34
|
-
|
|
35
|
+
COMPARE_FILE_STATUS_QUERY,
|
|
35
36
|
DEFAULT_BRANCH_QUERY,
|
|
36
37
|
GH_TIMEOUT_SECONDS,
|
|
37
38
|
HEAD_FLAG_ASSIGNMENT_PREFIX,
|
|
@@ -51,6 +52,12 @@ from hooks_constants.pull_request_proof_constants import (
|
|
|
51
52
|
MISSING_PROOF_REASON,
|
|
52
53
|
NEXT_HEADING_PATTERN,
|
|
53
54
|
PROOF_HEADING_PATTERN,
|
|
55
|
+
PURE_RENAME_STATUS,
|
|
56
|
+
RAW_HEADER_FIELD_COUNT,
|
|
57
|
+
RAW_RECORD_PREFIX,
|
|
58
|
+
RAW_RENAME_PATH_COUNT,
|
|
59
|
+
RAW_RENAME_STATUS_LETTER,
|
|
60
|
+
ALL_RAW_TWO_PATH_STATUS_LETTERS,
|
|
54
61
|
PROOF_IMAGE_PATTERN,
|
|
55
62
|
PULL_REQUEST_SCRIPT_CREATE_WORD,
|
|
56
63
|
PULL_REQUEST_SCRIPT_EDIT_WORD,
|
|
@@ -626,18 +633,31 @@ def missing_existing_work_reason(all_payload_fields: Mapping[str, object]) -> st
|
|
|
626
633
|
return None
|
|
627
634
|
|
|
628
635
|
|
|
629
|
-
|
|
636
|
+
class ChangedFile(NamedTuple):
|
|
637
|
+
path: str
|
|
638
|
+
old_path: str
|
|
639
|
+
is_pure_rename: bool
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
def _git_output(working_directory: str, *all_git_arguments: str) -> str | None:
|
|
630
643
|
try:
|
|
631
644
|
completed = subprocess.run(
|
|
632
645
|
["git", "-C", working_directory, *all_git_arguments],
|
|
633
646
|
capture_output=True,
|
|
634
647
|
text=True,
|
|
648
|
+
encoding="utf-8",
|
|
649
|
+
errors="replace",
|
|
635
650
|
timeout=GIT_TIMEOUT_SECONDS,
|
|
636
651
|
check=False,
|
|
637
652
|
)
|
|
638
653
|
except (OSError, subprocess.SubprocessError):
|
|
639
654
|
return None
|
|
640
|
-
return completed.stdout
|
|
655
|
+
return completed.stdout if completed.returncode == 0 else None
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
def _git_lines(working_directory: str, *all_git_arguments: str) -> list[str] | None:
|
|
659
|
+
output = _git_output(working_directory, *all_git_arguments)
|
|
660
|
+
return output.splitlines() if output is not None else None
|
|
641
661
|
|
|
642
662
|
|
|
643
663
|
def _first_merge_base(working_directory: str, head_reference: str) -> str | None:
|
|
@@ -667,9 +687,16 @@ def visible_changed_files(working_directory: str, head_branch: object) -> list[s
|
|
|
667
687
|
origin/HEAD, origin/main and main that git can reach::
|
|
668
688
|
|
|
669
689
|
checkout on feature, page.html changed -> ["page.html"]
|
|
690
|
+
page.html renamed with no edit -> []
|
|
691
|
+
page.html renamed and edited -> ["page.html"]
|
|
670
692
|
named head exists nowhere in the clone -> None
|
|
671
693
|
no base reference reachable -> None
|
|
672
694
|
|
|
695
|
+
A rename whose old and new blob ids match and whose old and new names end the
|
|
696
|
+
same way, such as a.html to b/a.html, changes nothing people see. One that
|
|
697
|
+
changes the ending, such as notes.txt or Widget.tsx to page.html, still makes a
|
|
698
|
+
page. A copy or a rename with an edit keeps its path.
|
|
699
|
+
|
|
673
700
|
Args:
|
|
674
701
|
working_directory: The session directory the hook payload names.
|
|
675
702
|
head_branch: The head branch a create call names, or None.
|
|
@@ -682,14 +709,64 @@ def visible_changed_files(working_directory: str, head_branch: object) -> list[s
|
|
|
682
709
|
merge_base = _first_merge_base(working_directory, head_reference)
|
|
683
710
|
if merge_base is None:
|
|
684
711
|
return None
|
|
685
|
-
|
|
686
|
-
if
|
|
712
|
+
raw_output = _git_output(working_directory, "diff", "--raw", "--no-abbrev", "-M", "-z", merge_base, head_reference)
|
|
713
|
+
if raw_output is None:
|
|
714
|
+
return None
|
|
715
|
+
all_changes = _git_changes(raw_output.split("\0"))
|
|
716
|
+
if all_changes is None:
|
|
717
|
+
return None
|
|
718
|
+
return _visible_paths(all_changes)
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
def _git_changes(all_raw_fields: list[str]) -> list[ChangedFile] | None:
|
|
722
|
+
all_changes = []
|
|
723
|
+
field_index = 0
|
|
724
|
+
while field_index < len(all_raw_fields) and all_raw_fields[field_index].startswith(RAW_RECORD_PREFIX):
|
|
725
|
+
all_header_fields = all_raw_fields[field_index][1:].split(" ")
|
|
726
|
+
if len(all_header_fields) != RAW_HEADER_FIELD_COUNT:
|
|
727
|
+
return None
|
|
728
|
+
old_mode, new_mode, old_blob, new_blob, status = all_header_fields
|
|
729
|
+
path_count = RAW_RENAME_PATH_COUNT if status[:1] in ALL_RAW_TWO_PATH_STATUS_LETTERS else 1
|
|
730
|
+
all_paths = all_raw_fields[field_index + 1 : field_index + 1 + path_count]
|
|
731
|
+
if len(all_paths) != path_count:
|
|
732
|
+
return None
|
|
733
|
+
is_pure_rename = status.startswith(RAW_RENAME_STATUS_LETTER) and old_mode == new_mode and old_blob == new_blob
|
|
734
|
+
all_changes.append(ChangedFile(path=all_paths[-1], old_path=all_paths[0], is_pure_rename=is_pure_rename))
|
|
735
|
+
field_index += 1 + path_count
|
|
736
|
+
if any(all_raw_fields[field_index:]):
|
|
687
737
|
return None
|
|
688
|
-
return
|
|
738
|
+
return all_changes
|
|
689
739
|
|
|
690
740
|
|
|
691
|
-
def
|
|
692
|
-
|
|
741
|
+
def _compare_changes(all_json_lines: list[str]) -> list[ChangedFile] | None:
|
|
742
|
+
all_changes = []
|
|
743
|
+
for each_line in all_json_lines:
|
|
744
|
+
try:
|
|
745
|
+
status, old_path, path = json.loads(each_line)
|
|
746
|
+
except (ValueError, TypeError):
|
|
747
|
+
return None
|
|
748
|
+
all_changes.append(ChangedFile(path=path, old_path=old_path, is_pure_rename=status == PURE_RENAME_STATUS))
|
|
749
|
+
return all_changes
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
def _visible_suffix(path: str) -> str | None:
|
|
753
|
+
lowered_path = path.lower()
|
|
754
|
+
for each_suffix in ALL_VISIBLE_FILE_SUFFIXES:
|
|
755
|
+
if lowered_path.endswith(each_suffix):
|
|
756
|
+
return each_suffix
|
|
757
|
+
return None
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
def _is_unchanged_move(change: ChangedFile) -> bool:
|
|
761
|
+
return change.is_pure_rename and _visible_suffix(change.old_path) == _visible_suffix(change.path)
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
def _visible_paths(all_changes: list[ChangedFile]) -> list[str]:
|
|
765
|
+
return [
|
|
766
|
+
each_change.path
|
|
767
|
+
for each_change in all_changes
|
|
768
|
+
if _visible_suffix(each_change.path) is not None and not _is_unchanged_move(each_change)
|
|
769
|
+
]
|
|
693
770
|
|
|
694
771
|
|
|
695
772
|
def _gh_api_lines(endpoint: str, query: str) -> list[str] | None:
|
|
@@ -698,6 +775,8 @@ def _gh_api_lines(endpoint: str, query: str) -> list[str] | None:
|
|
|
698
775
|
["gh", "api", endpoint, "--jq", query],
|
|
699
776
|
capture_output=True,
|
|
700
777
|
text=True,
|
|
778
|
+
encoding="utf-8",
|
|
779
|
+
errors="replace",
|
|
701
780
|
timeout=GH_TIMEOUT_SECONDS,
|
|
702
781
|
check=False,
|
|
703
782
|
)
|
|
@@ -718,16 +797,19 @@ def _compare_base_branch(repository: str, base_branch: str | None) -> str | None
|
|
|
718
797
|
|
|
719
798
|
|
|
720
799
|
def _visible_compare_paths(repository: str, base_branch: str, head_branch: str) -> list[str] | None:
|
|
721
|
-
|
|
800
|
+
all_status_lines = _gh_api_lines(
|
|
722
801
|
COMPARE_ENDPOINT_TEMPLATE.format(
|
|
723
802
|
repository=repository, base_branch=base_branch, head_branch=head_branch
|
|
724
803
|
),
|
|
725
|
-
|
|
804
|
+
COMPARE_FILE_STATUS_QUERY,
|
|
726
805
|
)
|
|
727
|
-
if
|
|
806
|
+
if all_status_lines is None:
|
|
807
|
+
return None
|
|
808
|
+
all_changes = _compare_changes(all_status_lines)
|
|
809
|
+
if all_changes is None:
|
|
728
810
|
return None
|
|
729
|
-
all_visible_paths = _visible_paths(
|
|
730
|
-
if all_visible_paths or len(
|
|
811
|
+
all_visible_paths = _visible_paths(all_changes)
|
|
812
|
+
if all_visible_paths or len(all_status_lines) < COMPARE_FILE_LIST_LIMIT:
|
|
731
813
|
return all_visible_paths
|
|
732
814
|
return None
|
|
733
815
|
|
|
@@ -741,13 +823,15 @@ def remote_visible_changed_files(
|
|
|
741
823
|
|
|
742
824
|
compare lists tool.py -> []
|
|
743
825
|
compare lists web/page.html -> ["web/page.html"]
|
|
826
|
+
compare lists web/page.html renamed, no edit -> []
|
|
744
827
|
compare returns 300 paths and none are visible -> None
|
|
745
828
|
repository or head missing -> None
|
|
746
829
|
|
|
747
830
|
The base is the named base branch, else the repository's default branch.
|
|
748
831
|
A page of 300 paths is the compare API's file cap. A visible file on that
|
|
749
832
|
page is returned. A full page with none is None, because a later file can
|
|
750
|
-
still be one people see.
|
|
833
|
+
still be one people see. A pure rename counts toward the 300. It is not
|
|
834
|
+
visible when the file it moved from ended the same way.
|
|
751
835
|
|
|
752
836
|
Args:
|
|
753
837
|
repository: The owner/name the create call names, or None.
|