|
| 1 | +// Execute the privileged workflow handler with mocked GitHub responses. |
| 2 | +const fs = require('node:fs'); |
| 3 | +const path = require('node:path'); |
| 4 | +const workflow = fs.readFileSync(path.join(__dirname, '../../.github/workflows/planner-main-merge.yml'), 'utf8'); |
| 5 | +const script = workflow.split(' script: |\n')[1].split('\n').map(line => line.slice(12)).join('\n'); |
| 6 | +const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor; |
| 7 | +const handler = new AsyncFunction('github', 'context', 'core', script); |
| 8 | + |
| 9 | +async function verifyMergeGuards() { |
| 10 | + for (const scenario of ['valid', 'stale-head', 'foreign-repo', 'draft', 'stale-main', 'extra-file', 'empty-diff', 'no-pr']) { |
| 11 | + let merged = false; |
| 12 | + let failed = false; |
| 13 | + const pr = { |
| 14 | + number: 12, |
| 15 | + head: {repo: {full_name: scenario === 'foreign-repo' ? 'else/repo' : 'o/r'}, sha: scenario === 'stale-head' ? 'old' : 'new'}, |
| 16 | + draft: scenario === 'draft', |
| 17 | + }; |
| 18 | + const github = { |
| 19 | + rest: { |
| 20 | + pulls: { |
| 21 | + list: async () => ({data: scenario === 'no-pr' ? [] : [pr]}), |
| 22 | + get: async () => ({data: pr}), |
| 23 | + listFiles: () => {}, |
| 24 | + merge: async args => { |
| 25 | + if (args.sha !== 'new' || args.pull_number !== 12) throw Error('Wrong merge target'); |
| 26 | + merged = true; |
| 27 | + }, |
| 28 | + }, |
| 29 | + repos: {compareCommitsWithBasehead: async () => ({data: { |
| 30 | + merge_base_commit: {sha: scenario === 'stale-main' ? 'old' : 'base'}, |
| 31 | + base_commit: {sha: 'base'}, |
| 32 | + }})}, |
| 33 | + }, |
| 34 | + paginate: async () => scenario === 'empty-diff' ? [] : [{filename: scenario === 'extra-file' ? 'other.rs' : 'Cargo.toml'}], |
| 35 | + }; |
| 36 | + await handler(github, {repo: {owner: 'o', repo: 'r'}, payload: {workflow_run: {head_sha: 'new'}}}, { |
| 37 | + info: () => {}, setFailed: () => { failed = true; }, |
| 38 | + }); |
| 39 | + if (merged !== (scenario === 'valid')) throw Error(`Unexpected merge: ${scenario}`); |
| 40 | + if (failed !== ['extra-file', 'empty-diff'].includes(scenario)) throw Error(`Unexpected failure: ${scenario}`); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +verifyMergeGuards().catch(error => { console.error(error); process.exitCode = 1; }); |
0 commit comments