claude-dev-env 8.119.0 → 8.119.2
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/bin/git_hooks_installer.mjs +26 -2
- package/bin/git_hooks_installer.test.mjs +36 -0
- package/bin/install-transaction.mjs +3 -1
- package/bin/install.mjs +12 -8
- package/bin/install.test.mjs +89 -0
- package/bin/install.transaction.test.mjs +34 -0
- package/bin/merge_profile_settings.mjs +3 -2
- package/bin/merge_profile_settings.test.mjs +25 -1
- package/bin/profile-stop-gate.mjs +2 -1
- package/bin/profile-stop-gate.test.mjs +26 -1
- package/bin/write-file-atomically.mjs +148 -0
- package/bin/write-file-atomically.test.mjs +263 -0
- package/hooks/blocking/followup_pr_dedupe.py +71 -13
- package/hooks/blocking/merge_review_gate.py +4 -0
- package/hooks/blocking/merge_review_gate_parts/create_folders.py +6 -2
- package/hooks/blocking/merge_review_gate_parts/open_request.py +40 -15
- package/hooks/blocking/resolve_thread_reply_gate.py +116 -1
- package/hooks/blocking/test_cloud_graphql_gate.py +4 -0
- package/hooks/blocking/test_followup_pr_dedupe.py +99 -0
- package/hooks/blocking/test_gh_global_account_switch_gate.py +4 -0
- package/hooks/blocking/test_headless_claude_broker_gate.py +4 -0
- package/hooks/blocking/test_merge_review_gate.py +133 -0
- package/hooks/blocking/test_python_windows_invocation_gate.py +4 -0
- package/hooks/blocking/test_resolve_thread_reply_gate.py +430 -0
- package/hooks/blocking/tests/test_create_folders.py +7 -0
- package/hooks/blocking/tests/test_open_request.py +21 -0
- package/hooks/features/conduct-gates.md +3 -3
- package/hooks/hooks_constants/followup_pr_dedupe_constants.py +5 -0
- package/hooks/hooks_constants/merge_review_gate_constants.py +5 -0
- package/hooks/hooks_constants/pytest_invocation.py +24 -9
- package/hooks/hooks_constants/resolve_thread_reply_gate_constants.py +21 -1
- package/hooks/hooks_constants/test_pytest_invocation.py +25 -0
- package/hooks/hooks_constants/test_shell_command_mutation.py +11 -0
- package/hooks/hooks_constants/transcript_skill_scan_constants.py +22 -0
- package/hooks/session/test_npx_pele_guidance.py +4 -0
- package/hooks/test_transcript_skill_scan.py +72 -0
- package/hooks/transcript_skill_scan.py +138 -4
- package/hooks/validators/config/security_checks_constants.py +88 -0
- package/hooks/validators/security_checks.py +186 -1
- package/hooks/validators/test_security_checks.py +149 -0
- package/package.json +1 -1
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import { strict as assert } from 'node:assert';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import {
|
|
5
|
+
chmodSync,
|
|
6
|
+
lstatSync,
|
|
7
|
+
mkdirSync,
|
|
8
|
+
mkdtempSync,
|
|
9
|
+
readFileSync,
|
|
10
|
+
readdirSync,
|
|
11
|
+
readlinkSync,
|
|
12
|
+
rmSync,
|
|
13
|
+
statSync,
|
|
14
|
+
symlinkSync,
|
|
15
|
+
utimesSync,
|
|
16
|
+
writeFileSync,
|
|
17
|
+
} from 'node:fs';
|
|
18
|
+
import { tmpdir } from 'node:os';
|
|
19
|
+
import { join } from 'node:path';
|
|
20
|
+
|
|
21
|
+
import { writeFileAtomically } from './write-file-atomically.mjs';
|
|
22
|
+
|
|
23
|
+
const FILE_SIZE_LIMIT_COMMAND = 'prlimit';
|
|
24
|
+
const isFileSizeLimitAvailable = spawnSync(FILE_SIZE_LIMIT_COMMAND, ['--version']).status === 0;
|
|
25
|
+
const isPosixPlatform = process.platform !== 'win32';
|
|
26
|
+
const PRIOR_SETTINGS_TEXT = '{\n "prior": true\n}\n';
|
|
27
|
+
const NEW_SETTINGS_TEXT = `{\n "next": "${'x'.repeat(4096)}"\n}\n`;
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
function withSandbox(runInSandbox) {
|
|
31
|
+
const sandboxRoot = mkdtempSync(join(tmpdir(), 'cdev-atomic-write-'));
|
|
32
|
+
try {
|
|
33
|
+
runInSandbox(sandboxRoot);
|
|
34
|
+
} finally {
|
|
35
|
+
rmSync(sandboxRoot, { recursive: true, force: true });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
test('writeFileAtomically should replace an existing file with the complete new text and leave no temporary file', () => {
|
|
41
|
+
withSandbox((sandboxRoot) => {
|
|
42
|
+
const settingsPath = join(sandboxRoot, 'settings.json');
|
|
43
|
+
writeFileSync(settingsPath, PRIOR_SETTINGS_TEXT);
|
|
44
|
+
|
|
45
|
+
writeFileAtomically(settingsPath, NEW_SETTINGS_TEXT);
|
|
46
|
+
|
|
47
|
+
assert.equal(readFileSync(settingsPath, 'utf8'), NEW_SETTINGS_TEXT);
|
|
48
|
+
assert.deepEqual(readdirSync(sandboxRoot), ['settings.json']);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
test('writeFileAtomically should create a file that does not exist yet', () => {
|
|
54
|
+
withSandbox((sandboxRoot) => {
|
|
55
|
+
const settingsPath = join(sandboxRoot, 'settings.json');
|
|
56
|
+
|
|
57
|
+
writeFileAtomically(settingsPath, NEW_SETTINGS_TEXT);
|
|
58
|
+
|
|
59
|
+
assert.equal(readFileSync(settingsPath, 'utf8'), NEW_SETTINGS_TEXT);
|
|
60
|
+
assert.deepEqual(readdirSync(sandboxRoot), ['settings.json']);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
test(
|
|
66
|
+
'writeFileAtomically should keep the mode of the file it replaces',
|
|
67
|
+
{ skip: isPosixPlatform ? false : 'Windows keeps no POSIX mode bits' },
|
|
68
|
+
() => {
|
|
69
|
+
withSandbox((sandboxRoot) => {
|
|
70
|
+
const settingsPath = join(sandboxRoot, 'settings.json');
|
|
71
|
+
writeFileSync(settingsPath, PRIOR_SETTINGS_TEXT);
|
|
72
|
+
chmodSync(settingsPath, 0o600);
|
|
73
|
+
|
|
74
|
+
writeFileAtomically(settingsPath, NEW_SETTINGS_TEXT);
|
|
75
|
+
|
|
76
|
+
assert.equal(statSync(settingsPath).mode & 0o777, 0o600);
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
test(
|
|
83
|
+
'writeFileAtomically should write through a linked settings.json and keep the link',
|
|
84
|
+
{ skip: isPosixPlatform ? false : 'creating a file link on Windows needs a privilege the runner may lack' },
|
|
85
|
+
() => {
|
|
86
|
+
withSandbox((sandboxRoot) => {
|
|
87
|
+
const sharedDirectory = join(sandboxRoot, 'shared');
|
|
88
|
+
const profileDirectory = join(sandboxRoot, 'profile');
|
|
89
|
+
mkdirSync(sharedDirectory);
|
|
90
|
+
mkdirSync(profileDirectory);
|
|
91
|
+
const sharedSettingsPath = join(sharedDirectory, 'settings.json');
|
|
92
|
+
const linkedSettingsPath = join(profileDirectory, 'settings.json');
|
|
93
|
+
writeFileSync(sharedSettingsPath, PRIOR_SETTINGS_TEXT);
|
|
94
|
+
symlinkSync(sharedSettingsPath, linkedSettingsPath);
|
|
95
|
+
|
|
96
|
+
writeFileAtomically(linkedSettingsPath, NEW_SETTINGS_TEXT);
|
|
97
|
+
|
|
98
|
+
assert.equal(lstatSync(linkedSettingsPath).isSymbolicLink(), true, 'the profile entry is still a link');
|
|
99
|
+
assert.equal(readlinkSync(linkedSettingsPath), sharedSettingsPath);
|
|
100
|
+
assert.equal(readFileSync(sharedSettingsPath, 'utf8'), NEW_SETTINGS_TEXT);
|
|
101
|
+
assert.deepEqual(readdirSync(sharedDirectory), ['settings.json']);
|
|
102
|
+
assert.deepEqual(readdirSync(profileDirectory), ['settings.json']);
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
test(
|
|
109
|
+
'writeFileAtomically should create the missing file at the end of a chain of links and keep every link',
|
|
110
|
+
{ skip: isPosixPlatform ? false : 'creating a file link on Windows needs a privilege the runner may lack' },
|
|
111
|
+
() => {
|
|
112
|
+
withSandbox((sandboxRoot) => {
|
|
113
|
+
const profileDirectory = join(sandboxRoot, 'profile');
|
|
114
|
+
const dotfilesDirectory = join(sandboxRoot, 'dotfiles');
|
|
115
|
+
const storeDirectory = join(sandboxRoot, 'store');
|
|
116
|
+
mkdirSync(profileDirectory);
|
|
117
|
+
mkdirSync(dotfilesDirectory);
|
|
118
|
+
mkdirSync(storeDirectory);
|
|
119
|
+
const profileSettingsPath = join(profileDirectory, 'settings.json');
|
|
120
|
+
const dotfilesSettingsPath = join(dotfilesDirectory, 'settings.json');
|
|
121
|
+
const storedSettingsPath = join(storeDirectory, 'settings.json');
|
|
122
|
+
symlinkSync(dotfilesSettingsPath, profileSettingsPath);
|
|
123
|
+
symlinkSync(join('..', 'store', 'settings.json'), dotfilesSettingsPath);
|
|
124
|
+
|
|
125
|
+
writeFileAtomically(profileSettingsPath, NEW_SETTINGS_TEXT);
|
|
126
|
+
|
|
127
|
+
assert.equal(lstatSync(profileSettingsPath).isSymbolicLink(), true, 'the profile entry is still a link');
|
|
128
|
+
assert.equal(lstatSync(dotfilesSettingsPath).isSymbolicLink(), true, 'the middle entry is still a link');
|
|
129
|
+
assert.equal(readFileSync(storedSettingsPath, 'utf8'), NEW_SETTINGS_TEXT);
|
|
130
|
+
assert.deepEqual(readdirSync(storeDirectory), ['settings.json']);
|
|
131
|
+
assert.deepEqual(readdirSync(dotfilesDirectory), ['settings.json']);
|
|
132
|
+
});
|
|
133
|
+
},
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
test(
|
|
138
|
+
'writeFileAtomically should refuse a link that loops back to itself and leave the link in place',
|
|
139
|
+
{ skip: isPosixPlatform ? false : 'creating a file link on Windows needs a privilege the runner may lack' },
|
|
140
|
+
() => {
|
|
141
|
+
withSandbox((sandboxRoot) => {
|
|
142
|
+
const settingsPath = join(sandboxRoot, 'settings.json');
|
|
143
|
+
symlinkSync('settings.json', settingsPath);
|
|
144
|
+
|
|
145
|
+
assert.throws(() => writeFileAtomically(settingsPath, NEW_SETTINGS_TEXT), { code: 'ELOOP' });
|
|
146
|
+
|
|
147
|
+
assert.equal(lstatSync(settingsPath).isSymbolicLink(), true, 'the looping entry is still a link');
|
|
148
|
+
assert.deepEqual(readdirSync(sandboxRoot), ['settings.json']);
|
|
149
|
+
});
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
test(
|
|
155
|
+
'writeFileAtomically should keep the prior file whole and remove its temporary file when the write fails part way',
|
|
156
|
+
{ skip: isFileSizeLimitAvailable ? false : `${FILE_SIZE_LIMIT_COMMAND} is not on PATH` },
|
|
157
|
+
() => {
|
|
158
|
+
withSandbox((sandboxRoot) => {
|
|
159
|
+
const settingsPath = join(sandboxRoot, 'settings.json');
|
|
160
|
+
writeFileSync(settingsPath, PRIOR_SETTINGS_TEXT);
|
|
161
|
+
const writerScriptPath = join(sandboxRoot, 'write.mjs');
|
|
162
|
+
writeFileSync(writerScriptPath, [
|
|
163
|
+
`import { writeFileAtomically } from ${JSON.stringify(new URL('./write-file-atomically.mjs', import.meta.url).href)};`,
|
|
164
|
+
`writeFileAtomically(${JSON.stringify(settingsPath)}, ${JSON.stringify(NEW_SETTINGS_TEXT)});`,
|
|
165
|
+
].join('\n'));
|
|
166
|
+
|
|
167
|
+
const limitedRun = spawnSync(
|
|
168
|
+
FILE_SIZE_LIMIT_COMMAND,
|
|
169
|
+
[`--fsize=${Math.floor(NEW_SETTINGS_TEXT.length / 2)}`, process.execPath, writerScriptPath],
|
|
170
|
+
{ encoding: 'utf8' },
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
assert.notEqual(limitedRun.status, 0, 'the write past the file size limit fails');
|
|
174
|
+
assert.match(limitedRun.stderr, /EFBIG/);
|
|
175
|
+
assert.equal(readFileSync(settingsPath, 'utf8'), PRIOR_SETTINGS_TEXT);
|
|
176
|
+
assert.deepEqual(readdirSync(sandboxRoot).sort(), ['settings.json', 'write.mjs']);
|
|
177
|
+
});
|
|
178
|
+
},
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
const KILL_AT_TARGET_CHANGE_PRELOAD = `
|
|
183
|
+
const fs = require('node:fs');
|
|
184
|
+
const targetPath = process.env.KILL_TARGET_PATH;
|
|
185
|
+
const originalWriteFileSync = fs.writeFileSync;
|
|
186
|
+
const originalRenameSync = fs.renameSync;
|
|
187
|
+
fs.writeFileSync = function (filePath, data, options) {
|
|
188
|
+
if (String(filePath) === targetPath) {
|
|
189
|
+
const text = String(data);
|
|
190
|
+
originalWriteFileSync(filePath, text.slice(0, Math.floor(text.length / 2)));
|
|
191
|
+
process.kill(process.pid, 'SIGKILL');
|
|
192
|
+
}
|
|
193
|
+
return originalWriteFileSync.apply(this, arguments);
|
|
194
|
+
};
|
|
195
|
+
fs.renameSync = function (sourcePath, destinationPath) {
|
|
196
|
+
if (String(destinationPath) === targetPath) {
|
|
197
|
+
process.kill(process.pid, 'SIGKILL');
|
|
198
|
+
}
|
|
199
|
+
return originalRenameSync.apply(this, arguments);
|
|
200
|
+
};
|
|
201
|
+
require('node:module').syncBuiltinESMExports();
|
|
202
|
+
`;
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
test(
|
|
206
|
+
'writeFileAtomically should keep the prior file whole when the writer is killed as the target changes',
|
|
207
|
+
{ skip: isPosixPlatform ? false : 'a SIGKILL a process sends itself is POSIX only' },
|
|
208
|
+
() => {
|
|
209
|
+
withSandbox((sandboxRoot) => {
|
|
210
|
+
const settingsPath = join(sandboxRoot, 'settings.json');
|
|
211
|
+
writeFileSync(settingsPath, PRIOR_SETTINGS_TEXT);
|
|
212
|
+
const preloadPath = join(sandboxRoot, 'kill-at-target-change.cjs');
|
|
213
|
+
writeFileSync(preloadPath, KILL_AT_TARGET_CHANGE_PRELOAD);
|
|
214
|
+
const writerScriptPath = join(sandboxRoot, 'write.mjs');
|
|
215
|
+
writeFileSync(writerScriptPath, [
|
|
216
|
+
`import { writeFileAtomically } from ${JSON.stringify(new URL('./write-file-atomically.mjs', import.meta.url).href)};`,
|
|
217
|
+
`writeFileAtomically(${JSON.stringify(settingsPath)}, ${JSON.stringify(NEW_SETTINGS_TEXT)});`,
|
|
218
|
+
].join('\n'));
|
|
219
|
+
|
|
220
|
+
const killedRun = spawnSync(
|
|
221
|
+
process.execPath,
|
|
222
|
+
['--require', preloadPath, writerScriptPath],
|
|
223
|
+
{ encoding: 'utf8', env: { ...process.env, KILL_TARGET_PATH: settingsPath } },
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
assert.equal(killedRun.signal, 'SIGKILL', killedRun.stderr);
|
|
227
|
+
assert.equal(readFileSync(settingsPath, 'utf8'), PRIOR_SETTINGS_TEXT);
|
|
228
|
+
});
|
|
229
|
+
},
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
function exitedProcessId() {
|
|
234
|
+
return spawnSync(process.execPath, ['-e', '']).pid;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
test('writeFileAtomically should remove old temporary files of this target whose writer is gone', () => {
|
|
239
|
+
withSandbox((sandboxRoot) => {
|
|
240
|
+
const settingsPath = join(sandboxRoot, 'settings.json');
|
|
241
|
+
writeFileSync(settingsPath, PRIOR_SETTINGS_TEXT);
|
|
242
|
+
const goneWriterId = exitedProcessId();
|
|
243
|
+
const hourAgo = new Date(Date.now() - 60 * 60 * 1000);
|
|
244
|
+
const staleName = `.settings.json.${goneWriterId}.aaaaaaaaaaaa.tmp`;
|
|
245
|
+
const liveWriterName = `.settings.json.${process.pid}.bbbbbbbbbbbb.tmp`;
|
|
246
|
+
const freshName = `.settings.json.${goneWriterId}.cccccccccccc.tmp`;
|
|
247
|
+
const otherTargetName = `.other.json.${goneWriterId}.dddddddddddd.tmp`;
|
|
248
|
+
for (const eachName of [staleName, liveWriterName, freshName, otherTargetName]) {
|
|
249
|
+
writeFileSync(join(sandboxRoot, eachName), '{"half":');
|
|
250
|
+
}
|
|
251
|
+
for (const eachName of [staleName, liveWriterName, otherTargetName]) {
|
|
252
|
+
utimesSync(join(sandboxRoot, eachName), hourAgo, hourAgo);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
writeFileAtomically(settingsPath, NEW_SETTINGS_TEXT);
|
|
256
|
+
|
|
257
|
+
assert.equal(readFileSync(settingsPath, 'utf8'), NEW_SETTINGS_TEXT);
|
|
258
|
+
assert.deepEqual(
|
|
259
|
+
readdirSync(sandboxRoot).sort(),
|
|
260
|
+
[freshName, otherTargetName, liveWriterName, 'settings.json'].sort(),
|
|
261
|
+
);
|
|
262
|
+
});
|
|
263
|
+
});
|
|
@@ -8,7 +8,7 @@ import sys
|
|
|
8
8
|
import urllib.error
|
|
9
9
|
import urllib.request
|
|
10
10
|
from collections.abc import Callable, Iterator, Mapping
|
|
11
|
-
from dataclasses import dataclass
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
12
|
from enum import Enum
|
|
13
13
|
from http import HTTPStatus
|
|
14
14
|
from pathlib import Path
|
|
@@ -45,6 +45,7 @@ from hooks_constants.followup_pr_dedupe_constants import (
|
|
|
45
45
|
OPEN_PULL_REQUESTS_PATH_TEMPLATE,
|
|
46
46
|
OPTION_AND_VALUE_WIDTH,
|
|
47
47
|
OPTION_PREFIX,
|
|
48
|
+
PREFIX_ORIGIN_FOLLOWUP_SENTENCE,
|
|
48
49
|
PYTHON_PROGRAM_NAME_PATTERN,
|
|
49
50
|
READ_TIMEOUT_SECONDS,
|
|
50
51
|
REPOSITORY_OPTION,
|
|
@@ -75,11 +76,20 @@ from hooks_constants.shell_command_wrappers import (
|
|
|
75
76
|
|
|
76
77
|
@dataclass(frozen=True)
|
|
77
78
|
class PullRequestDraft:
|
|
78
|
-
"""The repository and body of a pull request about to be opened.
|
|
79
|
+
"""The repository and body of a pull request about to be opened.
|
|
80
|
+
|
|
81
|
+
Attributes:
|
|
82
|
+
owner: Repository owner.
|
|
83
|
+
repo: Repository name.
|
|
84
|
+
body: The create's body.
|
|
85
|
+
is_prefix_origin_check: True for the origin draft a create gets when only a
|
|
86
|
+
``GH_REPO=`` prefix names its repository. Equal drafts compare equal either way.
|
|
87
|
+
"""
|
|
79
88
|
|
|
80
89
|
owner: str
|
|
81
90
|
repo: str
|
|
82
91
|
body: str
|
|
92
|
+
is_prefix_origin_check: bool = field(default=False, compare=False)
|
|
83
93
|
|
|
84
94
|
|
|
85
95
|
@dataclass(frozen=True)
|
|
@@ -435,16 +445,28 @@ def _arguments_draft(
|
|
|
435
445
|
repository_value = option_value(all_arguments, ALL_REPOSITORY_OPTIONS)
|
|
436
446
|
if repository_value is None and is_gh_repository_set:
|
|
437
447
|
return UnreadOriginDraft(body, GH_REPO_SET_FOLLOWUP_REASON_TEMPLATE)
|
|
438
|
-
if repository_value is
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
448
|
+
if repository_value is None:
|
|
449
|
+
return _origin_draft(body, working_directory)
|
|
450
|
+
repository = named_repository(repository_value)
|
|
451
|
+
return None if repository is None else PullRequestDraft(repository[0], repository[1], body)
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
def _origin_draft(body: str, working_directory: str) -> PullRequestDraft | UnreadOriginDraft | None:
|
|
455
|
+
remote_url = _origin_remote_url(working_directory)
|
|
456
|
+
if remote_url is None:
|
|
457
|
+
return UnreadOriginDraft(body)
|
|
458
|
+
repository = _github_repository(remote_url)
|
|
445
459
|
return None if repository is None else PullRequestDraft(repository[0], repository[1], body)
|
|
446
460
|
|
|
447
461
|
|
|
462
|
+
def _is_prefix_named_create(all_segment_tokens: list[str]) -> bool:
|
|
463
|
+
program, all_arguments = segment_program_and_arguments(all_segment_tokens)
|
|
464
|
+
if program != GH_PROGRAM_NAME or prefix_repository(all_segment_tokens) is None:
|
|
465
|
+
return False
|
|
466
|
+
all_action_arguments = _gh_action_arguments(all_arguments, ALL_GH_CREATE_WORDS)
|
|
467
|
+
return all_action_arguments is not None and option_value(all_action_arguments, ALL_REPOSITORY_OPTIONS) is None
|
|
468
|
+
|
|
469
|
+
|
|
448
470
|
def sets_gh_repository(command: str) -> bool:
|
|
449
471
|
"""Return True when a command sets or exports ``GH_REPO`` outside a create's own prefix.
|
|
450
472
|
|
|
@@ -492,14 +514,47 @@ def _assigned_name(argument: str) -> str:
|
|
|
492
514
|
|
|
493
515
|
|
|
494
516
|
def _shell_followup_drafts(command: str, working_directory: str) -> list[PullRequestDraft | UnreadOriginDraft]:
|
|
517
|
+
"""Return the follow-up draft of each create in a shell command, in command order.
|
|
518
|
+
|
|
519
|
+
::
|
|
520
|
+
|
|
521
|
+
gh pr create --repo o/r --body "Follow-up to #7" -> [o/r]
|
|
522
|
+
GH_REPO=o/r env -u GH_REPO gh pr create --body "Follow-up to #7" -> [o/r, origin]
|
|
523
|
+
|
|
524
|
+
A create whose repository comes only from a ``GH_REPO=`` prefix also
|
|
525
|
+
gets an origin draft, since a wrapper such as ``env -u GH_REPO`` can drop
|
|
526
|
+
the prefix before gh runs. A prefix value that names no repository, such
|
|
527
|
+
as ``GH_REPO=nonsense``, still gets the origin draft. That origin draft is
|
|
528
|
+
left out when the origin remote gives no GitHub repository, since gh then
|
|
529
|
+
has none to open in.
|
|
530
|
+
|
|
531
|
+
Args:
|
|
532
|
+
command: The shell command text.
|
|
533
|
+
working_directory: The folder the command runs in.
|
|
534
|
+
"""
|
|
495
535
|
is_gh_repository_set = sets_gh_repository(command)
|
|
496
|
-
all_drafts =
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
536
|
+
all_drafts: list[PullRequestDraft | UnreadOriginDraft | None] = []
|
|
537
|
+
for each_segment in all_command_segments(command):
|
|
538
|
+
all_arguments = create_arguments(each_segment)
|
|
539
|
+
if all_arguments is None:
|
|
540
|
+
continue
|
|
541
|
+
draft = _arguments_draft(all_arguments, working_directory, is_gh_repository_set)
|
|
542
|
+
all_drafts.append(draft)
|
|
543
|
+
if not isinstance(draft, UnreadOriginDraft) and _is_prefix_named_create(each_segment):
|
|
544
|
+
all_drafts.append(_prefix_origin_draft(all_arguments, working_directory))
|
|
500
545
|
return list(dict.fromkeys(each_draft for each_draft in all_drafts if each_draft is not None))
|
|
501
546
|
|
|
502
547
|
|
|
548
|
+
def _prefix_origin_draft(all_arguments: list[str], working_directory: str) -> PullRequestDraft | None:
|
|
549
|
+
body = body_from_arguments(all_arguments, working_directory)
|
|
550
|
+
if body is None or followup_parent_number(body) is None:
|
|
551
|
+
return None
|
|
552
|
+
origin_draft = _origin_draft(body, working_directory)
|
|
553
|
+
if not isinstance(origin_draft, PullRequestDraft):
|
|
554
|
+
return None
|
|
555
|
+
return PullRequestDraft(origin_draft.owner, origin_draft.repo, body, is_prefix_origin_check=True)
|
|
556
|
+
|
|
557
|
+
|
|
503
558
|
def shell_command(all_payload_fields: Mapping[str, object]) -> str | None:
|
|
504
559
|
"""Return the command text of a Bash or PowerShell call, or None for any other tool.
|
|
505
560
|
|
|
@@ -641,6 +696,7 @@ def duplicate_followup_reason(
|
|
|
641
696
|
::
|
|
642
697
|
|
|
643
698
|
open pull request #9 says "Follow-up to #7" -> DUPLICATE_FOLLOWUP_REASON_TEMPLATE
|
|
699
|
+
that #9 is in origin, found for a GH_REPO= prefix -> PREFIX_ORIGIN_FOLLOWUP_SENTENCE + the same
|
|
644
700
|
the read gives no answer inside its allowance -> UNANSWERED_FOLLOWUP_REASON_TEMPLATE
|
|
645
701
|
the read answers with a GitHub server error -> UNANSWERED_FOLLOWUP_REASON_TEMPLATE
|
|
646
702
|
no --repo, and the origin read gives nothing -> UNREAD_ORIGIN_FOLLOWUP_REASON_TEMPLATE
|
|
@@ -672,6 +728,8 @@ def duplicate_followup_reason(
|
|
|
672
728
|
if answer is UnreadOpenPullRequests.FAILED:
|
|
673
729
|
continue
|
|
674
730
|
reason = _reason_for_parent(parent_number, answer)
|
|
731
|
+
if reason is not None and each_draft.is_prefix_origin_check:
|
|
732
|
+
return PREFIX_ORIGIN_FOLLOWUP_SENTENCE + reason
|
|
675
733
|
if reason is not None:
|
|
676
734
|
return reason
|
|
677
735
|
return None
|
|
@@ -44,6 +44,7 @@ from blocking.gate_run_reads import gate_run, read_deadlines_started
|
|
|
44
44
|
from blocking.merge_review_gate_parts.open_request import (
|
|
45
45
|
OpenCall,
|
|
46
46
|
OpenRequest,
|
|
47
|
+
ShellOpenCall,
|
|
47
48
|
open_calls_from,
|
|
48
49
|
open_request_for,
|
|
49
50
|
)
|
|
@@ -94,6 +95,7 @@ from hooks_constants.merge_review_gate_constants import (
|
|
|
94
95
|
OPTION_WITH_VALUE_PATTERN,
|
|
95
96
|
OWNER_FIELD,
|
|
96
97
|
PERMISSION_DECISION_REASON_KEY,
|
|
98
|
+
PREFIX_ORIGIN_CHECK_SENTENCE,
|
|
97
99
|
PR_NUMBER_PATTERN,
|
|
98
100
|
PR_URL_PATTERN,
|
|
99
101
|
RECEIPT_HEAD_KEY,
|
|
@@ -493,6 +495,8 @@ def _deny_decision(reason: str) -> dict[str, object]:
|
|
|
493
495
|
def _first_open_deny_reason(all_open_calls: list[OpenCall]) -> str | None:
|
|
494
496
|
for each_call in all_open_calls:
|
|
495
497
|
reason = open_deny_reason(open_request_for(each_call))
|
|
498
|
+
if reason is not None and isinstance(each_call, ShellOpenCall) and each_call.is_prefix_origin_check:
|
|
499
|
+
return reason + PREFIX_ORIGIN_CHECK_SENTENCE
|
|
496
500
|
if reason is not None:
|
|
497
501
|
return reason
|
|
498
502
|
return None
|
|
@@ -128,7 +128,9 @@ def all_placed_creates(command: str, folder: str | None) -> list[PlacedCreate]:
|
|
|
128
128
|
are read for a wrapper too. A text whose quotes do not close is read as
|
|
129
129
|
written and with its quoted line breaks joined, each in POSIX and raw
|
|
130
130
|
words. ``wrapped_creates.unplaced_creates`` gives each create the
|
|
131
|
-
walk did not place, and it comes back unfollowed.
|
|
131
|
+
walk did not place, and it comes back unfollowed. Two creates with the
|
|
132
|
+
same options stay two creates when their command words differ, so a
|
|
133
|
+
``GH_REPO=`` prefix on one of them is kept.
|
|
132
134
|
|
|
133
135
|
Args:
|
|
134
136
|
command: The shell command text.
|
|
@@ -142,7 +144,9 @@ def all_placed_creates(command: str, folder: str | None) -> list[PlacedCreate]:
|
|
|
142
144
|
PlacedCreate(each_arguments, None, False, each_words)
|
|
143
145
|
for each_arguments, each_words in unplaced_creates(command, all_placed_arguments)
|
|
144
146
|
)
|
|
145
|
-
return list(
|
|
147
|
+
return list(
|
|
148
|
+
{(each_create, each_create.all_command_words): each_create for each_create in all_creates}.values()
|
|
149
|
+
)
|
|
146
150
|
|
|
147
151
|
|
|
148
152
|
def _walk_text(walk: _Walk, text: str, state: FolderState, depth: int) -> FolderState:
|
|
@@ -5,7 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
import os
|
|
6
6
|
import sys
|
|
7
7
|
import tempfile
|
|
8
|
-
from dataclasses import dataclass
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
9
|
from pathlib import Path
|
|
10
10
|
|
|
11
11
|
hooks_directory = str(Path(__file__).resolve().parent.parent.parent)
|
|
@@ -100,12 +100,15 @@ class ShellOpenCall:
|
|
|
100
100
|
folder: The folder in effect at the create, after each folder change before it.
|
|
101
101
|
is_folder_followed: False when a folder change before it could not be followed,
|
|
102
102
|
or when the create names no repository in a command that sets ``GH_REPO``.
|
|
103
|
+
is_prefix_origin_check: True for the origin call a create gets when only a
|
|
104
|
+
``GH_REPO=`` prefix names its repository. Equal calls compare equal either way.
|
|
103
105
|
"""
|
|
104
106
|
|
|
105
107
|
repository: tuple[str, str] | None
|
|
106
108
|
branch: str
|
|
107
109
|
folder: str | None
|
|
108
110
|
is_folder_followed: bool = True
|
|
111
|
+
is_prefix_origin_check: bool = field(default=False, compare=False)
|
|
109
112
|
|
|
110
113
|
|
|
111
114
|
OpenCall = ToolOpenCall | ShellOpenCall
|
|
@@ -121,6 +124,12 @@ def open_calls_from(all_payload_fields: dict[str, object]) -> list[OpenCall]:
|
|
|
121
124
|
gh pr merge 12 -> []
|
|
122
125
|
|
|
123
126
|
Each walked create gives one call per head it names, in its own folder.
|
|
127
|
+
A create whose repository comes only from a ``GH_REPO=`` prefix also gives
|
|
128
|
+
one origin call per head, since a wrapper such as ``env -u GH_REPO`` can
|
|
129
|
+
drop the prefix before gh runs::
|
|
130
|
+
|
|
131
|
+
GH_REPO=o/r env -u GH_REPO gh pr create -> [ShellOpenCall o/r, ShellOpenCall origin]
|
|
132
|
+
|
|
124
133
|
The pattern reading of the first create adds one call no walk call equals.
|
|
125
134
|
|
|
126
135
|
Args:
|
|
@@ -212,17 +221,33 @@ def _shell_open_calls(command: str, folder: str | None) -> list[OpenCall]:
|
|
|
212
221
|
|
|
213
222
|
|
|
214
223
|
def _placed_open_calls(placed_create: PlacedCreate, is_gh_repository_set: bool) -> list[ShellOpenCall]:
|
|
215
|
-
|
|
224
|
+
flag_value = _flag_value(placed_create)
|
|
225
|
+
prefix_value = prefix_repository(list(placed_create.all_command_words)) if flag_value is None else None
|
|
226
|
+
repository_value = flag_value if flag_value is not None else prefix_value
|
|
227
|
+
repository = named_repository(repository_value) if repository_value is not None else None
|
|
216
228
|
is_repository_read = repository is not None or not is_gh_repository_set
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
229
|
+
all_calls: list[ShellOpenCall] = []
|
|
230
|
+
for each_head in _placed_heads(placed_create) or [CURRENT_HEAD_REFERENCE]:
|
|
231
|
+
all_calls.append(
|
|
232
|
+
ShellOpenCall(
|
|
233
|
+
repository=repository,
|
|
234
|
+
branch=each_head,
|
|
235
|
+
folder=placed_create.folder,
|
|
236
|
+
is_folder_followed=placed_create.is_folder_followed and is_repository_read,
|
|
237
|
+
is_prefix_origin_check=prefix_value is not None and repository is None,
|
|
238
|
+
)
|
|
223
239
|
)
|
|
224
|
-
|
|
225
|
-
|
|
240
|
+
if prefix_value is not None:
|
|
241
|
+
all_calls.append(
|
|
242
|
+
ShellOpenCall(
|
|
243
|
+
repository=None,
|
|
244
|
+
branch=each_head,
|
|
245
|
+
folder=placed_create.folder,
|
|
246
|
+
is_folder_followed=placed_create.is_folder_followed,
|
|
247
|
+
is_prefix_origin_check=True,
|
|
248
|
+
)
|
|
249
|
+
)
|
|
250
|
+
return all_calls
|
|
226
251
|
|
|
227
252
|
|
|
228
253
|
def _option_words(placed_create: PlacedCreate) -> list[str]:
|
|
@@ -230,10 +255,9 @@ def _option_words(placed_create: PlacedCreate) -> list[str]:
|
|
|
230
255
|
return all_gh_words if program == GH_PROGRAM_NAME else list(placed_create.all_arguments)
|
|
231
256
|
|
|
232
257
|
|
|
233
|
-
def
|
|
258
|
+
def _flag_value(placed_create: PlacedCreate) -> str | None:
|
|
234
259
|
all_values = all_option_values(_option_words(placed_create), LONG_REPOSITORY_OPTION, REPOSITORY_SHORT_LETTER)
|
|
235
|
-
|
|
236
|
-
return named_repository(flag_value) if flag_value is not None else None
|
|
260
|
+
return all_values[-1] if all_values else None
|
|
237
261
|
|
|
238
262
|
|
|
239
263
|
def _placed_heads(placed_create: PlacedCreate) -> list[str]:
|
|
@@ -249,8 +273,9 @@ def _floor_open_call(command: str, folder: str | None) -> ShellOpenCall | None:
|
|
|
249
273
|
|
|
250
274
|
GH_REPO=o/r env -u GH_REPO gh pr create --fill -> origin, HEAD
|
|
251
275
|
|
|
252
|
-
There gh opens in origin
|
|
253
|
-
|
|
276
|
+
There gh opens in origin. The walk also asks for a clean pass on origin
|
|
277
|
+
for every create a ``GH_REPO=`` prefix names, so this call repeats that
|
|
278
|
+
check for the first create.
|
|
254
279
|
"""
|
|
255
280
|
options_only = CREATE_TEXT_OPTION_PATTERN.sub(" ", command)
|
|
256
281
|
create_match = GH_PR_CREATE_PATTERN.search(
|