Skip to content

Commit

Permalink
Prettier check now generates a diff comment on PR (alshedivat#2085)
Browse files Browse the repository at this point in the history
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 <george.gcac@gmail.com>
  • Loading branch information
george-gca committed Jan 22, 2024
1 parent 30ee72c commit 806425f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 26 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/prettier-on-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Prettier code formatter (PR)

on:
pull_request:
branches:
- master
- main
workflow_dispatch:

jobs:
check:
# available images: https://github.com/actions/runner-images#available-images
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Setup Node.js ⚙️
uses: actions/setup-node@v4
- name: Install Prettier 💾
run: npm install --save-dev --save-exact prettier @shopify/prettier-plugin-liquid
- name: Prettier Check 🔎
id: prettier
run: npx prettier . --check
- name: Create diff 📝
# https://docs.github.com/en/actions/learn-github-actions/expressions#failure
if: ${{ failure() }}
run: |
npx prettier . --write
git diff -- . ':(exclude)package-lock.json' ':(exclude)package.json' > diff.txt
npm install -g diff2html-cli
diff2html -i file -s side -F diff.html -- diff.txt
- name: PR comment with html diff
# https://docs.github.com/en/actions/learn-github-actions/expressions#failure-with-conditions
if: ${{ failure() && steps.prettier.conclusion == 'failure' }}
uses: thollander/actions-comment-pull-request@v2
with:
filePath: diff.html
38 changes: 38 additions & 0 deletions .github/workflows/prettier-on-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Prettier code formatter (Push)

on:
push:
branches:
- master
- main
workflow_dispatch:

jobs:
check:
# available images: https://github.com/actions/runner-images#available-images
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Setup Node.js ⚙️
uses: actions/setup-node@v4
- name: Install Prettier 💾
run: npm install --save-dev --save-exact prettier @shopify/prettier-plugin-liquid
- name: Prettier Check 🔎
id: prettier
run: npx prettier . --check
- name: Create diff 📝
# https://docs.github.com/en/actions/learn-github-actions/expressions#failure
if: ${{ failure() }}
run: |
npx prettier . --write
git diff -- . ':(exclude)package-lock.json' ':(exclude)package.json' > diff.txt
npm install -g diff2html-cli
diff2html -i file -s side -F diff.html -- diff.txt
- name: Upload html diff
if: ${{ failure() && steps.prettier.conclusion == 'failure' }}
uses: actions/upload-artifact@v4
with:
name: HTML Diff
path: diff.html
retention-days: 3
26 changes: 0 additions & 26 deletions .github/workflows/prettier.yml

This file was deleted.

0 comments on commit 806425f

Please sign in to comment.