Skip to content

Commit 3c5c393

Browse files
Uses custom action to run prettier on PRs and on pushes to main (#2564)
Also adds note to docs for running prettier as contributor.
1 parent fad2fde commit 3c5c393

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

.github/workflows/prettier.yaml

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
name: Prettier
2-
# minimally modified from https://github.com/creyD/prettier_action#example-2-using-the-only_changed-or-same_commit-option-on-pr
32

43
on:
54
pull_request:
6-
branches:
5+
branches:
6+
- main
7+
types: [opened, synchronize, reopened]
8+
push:
9+
branches:
710
- main
811

912
jobs:
@@ -14,23 +17,38 @@ jobs:
1417
- name: Checkout
1518
uses: actions/checkout@v4
1619
with:
17-
ref: ${{github.event.pull_request.head.ref}}
18-
repository: ${{github.event.pull_request.head.repo.full_name}}
19-
fetch-depth: 0
20-
21-
- name: Check if PR is from a fork
22-
id: check_fork
23-
run: echo "::set-output name=is_fork::${{ github.event.pull_request.head.repo.fork }}"
20+
fetch-depth: 0 # Needed to compare with origin/main
2421

25-
- name: Prettify code
26-
id: prettify
27-
uses: creyD/[email protected]
22+
- name: Set up Node.js
23+
uses: actions/setup-node@v4
2824
with:
29-
prettier_options: --write **/index.html --print-width 200
30-
only_changed: True
31-
commit_message: "chore: prettier"
32-
continue-on-error: true
33-
34-
- name: Handle commit failure
35-
if: ${{ steps.prettify.outcome == 'failure' && steps.check_fork.outputs.is_fork == 'true' }}
36-
run: echo "Please run prettier locally"
25+
node-version: 'lts/*'
26+
27+
- name: Install Prettier
28+
run: npm install --global [email protected]
29+
30+
- name: Get changed files and run Prettier
31+
id: run_prettier
32+
run: |
33+
git fetch origin main
34+
35+
changed_files=$(git diff --name-only origin/main...HEAD | grep -E '\.(js|json|css|html)$' || true)
36+
37+
if [[ -n "$changed_files" ]]; then
38+
echo "$changed_files" | xargs prettier --write --print-width 200
39+
echo "changed=true" >> "$GITHUB_OUTPUT"
40+
else
41+
echo "changed=false" >> "$GITHUB_OUTPUT"
42+
fi
43+
44+
- name: Commit and push changes
45+
if: steps.run_prettier.outputs.changed == 'true'
46+
run: |
47+
git config --local user.name "github-actions[bot]"
48+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
49+
50+
git add .
51+
git commit -m "Chore: Format files with Prettier"
52+
53+
# Push to the same branch this workflow was triggered from
54+
git push origin HEAD:${{ github.head_ref || github.ref_name }}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ When preparing GitHub pull requests:
8080
* Provide a complete summary and description for each pull request. The Working Group needs to understand the rationale for proposed changes. This description may need to be very detailed in some cases, or may be quite brief, for example if providing a change to address a spelling issue.
8181
* Following the editorial documentation below will help prepare a pull request that is ready for inclusion with minimal editing.
8282
* Keep it up-to-date with the base branch, for example by selecting "rebase" under the "Update branch options" menu on the GitHub web interface.
83+
* Run prettier to preserve spec formatting. For consistency, please use `--print-width 200`.
8384

8485
When a pull request is accepted by the Working Group, an editor will integrate changes. Pull requests and issues that are accepted by the working group will be merged into the source documents and the commenter will receive a notification from GitHub that the pull request was accepted.
8586

0 commit comments

Comments
 (0)