|
| 1 | +const { describe, it, beforeEach, afterEach } = require('node:test'); |
| 2 | +const assert = require('node:assert/strict'); |
| 3 | + |
| 4 | +function freshRequire() { |
| 5 | + const indexPath = require.resolve('./index.js'); |
| 6 | + const helpersPath = require.resolve('./helpers/index.js'); |
| 7 | + const labelsPath = require.resolve('./helpers/labels.js'); |
| 8 | + const constantsPath = require.resolve('./helpers/constants.js'); |
| 9 | + |
| 10 | + delete require.cache[indexPath]; |
| 11 | + delete require.cache[helpersPath]; |
| 12 | + delete require.cache[labelsPath]; |
| 13 | + delete require.cache[constantsPath]; |
| 14 | + |
| 15 | + return require('./index.js'); |
| 16 | +} |
| 17 | + |
| 18 | +function createGithubMock() { |
| 19 | + const removedLabels = []; |
| 20 | + const pullUpdates = []; |
| 21 | + |
| 22 | + return { |
| 23 | + removedLabels, |
| 24 | + pullUpdates, |
| 25 | + rest: { |
| 26 | + issues: { |
| 27 | + removeLabel: async ({ name }) => { |
| 28 | + removedLabels.push(name); |
| 29 | + }, |
| 30 | + }, |
| 31 | + pulls: { |
| 32 | + update: async (params) => { |
| 33 | + pullUpdates.push(params); |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +function createContext(overrides = {}) { |
| 41 | + return { |
| 42 | + repo: { owner: 'hiero-ledger', repo: 'hiero-sdk-python' }, |
| 43 | + payload: { |
| 44 | + review: { state: 'changes_requested' }, |
| 45 | + pull_request: { |
| 46 | + number: 42, |
| 47 | + node_id: 'PR_node_42', |
| 48 | + draft: false, |
| 49 | + user: { login: 'contributor', type: 'User' }, |
| 50 | + labels: [ |
| 51 | + { name: 'queue:committers' }, |
| 52 | + { name: 'status: ready-to-merge' }, |
| 53 | + { name: 'some other label' }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + ...overrides, |
| 57 | + }, |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +describe('revision-guard index', () => { |
| 62 | + beforeEach(() => { |
| 63 | + delete process.env.REVISION_GUARD_MANAGED_LABELS; |
| 64 | + }); |
| 65 | + |
| 66 | + afterEach(() => { |
| 67 | + delete process.env.REVISION_GUARD_MANAGED_LABELS; |
| 68 | + }); |
| 69 | + |
| 70 | + it('converts a ready PR to draft and removes only managed labels', async () => { |
| 71 | + const handler = freshRequire(); |
| 72 | + const github = createGithubMock(); |
| 73 | + const context = createContext(); |
| 74 | + |
| 75 | + await handler({ github, context, core: { info() {} } }); |
| 76 | + |
| 77 | + assert.deepEqual(github.pullUpdates, [ |
| 78 | + { owner: 'hiero-ledger', repo: 'hiero-sdk-python', pull_number: 42, draft: true }, |
| 79 | + ]); |
| 80 | + assert.deepEqual(github.removedLabels, [ |
| 81 | + 'queue:committers', |
| 82 | + 'status: ready-to-merge', |
| 83 | + ]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('skips bot-authored PRs', async () => { |
| 87 | + const handler = freshRequire(); |
| 88 | + const github = createGithubMock(); |
| 89 | + const context = createContext({ |
| 90 | + pull_request: { |
| 91 | + number: 43, |
| 92 | + node_id: 'PR_node_43', |
| 93 | + draft: false, |
| 94 | + user: { login: 'github-actions[bot]', type: 'Bot' }, |
| 95 | + labels: [{ name: 'queue:committers' }], |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + await handler({ github, context, core: { info() {} } }); |
| 100 | + |
| 101 | + assert.equal(github.pullUpdates.length, 0); |
| 102 | + assert.equal(github.removedLabels.length, 0); |
| 103 | + }); |
| 104 | + |
| 105 | + it('skips already-draft PRs', async () => { |
| 106 | + const handler = freshRequire(); |
| 107 | + const github = createGithubMock(); |
| 108 | + const context = createContext({ |
| 109 | + pull_request: { |
| 110 | + number: 44, |
| 111 | + node_id: 'PR_node_44', |
| 112 | + draft: true, |
| 113 | + user: { login: 'contributor', type: 'User' }, |
| 114 | + labels: [{ name: 'queue:committers' }], |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + await handler({ github, context, core: { info() {} } }); |
| 119 | + |
| 120 | + assert.equal(github.pullUpdates.length, 0); |
| 121 | + assert.equal(github.removedLabels.length, 0); |
| 122 | + }); |
| 123 | + |
| 124 | + it('uses configurable managed labels and still removes defaults', async () => { |
| 125 | + process.env.REVISION_GUARD_MANAGED_LABELS = 'custom: one, custom: two'; |
| 126 | + const handler = freshRequire(); |
| 127 | + const github = createGithubMock(); |
| 128 | + const context = createContext({ |
| 129 | + pull_request: { |
| 130 | + number: 45, |
| 131 | + node_id: 'PR_node_45', |
| 132 | + draft: false, |
| 133 | + user: { login: 'contributor', type: 'User' }, |
| 134 | + labels: [ |
| 135 | + { name: 'custom: one' }, |
| 136 | + { name: 'queue:committers' }, |
| 137 | + { name: 'custom: two' }, |
| 138 | + ], |
| 139 | + }, |
| 140 | + }); |
| 141 | + |
| 142 | + await handler({ github, context, core: { info() {} } }); |
| 143 | + |
| 144 | + // Draft conversion must also fire for configurable-label scenarios. |
| 145 | + assert.deepEqual(github.pullUpdates, [ |
| 146 | + { owner: 'hiero-ledger', repo: 'hiero-sdk-python', pull_number: 45, draft: true }, |
| 147 | + ]); |
| 148 | + // Custom labels AND the matching default (queue:committers) must both be removed. |
| 149 | + assert.deepEqual(github.removedLabels, ['queue:committers', 'custom: one', 'custom: two']); |
| 150 | + }); |
| 151 | +}); |
0 commit comments