Skip to content

Commit 8af6b73

Browse files
authored
Prettier check now generates a diff comment on PR (cotes2020#2085)
Splitted prettier GitHub action into two: - `on push`, only runs on direct pushes, if test fails generates an artifact (that lasts for 3 days) with an html version of the changes needed to pass prettier test - `on PR`, only runs on PRs, if test fails comments on the PR with the HTML content of the diff I couldn't actually test the `on PR` version since it needs to be on a PR in the master branch, so this will only be triggered after this PR is accepted, and for the next PR that fails prettier test. PS: currently the artifact is a zip file with the html inside. It is not currently possible to generate it other way, we have to wait for [this issue](actions/upload-artifact#14) to be closed. --------- Signed-off-by: George Araújo <[email protected]>
1 parent 3927200 commit 8af6b73

File tree

3 files changed

+75
-26
lines changed

3 files changed

+75
-26
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Prettier code formatter (PR)
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
- main
8+
workflow_dispatch:
9+
10+
jobs:
11+
check:
12+
# available images: https://github.com/actions/runner-images#available-images
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout 🛎️
16+
uses: actions/checkout@v4
17+
- name: Setup Node.js ⚙️
18+
uses: actions/setup-node@v4
19+
- name: Install Prettier 💾
20+
run: npm install --save-dev --save-exact prettier @shopify/prettier-plugin-liquid
21+
- name: Prettier Check 🔎
22+
id: prettier
23+
run: npx prettier . --check
24+
- name: Create diff 📝
25+
# https://docs.github.com/en/actions/learn-github-actions/expressions#failure
26+
if: ${{ failure() }}
27+
run: |
28+
npx prettier . --write
29+
git diff -- . ':(exclude)package-lock.json' ':(exclude)package.json' > diff.txt
30+
npm install -g diff2html-cli
31+
diff2html -i file -s side -F diff.html -- diff.txt
32+
- name: PR comment with html diff
33+
# https://docs.github.com/en/actions/learn-github-actions/expressions#failure-with-conditions
34+
if: ${{ failure() && steps.prettier.conclusion == 'failure' }}
35+
uses: thollander/actions-comment-pull-request@v2
36+
with:
37+
filePath: diff.html
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Prettier code formatter (Push)
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- main
8+
workflow_dispatch:
9+
10+
jobs:
11+
check:
12+
# available images: https://github.com/actions/runner-images#available-images
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout 🛎️
16+
uses: actions/checkout@v4
17+
- name: Setup Node.js ⚙️
18+
uses: actions/setup-node@v4
19+
- name: Install Prettier 💾
20+
run: npm install --save-dev --save-exact prettier @shopify/prettier-plugin-liquid
21+
- name: Prettier Check 🔎
22+
id: prettier
23+
run: npx prettier . --check
24+
- name: Create diff 📝
25+
# https://docs.github.com/en/actions/learn-github-actions/expressions#failure
26+
if: ${{ failure() }}
27+
run: |
28+
npx prettier . --write
29+
git diff -- . ':(exclude)package-lock.json' ':(exclude)package.json' > diff.txt
30+
npm install -g diff2html-cli
31+
diff2html -i file -s side -F diff.html -- diff.txt
32+
- name: Upload html diff
33+
if: ${{ failure() && steps.prettier.conclusion == 'failure' }}
34+
uses: actions/upload-artifact@v4
35+
with:
36+
name: HTML Diff
37+
path: diff.html
38+
retention-days: 3

.github/workflows/prettier.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)