|
26 | 26 | - 'review_requested' |
27 | 27 | - 'review_request_removed' |
28 | 28 |
|
29 | | -permissions: |
30 | | - actions: 'write' |
31 | | - contents: 'read' |
32 | | - id-token: 'write' |
33 | | - pull-requests: 'read' |
| 29 | +permissions: {} |
34 | 30 |
|
35 | 31 | concurrency: |
36 | | - group: '${{ github.workflow }}-${{ github.head_ref || github.ref }}' |
| 32 | + group: '${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}' |
37 | 33 | cancel-in-progress: true |
38 | 34 |
|
39 | 35 | jobs: |
40 | 36 | multi-approvers: |
41 | 37 | runs-on: 'ubuntu-latest' |
| 38 | + permissions: |
| 39 | + pull-requests: 'read' |
42 | 40 | steps: |
43 | 41 | - name: 'Multi-approvers' |
44 | | - uses: 'abcxyz/actions/.github/actions/multi-approvers@7c003a22a2308639325a29f9d4f4ff60781f6ce1' # main |
| 42 | + uses: 'actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3' # v9.0.0 |
| 43 | + env: |
| 44 | + REQUIRED_INTERNAL_APPROVALS: '2' |
| 45 | + # Existing automation bot allowlist migrated from previous config. |
| 46 | + USER_ID_ALLOWLIST: '25180681,55107282,122572305,78513119,49699333,70984784,44816363,205009765,56741989,224858768,104649659' |
45 | 47 | with: |
46 | | - team: 'googlers' |
47 | | - token: '${{ secrets.MULTI_APPROVERS_TOKEN }}' |
48 | | - user-id-allowlist: '25180681,55107282,122572305,78513119,49699333,70984784,44816363,205009765,56741989,224858768,104649659' |
| 48 | + retries: '2' |
| 49 | + script: | |
| 50 | + const INTERNAL_ROLES = new Set(['admin', 'maintain', 'write']); |
| 51 | + const INTERNAL_PERMISSIONS = new Set(['admin', 'write']); |
| 52 | +
|
| 53 | + const prNumber = context.payload.pull_request?.number; |
| 54 | + if (!prNumber) { |
| 55 | + core.setFailed('No pull_request found in event payload.'); |
| 56 | + return; |
| 57 | + } |
| 58 | +
|
| 59 | + const { owner, repo } = context.repo; |
| 60 | + const { data: pr } = await github.rest.pulls.get({ |
| 61 | + owner, |
| 62 | + repo, |
| 63 | + pull_number: prNumber, |
| 64 | + }); |
| 65 | +
|
| 66 | + const author = pr.user; |
| 67 | + if (!author?.login) { |
| 68 | + core.setFailed(`Pull request #${prNumber} has no author.`); |
| 69 | + return; |
| 70 | + } |
| 71 | +
|
| 72 | + const allowlist = new Set( |
| 73 | + (process.env.USER_ID_ALLOWLIST || '') |
| 74 | + .split(',') |
| 75 | + .map((id) => id.trim()) |
| 76 | + .filter(Boolean), |
| 77 | + ); |
| 78 | +
|
| 79 | + if (allowlist.has(String(author.id))) { |
| 80 | + core.info(`Author ${author.login} (${author.id}) is in user-id-allowlist.`); |
| 81 | + return; |
| 82 | + } |
| 83 | +
|
| 84 | + const permissionCache = new Map(); |
| 85 | + async function isInternalUser(username) { |
| 86 | + if (permissionCache.has(username)) { |
| 87 | + return permissionCache.get(username); |
| 88 | + } |
| 89 | + try { |
| 90 | + const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ |
| 91 | + owner, |
| 92 | + repo, |
| 93 | + username, |
| 94 | + }); |
| 95 | + const isInternal = |
| 96 | + INTERNAL_ROLES.has(data.role_name) || |
| 97 | + INTERNAL_PERMISSIONS.has(data.permission); |
| 98 | + core.info( |
| 99 | + `User ${username}: role_name=${data.role_name}, permission=${data.permission}, internal=${isInternal}`, |
| 100 | + ); |
| 101 | + permissionCache.set(username, isInternal); |
| 102 | + return isInternal; |
| 103 | + } catch (err) { |
| 104 | + if (err?.status === 404) { |
| 105 | + core.info(`User ${username} is not a collaborator (404), internal=false`); |
| 106 | + permissionCache.set(username, false); |
| 107 | + return false; |
| 108 | + } |
| 109 | + throw err; |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + if (await isInternalUser(author.login)) { |
| 114 | + core.info(`Author ${author.login} is an internal user.`); |
| 115 | + return; |
| 116 | + } |
| 117 | +
|
| 118 | + const requiredApprovals = Number.parseInt( |
| 119 | + process.env.REQUIRED_INTERNAL_APPROVALS || '2', |
| 120 | + 10, |
| 121 | + ); |
| 122 | + if (!Number.isInteger(requiredApprovals) || requiredApprovals < 1) { |
| 123 | + core.setFailed( |
| 124 | + `Invalid REQUIRED_INTERNAL_APPROVALS: ${process.env.REQUIRED_INTERNAL_APPROVALS}`, |
| 125 | + ); |
| 126 | + return; |
| 127 | + } |
| 128 | +
|
| 129 | + const reviews = await github.paginate(github.rest.pulls.listReviews, { |
| 130 | + owner, |
| 131 | + repo, |
| 132 | + pull_number: prNumber, |
| 133 | + per_page: 100, |
| 134 | + }); |
| 135 | +
|
| 136 | + // Sort chronologically so the latest substantive state per reviewer wins. |
| 137 | + const submittedTimestamp = (review) => { |
| 138 | + const parsed = Date.parse(review.submitted_at || ''); |
| 139 | + return Number.isFinite(parsed) ? parsed : 0; |
| 140 | + }; |
| 141 | + reviews.sort((a, b) => { |
| 142 | + const timeDiff = submittedTimestamp(a) - submittedTimestamp(b); |
| 143 | + if (timeDiff !== 0) { |
| 144 | + return timeDiff; |
| 145 | + } |
| 146 | + return (a.id || 0) - (b.id || 0); |
| 147 | + }); |
| 148 | +
|
| 149 | + const latestReviewByUser = new Map(); |
| 150 | + for (const review of reviews) { |
| 151 | + const reviewer = review.user; |
| 152 | + if (!reviewer?.id || !reviewer?.login) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + if (reviewer.id === author.id) { |
| 156 | + continue; |
| 157 | + } |
| 158 | + if (review.state === 'COMMENTED' || review.state === 'PENDING') { |
| 159 | + continue; |
| 160 | + } |
| 161 | + latestReviewByUser.set(reviewer.id, { |
| 162 | + login: reviewer.login, |
| 163 | + state: review.state, |
| 164 | + }); |
| 165 | + } |
| 166 | +
|
| 167 | + const internalApprovers = []; |
| 168 | + for (const review of latestReviewByUser.values()) { |
| 169 | + if (review.state !== 'APPROVED') { |
| 170 | + continue; |
| 171 | + } |
| 172 | + if (await isInternalUser(review.login)) { |
| 173 | + internalApprovers.push(review.login); |
| 174 | + } |
| 175 | + } |
| 176 | +
|
| 177 | + if (internalApprovers.length >= requiredApprovals) { |
| 178 | + core.info( |
| 179 | + `Found ${internalApprovers.length} internal approvals (${internalApprovers.join(', ')}), required ${requiredApprovals}.`, |
| 180 | + ); |
| 181 | + return; |
| 182 | + } |
| 183 | +
|
| 184 | + core.setFailed( |
| 185 | + `PR from external author ${author.login} requires ${requiredApprovals} internal approvals (write/maintain/admin), found ${internalApprovers.length}${internalApprovers.length ? ` (${internalApprovers.join(', ')})` : ''}.`, |
| 186 | + ); |
0 commit comments