Files
anomalyco__opentui/.github/workflows/format.yml
2026-09-04 21:09:49 +02:00

168 lines
5.8 KiB
YAML

name: Format PR
on:
issue_comment:
types: [created]
jobs:
format-pr:
if: |
github.event.issue.pull_request &&
github.event.issue.state == 'open' &&
github.event.comment.body == '/fmt' &&
contains(fromJson('["OWNER","MEMBER"]'), github.event.comment.author_association)
runs-on: blacksmith-4vcpu-ubuntu-2404
permissions:
contents: write
issues: write
pull-requests: read
steps:
- name: React to trigger comment
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions \
-f content='eyes'
- name: Get PR details
id: pr-details
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api /repos/${{ github.repository }}/pulls/${{ github.event.issue.number }} > pr_data.json
echo "number=${{ github.event.issue.number }}" >> $GITHUB_OUTPUT
echo "head_ref=$(jq -r .head.ref pr_data.json)" >> $GITHUB_OUTPUT
echo "head_repo=$(jq -r .head.repo.full_name pr_data.json)" >> $GITHUB_OUTPUT
- name: Determine run mode
id: run-mode
run: |
if [ "${{ steps.pr-details.outputs.head_repo }}" = "${{ github.repository }}" ]; then
echo "run=true" >> $GITHUB_OUTPUT
echo "skip_reason=" >> $GITHUB_OUTPUT
exit 0
fi
echo "run=false" >> $GITHUB_OUTPUT
echo "skip_reason=fork" >> $GITHUB_OUTPUT
- name: Comment skipped reason
if: steps.run-mode.outputs.run != 'true'
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if [ "${{ steps.run-mode.outputs.skip_reason }}" = "fork" ]; then
BODY='Skipping `/fmt`: this PR comes from a fork, and this workflow cannot push commits to fork branches with `GITHUB_TOKEN`.'
else
BODY='Skipping `/fmt` for this PR.'
fi
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/issues/${{ steps.pr-details.outputs.number }}/comments \
-f body="$BODY"
- name: Checkout PR branch
if: steps.run-mode.outputs.run == 'true'
uses: actions/checkout@v6
with:
ref: ${{ steps.pr-details.outputs.head_ref }}
fetch-depth: 0
- name: Setup Bun
if: steps.run-mode.outputs.run == 'true'
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.4.1
- name: Install dependencies
if: steps.run-mode.outputs.run == 'true'
run: bun install --frozen-lockfile
- name: Run formatter
if: steps.run-mode.outputs.run == 'true'
run: bun fmt
- name: Commit formatting changes
id: commit
if: steps.run-mode.outputs.run == 'true'
run: |
if [ -z "$(git status --porcelain)" ]; then
echo "committed=false" >> $GITHUB_OUTPUT
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "style: run bun fmt"
git push origin "HEAD:${{ steps.pr-details.outputs.head_ref }}"
echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
echo "committed=true" >> $GITHUB_OUTPUT
- name: Comment result
if: steps.run-mode.outputs.run == 'true'
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if [ "${{ steps.commit.outputs.committed }}" = "true" ]; then
BODY="Ran \`bun fmt\` and pushed commit \`${{ steps.commit.outputs.sha }}\`."
else
BODY="Ran \`bun fmt\`. No formatting changes were needed."
fi
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/issues/${{ steps.pr-details.outputs.number }}/comments \
-f body="$BODY"
- name: React to comment on successful format run
if: success() && steps.run-mode.outputs.run == 'true'
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions \
-f content='hooray'
- name: React to comment when skipped
if: success() && steps.run-mode.outputs.run != 'true'
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions \
-f content='confused'
- name: React to comment on failure
if: failure()
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions \
-f content='-1'