Files
maxffarrell 1c6bc548ae chore: update workflows (#2630)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-04-13 05:59:32 -07:00

151 lines
5.1 KiB
YAML

name: Auto-format PR on Comment
on:
issue_comment:
types: [created]
jobs:
format:
# only run on PR comments
if: github.event.issue.pull_request
runs-on: macos-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Check trigger and permissions
uses: actions/github-script@v8
id: check-permission
with:
script: |
const commentBody = context.payload.comment.body;
const hasTrigger = commentBody.includes('/format') || commentBody.includes('/autoformat');
if (!hasTrigger) {
console.log('Comment does not contain trigger phrase. Skipping.');
core.setOutput('should-continue', 'false');
return false;
}
const { data: perm } = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: context.payload.comment.user.login
});
const hasWriteAccess = ['admin', 'write'].includes(perm.permission);
if (!hasWriteAccess) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '❌ Sorry, you need write access to trigger auto-formatting.'
});
core.setOutput('should-continue', 'false');
return false;
}
core.setOutput('should-continue', 'true');
return true;
- name: Get PR details
if: steps.check-permission.outputs.should-continue == 'true'
uses: actions/github-script@v8
id: get-pr
with:
script: |
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
core.setOutput('head-ref', pr.head.ref);
core.setOutput('head-sha', pr.head.sha);
core.setOutput('base-ref', pr.base.ref);
core.setOutput('pr-number', pr.number);
- name: React to comment
if: steps.check-permission.outputs.should-continue == 'true'
uses: actions/github-script@v8
with:
script: |
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket'
});
- name: Checkout PR branch
if: steps.check-permission.outputs.should-continue == 'true'
uses: actions/checkout@v6
with:
ref: ${{ steps.get-pr.outputs.head-ref }}
- uses: pnpm/action-setup@v5
if: steps.check-permission.outputs.should-continue == 'true'
- uses: actions/setup-node@v6
if: steps.check-permission.outputs.should-continue == 'true'
with:
node-version: 24
cache: pnpm
- name: Install dependencies
if: steps.check-permission.outputs.should-continue == 'true'
run: pnpm install
- name: Run formatter
if: steps.check-permission.outputs.should-continue == 'true'
run: pnpm format .
- name: Check for changes
if: steps.check-permission.outputs.should-continue == 'true'
id: check-changes
run: |
if [[ -z $(git status --porcelain) ]]; then
echo "has-changes=false" >> $GITHUB_OUTPUT
else
echo "has-changes=true" >> $GITHUB_OUTPUT
fi
- name: Commit and push changes
if: steps.check-permission.outputs.should-continue == 'true' && steps.check-changes.outputs.has-changes == 'true'
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add .
git commit -m "chore: auto-format code via GitHub Action
Triggered by @${{ github.event.comment.user.login }} in PR #${{ steps.get-pr.outputs.pr-number }}"
git push origin HEAD:${{ steps.get-pr.outputs.head-ref }}
- name: Comment on PR with result
uses: actions/github-script@v8
if: always() && steps.check-permission.outputs.should-continue == 'true'
with:
script: |
const hasChanges = '${{ steps.check-changes.outputs.has-changes }}' === 'true';
const success = '${{ job.status }}' === 'success';
let body;
if (!success) {
body = '❌ Auto-formatting failed. Please check the workflow logs.';
} else if (hasChanges) {
body = '✅ Code has been auto-formatted and changes have been pushed to this PR.';
} else {
body = '✅ No formatting changes needed. Code is already properly formatted.';
}
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body
});