@jakobm/peacekeeper 0.19.0 → 0.21.0
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/README.md +85 -32
- package/dist/bin/peacekeeper.js +36 -0
- package/{bin/peacekeeper.yaml → dist/cmdabra.yaml} +7 -0
- package/dist/package.json +57 -0
- package/dist/src/container.js +89 -0
- package/dist/src/modules/cli/interface.js +1 -0
- package/dist/src/modules/cli/service.js +48 -0
- package/dist/src/modules/cli/types.js +2 -0
- package/dist/src/modules/configuration/interface.js +6 -0
- package/{scripts → dist/src/modules/configuration}/project-config-defaults.json +1 -0
- package/dist/src/modules/configuration/project-config.js +114 -0
- package/dist/src/modules/configuration/update-config.js +42 -0
- package/dist/src/modules/dependencies.yaml +16 -0
- package/dist/src/modules/external.js +48 -0
- package/dist/src/modules/lifecycle/checkpoint-codex-changes.js +204 -0
- package/dist/src/modules/lifecycle/checkpoint-codex-session-start.js +23 -0
- package/dist/src/modules/lifecycle/checkpoint-codex-stop.js +69 -0
- package/dist/src/modules/lifecycle/checkpoint-instructions.js +21 -0
- package/dist/src/modules/lifecycle/cleanup-ended-session.js +265 -0
- package/dist/src/modules/lifecycle/interface.js +7 -0
- package/dist/src/modules/lifecycle/log-script-invocation.js +115 -0
- package/dist/src/modules/lifecycle/queue-merged-worktree-cleanup.js +37 -0
- package/dist/src/modules/maintenance/cleanup-merged-branches.js +298 -0
- package/dist/src/modules/maintenance/interface.js +7 -0
- package/dist/src/modules/maintenance/remove-empty-directories.js +114 -0
- package/{scripts → dist/src/modules/projects}/check-project-requirements.txt +35 -22
- package/dist/src/modules/projects/check-project.js +344 -0
- package/dist/src/modules/projects/create-project-core.js +186 -0
- package/dist/src/modules/projects/create-project.js +19 -0
- package/dist/src/modules/projects/generate-github-setup.js +13 -0
- package/dist/src/modules/projects/github-setup-prefix.sh +5 -0
- package/dist/src/modules/projects/github-setup-repository-template.sh +90 -0
- package/dist/src/modules/projects/github-setup-suffix-template.sh +5 -0
- package/dist/src/modules/projects/install-hooks.js +151 -0
- package/dist/src/modules/projects/interface.js +6 -0
- package/dist/src/modules/releases/assess-release-core.js +145 -0
- package/dist/src/modules/releases/assess-release.js +8 -0
- package/dist/src/modules/releases/interface.js +9 -0
- package/dist/src/modules/releases/release-core.js +529 -0
- package/dist/src/modules/releases/release-protection.js +147 -0
- package/dist/src/modules/releases/release.js +23 -0
- package/dist/src/modules/releases/verify-release-core.js +44 -0
- package/dist/src/modules/releases/verify-release-protection.js +25 -0
- package/dist/src/modules/releases/verify-release-version.js +14 -0
- package/dist/src/modules/releases/verify-release.js +30 -0
- package/dist/src/modules/testing/check-test-receipt.js +46 -0
- package/dist/src/modules/testing/interface.js +5 -0
- package/dist/src/modules/testing/receipt-state.js +99 -0
- package/dist/src/modules/testing/record-test-receipt.js +29 -0
- package/dist/src/modules/testing/run-tests.js +57 -0
- package/package.json +25 -9
- package/bin/peacekeeper.js +0 -68
- package/scripts/assess-release-core.js +0 -146
- package/scripts/assess-release.js +0 -7
- package/scripts/check-project.js +0 -333
- package/scripts/check-test-receipt.js +0 -49
- package/scripts/checkpoint-codex-changes.js +0 -232
- package/scripts/checkpoint-codex-session-start.js +0 -24
- package/scripts/checkpoint-codex-stop.js +0 -86
- package/scripts/checkpoint-instructions.js +0 -19
- package/scripts/cleanup-ended-session.js +0 -318
- package/scripts/cleanup-merged-branches.js +0 -382
- package/scripts/create-project-core.js +0 -195
- package/scripts/create-project.js +0 -26
- package/scripts/install-hooks.js +0 -155
- package/scripts/log-script-invocation.js +0 -125
- package/scripts/project-config.js +0 -115
- package/scripts/queue-merged-worktree-cleanup.js +0 -58
- package/scripts/receipt-state.js +0 -91
- package/scripts/record-test-receipt.js +0 -37
- package/scripts/release-core.js +0 -529
- package/scripts/release-protection.js +0 -96
- package/scripts/release.js +0 -18
- package/scripts/remove-empty-directories.js +0 -163
- package/scripts/run-tests.js +0 -66
- package/scripts/update-config.js +0 -41
- package/scripts/verify-release-core.js +0 -39
- package/scripts/verify-release-protection.js +0 -19
- package/scripts/verify-release-version.js +0 -14
- package/scripts/verify-release.js +0 -26
- /package/{scripts → dist/src/modules/projects}/codex-setup-prefix.sh +0 -0
- /package/{scripts → dist/src/modules/releases}/assess-release-schema.json +0 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remove local and remote branches that are fully merged into the locally known
|
|
3
|
+
* the configured remote default branch. A worktree on an eligible local branch is removed first when it
|
|
4
|
+
* has no tracked or untracked changes.
|
|
5
|
+
*
|
|
6
|
+
* Recommended usage:
|
|
7
|
+
*
|
|
8
|
+
* npm exec --no -- peacekeeper cleanup-merged-branches # Dry-run (default).
|
|
9
|
+
* npm exec --no -- peacekeeper cleanup-merged-branches --dry-run # Explicit dry-run.
|
|
10
|
+
* npm exec --no -- peacekeeper cleanup-merged-branches --execute # Apply the displayed cleanup.
|
|
11
|
+
*
|
|
12
|
+
* Every run fetches the configured remote, including dry-runs, so the cleanup
|
|
13
|
+
* plan uses current remote references. Dry-run does not remove anything,
|
|
14
|
+
* but this fetch still updates local remote references. The script never deletes
|
|
15
|
+
* the configured default branch. It skips the current worktree, locked worktrees,
|
|
16
|
+
* and worktrees with tracked or untracked changes. Ignored files do not count as
|
|
17
|
+
* changes. Local and remote deletions are guarded by the exact commit IDs
|
|
18
|
+
* observed while building the cleanup plan, so a branch changed concurrently is
|
|
19
|
+
* preserved.
|
|
20
|
+
*/
|
|
21
|
+
export default ({ configuration, external: { execFileSync, fs, output, path, runtime, spawnSync, } }) => {
|
|
22
|
+
const { projectConfig } = configuration;
|
|
23
|
+
function fail(message) {
|
|
24
|
+
output.error(`Error: ${message}`);
|
|
25
|
+
runtime.exit(1);
|
|
26
|
+
}
|
|
27
|
+
function runGit(args, options = {}) {
|
|
28
|
+
const result = spawnSync('git', args, {
|
|
29
|
+
cwd: options.cwd,
|
|
30
|
+
encoding: 'utf8',
|
|
31
|
+
stdio: options.stdio || ['ignore', 'pipe', 'pipe'],
|
|
32
|
+
});
|
|
33
|
+
if (result.status !== 0) {
|
|
34
|
+
const detail = result.stderr?.trim() || result.stdout?.trim();
|
|
35
|
+
throw new Error(detail || `git ${args.join(' ')} failed`);
|
|
36
|
+
}
|
|
37
|
+
return result.stdout;
|
|
38
|
+
}
|
|
39
|
+
function mergedBranches(repositoryRoot, namespace) {
|
|
40
|
+
const { remote, defaultBranch } = projectConfig(repositoryRoot).git;
|
|
41
|
+
const output = runGit([
|
|
42
|
+
'for-each-ref',
|
|
43
|
+
`--merged=refs/remotes/${remote}/${defaultBranch}`,
|
|
44
|
+
'--format=%(refname)\t%(objectname)\t%(symref)',
|
|
45
|
+
namespace,
|
|
46
|
+
], { cwd: repositoryRoot });
|
|
47
|
+
return output
|
|
48
|
+
.trimEnd()
|
|
49
|
+
.split('\n')
|
|
50
|
+
.filter(Boolean)
|
|
51
|
+
.map((line) => line.split('\t'))
|
|
52
|
+
.filter(([, , symbolicTarget]) => !symbolicTarget)
|
|
53
|
+
.map(([refName, objectId]) => ({ refName, objectId }));
|
|
54
|
+
}
|
|
55
|
+
function listWorktrees(repositoryRoot) {
|
|
56
|
+
const fields = runGit(['worktree', 'list', '--porcelain', '-z'], {
|
|
57
|
+
cwd: repositoryRoot,
|
|
58
|
+
}).split('\0');
|
|
59
|
+
const worktrees = [];
|
|
60
|
+
let current = null;
|
|
61
|
+
for (const field of fields) {
|
|
62
|
+
if (field === '') {
|
|
63
|
+
if (current) {
|
|
64
|
+
worktrees.push(current);
|
|
65
|
+
current = null;
|
|
66
|
+
}
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const separator = field.indexOf(' ');
|
|
70
|
+
const key = separator === -1 ? field : field.slice(0, separator);
|
|
71
|
+
const value = separator === -1 ? '' : field.slice(separator + 1);
|
|
72
|
+
if (key === 'worktree') {
|
|
73
|
+
current = { path: value };
|
|
74
|
+
}
|
|
75
|
+
else if (current) {
|
|
76
|
+
current[key] = value;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return worktrees;
|
|
80
|
+
}
|
|
81
|
+
function samePath(first, second) {
|
|
82
|
+
try {
|
|
83
|
+
return fs.realpathSync(first) === fs.realpathSync(second);
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return path.resolve(first) === path.resolve(second);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function branchNameFromLocalRef(refName) {
|
|
90
|
+
return refName.slice('refs/heads/'.length);
|
|
91
|
+
}
|
|
92
|
+
function branchNameFromRemoteRef(refName, remote) {
|
|
93
|
+
return refName.slice(`refs/remotes/${remote}/`.length);
|
|
94
|
+
}
|
|
95
|
+
function buildPlan(repositoryRoot) {
|
|
96
|
+
const { remote, defaultBranch } = projectConfig(repositoryRoot).git;
|
|
97
|
+
const localBranches = new Map(mergedBranches(repositoryRoot, 'refs/heads')
|
|
98
|
+
.map(({ refName, objectId }) => [branchNameFromLocalRef(refName), objectId]));
|
|
99
|
+
const remoteBranches = new Map(mergedBranches(repositoryRoot, `refs/remotes/${remote}`)
|
|
100
|
+
.map(({ refName, objectId }) => [branchNameFromRemoteRef(refName, remote), objectId])
|
|
101
|
+
.filter(([branch]) => branch !== 'HEAD'));
|
|
102
|
+
localBranches.delete(defaultBranch);
|
|
103
|
+
remoteBranches.delete(defaultBranch);
|
|
104
|
+
const currentRoot = runGit(['rev-parse', '--show-toplevel'], {
|
|
105
|
+
cwd: repositoryRoot,
|
|
106
|
+
}).trim();
|
|
107
|
+
const worktrees = listWorktrees(repositoryRoot);
|
|
108
|
+
const names = [...new Set([...localBranches.keys(), ...remoteBranches.keys()])].sort();
|
|
109
|
+
return names.map((branch) => {
|
|
110
|
+
const branchWorktrees = localBranches.has(branch)
|
|
111
|
+
? worktrees.filter((worktree) => worktree.branch === `refs/heads/${branch}`)
|
|
112
|
+
: [];
|
|
113
|
+
let skipReason = null;
|
|
114
|
+
for (const worktree of branchWorktrees) {
|
|
115
|
+
if (samePath(worktree.path, currentRoot)) {
|
|
116
|
+
skipReason = `it is checked out in the current worktree: ${worktree.path}`;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
if (Object.hasOwn(worktree, 'locked')) {
|
|
120
|
+
skipReason = `its worktree is locked: ${worktree.path}`;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
const status = runGit(['status', '--porcelain', '--untracked-files=all'], { cwd: worktree.path });
|
|
125
|
+
if (status !== '') {
|
|
126
|
+
skipReason = `its worktree has changes: ${worktree.path}`;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
skipReason = `its worktree could not be inspected: ${worktree.path} (${error instanceof Error ? error.message : String(error)})`;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
branch,
|
|
137
|
+
local: localBranches.has(branch),
|
|
138
|
+
localObjectId: localBranches.get(branch),
|
|
139
|
+
remote: remoteBranches.has(branch),
|
|
140
|
+
remoteObjectId: remoteBranches.get(branch),
|
|
141
|
+
worktrees: branchWorktrees,
|
|
142
|
+
skipReason,
|
|
143
|
+
};
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
function printAction(execute, dryRunText, executeText, log) {
|
|
147
|
+
log(execute ? executeText : `Would ${dryRunText}`);
|
|
148
|
+
}
|
|
149
|
+
function localPlanChangedReason(repositoryRoot, item) {
|
|
150
|
+
if (!item.local)
|
|
151
|
+
return null;
|
|
152
|
+
try {
|
|
153
|
+
const branchRef = `refs/heads/${item.branch}`;
|
|
154
|
+
const tip = runGit(['rev-parse', '--verify', branchRef], { cwd: repositoryRoot }).trim();
|
|
155
|
+
if (tip !== item.localObjectId)
|
|
156
|
+
return 'its local branch changed after planning';
|
|
157
|
+
const current = listWorktrees(repositoryRoot);
|
|
158
|
+
const attached = current.filter((worktree) => worktree.branch === branchRef);
|
|
159
|
+
if (attached.length !== item.worktrees.length)
|
|
160
|
+
return 'its worktrees changed after planning';
|
|
161
|
+
for (const planned of item.worktrees) {
|
|
162
|
+
const worktree = current.find((candidate) => samePath(candidate.path, planned.path));
|
|
163
|
+
if (!worktree || worktree.branch !== branchRef || worktree.HEAD !== item.localObjectId) {
|
|
164
|
+
return `its worktree changed after planning: ${planned.path}`;
|
|
165
|
+
}
|
|
166
|
+
if (Object.hasOwn(worktree, 'locked'))
|
|
167
|
+
return `its worktree is locked: ${planned.path}`;
|
|
168
|
+
if (samePath(worktree.path, repositoryRoot))
|
|
169
|
+
return `it is checked out in the current worktree: ${planned.path}`;
|
|
170
|
+
if (runGit(['status', '--porcelain', '--untracked-files=all'], { cwd: planned.path }) !== '') {
|
|
171
|
+
return `its worktree has changes: ${planned.path}`;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
return `its local state could not be verified: ${error instanceof Error ? error.message : String(error)}`;
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
function applyPlan(repositoryRoot, plan, execute, log = output.log) {
|
|
181
|
+
const { remote } = projectConfig(repositoryRoot).git;
|
|
182
|
+
let removedWorktrees = 0;
|
|
183
|
+
let deletedLocalBranches = 0;
|
|
184
|
+
let deletedRemoteBranches = 0;
|
|
185
|
+
let skippedBranches = 0;
|
|
186
|
+
items: for (const item of plan) {
|
|
187
|
+
const skipReason = item.skipReason || (execute && localPlanChangedReason(repositoryRoot, item));
|
|
188
|
+
if (skipReason) {
|
|
189
|
+
log(`Skip ${item.branch}: ${skipReason}`);
|
|
190
|
+
skippedBranches += 1;
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
for (const [index, worktree] of item.worktrees.entries()) {
|
|
194
|
+
printAction(execute, `remove worktree ${worktree.path} (branch ${item.branch})`, `Removing worktree ${worktree.path} (branch ${item.branch})`, log);
|
|
195
|
+
if (execute) {
|
|
196
|
+
const changedReason = localPlanChangedReason(repositoryRoot, {
|
|
197
|
+
...item, worktrees: item.worktrees.slice(index),
|
|
198
|
+
});
|
|
199
|
+
if (changedReason) {
|
|
200
|
+
log(`Skip ${item.branch}: ${changedReason}`);
|
|
201
|
+
skippedBranches += 1;
|
|
202
|
+
continue items;
|
|
203
|
+
}
|
|
204
|
+
runGit(['worktree', 'remove', worktree.path], { cwd: repositoryRoot });
|
|
205
|
+
}
|
|
206
|
+
removedWorktrees += 1;
|
|
207
|
+
}
|
|
208
|
+
if (item.local) {
|
|
209
|
+
printAction(execute, `delete local branch ${item.branch}`, `Deleting local branch ${item.branch}`, log);
|
|
210
|
+
if (execute) {
|
|
211
|
+
const changedReason = localPlanChangedReason(repositoryRoot, { ...item, worktrees: [] });
|
|
212
|
+
if (changedReason) {
|
|
213
|
+
log(`Skip ${item.branch}: ${changedReason}`);
|
|
214
|
+
skippedBranches += 1;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
runGit([
|
|
218
|
+
'update-ref',
|
|
219
|
+
'-d',
|
|
220
|
+
`refs/heads/${item.branch}`,
|
|
221
|
+
item.localObjectId,
|
|
222
|
+
], { cwd: repositoryRoot });
|
|
223
|
+
const configResult = spawnSync('git', ['config', '--remove-section', `branch.${item.branch}`], { cwd: repositoryRoot, encoding: 'utf8' });
|
|
224
|
+
if (configResult.status !== 0 && configResult.status !== 5) {
|
|
225
|
+
output.warn(`Warning: could not remove configuration for branch ${item.branch}: `
|
|
226
|
+
+ configResult.stderr.trim());
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
deletedLocalBranches += 1;
|
|
230
|
+
}
|
|
231
|
+
if (item.remote) {
|
|
232
|
+
printAction(execute, `delete remote branch ${remote}/${item.branch}`, `Deleting remote branch ${remote}/${item.branch}`, log);
|
|
233
|
+
if (execute) {
|
|
234
|
+
runGit([
|
|
235
|
+
'push',
|
|
236
|
+
`--force-with-lease=refs/heads/${item.branch}:${item.remoteObjectId}`,
|
|
237
|
+
remote,
|
|
238
|
+
`:refs/heads/${item.branch}`,
|
|
239
|
+
], {
|
|
240
|
+
cwd: repositoryRoot,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
deletedRemoteBranches += 1;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
log('');
|
|
247
|
+
log(`${execute ? 'Removed' : 'Would remove'} ${removedWorktrees} worktree(s), `
|
|
248
|
+
+ `${deletedLocalBranches} local branch(es), and ${deletedRemoteBranches} remote branch(es).`);
|
|
249
|
+
if (skippedBranches > 0) {
|
|
250
|
+
log(`Skipped ${skippedBranches} branch(es).`);
|
|
251
|
+
}
|
|
252
|
+
if (!execute) {
|
|
253
|
+
log('');
|
|
254
|
+
log('To apply this cleanup, run:');
|
|
255
|
+
log(' npm exec --no -- peacekeeper cleanup-merged-branches --execute');
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function run({ execute = false } = {}) {
|
|
259
|
+
try {
|
|
260
|
+
execFileSync('git', ['--version'], { stdio: 'ignore' });
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
fail('required command not found: git');
|
|
264
|
+
}
|
|
265
|
+
let repositoryRoot;
|
|
266
|
+
try {
|
|
267
|
+
repositoryRoot = runGit(['rev-parse', '--show-toplevel']).trim();
|
|
268
|
+
runGit(['remote', 'get-url', projectConfig(repositoryRoot).git.remote], { cwd: repositoryRoot });
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
fail(error instanceof Error ? error.message : String(error));
|
|
272
|
+
}
|
|
273
|
+
output.log(execute ? 'Execute mode.' : 'Dry-run mode. No worktrees or branches will be removed.');
|
|
274
|
+
output.log(`Fetching and pruning ${projectConfig(repositoryRoot).git.remote} before building the cleanup plan.`);
|
|
275
|
+
try {
|
|
276
|
+
const { remote, defaultBranch } = projectConfig(repositoryRoot).git;
|
|
277
|
+
runGit(['fetch', '--prune', remote], { cwd: repositoryRoot });
|
|
278
|
+
runGit(['show-ref', '--verify', '--quiet', `refs/remotes/${remote}/${defaultBranch}`], {
|
|
279
|
+
cwd: repositoryRoot,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
fail(error instanceof Error ? error.message : String(error));
|
|
284
|
+
}
|
|
285
|
+
output.log('');
|
|
286
|
+
try {
|
|
287
|
+
const plan = buildPlan(repositoryRoot);
|
|
288
|
+
applyPlan(repositoryRoot, plan, execute);
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
fail(error instanceof Error ? error.message : String(error));
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
const api = {
|
|
295
|
+
applyPlan, buildPlan, listWorktrees, run,
|
|
296
|
+
};
|
|
297
|
+
return api;
|
|
298
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export default ({ cleanupMergedBranches, removeEmptyDirectories }) => ({
|
|
2
|
+
applyPlan: cleanupMergedBranches.applyPlan,
|
|
3
|
+
buildPlan: cleanupMergedBranches.buildPlan,
|
|
4
|
+
cleanupMergedBranches: cleanupMergedBranches.run,
|
|
5
|
+
listWorktrees: cleanupMergedBranches.listWorktrees,
|
|
6
|
+
removeEmptyDirectories: removeEmptyDirectories.main,
|
|
7
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
export default ({ external: { execFileSync, fs, output, path, runtime, } }) => {
|
|
2
|
+
const PROTECTED_ROOT_DIRECTORIES = new Set(['.git', 'node_modules', 'tmp']);
|
|
3
|
+
function findRepositoryRoot(cwd) {
|
|
4
|
+
return execFileSync('git', ['-C', cwd, 'rev-parse', '--show-toplevel'], {
|
|
5
|
+
encoding: 'utf8',
|
|
6
|
+
}).trim();
|
|
7
|
+
}
|
|
8
|
+
function listPresentFiles(root) {
|
|
9
|
+
const output = execFileSync('git', [
|
|
10
|
+
'-C',
|
|
11
|
+
root,
|
|
12
|
+
'ls-files',
|
|
13
|
+
'-z',
|
|
14
|
+
'--cached',
|
|
15
|
+
'--others',
|
|
16
|
+
'--exclude-standard',
|
|
17
|
+
], { encoding: 'buffer' });
|
|
18
|
+
return output
|
|
19
|
+
.toString('utf8')
|
|
20
|
+
.split('\0')
|
|
21
|
+
.filter(Boolean)
|
|
22
|
+
.filter((relativePath) => fs.lstatSync(path.join(root, relativePath), {
|
|
23
|
+
throwIfNoEntry: false,
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
function requiredDirectories(root, files) {
|
|
27
|
+
const required = new Set();
|
|
28
|
+
for (const file of files) {
|
|
29
|
+
const absolutePath = path.join(root, file);
|
|
30
|
+
let directory = fs.lstatSync(absolutePath).isDirectory()
|
|
31
|
+
? file.replace(/\/+$/u, '')
|
|
32
|
+
: path.posix.dirname(file);
|
|
33
|
+
while (directory !== '.') {
|
|
34
|
+
required.add(directory);
|
|
35
|
+
directory = path.posix.dirname(directory);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return required;
|
|
39
|
+
}
|
|
40
|
+
function findEmptyDirectories(root) {
|
|
41
|
+
const required = requiredDirectories(root, listPresentFiles(root));
|
|
42
|
+
function visit(absoluteDirectory, relativeDirectory) {
|
|
43
|
+
// A nested repository owns its contents, even when its parent is ignored.
|
|
44
|
+
if (relativeDirectory !== '' && fs.lstatSync(path.join(absoluteDirectory, '.git'), {
|
|
45
|
+
throwIfNoEntry: false,
|
|
46
|
+
}))
|
|
47
|
+
return { keep: true, directories: [] };
|
|
48
|
+
let keep = relativeDirectory === '' || required.has(relativeDirectory);
|
|
49
|
+
const directories = [];
|
|
50
|
+
for (const entry of fs.readdirSync(absoluteDirectory, {
|
|
51
|
+
withFileTypes: true,
|
|
52
|
+
})) {
|
|
53
|
+
if (!entry.isDirectory())
|
|
54
|
+
continue;
|
|
55
|
+
if (relativeDirectory === ''
|
|
56
|
+
&& PROTECTED_ROOT_DIRECTORIES.has(entry.name))
|
|
57
|
+
continue;
|
|
58
|
+
const relativePath = relativeDirectory
|
|
59
|
+
? `${relativeDirectory}/${entry.name}`
|
|
60
|
+
: entry.name;
|
|
61
|
+
const absolutePath = path.join(absoluteDirectory, entry.name);
|
|
62
|
+
const child = visit(absolutePath, relativePath);
|
|
63
|
+
keep ||= child.keep;
|
|
64
|
+
directories.push(...child.directories);
|
|
65
|
+
}
|
|
66
|
+
return { keep, directories: keep ? directories : [relativeDirectory] };
|
|
67
|
+
}
|
|
68
|
+
return visit(root, '').directories;
|
|
69
|
+
}
|
|
70
|
+
function removeEmptyDirectories(root, directories) {
|
|
71
|
+
for (const directory of directories) {
|
|
72
|
+
fs.rmSync(path.join(root, directory), { recursive: true });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function run({ execute = false, cwd, log }) {
|
|
76
|
+
const root = findRepositoryRoot(cwd);
|
|
77
|
+
const directories = findEmptyDirectories(root);
|
|
78
|
+
if (directories.length === 0) {
|
|
79
|
+
log('No empty directories found.');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (execute) {
|
|
83
|
+
removeEmptyDirectories(root, directories);
|
|
84
|
+
for (const directory of directories)
|
|
85
|
+
log(`Removed ${directory}`);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
for (const directory of directories)
|
|
89
|
+
log(`Would remove ${directory}`);
|
|
90
|
+
log('');
|
|
91
|
+
log('To remove these directories, run:');
|
|
92
|
+
log(' npm exec --no -- peacekeeper remove-empty-directories --execute');
|
|
93
|
+
}
|
|
94
|
+
function main({ execute = false } = {}) {
|
|
95
|
+
try {
|
|
96
|
+
run({
|
|
97
|
+
execute,
|
|
98
|
+
cwd: runtime.cwd(),
|
|
99
|
+
log: output.log,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
output.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
|
|
104
|
+
runtime.exitCode = 1;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const api = {
|
|
108
|
+
findEmptyDirectories,
|
|
109
|
+
main,
|
|
110
|
+
removeEmptyDirectories,
|
|
111
|
+
run,
|
|
112
|
+
};
|
|
113
|
+
return api;
|
|
114
|
+
};
|
|
@@ -30,6 +30,7 @@ Project configuration
|
|
|
30
30
|
|
|
31
31
|
Example configuration fields (merge into the full project configuration):
|
|
32
32
|
"git": {
|
|
33
|
+
"requiredApprover": "OWNER",
|
|
33
34
|
"releaseTestWorkflow": "package.yml",
|
|
34
35
|
"releaseRequiredChecks": ["test"]
|
|
35
36
|
},
|
|
@@ -39,6 +40,14 @@ Project configuration
|
|
|
39
40
|
}
|
|
40
41
|
projectChecks and both its fields are mandatory in an existing configuration.
|
|
41
42
|
|
|
43
|
+
git.requiredApprover must identify the personal GitHub account that owns the
|
|
44
|
+
repository. Its bundled default is null so migrations must choose explicitly.
|
|
45
|
+
check-project requires .github/CODEOWNERS to contain exactly one non-comment
|
|
46
|
+
rule matching that account:
|
|
47
|
+
* @OWNER
|
|
48
|
+
Additional ownership patterns or owners are rejected because GitHub accepts
|
|
49
|
+
any matching code owner's approval, which would weaken the identity guarantee.
|
|
50
|
+
|
|
42
51
|
Package metadata: all projects, including private packages
|
|
43
52
|
package.json must declare author and repository.
|
|
44
53
|
author must be a nonblank string or an object with a nonblank string name.
|
|
@@ -71,7 +80,7 @@ Toolchain: package.json and all Node-based CI jobs
|
|
|
71
80
|
Codex worktree environment
|
|
72
81
|
.codex/environments/environment.toml must be valid TOML with version = 1.
|
|
73
82
|
setup.script must start character-for-character with the contents of the
|
|
74
|
-
bundled
|
|
83
|
+
bundled src/modules/projects/codex-setup-prefix.sh reference file, including comments,
|
|
75
84
|
whitespace and the newline after "# Add custom project setup below".
|
|
76
85
|
Comparison uses the parsed TOML string, not the file's quoting style.
|
|
77
86
|
Arbitrary custom setup may follow, or the script may end at that newline.
|
|
@@ -194,8 +203,8 @@ Publication: publishesToNpm=true
|
|
|
194
203
|
Accepted direct verification commands:
|
|
195
204
|
peacekeeper verify-release
|
|
196
205
|
npm exec --no -- peacekeeper verify-release
|
|
197
|
-
node bin/peacekeeper.js verify-release
|
|
198
|
-
node node_modules/@jakobm/peacekeeper/bin/peacekeeper.js verify-release
|
|
206
|
+
node dist/bin/peacekeeper.js verify-release
|
|
207
|
+
node node_modules/@jakobm/peacekeeper/dist/bin/peacekeeper.js verify-release
|
|
199
208
|
The node paths may also start with ./.
|
|
200
209
|
|
|
201
210
|
Verification must receive these environment inputs, directly on the step or
|
|
@@ -246,30 +255,31 @@ Online release policy and GitHub token setup
|
|
|
246
255
|
|
|
247
256
|
Configure the default branch from git.defaultBranch with every exact job name
|
|
248
257
|
in git.releaseRequiredChecks mandatory and "Require branches to be up to date
|
|
249
|
-
before merging" enabled.
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
- Active branch rulesets requiring strict status checks, without any bypass
|
|
253
|
-
actors. Organization-inherited rulesets are included in verification.
|
|
258
|
+
before merging" enabled. The base policy may use either classic branch
|
|
259
|
+
protection with strict status checks or active branch rulesets requiring
|
|
260
|
+
strict status checks. Its bypass configuration must be inspected manually.
|
|
254
261
|
Merge queues are not currently an alternative to this strict policy. For the
|
|
255
262
|
matrix example, also require its matrix checks as described above.
|
|
256
263
|
|
|
264
|
+
Add exactly one active owner-approval ruleset. It must contain only one
|
|
265
|
+
pull_request rule requiring exactly one code-owner approval and stale approval
|
|
266
|
+
dismissal. Its only bypass actor must be the numeric user ID corresponding to
|
|
267
|
+
git.requiredApprover, with actor_type User and bypass_mode pull_request. The
|
|
268
|
+
configured account must be the personal repository owner. This approval-only
|
|
269
|
+
bypass lets that owner merge a PR they authored, but the separate base policy
|
|
270
|
+
still prevents direct pushes and requires strict CI. Any other bypass violates
|
|
271
|
+
the policy and must be caught by the manual inspection.
|
|
272
|
+
|
|
257
273
|
Create a fine-grained GitHub PAT or GitHub App installation token with access
|
|
258
274
|
to this repository and these repository permissions:
|
|
259
275
|
Administration: read
|
|
260
276
|
Contents: read/write
|
|
261
277
|
Pull requests: read
|
|
262
278
|
Actions: read
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
Consult GitHub's organization ruleset endpoint documentation for supported
|
|
268
|
-
token types and organization permissions (the fine-grained token section):
|
|
269
|
-
https://docs.github.com/en/rest/orgs/rules#get-an-organization-repository-ruleset
|
|
270
|
-
The token must also have write access to the contributing ruleset for GitHub
|
|
271
|
-
to expose bypass actors. Missing bypass information causes verification to
|
|
272
|
-
fail; confirm access with verify-release-protection.
|
|
279
|
+
GitHub exposes bypass actors only to callers with write access to the ruleset.
|
|
280
|
+
Peacekeeper therefore does not verify them and prints this warning instead:
|
|
281
|
+
Cannot verify bypass users unless given write access. Run a manual check
|
|
282
|
+
instead; available as a gist.
|
|
273
283
|
|
|
274
284
|
Recommended Actions secret name: RELEASE_POLICY_TOKEN. A maintainer with
|
|
275
285
|
permission to manage Actions secrets can save it with:
|
|
@@ -392,13 +402,16 @@ Default-branch GitHub policy chain
|
|
|
392
402
|
This applies to private projects too; configure nonempty releaseRequiredChecks.
|
|
393
403
|
|
|
394
404
|
Online verification reads the actual GitHub default branch and requires PRs,
|
|
395
|
-
strict mandatory configured checks
|
|
396
|
-
|
|
397
|
-
|
|
405
|
+
strict mandatory configured checks, and merge commits only. It also verifies
|
|
406
|
+
the separate owner-approval ruleset described above, except for its bypass
|
|
407
|
+
actors. Enable
|
|
408
|
+
repository allow_auto_merge; this only offers per-PR opt-in and does not
|
|
409
|
+
activate auto-merge for any PR.
|
|
398
410
|
Restrict merge methods either repository-wide (merge enabled, squash and rebase
|
|
399
411
|
disabled) or with a default-branch PR ruleset allowing only merge. Other branches
|
|
400
412
|
are not checked. Linear-history rules or PR rules excluding merge are errors.
|
|
401
|
-
|
|
413
|
+
Base PR requirements, like status checks, require active rules. Inspect all
|
|
414
|
+
bypass actors manually.
|
|
402
415
|
Existing automatic head-branch deletion requirements still apply.
|
|
403
416
|
|
|
404
417
|
Missing CI is detected on the next local npm test. GitHub settings are checked
|