From 5a6c2512915284f5f9d37040987438a6d9c160cc Mon Sep 17 00:00:00 2001 From: George <31376482+george-gca@users.noreply.github.com> Date: Mon, 22 Jan 2024 13:30:24 -0300 Subject: [PATCH] Prettier check now generates a diff comment on PR (#2085) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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](https://github.com/actions/upload-artifact/issues/14) to be closed. --------- Signed-off-by: George AraΓΊjo --- .github/workflows/prettier-on-pr.yml | 37 +++++++++++++++++++++++++ .github/workflows/prettier-on-push.yml | 38 ++++++++++++++++++++++++++ .github/workflows/prettier.yml | 26 ------------------ 3 files changed, 75 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/prettier-on-pr.yml create mode 100644 .github/workflows/prettier-on-push.yml delete mode 100644 .github/workflows/prettier.yml diff --git a/.github/workflows/prettier-on-pr.yml b/.github/workflows/prettier-on-pr.yml new file mode 100644 index 000000000000..5bb850ddf361 --- /dev/null +++ b/.github/workflows/prettier-on-pr.yml @@ -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 diff --git a/.github/workflows/prettier-on-push.yml b/.github/workflows/prettier-on-push.yml new file mode 100644 index 000000000000..719757daea1c --- /dev/null +++ b/.github/workflows/prettier-on-push.yml @@ -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 diff --git a/.github/workflows/prettier.yml b/.github/workflows/prettier.yml deleted file mode 100644 index c71cd3939580..000000000000 --- a/.github/workflows/prettier.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: Prettier code formatter - -on: - push: - branches: - - master - - main - 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 πŸ”Ž - run: npx prettier . --check