|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +const assert = require('node:assert/strict'); |
| 5 | +const test = require('node:test'); |
| 6 | +const checkIssueAcceptance = require('./check_issue_acceptance.cjs'); |
| 7 | + |
| 8 | +async function check({ permission = 'read', issues = [], body = '', permissionError } = {}) { |
| 9 | + const failures = []; |
| 10 | + const statuses = []; |
| 11 | + const github = { |
| 12 | + rest: { |
| 13 | + pulls: { |
| 14 | + get: async () => ({ data: { |
| 15 | + number: 7, user: { login: 'contributor' }, head: { sha: 'pr-head' }, body, |
| 16 | + } }), |
| 17 | + }, |
| 18 | + repos: { |
| 19 | + getCollaboratorPermissionLevel: async ({ username }) => { |
| 20 | + assert.equal(username, 'contributor'); |
| 21 | + if (permissionError) throw Object.assign(new Error('lookup failed'), { status: permissionError }); |
| 22 | + return { data: { permission } }; |
| 23 | + }, |
| 24 | + createCommitStatus: async status => { statuses.push(status); }, |
| 25 | + }, |
| 26 | + issues: { listLabelsOnIssue() {} }, |
| 27 | + }, |
| 28 | + graphql: async () => ({ repository: { pullRequest: { closingIssuesReferences: { |
| 29 | + nodes: issues.map(issue => ({ |
| 30 | + number: issue.number, |
| 31 | + url: `https://github.com/${issue.owner || 'NVIDIA'}/OpenShell/issues/${issue.number}`, |
| 32 | + repository: { name: 'OpenShell', owner: { login: issue.owner || 'NVIDIA' } }, |
| 33 | + })), |
| 34 | + pageInfo: { hasNextPage: false, endCursor: null }, |
| 35 | + } } } }), |
| 36 | + paginate: async (_method, { issue_number }) => issues.find(issue => issue.number === issue_number).labels || [], |
| 37 | + }; |
| 38 | + await checkIssueAcceptance({ |
| 39 | + github, |
| 40 | + context: { |
| 41 | + repo: { owner: 'NVIDIA', repo: 'OpenShell' }, |
| 42 | + payload: { pull_request: { number: 7 } }, |
| 43 | + actor: 'maintainer', sha: 'base-commit', serverUrl: 'https://github.com', runId: 42, |
| 44 | + }, |
| 45 | + core: { info() {}, setFailed: message => failures.push(message) }, |
| 46 | + }); |
| 47 | + assert.deepEqual(statuses.map(status => status.state), ['pending', failures.length ? 'failure' : 'success']); |
| 48 | + assert(statuses.every(status => status.sha === 'pr-head' && status.context === 'PR Issue Acceptance')); |
| 49 | + return failures; |
| 50 | +} |
| 51 | + |
| 52 | +for (const permission of ['write', 'maintain', 'admin']) { |
| 53 | + test(`${permission} access allows an issue-less PR`, async () => { |
| 54 | + assert.deepEqual(await check({ permission }), []); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +for (const body of ['', 'No issue required: typo fix', '<!-- No issue required: example -->']) { |
| 59 | + test(`community PR without linked issues fails for body ${JSON.stringify(body)}`, async () => { |
| 60 | + const failures = await check({ body }); |
| 61 | + assert.equal(failures.length, 1); |
| 62 | + assert.match(failures[0], /must link an issue with state:accepted/); |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +test('all linked issues must be accepted, regardless of PR body or triggering actor', async () => { |
| 67 | + const failures = await check({ |
| 68 | + body: 'No issue required: small fix', |
| 69 | + issues: [{ number: 1, labels: [{ name: 'state:accepted' }] }, { number: 2 }], |
| 70 | + }); |
| 71 | + assert.equal(failures.length, 1); |
| 72 | + assert.match(failures[0], /issues\/2/); |
| 73 | +}); |
| 74 | + |
| 75 | +test('accepted issues in the target repository pass', async () => { |
| 76 | + assert.deepEqual(await check({ issues: [{ number: 1, labels: [{ name: 'state:accepted' }] }] }), []); |
| 77 | +}); |
| 78 | + |
| 79 | +test('accepted issues in another repository cannot bypass the gate', async () => { |
| 80 | + const failures = await check({ issues: [{ number: 1, owner: 'contributor', labels: [{ name: 'state:accepted' }] }] }); |
| 81 | + assert.equal(failures.length, 1); |
| 82 | + assert.match(failures[0], /Acceptance must be recorded in NVIDIA\/OpenShell/); |
| 83 | +}); |
| 84 | + |
| 85 | +test('a non-collaborator still needs an accepted issue', async () => { |
| 86 | + assert.equal((await check({ permissionError: 404, body: 'No issue required: typo fix' })).length, 1); |
| 87 | +}); |
| 88 | + |
| 89 | +test('permission lookup failures do not grant an exemption', async () => { |
| 90 | + await assert.rejects(check({ permissionError: 403 }), /lookup failed/); |
| 91 | +}); |
0 commit comments