Skip to content

Add automated PR review workflow with /review command #5

Add automated PR review workflow with /review command

Add automated PR review workflow with /review command #5

name: PR Review on Command
on:
issue_comment:
types: [created]
workflow_dispatch:
inputs:
pr_number:
description: "PR number to review"
required: true
type: number
pull_request:
types: [opened, synchronize, reopened]
push:
branches:
- '**'
permissions:
contents: read
pull-requests: write
issues: write
jobs:
debug:
runs-on: ubuntu-latest
steps:
- name: Debug info
run: |
echo "Event name: ${{ github.event_name }}"
echo "Ref: ${{ github.ref }}"
echo "Actor: ${{ github.actor }}"
check-comment:
runs-on: ubuntu-latest
outputs:
should_run: ${{ steps.check.outputs.result }}
pr_number: ${{ steps.get-pr.outputs.pr_number }}
steps:
- name: Get PR number
id: get-pr
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
elif [ "${{ github.event_name }}" == "push" ]; then
echo "pr_number=0" >> $GITHUB_OUTPUT
elif [ "${{ github.event_name }}" == "issue_comment" ]; then
echo "pr_number=${{ github.event.issue.number }}" >> $GITHUB_OUTPUT
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "pr_number=${{ inputs.pr_number }}" >> $GITHUB_OUTPUT
fi
- name: Check conditions
id: check
run: |
echo "Event: ${{ github.event_name }}"
if [ "${{ github.event_name }}" == "pull_request" ] || [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "result=true" >> $GITHUB_OUTPUT
else
echo "result=false" >> $GITHUB_OUTPUT
fi
- name: Add reaction
if: github.event_name == 'issue_comment'
uses: actions/github-script@v7
with:
script: |
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'eyes'
});
run-review:
needs: [check-comment]
if: always() && needs.check-comment.outputs.should_run == 'true' && needs.check-comment.outputs.pr_number != '0'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Get PR details
id: pr-details
uses: actions/github-script@v7
with:
script: |
const prNumber = ${{ needs.check-comment.outputs.pr_number }};
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
core.setOutput('pr_title', pr.title);
core.setOutput('pr_author', pr.user.login);
core.setOutput('files_changed', files.length);
- name: Check API key
id: check-key
run: |
if [ -n "${{ secrets.ANTHROPIC_API_KEY }}" ]; then
echo "has_key=true" >> $GITHUB_OUTPUT
else
echo "has_key=false" >> $GITHUB_OUTPUT
fi
- name: Run PR Reviewer
if: steps.check-key.outputs.has_key == 'true'
uses: docker/cagent-action@1f7ec0445e138a587639fc9c046076e22d184349
with:
agent: agentcatalog/github-action-pr-reviewer:2.0.0
mcp-gateway: true
anthropic-api-key: ${{ secrets.ANTHROPIC_API_KEY }}
github-token: ${{ secrets.GITHUB_TOKEN }}
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
continue-on-error: true
- name: Determine trigger source
id: trigger
run: |
EVENT="${{ github.event_name }}"
if [ "$EVENT" == "workflow_dispatch" ]; then
echo "source=Manual trigger" >> $GITHUB_OUTPUT
elif [ "$EVENT" == "pull_request" ]; then
echo "source=PR update" >> $GITHUB_OUTPUT
elif [ "$EVENT" == "issue_comment" ]; then
echo "source=@${{ github.event.comment.user.login }}" >> $GITHUB_OUTPUT
else
echo "source=$EVENT" >> $GITHUB_OUTPUT
fi
- name: Post comment
uses: actions/github-script@v7
with:
script: |
const hasKey = '${{ steps.check-key.outputs.has_key }}' === 'true';
const prNumber = ${{ needs.check-comment.outputs.pr_number }};
const triggeredBy = '${{ steps.trigger.outputs.source }}';
let body;
if (hasKey) {
body = `## 🤖 AI PR Review Complete\n\nThe automated PR review has been completed.\n\n**Triggered by:** ${triggeredBy}\n**Workflow:** [View Details](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})`;
} else {
body = `## 🤖 PR Review Test\n\n✅ Workflow test successful!\n\n**PR:** ${{ steps.pr-details.outputs.pr_title }}\n**Author:** @${{ steps.pr-details.outputs.pr_author }}\n**Files:** ${{ steps.pr-details.outputs.files_changed }}\n**Triggered by:** ${triggeredBy}\n\n⚠️ Add ANTHROPIC_API_KEY to enable AI reviews.`;
}
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: body
});
- name: Success reaction
if: success() && github.event_name == 'issue_comment'
uses: actions/github-script@v7
with:
script: |
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket'
});