|
| 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 | + |
| 21 | + return { |
| 22 | + removedLabels, |
| 23 | + graphqlCalls: [], |
| 24 | + graphql: async function (_query, variables) { |
| 25 | + this.graphqlCalls.push(variables); |
| 26 | + }, |
| 27 | + rest: { |
| 28 | + issues: { |
| 29 | + removeLabel: async ({ name }) => { |
| 30 | + removedLabels.push(name); |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +function createContext(overrides = {}) { |
| 38 | + return { |
| 39 | + repo: { owner: 'hiero-ledger', repo: 'hiero-sdk-python' }, |
| 40 | + payload: { |
| 41 | + review: { state: 'changes_requested' }, |
| 42 | + pull_request: { |
| 43 | + number: 42, |
| 44 | + node_id: 'PR_node_42', |
| 45 | + draft: false, |
| 46 | + user: { login: 'contributor', type: 'User' }, |
| 47 | + labels: [ |
| 48 | + { name: 'queue:committers' }, |
| 49 | + { name: 'status: ready-to-merge' }, |
| 50 | + { name: 'some other label' }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + ...overrides, |
| 54 | + }, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +describe('revision-guard index', () => { |
| 59 | + beforeEach(() => { |
| 60 | + delete process.env.REVISION_GUARD_MANAGED_LABELS; |
| 61 | + }); |
| 62 | + |
| 63 | + afterEach(() => { |
| 64 | + delete process.env.REVISION_GUARD_MANAGED_LABELS; |
| 65 | + }); |
| 66 | + |
| 67 | + it('converts a ready PR to draft and removes only managed labels', async () => { |
| 68 | + const handler = freshRequire(); |
| 69 | + const github = createGithubMock(); |
| 70 | + const context = createContext(); |
| 71 | + |
| 72 | + await handler({ github, context, core: { info() {} } }); |
| 73 | + |
| 74 | + assert.deepEqual(github.graphqlCalls, [{ pullRequestId: 'PR_node_42' }]); |
| 75 | + assert.deepEqual(github.removedLabels, [ |
| 76 | + 'queue:committers', |
| 77 | + 'status: ready-to-merge', |
| 78 | + ]); |
| 79 | + }); |
| 80 | + |
| 81 | + it('skips bot-authored PRs', async () => { |
| 82 | + const handler = freshRequire(); |
| 83 | + const github = createGithubMock(); |
| 84 | + const context = createContext({ |
| 85 | + pull_request: { |
| 86 | + number: 43, |
| 87 | + node_id: 'PR_node_43', |
| 88 | + draft: false, |
| 89 | + user: { login: 'github-actions[bot]', type: 'Bot' }, |
| 90 | + labels: [{ name: 'queue:committers' }], |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + await handler({ github, context, core: { info() {} } }); |
| 95 | + |
| 96 | + assert.equal(github.graphqlCalls.length, 0); |
| 97 | + assert.equal(github.removedLabels.length, 0); |
| 98 | + }); |
| 99 | + |
| 100 | + it('skips already-draft PRs', async () => { |
| 101 | + const handler = freshRequire(); |
| 102 | + const github = createGithubMock(); |
| 103 | + const context = createContext({ |
| 104 | + pull_request: { |
| 105 | + number: 44, |
| 106 | + node_id: 'PR_node_44', |
| 107 | + draft: true, |
| 108 | + user: { login: 'contributor', type: 'User' }, |
| 109 | + labels: [{ name: 'queue:committers' }], |
| 110 | + }, |
| 111 | + }); |
| 112 | + |
| 113 | + await handler({ github, context, core: { info() {} } }); |
| 114 | + |
| 115 | + assert.equal(github.graphqlCalls.length, 0); |
| 116 | + assert.equal(github.removedLabels.length, 0); |
| 117 | + }); |
| 118 | + |
| 119 | + it('uses configurable managed labels', async () => { |
| 120 | + process.env.REVISION_GUARD_MANAGED_LABELS = 'custom: one, custom: two'; |
| 121 | + const handler = freshRequire(); |
| 122 | + const github = createGithubMock(); |
| 123 | + const context = createContext({ |
| 124 | + pull_request: { |
| 125 | + number: 45, |
| 126 | + node_id: 'PR_node_45', |
| 127 | + draft: false, |
| 128 | + user: { login: 'contributor', type: 'User' }, |
| 129 | + labels: [ |
| 130 | + { name: 'custom: one' }, |
| 131 | + { name: 'queue:committers' }, |
| 132 | + { name: 'custom: two' }, |
| 133 | + ], |
| 134 | + }, |
| 135 | + }); |
| 136 | + |
| 137 | + await handler({ github, context, core: { info() {} } }); |
| 138 | + |
| 139 | + assert.deepEqual(github.removedLabels, ['custom: one', 'custom: two']); |
| 140 | + }); |
| 141 | +}); |
0 commit comments