diff --git a/.github/workflows/review.yml b/.github/workflows/review.yml new file mode 100644 index 0000000000..cc6afa0e8f --- /dev/null +++ b/.github/workflows/review.yml @@ -0,0 +1,83 @@ +name: Pull request reviews + +on: + pull_request_review: + types: [submitted] + +jobs: + check-author: + if: github.event.review.state == 'approved' + runs-on: ubuntu-latest + permissions: + pull-requests: write + contents: read + issues: write + steps: + - name: Check if PR author is a contributor + uses: actions/github-script@v7 + with: + script: | + const { pull_request, review } = context.payload; + const { owner, repo } = context.repo; + + const author = pull_request.user.login; + const reviewer = review.user.login; + + if (reviewer === author) { + core.info("Author reviewed their own pull request, skipping."); + return; + } + + const getPermissionLevel = async (username) => { + const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ + username, + owner, + repo, + }); + + return data.permission; + }; + + const isMaintainer = async (username) => + ["maintain", "admin"].includes(await getPermissionLevel(username)); + + const isContributor = async (username) => + ["write", "maintain", "admin"].includes(await getPermissionLevel(username)); + + if (await isContributor(author)) { + core.info(`Author @${author} already has write access.`); + return; + } + + if (!await isMaintainer(reviewer)) { + core.info(`Reviewer @${reviewer} is not a maintainer.`); + return; + } + + const comments = await github.paginate(github.rest.issues.listComments, { + issue_number: pull_request.number, + per_page: 100, + owner, + repo, + }); + + const nudgeMarker = ""; + + if (comments.some(c => (c.body || "").includes(nudgeMarker))) { + core.info("Reviewer has already been nudged."); + return; + }; + + const body = `Hey @${reviewer}! Thanks for the approval. Please ` + + `make sure to add ${author} to jj-vcs/contributors so they ` + + "can merge their pull request. Feel free to also drop a note " + + "in Discord so they can get their contributor role. Thanks!"; + + await github.rest.issues.createComment({ + issue_number: pull_request.number, + body: nudgeMarker + body, + owner, + repo, + }); + + core.notice(`Pinged ${reviewer} to add ${author} to jj-vcs/contributors.`);