|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | +import { afterEach, describe, expect, it } from 'vitest' |
| 7 | + |
| 8 | +const script = fileURLToPath(new URL('../.github/scripts/resolve-audit-base.sh', import.meta.url)) |
| 9 | +const fixtures: string[] = [] |
| 10 | + |
| 11 | +function git(cwd: string, ...args: string[]): string { |
| 12 | + const result = spawnSync('git', args, { cwd, encoding: 'utf8' }) |
| 13 | + if (result.status !== 0) throw new Error(`git ${args.join(' ')}: ${result.stderr}`) |
| 14 | + return result.stdout.trim() |
| 15 | +} |
| 16 | + |
| 17 | +function fixture() { |
| 18 | + const root = mkdtempSync(join(tmpdir(), 'audit-base-')) |
| 19 | + fixtures.push(root) |
| 20 | + const origin = join(root, 'origin.git') |
| 21 | + const repo = join(root, 'repo') |
| 22 | + git(root, 'init', '--quiet', '--bare', origin) |
| 23 | + git(root, 'init', '--quiet', '-b', 'foundation', repo) |
| 24 | + git(repo, 'config', 'user.email', 'test@example.com') |
| 25 | + git(repo, 'config', 'user.name', 'Test') |
| 26 | + git(repo, 'config', 'commit.gpgsign', 'false') |
| 27 | + git(repo, 'remote', 'add', 'origin', origin) |
| 28 | + git(repo, 'commit', '--quiet', '--allow-empty', '-m', 'initial') |
| 29 | + git(repo, 'branch', 'parent') |
| 30 | + writeFileSync(join(repo, 'foundation.sql'), 'SELECT 1;\n') |
| 31 | + git(repo, 'add', '.') |
| 32 | + git(repo, 'commit', '--quiet', '-m', 'foundation') |
| 33 | + const before = git(repo, 'rev-parse', 'HEAD') |
| 34 | + git(repo, 'checkout', '--quiet', 'parent') |
| 35 | + writeFileSync(join(repo, 'upstream.sql'), 'SELECT 2;\n') |
| 36 | + git(repo, 'add', '.') |
| 37 | + git(repo, 'commit', '--quiet', '-m', 'upstream') |
| 38 | + const base = git(repo, 'rev-parse', 'HEAD') |
| 39 | + git(repo, 'checkout', '--quiet', 'foundation') |
| 40 | + git(repo, 'merge', '--quiet', '--no-ff', 'parent', '-m', 'merge parent') |
| 41 | + const head = git(repo, 'rev-parse', 'HEAD') |
| 42 | + git(repo, 'push', '--quiet', 'origin', 'parent', 'foundation') |
| 43 | + const pr = { |
| 44 | + state: 'open', |
| 45 | + head: { ref: 'foundation', sha: head, repo: { full_name: 'example/repo' } }, |
| 46 | + base: { sha: base }, |
| 47 | + } |
| 48 | + return { root, repo, base, before, head, pr } |
| 49 | +} |
| 50 | + |
| 51 | +function resolveBase(data: ReturnType<typeof fixture>, overrides: Record<string, string> = {}) { |
| 52 | + const output = join(data.root, 'output') |
| 53 | + writeFileSync(output, '') |
| 54 | + const result = spawnSync( |
| 55 | + 'bash', |
| 56 | + [ |
| 57 | + '--noprofile', |
| 58 | + '--norc', |
| 59 | + '-c', |
| 60 | + 'gh() { printf "%s" "$PR_RESPONSE"; return "$API_EXIT"; }; source "$AUDIT_SCRIPT"', |
| 61 | + ], |
| 62 | + { |
| 63 | + cwd: data.repo, |
| 64 | + encoding: 'utf8', |
| 65 | + env: { |
| 66 | + ...process.env, |
| 67 | + AUDIT_SCRIPT: script, |
| 68 | + GITHUB_EVENT_NAME: 'workflow_dispatch', |
| 69 | + GITHUB_REPOSITORY: 'example/repo', |
| 70 | + GITHUB_REF_TYPE: 'branch', |
| 71 | + GITHUB_REF_NAME: 'foundation', |
| 72 | + GITHUB_SHA: data.head, |
| 73 | + GITHUB_BASE_REF: 'parent', |
| 74 | + GITHUB_BEFORE: '', |
| 75 | + GITHUB_OUTPUT: output, |
| 76 | + PR_RESPONSE: JSON.stringify([[data.pr]]), |
| 77 | + API_EXIT: '0', |
| 78 | + ...overrides, |
| 79 | + }, |
| 80 | + } |
| 81 | + ) |
| 82 | + return { ...result, output: readFileSync(output, 'utf8').trim() } |
| 83 | +} |
| 84 | + |
| 85 | +afterEach(() => { |
| 86 | + for (const root of fixtures.splice(0)) rmSync(root, { recursive: true, force: true }) |
| 87 | +}) |
| 88 | + |
| 89 | +describe('diff audit base selection', () => { |
| 90 | + it('pins the actual PR base for a dispatched merge head instead of its first parent', () => { |
| 91 | + const data = fixture() |
| 92 | + const result = resolveBase(data) |
| 93 | + expect(result.status, result.stderr).toBe(0) |
| 94 | + expect(result.output).toBe(`ref=${data.base}`) |
| 95 | + expect(git(data.repo, 'diff', '--name-only', data.base, 'HEAD')).toBe('foundation.sql') |
| 96 | + expect(git(data.repo, 'diff', '--name-only', data.before, 'HEAD')).toBe('upstream.sql') |
| 97 | + }) |
| 98 | + |
| 99 | + it('fails closed when multiple open PRs across pages claim the dispatched branch', () => { |
| 100 | + const data = fixture() |
| 101 | + const result = resolveBase(data, { PR_RESPONSE: JSON.stringify([[data.pr], [data.pr]]) }) |
| 102 | + expect(result.status).not.toBe(0) |
| 103 | + expect(result.output).toBe('') |
| 104 | + }) |
| 105 | + |
| 106 | + it.each(['missing', 'moved', 'foreign', 'tag', 'api', 'invalid-base', 'fetch'] as const)( |
| 107 | + 'does not publish an audit base when resolution is %s', |
| 108 | + (failure) => { |
| 109 | + const data = fixture() |
| 110 | + const overrides: Record<string, string> = {} |
| 111 | + if (failure === 'missing') overrides.PR_RESPONSE = '[[]]' |
| 112 | + if (failure === 'moved') overrides.GITHUB_SHA = data.before |
| 113 | + if (failure === 'foreign') overrides.GITHUB_REPOSITORY = 'another/repo' |
| 114 | + if (failure === 'tag') overrides.GITHUB_REF_TYPE = 'tag' |
| 115 | + if (failure === 'api') overrides.API_EXIT = '73' |
| 116 | + if (failure === 'invalid-base') |
| 117 | + overrides.PR_RESPONSE = JSON.stringify([[{ ...data.pr, base: { sha: 'HEAD~1' } }]]) |
| 118 | + if (failure === 'fetch') |
| 119 | + git(data.repo, 'remote', 'set-url', 'origin', join(data.root, 'missing.git')) |
| 120 | + const result = resolveBase(data, overrides) |
| 121 | + expect(result.status).not.toBe(0) |
| 122 | + expect(result.output).toBe('') |
| 123 | + } |
| 124 | + ) |
| 125 | + |
| 126 | + it('keeps the normal PR base branch even if no API association is available', () => { |
| 127 | + const data = fixture() |
| 128 | + const result = resolveBase(data, { GITHUB_EVENT_NAME: 'pull_request', API_EXIT: '73' }) |
| 129 | + expect(result.status, result.stderr).toBe(0) |
| 130 | + expect(result.output).toBe('ref=origin/parent') |
| 131 | + expect(git(data.repo, 'rev-parse', 'origin/parent')).toBe(data.base) |
| 132 | + }) |
| 133 | + |
| 134 | + it('keeps the pre-push SHA for an existing branch', () => { |
| 135 | + const data = fixture() |
| 136 | + const result = resolveBase(data, { GITHUB_EVENT_NAME: 'push', GITHUB_BEFORE: data.before }) |
| 137 | + expect(result.status, result.stderr).toBe(0) |
| 138 | + expect(result.output).toBe(`ref=${data.before}`) |
| 139 | + }) |
| 140 | + |
| 141 | + it('keeps the first-parent fallback for a new branch push', () => { |
| 142 | + const data = fixture() |
| 143 | + const result = resolveBase(data, { GITHUB_EVENT_NAME: 'push', GITHUB_BEFORE: '0'.repeat(40) }) |
| 144 | + expect(result.status, result.stderr).toBe(0) |
| 145 | + expect(result.output).toBe('ref=HEAD~1') |
| 146 | + }) |
| 147 | +}) |
0 commit comments