|
| 1 | +name: Format (Web Edits) |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + format: |
| 13 | + # Only attempt to push if the PR branch is in the same repo (not a fork) |
| 14 | + if: ${{ github.event.pull_request.head.repo.full_name == github.repository }} |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Detect if PR contains Web UI commits |
| 19 | + id: webui |
| 20 | + uses: actions/github-script@v7 |
| 21 | + with: |
| 22 | + script: | |
| 23 | + const { owner, repo } = context.repo; |
| 24 | + const pr = context.payload.pull_request.number; |
| 25 | +
|
| 26 | + // List commits on the PR |
| 27 | + const commits = await github.paginate( |
| 28 | + github.rest.pulls.listCommits, |
| 29 | + { owner, repo, pull_number: pr, per_page: 100 } |
| 30 | + ); |
| 31 | +
|
| 32 | + // GitHub web editor commits are typically committed by `web-flow` |
| 33 | + const hasWebFlow = commits.some(c => c.committer?.login === "web-flow"); |
| 34 | +
|
| 35 | + core.setOutput("is_webui", hasWebFlow ? "true" : "false"); |
| 36 | +
|
| 37 | + - name: Checkout PR branch |
| 38 | + if: ${{ steps.webui.outputs.is_webui == 'true' }} |
| 39 | + uses: actions/checkout@v4 |
| 40 | + with: |
| 41 | + ref: ${{ github.event.pull_request.head.ref }} |
| 42 | + fetch-depth: 0 |
| 43 | + |
| 44 | + - name: Setup Node |
| 45 | + if: ${{ steps.webui.outputs.is_webui == 'true' }} |
| 46 | + uses: actions/setup-node@v4 |
| 47 | + with: |
| 48 | + node-version: 20 |
| 49 | + cache: npm |
| 50 | + |
| 51 | + - name: Install deps |
| 52 | + if: ${{ steps.webui.outputs.is_webui == 'true' }} |
| 53 | + run: npm ci |
| 54 | + |
| 55 | + - name: Get changed files |
| 56 | + if: ${{ steps.webui.outputs.is_webui == 'true' }} |
| 57 | + id: changed |
| 58 | + uses: actions/github-script@v7 |
| 59 | + with: |
| 60 | + script: | |
| 61 | + const { owner, repo } = context.repo; |
| 62 | + const pr = context.payload.pull_request.number; |
| 63 | +
|
| 64 | + const files = await github.paginate( |
| 65 | + github.rest.pulls.listFiles, |
| 66 | + { owner, repo, pull_number: pr, per_page: 100 } |
| 67 | + ); |
| 68 | +
|
| 69 | + // Only include added or modified files (not removed) |
| 70 | + const paths = files |
| 71 | + .filter(f => f.status !== "removed") |
| 72 | + .map(f => f.filename); |
| 73 | +
|
| 74 | + core.setOutput("files", paths.join("\n")); |
| 75 | +
|
| 76 | + - name: Format changed files |
| 77 | + if: ${{ steps.webui.outputs.is_webui == 'true' && steps.changed.outputs.files != '' }} |
| 78 | + run: | |
| 79 | + echo "${{ steps.changed.outputs.files }}" | xargs npx prettier --write --ignore-unknown |
| 80 | +
|
| 81 | + - name: Commit & push (only if changes) |
| 82 | + if: ${{ steps.webui.outputs.is_webui == 'true' }} |
| 83 | + run: | |
| 84 | + if git diff --quiet; then |
| 85 | + echo "No changes." |
| 86 | + exit 0 |
| 87 | + fi |
| 88 | + git config user.name "github-actions[bot]" |
| 89 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 90 | + git add -A |
| 91 | + git commit -m "chore: format files (web UI PR)" |
| 92 | + git push |
0 commit comments