|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +module.exports = async function checkIssueAcceptance({ github, context, core }) { |
| 5 | + const { owner, repo } = context.repo; |
| 6 | + const { data: pr } = await github.rest.pulls.get({ |
| 7 | + owner, repo, pull_number: context.payload.pull_request.number, |
| 8 | + }); |
| 9 | + const author = pr.user.login; |
| 10 | + |
| 11 | + async function evaluate() { |
| 12 | + try { |
| 13 | + const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ |
| 14 | + owner, repo, username: author, |
| 15 | + }); |
| 16 | + // The REST API maps maintain to write and triage to read. |
| 17 | + if (['write', 'maintain', 'admin'].includes(data.permission)) { |
| 18 | + core.info(`${author} has repository write access; acceptance gate passed.`); |
| 19 | + return; |
| 20 | + } |
| 21 | + } catch (error) { |
| 22 | + if (error.status !== 404) throw error; |
| 23 | + } |
| 24 | + |
| 25 | + const issues = []; |
| 26 | + let cursor = null; |
| 27 | + do { |
| 28 | + const result = await github.graphql(` |
| 29 | + query($owner: String!, $repo: String!, $number: Int!, $cursor: String) { |
| 30 | + repository(owner: $owner, name: $repo) { |
| 31 | + pullRequest(number: $number) { |
| 32 | + closingIssuesReferences(first: 100, after: $cursor) { |
| 33 | + nodes { number url repository { name owner { login } } } |
| 34 | + pageInfo { hasNextPage endCursor } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + `, { owner, repo, number: pr.number, cursor }); |
| 40 | + const linked = result.repository.pullRequest.closingIssuesReferences; |
| 41 | + issues.push(...linked.nodes); |
| 42 | + cursor = linked.pageInfo.hasNextPage ? linked.pageInfo.endCursor : null; |
| 43 | + } while (cursor); |
| 44 | + |
| 45 | + if (issues.length === 0) { |
| 46 | + return 'Community PRs must link an issue with state:accepted using ' |
| 47 | + + 'Fixes #NNN or Closes #NNN.'; |
| 48 | + } |
| 49 | + |
| 50 | + const unaccepted = []; |
| 51 | + for (const issue of issues) { |
| 52 | + if (`${issue.repository.owner.login}/${issue.repository.name}`.toLowerCase() |
| 53 | + !== `${owner}/${repo}`.toLowerCase()) { |
| 54 | + return `Acceptance must be recorded in ${owner}/${repo}, not ${issue.url}.`; |
| 55 | + } |
| 56 | + const labels = await github.paginate(github.rest.issues.listLabelsOnIssue, { |
| 57 | + owner: issue.repository.owner.login, |
| 58 | + repo: issue.repository.name, |
| 59 | + issue_number: issue.number, |
| 60 | + per_page: 100, |
| 61 | + }); |
| 62 | + if (!labels.some(label => label.name === 'state:accepted')) { |
| 63 | + unaccepted.push(issue.url); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (unaccepted.length) { |
| 68 | + return 'Community PRs require state:accepted on every linked issue. ' |
| 69 | + + `Ask a maintainer to accept: ${unaccepted.join(', ')}. ` |
| 70 | + + 'After acceptance, rerun this workflow.'; |
| 71 | + } |
| 72 | + core.info(`All ${issues.length} linked issue(s) have state:accepted.`); |
| 73 | + } |
| 74 | + |
| 75 | + const failure = await evaluate(); |
| 76 | + if (failure) core.setFailed(failure); |
| 77 | +}; |
0 commit comments