Skip to content

Commit b4d4470

Browse files
committed
fix(ci): guard runtime update branch lease
1 parent e63bacb commit b4d4470

4 files changed

Lines changed: 248 additions & 1 deletion

File tree

‎.github/workflows/ci.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ jobs:
6868
node --test npm/clawscan/test/*.test.mjs
6969
node --test scripts/build-npm-package.test.mjs
7070
node scripts/build-npm-package.mjs --version v0.0.0 --pack --smoke
71+
72+
- name: Test runtime update branch push
73+
run: node --test scripts/push-runtime-tool-update-branch.test.mjs

‎.github/workflows/runtime-tool-updates.yml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ permissions:
99
contents: write
1010
pull-requests: write
1111

12+
concurrency:
13+
group: runtime-tool-updates
14+
cancel-in-progress: false
15+
1216
jobs:
1317
update-runtime-tools:
1418
name: Update runtime tool pins
@@ -64,7 +68,7 @@ jobs:
6468
git checkout -B "$BRANCH"
6569
git add docker/clawscan-runtime/Dockerfile
6670
git commit -m "chore: update clawscan runtime tool pins"
67-
git push --force-with-lease origin "$BRANCH"
71+
scripts/push-runtime-tool-update-branch.sh origin "$BRANCH"
6872
6973
cat > /tmp/runtime-tool-update-pr.md <<'EOF'
7074
Updates pinned tool versions in `docker/clawscan-runtime/Dockerfile`.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
if [[ $# -ne 2 ]]; then
5+
echo "usage: $0 <remote> <branch>" >&2
6+
exit 64
7+
fi
8+
9+
remote="$1"
10+
branch="$2"
11+
remote_ref="refs/heads/${branch}"
12+
tracking_ref="refs/remotes/${remote}/${branch}"
13+
14+
git remote get-url "$remote" >/dev/null
15+
git check-ref-format --branch "$branch" >/dev/null
16+
git check-ref-format "$tracking_ref" >/dev/null
17+
git rev-parse --verify "HEAD^{commit}" >/dev/null
18+
19+
set +e
20+
remote_output="$(git ls-remote --exit-code --heads "$remote" "$remote_ref")"
21+
remote_status=$?
22+
set -e
23+
24+
case "$remote_status" in
25+
0)
26+
if [[ "$remote_output" == *$'\n'* ]]; then
27+
echo "expected one remote ref for ${remote_ref}" >&2
28+
exit 1
29+
fi
30+
read -r observed_oid observed_ref extra <<< "$remote_output"
31+
if [[ ! "$observed_oid" =~ ^[0-9a-f]{40}$ || "$observed_ref" != "$remote_ref" || -n "${extra:-}" ]]; then
32+
echo "invalid remote ref response for ${remote_ref}" >&2
33+
exit 1
34+
fi
35+
36+
git fetch --no-tags --depth=1 "$remote" "+${remote_ref}:${tracking_ref}"
37+
fetched_oid="$(git rev-parse --verify "${tracking_ref}^{commit}")"
38+
if [[ "$fetched_oid" != "$observed_oid" ]]; then
39+
echo "${remote_ref} changed while its lease was being prepared" >&2
40+
exit 1
41+
fi
42+
expected_oid="$observed_oid"
43+
;;
44+
2)
45+
expected_oid=""
46+
;;
47+
*)
48+
echo "failed to inspect ${remote_ref} on ${remote}" >&2
49+
exit "$remote_status"
50+
;;
51+
esac
52+
53+
# Rebuild the standing automation branch only if its observed tip is unchanged.
54+
git push \
55+
--force-with-lease="${remote_ref}:${expected_oid}" \
56+
"$remote" \
57+
"HEAD:${remote_ref}"
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import assert from 'node:assert/strict';
2+
import { execFileSync, spawnSync } from 'node:child_process';
3+
import { chmodSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
4+
import { tmpdir } from 'node:os';
5+
import { join } from 'node:path';
6+
import { fileURLToPath } from 'node:url';
7+
import test from 'node:test';
8+
9+
const scriptPath = fileURLToPath(new URL('./push-runtime-tool-update-branch.sh', import.meta.url));
10+
const workflowPath = fileURLToPath(
11+
new URL('../.github/workflows/runtime-tool-updates.yml', import.meta.url),
12+
);
13+
const gitPath = execFileSync('sh', ['-c', 'command -v git'], { encoding: 'utf8' }).trim();
14+
const branch = 'automation/runtime-tool-updates';
15+
16+
function git(cwd, ...args) {
17+
return execFileSync(gitPath, args, { cwd, encoding: 'utf8' }).trim();
18+
}
19+
20+
function commitFile(repo, name, content, message) {
21+
writeFileSync(join(repo, name), content);
22+
git(repo, 'add', name);
23+
git(repo, 'commit', '-m', message);
24+
return git(repo, 'rev-parse', 'HEAD');
25+
}
26+
27+
function createFixture({ existingBranch }) {
28+
const root = mkdtempSync(join(tmpdir(), 'clawscan-runtime-lease-'));
29+
const remote = join(root, 'remote.git');
30+
const seed = join(root, 'seed');
31+
const worker = join(root, 'worker');
32+
33+
mkdirSync(seed);
34+
git(root, 'init', '--bare', remote);
35+
git(seed, 'init');
36+
git(seed, 'config', 'user.name', 'Test Seed');
37+
git(seed, 'config', 'user.email', '[email protected]');
38+
commitFile(seed, 'README.md', 'main\n', 'initial main');
39+
git(seed, 'branch', '-M', 'main');
40+
git(seed, 'remote', 'add', 'origin', remote);
41+
git(seed, 'push', '-u', 'origin', 'main');
42+
43+
if (existingBranch) {
44+
git(seed, 'switch', '-c', branch);
45+
commitFile(seed, 'runtime.txt', 'old\n', 'old runtime update');
46+
git(seed, 'push', 'origin', `HEAD:refs/heads/${branch}`);
47+
git(seed, 'switch', 'main');
48+
}
49+
50+
git(root, 'clone', '--branch', 'main', remote, worker);
51+
git(worker, 'config', 'user.name', 'Test Worker');
52+
git(worker, 'config', 'user.email', '[email protected]');
53+
const workerHead = commitFile(worker, 'runtime.txt', 'new\n', 'new runtime update');
54+
55+
return { root, remote, worker, workerHead };
56+
}
57+
58+
function runPush(worker, env = process.env) {
59+
return spawnSync('bash', [scriptPath, 'origin', branch], {
60+
cwd: worker,
61+
env,
62+
encoding: 'utf8',
63+
});
64+
}
65+
66+
function remoteHead(remote) {
67+
return git(remote, 'rev-parse', `refs/heads/${branch}`);
68+
}
69+
70+
function installRacingHook(fixture, { branchExists }) {
71+
const racer = join(fixture.root, 'racer');
72+
git(fixture.root, 'clone', '--branch', branchExists ? branch : 'main', fixture.remote, racer);
73+
git(racer, 'config', 'user.name', 'Test Racer');
74+
git(racer, 'config', 'user.email', '[email protected]');
75+
if (!branchExists) {
76+
git(racer, 'switch', '-c', branch);
77+
}
78+
const racerHead = commitFile(racer, 'race.txt', 'race\n', 'competing runtime update');
79+
80+
const hook = join(fixture.worker, '.git', 'hooks', 'pre-push');
81+
writeFileSync(
82+
hook,
83+
`#!/bin/sh\nset -eu\n"${gitPath}" -C "${racer}" push origin HEAD:refs/heads/${branch} >/dev/null\n`,
84+
);
85+
chmodSync(hook, 0o755);
86+
return racerHead;
87+
}
88+
89+
function installFetchRaceWrapper(fixture) {
90+
const racer = join(fixture.root, 'fetch-racer');
91+
git(fixture.root, 'clone', '--branch', branch, fixture.remote, racer);
92+
git(racer, 'config', 'user.name', 'Test Fetch Racer');
93+
git(racer, 'config', 'user.email', '[email protected]');
94+
const racerHead = commitFile(racer, 'fetch-race.txt', 'race\n', 'competing fetch-time update');
95+
96+
const bin = join(fixture.root, 'bin');
97+
const marker = join(fixture.root, 'fetch-race-fired');
98+
mkdirSync(bin);
99+
const wrapper = join(bin, 'git');
100+
writeFileSync(
101+
wrapper,
102+
`#!/bin/sh
103+
set -eu
104+
if [ "\${1:-}" = "fetch" ] && [ ! -e "${marker}" ]; then
105+
: > "${marker}"
106+
"${gitPath}" -C "${racer}" push origin HEAD:refs/heads/${branch} >/dev/null
107+
fi
108+
exec "${gitPath}" "$@"
109+
`,
110+
);
111+
chmodSync(wrapper, 0o755);
112+
return {
113+
env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
114+
racerHead,
115+
};
116+
}
117+
118+
test('creates the automation branch only while it remains absent', () => {
119+
const fixture = createFixture({ existingBranch: false });
120+
try {
121+
const result = runPush(fixture.worker);
122+
assert.equal(result.status, 0, result.stderr);
123+
assert.equal(remoteHead(fixture.remote), fixture.workerHead);
124+
} finally {
125+
rmSync(fixture.root, { recursive: true, force: true });
126+
}
127+
});
128+
129+
test('replaces the observed automation branch tip', () => {
130+
const fixture = createFixture({ existingBranch: true });
131+
try {
132+
const result = runPush(fixture.worker);
133+
assert.equal(result.status, 0, result.stderr);
134+
assert.equal(remoteHead(fixture.remote), fixture.workerHead);
135+
} finally {
136+
rmSync(fixture.root, { recursive: true, force: true });
137+
}
138+
});
139+
140+
test('rejects a competing update after observing an existing branch', () => {
141+
const fixture = createFixture({ existingBranch: true });
142+
try {
143+
const racerHead = installRacingHook(fixture, { branchExists: true });
144+
const result = runPush(fixture.worker);
145+
assert.notEqual(result.status, 0);
146+
assert.equal(remoteHead(fixture.remote), racerHead);
147+
} finally {
148+
rmSync(fixture.root, { recursive: true, force: true });
149+
}
150+
});
151+
152+
test('rejects a competing update between observation and fetch', () => {
153+
const fixture = createFixture({ existingBranch: true });
154+
try {
155+
const race = installFetchRaceWrapper(fixture);
156+
const result = runPush(fixture.worker, race.env);
157+
assert.notEqual(result.status, 0);
158+
assert.match(result.stderr, /changed while its lease was being prepared/u);
159+
assert.equal(remoteHead(fixture.remote), race.racerHead);
160+
} finally {
161+
rmSync(fixture.root, { recursive: true, force: true });
162+
}
163+
});
164+
165+
test('rejects a competing branch creation after observing it absent', () => {
166+
const fixture = createFixture({ existingBranch: false });
167+
try {
168+
const racerHead = installRacingHook(fixture, { branchExists: false });
169+
const result = runPush(fixture.worker);
170+
assert.notEqual(result.status, 0);
171+
assert.equal(remoteHead(fixture.remote), racerHead);
172+
} finally {
173+
rmSync(fixture.root, { recursive: true, force: true });
174+
}
175+
});
176+
177+
test('workflow serializes writers and delegates the guarded push', () => {
178+
const workflow = readFileSync(workflowPath, 'utf8');
179+
assert.match(workflow, /group: runtime-tool-updates/u);
180+
assert.match(workflow, /cancel-in-progress: false/u);
181+
assert.match(workflow, /scripts\/push-runtime-tool-update-branch\.sh origin "\$BRANCH"/u);
182+
assert.doesNotMatch(workflow, /git push --force-with-lease origin "\$BRANCH"/u);
183+
});

0 commit comments

Comments
 (0)