Skip to content

Commit 5441fad

Browse files
ntottenclaude
andcommitted
Format only PR-changed files in web edit workflow
Instead of running prettier on all **/*.md files, fetch the list of files changed in the PR via the GitHub API and format only those. This broadens formatting to all file types prettier supports while keeping the scope limited to what the PR actually touches. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0b0a24b commit 5441fad

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

.github/workflows/format.yaml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)