Skip to content

Auto Approve

Auto Approve #265

Workflow file for this run

name: Auto Approve
on:
workflow_run:
workflows: [Tests]
types: [completed]
jobs:
approve:
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.pull_requests[0] != null
permissions:
contents: read
pull-requests: write
steps:
- name: Approve PR
uses: actions/github-script@v7
with:
script: |
// workflow_run has pull_requests array directly
if (!github.event.workflow_run.pull_requests || github.event.workflow_run.pull_requests.length === 0) {
console.log('No PR associated with this workflow run');
return;
}
const pullNumber = github.event.workflow_run.pull_requests[0].number;
console.log(`Processing PR #${pullNumber}`);
// Get the full PR details
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullNumber
});
// Check if PR is from a fork (dependabot, etc.)
// Only approve PRs from trusted authors
const trustedAssociations = ['MEMBER', 'OWNER', 'COLLABORATOR'];
if (pr.head.repo === null) {
console.log('Skipping approval for fork PR');
return;
}
if (!trustedAssociations.includes(pr.author_association)) {
console.log(`Skipping approval for PR author with association: ${pr.author_association}`);
return;
}
// Get existing reviews
const { data: reviews } = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullNumber
});
const botApproved = reviews.some(r =>
r.user.login === 'github-actions[bot]' && r.state === 'APPROVED'
);
if (!botApproved) {
await github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullNumber,
event: 'APPROVE',
body: '✅ All pipeline checks passed. PR auto-approved by GitHub Actions.'
});
console.log(`Approved PR #${pullNumber}`);
} else {
console.log(`PR #${pullNumber} already approved`);
}