Skip to content
Merged
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
5 changes: 3 additions & 2 deletions scripts/lib/checkpoint-proof.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,15 @@ export async function resolveProofScope(options, dependencies) {
const { repository, git, github, remote } = dependencies;
assertScopeOptions(options, repository);
const canonical = github(`repos/${repository.fullName}`);
if (`github.com/${canonical.full_name}` !== repositoryId) throw new Error('Canonical repository mismatch.');
const canonicalId = `github.com/${canonical.full_name}`;
if (canonicalId.toLowerCase() !== repositoryId.toLowerCase()) throw new Error('Canonical repository mismatch.');
if (git('rev-parse', '--verify', `${candidate}^{commit}`) !== candidate) throw new Error('Candidate unavailable.');
const targetRef = event === 'push' ? 'refs/heads/main' : `refs/pull/${pullRequest}/head`;
assertProofTarget(candidate, git('rev-parse', 'HEAD'), await remote(targetRef));
await assertFreshMain(event, git, remote);
const base = event === 'push' ? git('rev-parse', `${candidate}^`) : git('merge-base', 'origin/main', candidate);
const checkpointHead = checkpointSource(event, candidate, repository, git, github);
return { repositoryId, event, candidate, targetRef, ...(pullRequest === undefined ? {} : { pullRequest }),
return { repositoryId: canonicalId, event, candidate, targetRef, ...(pullRequest === undefined ? {} : { pullRequest }),
base, checkpointHead, checkpointBase: git('merge-base', base, checkpointHead) };
}

Expand Down
32 changes: 32 additions & 0 deletions tests/checkpoint-proof.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,35 @@ test('CLI resolves a checkout subdirectory before rejecting source-tree proof ou
await assert.rejects(access(marker)); await assert.rejects(access(join(root, 'docs', 'handoff')));
} finally { await rm(root, { recursive: true, force: true }); }
});


for (const event of ['pull_request', 'push']) {
for (const [origin, input] of [
['fixture/test', 'github.com/fixture/test'],
['FIXTURE/TEST', 'github.com/Fixture/Test'],
['Fixture/Test', 'GITHUB.COM/FIXTURE/TEST'],
]) {
test(`${event} scope canonicalizes ${origin} origin and ${input} input`, async () => {
const candidate = 'a'.repeat(40), base = 'b'.repeat(40);
const canonicalId = 'github.com/Fixture/Test';
const repository = { fullName: origin, identity: `github.com/${origin}` };
const options = { repositoryId: input, event, candidate, ...(event === 'pull_request' ? { pullRequest: '5' } : {}) };
const dependencies = {
repository,
github: path => path.endsWith('/pulls') ? [] : { full_name: 'Fixture/Test' },
git: (...args) => args[0] === 'merge-base' || args.at(-1) === `${candidate}^` || args.at(-1) === 'origin/main' ? base : candidate,
remote: async ref => event === 'pull_request' && ref === 'refs/heads/main' ? base : candidate,
};
const resolved = await resolveProofScope(options, dependencies);
assert.equal(resolved.repositoryId, canonicalId);
assert.equal(JSON.parse(proofTransport(resolved, bytes).text).repositoryId, canonicalId);
assert.deepEqual(await resolveProofScope({ ...options, repositoryId: resolved.repositoryId }, dependencies), resolved);
for (const wrong of ['github.com/foreign/test', 'github.com/fixture/foreign']) {
await assert.rejects(resolveProofScope({ ...options, repositoryId: wrong }, dependencies), /Repository mismatch/);
}
for (const full_name of ['Foreign/Test', 'Fixture/Foreign']) {
await assert.rejects(resolveProofScope(options, { ...dependencies, github: () => ({ full_name }) }), /Canonical repository mismatch/);
}
});
}
}
Loading