Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions scripts/validate-foundation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,23 @@ fi
# major tag the README recommends to every caller. The exemption accepts the
# forms YAML allows around that reference — either quote style, and a trailing
# comment — so a rewrapped line does not read as an unpinned dependency.
if grep -rnE 'uses:[[:space:]]+[^./][^@[:space:]]+@(main|master|latest|v[0-9]+([.][0-9]+)*)' .github/workflows action.yml \
| grep -vE "uses:[[:space:]]+['\"]?tesslio/code-review-action@v[0-9]+['\"]?([[:space:]]+#.*)?[[:space:]]*$"; then
unpinned=0
while IFS= read -r line; do
reference="${line#*uses:}"
reference="${reference#"${reference%%[![:space:]]*}"}"
reference="${reference%%[[:space:]]*}"
reference="${reference#[\"\']}"
reference="${reference%[\"\']}"
if [[ "$reference" == ./* ]] ||
[[ "$reference" =~ ^tesslio/code-review-action@v[0-9]+$ ]] ||
[[ "$reference" =~ ^[^@[:space:]]+@[[:xdigit:]]{40}$ ]] ||
[[ "$reference" =~ ^docker://[^@[:space:]]+@sha256:[[:xdigit:]]{64}$ ]]; then
continue
fi
echo "unpinned Action reference: $reference" >&2
unpinned=1
done < <(grep -rhE '^[[:space:]]*(-[[:space:]]+)?uses:' .github/workflows action.yml)
if (( unpinned )); then
echo "third-party Actions must be pinned by full commit SHA" >&2
exit 1
fi
Expand Down
3 changes: 2 additions & 1 deletion src/github-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class GitHubCodeReviewApi {
for (let attempt = 0; ; attempt++) {
const canRetry = method !== 'POST';
let response;
let text;
try {
response = await this.fetchImpl(`${API_ORIGIN}${path}`, {
method,
Expand All @@ -147,12 +148,12 @@ export class GitHubCodeReviewApi {
...(body === undefined ? {} : { body: JSON.stringify(body) }),
signal: AbortSignal.timeout(this.timeoutMs),
});
text = await response.text();
} catch (error) {
if (!canRetry || attempt >= this.retryDelaysMs.length) throw error;
await this.sleep(this.retryDelaysMs[attempt]);
continue;
}
const text = await response.text();
if (response.ok) {
return {
body: text.length === 0 ? undefined : JSON.parse(text),
Expand Down
41 changes: 41 additions & 0 deletions test/action-pins.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import test from 'node:test';

const validation = readFileSync(new URL('../scripts/validate-foundation.sh', import.meta.url), 'utf8');
const pinCheck = validation.slice(validation.indexOf('# A reference'), validation.indexOf('node --test'));

function check(reference) {
const dir = mkdtempSync(join(tmpdir(), 'action-pins-'));
try {
mkdirSync(join(dir, '.github', 'workflows'), { recursive: true });
writeFileSync(join(dir, 'action.yml'), `steps:\n - uses: ${reference}\n`);
// The checker reads the same paths as the foundation script.
const result = spawnSync('bash', ['-c', pinCheck], { cwd: dir, encoding: 'utf8' });
assert.ifError(result.error);
return result;
} finally {
rmSync(dir, { recursive: true, force: true });
}
}

for (const reference of ['owner/action@release-candidate', 'owner/action@abc1234', 'owner/action@v2-beta', '"owner/action@feature" # mutable']) {
test(`foundation rejects a non-SHA third-party reference: ${reference}`, () => {
assert.equal(check(reference).status, 1);
});
}

for (const reference of [
'owner/action@' + 'a'.repeat(40),
'"owner/action@' + 'A'.repeat(40) + '" # immutable',
'docker://example/image@sha256:' + 'a'.repeat(64),
"'tesslio/code-review-action@v1' # own major tag",
'./local-action',
]) {
test(`foundation accepts an allowed reference: ${reference}`, () => {
assert.equal(check(reference).status, 0);
});
}
32 changes: 32 additions & 0 deletions test/github-api.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@ function response(status, body = '') {
});
}

test('retries a GET when reading its response body fails', async () => {
let calls = 0;
const delays = [];
const api = new GitHubCodeReviewApi({
token: 'token', repository: 'acme/widgets', retryDelaysMs: [5],
sleep: async (delay) => delays.push(delay),
fetchImpl: async () => {
calls++;
return calls === 1
? new Response(new ReadableStream({ start(controller) { controller.error(new Error('connection reset')); } }))
: response(200, '{"ok":true}');
},
});
assert.deepEqual(await api.request('/test'), { ok: true });
assert.equal(calls, 2);
assert.deepEqual(delays, [5]);
});

test('does not replay a POST when its response body fails', async () => {
let calls = 0;
const api = new GitHubCodeReviewApi({
token: 'token', repository: 'acme/widgets', retryDelaysMs: [0],
sleep: async () => {},
fetchImpl: async () => {
calls++;
return new Response(new ReadableStream({ start(controller) { controller.error(new Error('connection reset')); } }));
},
});
await assert.rejects(api.request('/test', { method: 'POST', body: {} }), /connection reset/);
assert.equal(calls, 1);
});

test('uses a timeout and retries bounded transient failures', async () => {
const calls = [];
const delays = [];
Expand Down