auto_approve_collaborator_pr #51
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Auto approve collaborator PRs | |
on: | |
repository_dispatch: | |
types: auto_approve_collaborator_pr | |
jobs: | |
approve: | |
name: Check validity & approve PR | |
runs-on: ubuntu-latest | |
steps: | |
- name: Get target repo info | |
uses: actions/github-script@v7 | |
id: repo-info | |
with: | |
script: | | |
const [owner, repo] = context.payload.client_payload.repo.split('/'); | |
core.setOutput('owner', owner); | |
core.setOutput('repo', repo); | |
- name: Generate access token | |
id: generate-token | |
uses: actions/create-github-app-token@v1 | |
with: | |
app-id: ${{ vars.RESTRICTED_ACTIONS_APP_ID }} | |
private-key: ${{ secrets.RESTRICTED_ACTIONS_APP_KEY }} | |
owner: ${{ steps.repo-info.outputs.owner }} | |
repositories: ${{ steps.repo-info.outputs.repo }} | |
- name: "Check if bot's review was dismissed (pull_request_review.dismissed only)" | |
id: re-review | |
uses: actions/github-script@v7 | |
env: | |
APP_SLUG: ${{ steps.generate-token.outputs.app-slug }} | |
REVIEWER_ID: ${{ github.event.client_payload.reviewer_id }} | |
with: | |
github-token: ${{ steps.generate-token.outputs.token }} | |
script: | | |
const reviewer_id = process.env.REVIEWER_ID; | |
console.log(reviewer_id); | |
if (reviewer_id === "0") { | |
console.log("No reviewer ID provided, continuing with re-review"); | |
core.setOutput('skip', 'false'); | |
} else { | |
console.log("Reviewer ID provided: " + reviewer_id); | |
const user_info = await github.rest.users.getByUsername({ | |
username: process.env.APP_SLUG + '[bot]' | |
}); | |
const bot_user_id = user_info.data.id; | |
if (bot_user_id != reviewer_id) { | |
console.log(`Not the bot's review that was dismissed (expected user ID: ${bot_user_id}, got: ${reviewer_id})`); | |
console.log('Skipping re-review'); | |
core.setOutput('skip', 'true'); | |
} else { | |
console.log("Bot's review was dismissed, continuing with re-review"); | |
core.setOutput('skip', 'false'); | |
} | |
} | |
- name: Get PR author | |
if: ${{ steps.re-review.outputs.skip != 'true' }} | |
uses: actions/github-script@v7 | |
id: pr-info | |
with: | |
github-token: ${{ steps.generate-token.outputs.token }} | |
script: | | |
const [owner, repo] = context.payload.client_payload.repo.split('/'); | |
console.log('Owner: ' + owner + ' / Repo: ' + repo + ' / Pull request ID: ' + context.payload.client_payload.pull_request_id); | |
const pr_info = await github.rest.pulls.get({ | |
owner: owner, | |
repo: repo, | |
pull_number: context.payload.client_payload.pull_request_id, | |
}); | |
console.log(pr_info.data.user.login); | |
console.log(pr_info.data.user.type); | |
core.setOutput('author', pr_info.data.user.login); | |
core.setOutput('bot', pr_info.data.user.type != 'User'); | |
- name: Get access level of user to repository | |
if: ${{ steps.re-review.outputs.skip != 'true' }} | |
uses: actions/github-script@v7 | |
id: check-access | |
env: | |
PR_AUTHOR: ${{ steps.pr-info.outputs.author }} | |
with: | |
github-token: ${{ steps.generate-token.outputs.token }} | |
result-encoding: string | |
script: | | |
console.log(process.env.PR_AUTHOR); | |
const [owner, repo] = context.payload.client_payload.repo.split('/'); | |
const perm_info = await github.rest.repos.getCollaboratorPermissionLevel({ | |
owner: owner, | |
repo: repo, | |
username: process.env.PR_AUTHOR, | |
}); | |
console.log(perm_info); | |
return perm_info.data.permission == 'admin' || perm_info.data.permission == 'write'; | |
- name: Approve PR | |
uses: actions/github-script@v7 | |
if: ${{ steps.re-review.outputs.skip != 'true' && steps.pr-info.outputs.bot == 'false' && steps.check-access.outputs.result == 'true' }} | |
with: | |
github-token: ${{ steps.generate-token.outputs.token }} | |
script: | | |
const [owner, repo] = context.payload.client_payload.repo.split('/'); | |
await github.rest.pulls.createReview({ | |
owner: owner, | |
repo: repo, | |
pull_number: context.payload.client_payload.pull_request_id, | |
event: 'APPROVE', | |
}); |