|
| 1 | +name: Want LGTM |
| 2 | + |
| 3 | +# A reusable CI workflow to check if required LGTMs specified in the PR body are present. |
| 4 | +# |
| 5 | +# Usage in caller repositories (e.g. dart-lang/tools): |
| 6 | +# |
| 7 | +# name: Want LGTM |
| 8 | +# on: |
| 9 | +# pull_request_target: |
| 10 | +# types: [opened, synchronize, reopened, edited] |
| 11 | +# pull_request_review: |
| 12 | +# types: [submitted, edited, dismissed] |
| 13 | +# jobs: |
| 14 | +# want-lgtm: |
| 15 | +# uses: dart-lang/ecosystem/.github/workflows/want_lgtm.yaml@main |
| 16 | + |
| 17 | +on: |
| 18 | + workflow_call: |
| 19 | + |
| 20 | +permissions: |
| 21 | + pull-requests: read |
| 22 | + issues: read |
| 23 | + |
| 24 | +jobs: |
| 25 | + check-lgtm: |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - name: Check required LGTMs |
| 29 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 30 | + with: |
| 31 | + script: | |
| 32 | + const pr = context.payload.pull_request; |
| 33 | + const pull_number = pr ? pr.number : context.issue.number; |
| 34 | +
|
| 35 | + if (!pull_number) { |
| 36 | + console.log('No pull request context found.'); |
| 37 | + return; |
| 38 | + } |
| 39 | +
|
| 40 | + let body = pr ? pr.body : null; |
| 41 | + if (body === null || body === undefined) { |
| 42 | + const { data: pullRequest } = await github.rest.pulls.get({ |
| 43 | + owner: context.repo.owner, |
| 44 | + repo: context.repo.repo, |
| 45 | + pull_number: pull_number, |
| 46 | + }); |
| 47 | + body = pullRequest.body || ''; |
| 48 | + } |
| 49 | +
|
| 50 | + const requiredUsers = new Set(); |
| 51 | + let requireAll = false; |
| 52 | +
|
| 53 | + const lines = body.split(/\r?\n/); |
| 54 | + for (const line of lines) { |
| 55 | + const match = line.match(/(?:WANT|NEEDS)[_-]LGTM\s*[:=]\s*(.*)/i); |
| 56 | + if (match) { |
| 57 | + const val = match[1].trim(); |
| 58 | + if (val.toLowerCase() === 'all') { |
| 59 | + requireAll = true; |
| 60 | + } else { |
| 61 | + const parts = val.split(/[,]+/); |
| 62 | + for (let part of parts) { |
| 63 | + const tokens = part.trim().split(/\s+/); |
| 64 | + for (let token of tokens) { |
| 65 | + const cleaned = token.replace(/^[^a-zA-Z0-9]+|[^a-zA-Z0-9-]+$/g, '').toLowerCase(); |
| 66 | + if (cleaned.length > 0 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(cleaned)) { |
| 67 | + requiredUsers.add(cleaned); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + const reviews = await github.paginate(github.rest.pulls.listReviews, { |
| 76 | + owner: context.repo.owner, |
| 77 | + repo: context.repo.repo, |
| 78 | + pull_number: pull_number, |
| 79 | + }); |
| 80 | +
|
| 81 | + if (requireAll) { |
| 82 | + const { data: requested } = await github.rest.pulls.listRequestedReviewers({ |
| 83 | + owner: context.repo.owner, |
| 84 | + repo: context.repo.repo, |
| 85 | + pull_number: pull_number, |
| 86 | + }); |
| 87 | + if (requested && requested.users) { |
| 88 | + for (const u of requested.users) { |
| 89 | + requiredUsers.add(u.login.toLowerCase()); |
| 90 | + } |
| 91 | + } |
| 92 | + for (const r of reviews) { |
| 93 | + if (r.user) { |
| 94 | + requiredUsers.add(r.user.login.toLowerCase()); |
| 95 | + } |
| 96 | + } |
| 97 | + if (requiredUsers.size === 0) { |
| 98 | + core.setFailed('WANT_LGTM=all specified, but no reviewers are assigned to this PR.'); |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | +
|
| 103 | + if (!requireAll && requiredUsers.size === 0) { |
| 104 | + console.log('No WANT_LGTM requirement found in PR description.'); |
| 105 | + return; |
| 106 | + } |
| 107 | +
|
| 108 | + const reqList = Array.from(requiredUsers); |
| 109 | + console.log(`Required LGTMs from users: ${reqList.join(', ')}`); |
| 110 | +
|
| 111 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 112 | + owner: context.repo.owner, |
| 113 | + repo: context.repo.repo, |
| 114 | + issue_number: pull_number, |
| 115 | + }); |
| 116 | +
|
| 117 | + const userLatestState = {}; |
| 118 | + const userHasApproved = {}; |
| 119 | + const userHasLgtm = {}; |
| 120 | +
|
| 121 | + for (const review of reviews) { |
| 122 | + if (!review.user) continue; |
| 123 | + const user = review.user.login.toLowerCase(); |
| 124 | + if (['APPROVED', 'CHANGES_REQUESTED', 'DISMISSED'].includes(review.state)) { |
| 125 | + userLatestState[user] = review.state; |
| 126 | + } |
| 127 | + if (review.state === 'APPROVED') { |
| 128 | + userHasApproved[user] = true; |
| 129 | + } |
| 130 | + if (review.body && /\blgtm\b/i.test(review.body)) { |
| 131 | + userHasLgtm[user] = true; |
| 132 | + } |
| 133 | + } |
| 134 | +
|
| 135 | + for (const comment of comments) { |
| 136 | + if (!comment.user) continue; |
| 137 | + const user = comment.user.login.toLowerCase(); |
| 138 | + if (comment.body && /\blgtm\b/i.test(comment.body)) { |
| 139 | + userHasLgtm[user] = true; |
| 140 | + } |
| 141 | + } |
| 142 | +
|
| 143 | + const missing = []; |
| 144 | + const approved = []; |
| 145 | + for (const reqUser of reqList) { |
| 146 | + const latestState = userLatestState[reqUser]; |
| 147 | + const hasLgtmSignal = userHasApproved[reqUser] || userHasLgtm[reqUser]; |
| 148 | + if (hasLgtmSignal && latestState !== 'CHANGES_REQUESTED') { |
| 149 | + approved.push(reqUser); |
| 150 | + } else { |
| 151 | + missing.push(reqUser); |
| 152 | + } |
| 153 | + } |
| 154 | +
|
| 155 | + if (missing.length > 0) { |
| 156 | + core.setFailed(`Missing required LGTMs from: ${missing.join(', ')}`); |
| 157 | + } else { |
| 158 | + console.log(`All required LGTMs received from: ${approved.join(', ')}`); |
| 159 | + } |
0 commit comments