|
| 1 | +name: Require maintainer approval |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + branches: [main] |
| 7 | + pull_request_review: |
| 8 | + types: [submitted, dismissed] |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + pull-requests: read |
| 13 | + statuses: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + gate: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + if: github.event.pull_request.base.ref == 'main' |
| 19 | + steps: |
| 20 | + - name: Check for maintainer approval |
| 21 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 22 | + with: |
| 23 | + script: | |
| 24 | + const MAINTAINERS = ['imran-siddique', 'pforest', 'podcastinator', 'Qiang-Xu']; |
| 25 | +
|
| 26 | + const author = context.payload.pull_request.user.login; |
| 27 | + if (MAINTAINERS.includes(author)) { |
| 28 | + core.info('Author ' + author + ' is a maintainer, skipping gate'); |
| 29 | + return; |
| 30 | + } |
| 31 | +
|
| 32 | + const prNumber = context.payload.pull_request.number; |
| 33 | + const { data: pr } = await github.rest.pulls.get({ |
| 34 | + owner: context.repo.owner, |
| 35 | + repo: context.repo.repo, |
| 36 | + pull_number: prNumber, |
| 37 | + }); |
| 38 | + const headSha = pr.head.sha; |
| 39 | +
|
| 40 | + const reviews = await github.paginate(github.rest.pulls.listReviews, { |
| 41 | + owner: context.repo.owner, |
| 42 | + repo: context.repo.repo, |
| 43 | + pull_number: prNumber, |
| 44 | + }); |
| 45 | +
|
| 46 | + const latestByMaintainer = new Map(); |
| 47 | + for (const review of reviews) { |
| 48 | + if ( |
| 49 | + review.user && |
| 50 | + review.user.type === 'User' && |
| 51 | + MAINTAINERS.includes(review.user.login) && |
| 52 | + review.state !== 'COMMENTED' |
| 53 | + ) { |
| 54 | + latestByMaintainer.set(review.user.login, review); |
| 55 | + } |
| 56 | + } |
| 57 | +
|
| 58 | + const approved = [...latestByMaintainer.values()].some( |
| 59 | + (r) => r.state === 'APPROVED' && r.commit_id === headSha |
| 60 | + ); |
| 61 | +
|
| 62 | + if (!approved) { |
| 63 | + core.setFailed('Waiting for maintainer approval of the current head commit before merging.'); |
| 64 | + } |
| 65 | +
|
0 commit comments