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
89 changes: 89 additions & 0 deletions apps/pythinker-code/test/scripts/check-nix-hash-fresh.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { execFileSync, spawnSync, type SpawnSyncReturns } from 'node:child_process';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';

import { afterEach, describe, expect, it } from 'vitest';

const checkScript = resolve(import.meta.dirname, '../../../../scripts/check-nix-hash-fresh.mjs');
const tempRoots: string[] = [];

afterEach(() => {
for (const root of tempRoots.splice(0)) rmSync(root, { recursive: true, force: true });
});

describe('check-nix-hash-fresh', () => {
it('uses current origin/main when a release branch tracks a stale upstream', () => {
const root = makeRepository('changeset-release/main');
const staleRelease = revParse(root, 'HEAD');
setRemoteRef(root, 'changeset-release/main', staleRelease);

git(root, ['switch', '-c', 'main']);
writeFileSync(join(root, 'pnpm-lock.yaml'), 'lock from current main\n');
commit(root, 'update lock on main');
const currentMain = revParse(root, 'HEAD');
setRemoteRef(root, 'main', currentMain);

git(root, ['switch', 'changeset-release/main']);
git(root, ['reset', '--hard', currentMain]);
writeFileSync(join(root, 'package.json'), '{"version":"1.0.1"}\n');
commit(root, 'version packages');

const result = runCheck(root);
expect(result.status, result.stderr).toBe(0);
});

it('keeps a lock-only branch change gated after that change reaches its upstream', () => {
const root = makeRepository('feature');
const currentMain = revParse(root, 'HEAD');
setRemoteRef(root, 'main', currentMain);

writeFileSync(join(root, 'pnpm-lock.yaml'), 'unmatched lock change\n');
commit(root, 'update lock without flake');
setRemoteRef(root, 'feature', revParse(root, 'HEAD'));
writeFileSync(join(root, 'README.md'), 'follow-up\n');
commit(root, 'add follow-up');

const result = runCheck(root);
expect(result.status, result.stderr).toBe(1);
expect(result.stderr).toContain('pnpm-lock.yaml changed on this branch but flake.nix did not');
});
});

function makeRepository(branch: string): string {
const root = mkdtempSync(join(tmpdir(), 'pythinker-nix-hash-check-'));
tempRoots.push(root);
git(root, ['init', '--initial-branch', branch]);
git(root, ['config', 'core.hooksPath', '.git/no-hooks']);
git(root, ['config', 'user.name', 'Test User']);
git(root, ['config', 'user.email', 'test@example.test']);
git(root, ['config', 'commit.gpgSign', 'false']);
git(root, ['remote', 'add', 'origin', join(root, 'unused-origin.git')]);
writeFileSync(join(root, 'pnpm-lock.yaml'), 'initial lock\n');
writeFileSync(join(root, 'flake.nix'), 'initial flake\n');
commit(root, 'initial');
return root;
}

function commit(root: string, message: string): void {
git(root, ['add', '.']);
git(root, ['commit', '-m', message]);
}

function setRemoteRef(root: string, branch: string, sha: string): void {
git(root, ['update-ref', `refs/remotes/origin/${branch}`, sha]);
git(root, ['config', `branch.${branch}.remote`, 'origin']);
git(root, ['config', `branch.${branch}.merge`, `refs/heads/${branch}`]);
}

function revParse(root: string, ref: string): string {
return git(root, ['rev-parse', ref]);
}

function git(root: string, args: string[]): string {
return execFileSync('git', args, { cwd: root, encoding: 'utf8' }).trim();
}

function runCheck(root: string): SpawnSyncReturns<string> {
return spawnSync(process.execPath, [checkScript], { cwd: root, encoding: 'utf8' });
}
5 changes: 4 additions & 1 deletion packages/oauth/src/managed-pythinker-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,11 @@ export function resolvePythinkerCodeOAuthKey(options: {
return PYTHINKER_CODE_OAUTH_KEY;
}

const publicEndpointIdentity = new TextEncoder().encode(
JSON.stringify({ oauthHost, baseUrl }),
);
const digest = createHash('sha256')
.update(JSON.stringify({ oauthHost, baseUrl }))
.update(publicEndpointIdentity)
.digest('hex')
.slice(0, 16);
return `${PYTHINKER_CODE_SCOPED_OAUTH_KEY_PREFIX}${digest}`;
Expand Down
3 changes: 1 addition & 2 deletions packages/oauth/test/managed-pythinker-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ describe('provisionManagedPythinkerCodeConfig', () => {
baseUrl: 'https://api.dev.example.test/coding/v1',
});

expect(devKey).not.toBe(PYTHINKER_CODE_OAUTH_KEY);
expect(devKey).toMatch(/^oauth\/pythinker-code-env-[a-f0-9]{16}$/);
expect(devKey).toBe('oauth/pythinker-code-env-51d35a57390d1c7e');
expect(
resolvePythinkerCodeOAuthKey({
oauthHost: 'https://auth.dev.example.test/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,11 @@ function resolvePythinkerCodeCredentialName() {
}

// Keep this in sync with packages/oauth/src/managed-pythinker-code.ts.
const publicEndpointIdentity = new TextEncoder().encode(
JSON.stringify({ oauthHost, baseUrl }),
);
const digest = createHash('sha256')
.update(JSON.stringify({ oauthHost, baseUrl }))
.update(publicEndpointIdentity)
.digest('hex')
.slice(0, 16);
return `pythinker-code-env-${digest}`;
Expand Down
21 changes: 13 additions & 8 deletions scripts/check-nix-hash-fresh.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ function git(args) {
}

function resolveBaseRef() {
try {
const upstream = git(['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{u}']);
if (upstream) return upstream;
} catch {
// no upstream configured
for (const [ref, fullRef] of [
['origin/HEAD', 'refs/remotes/origin/HEAD'],
['origin/main', 'refs/remotes/origin/main'],
]) {
try {
git(['show-ref', '--verify', '--quiet', fullRef]);
return ref;
} catch {
// ref is unavailable
}
}
try {
git(['show-ref', '--verify', '--quiet', 'refs/remotes/origin/HEAD']);
return 'origin/HEAD';
const upstream = git(['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{u}']);
return upstream || null;
} catch {
return null;
}
Expand Down Expand Up @@ -57,7 +62,7 @@ if (!lockChanged || flakeChanged) {

console.error(
'❌ pnpm-lock.yaml changed on this branch but flake.nix did not.\n' +
" flake.nix pins a fetchPnpmDeps hash of pnpm-lock.yaml; CI's nix build will fail with a hash mismatch.\n" +
' flake.nix pins a fetchPnpmDeps hash of pnpm-lock.yaml, so its hash may be stale.\n' +
' Refresh it: run a nix build (e.g. `nix build .#pythinker-code`), take the sha256-... hash\n' +
' from the mismatch error, paste it into the `hash = "sha256-...";` line in flake.nix, commit, and re-push.',
);
Expand Down
Loading