Skip to content

Commit

Permalink
Merge pull request #2522 from KaelenProctor/feature/github-actions-on…
Browse files Browse the repository at this point in the history
…ly-changed-files-v3

GitHub actions linting (eslint/prettier) only for changed files
  • Loading branch information
dekkerglen authored Jan 7, 2025
2 parents 0951d2c + 956e71a commit 75acdde
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 17 deletions.
21 changes: 21 additions & 0 deletions .github/actions/prettier/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Matteo Agnoletto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
95 changes: 95 additions & 0 deletions .github/actions/prettier/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: 'Run Prettier with reviewdog'
description: '🐶 Run Prettier with reviewdog to improve code checking, formatting and review experience for your codebase.'
author: 'EPMatt'
inputs:
github_token:
description: 'GITHUB_TOKEN'
default: '${{ github.token }}'
required: false
workdir:
description: |
Working directory relative to the root directory.
This is where the action will look for a
package.json which declares Prettier as a dependency.
Please note that this is different from the directory
Prettier will run on, which is defined in the prettier_flags input.
Default is `.`.
default: '.'
required: false
### Flags for reviewdog ###
level:
description: |
Report level for reviewdog [info,warning,error].
Default is `error`.
default: 'error'
required: false
reporter:
description: |
Reporter of reviewdog command [github-check,github-pr-check,github-pr-review].
Default is `github-pr-check`.
default: 'github-pr-check'
required: false
filter_mode:
description: |
Filtering mode for the reviewdog command [added,diff_context,file,nofilter].
Default is `added`.
default: 'added'
required: false
fail_on_error:
description: |
Exit code for reviewdog when errors are found [true,false].
Default is `false`.
default: 'false'
required: false
reviewdog_flags:
description: |
Additional reviewdog flags.
Default is ``.
default: ''
required: false
tool_name:
description: 'Tool name to use for reviewdog reporter'
default: 'prettier'
required: false
### Flags for prettier ###
prettier_flags:
description: |
Flags and args to pass to Prettier.
If you override this input, please make sure to append to it the directory
which Prettier will run on.
The path provided here is relative to the workdir path, provided in the workdir input.
Default is `.`, which makes Prettier run on the path provided in the workdir input.
default: '.'
required: false
runs:
using: 'composite'
steps:
- uses: reviewdog/action-setup@v1
with:
reviewdog_version: v0.20.3
- run: .github/actions/prettier/script.sh
shell: bash
env:
INPUT_GITHUB_TOKEN: ${{ inputs.github_token }}
INPUT_WORKDIR: ${{ inputs.workdir }}
INPUT_LEVEL: ${{ inputs.level }}
INPUT_REPORTER: ${{ inputs.reporter }}
INPUT_FILTER_MODE: ${{ inputs.filter_mode }}
INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }}
INPUT_REVIEWDOG_FLAGS: ${{ inputs.reviewdog_flags }}
INPUT_TOOL_NAME: ${{ inputs.tool_name }}
INPUT_PRETTIER_FLAGS: ${{ inputs.prettier_flags }}
- if: ${{ inputs.reporter == 'github-pr-review' && always() }}
uses: reviewdog/action-suggester@v1
with:
github_token: ${{ inputs.github_token }}
tool_name: ${{ inputs.tool_name }}
level: ${{ inputs.level }}
filter_mode: ${{ inputs.filter_mode }}
fail_on_error: ${{ inputs.fail_on_error }}
reviewdog_flags: ${{ inputs.reviewdog_flags }}

# Ref: https://haya14busa.github.io/github-action-brandings/
branding:
icon: 'align-left'
color: 'blue'
1 change: 1 addition & 0 deletions .github/actions/prettier/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A copy of https://github.com/EPMatt/reviewdog-action-prettier but with reviewdog upgraded to latest, as the version used does not correctly check github token permissions in forks.
61 changes: 61 additions & 0 deletions .github/actions/prettier/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash
set -e

# Move to the provided workdir
cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit 1

# Install prettier
if [ ! -f "$(npm root)"/.bin/prettier ]; then
echo "::group::🔄 Running npm install to install prettier..."
npm install
echo "::endgroup::"
fi

if [ ! -f "$(npm root)"/.bin/prettier ]; then
echo "❌ Unable to locate or install prettier. Did you provide a workdir which contains a valid package.json?"
exit 1
else
echo ℹ️ prettier version: "$("$(npm root)"/.bin/prettier --version)"
fi

echo "::group::📝 Running prettier with reviewdog 🐶 ..."

export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"

# if reporter is github-pr-review, run prettier in write mode and report code suggestions
if [ "$INPUT_REPORTER" = "github-pr-review" ]; then
"$(npm root)"/.bin/prettier --write "${INPUT_PRETTIER_FLAGS}" 2>&1 \
| reviewdog \
-efm="%E[%trror] %f: %m (%l:%c)" \
-efm="%C[error]%r" \
-efm="%Z[error]%r" \
-efm="%-G%r" \
-name="${INPUT_TOOL_NAME}" \
-reporter="${INPUT_REPORTER}" \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
-level="${INPUT_LEVEL}" \
"${INPUT_REVIEWDOG_FLAGS}"
# else run prettier in check mode and report warnings and errors
else

# shellcheck disable=SC2086
"$(npm root)"/.bin/prettier --check "${INPUT_PRETTIER_FLAGS}" 2>&1 | sed --regexp-extended 's/(\[warn\].*)$/\1 File is not properly formatted./' \
| reviewdog \
-efm="%-G[warn] Code style issues found in the above file(s). Forgot to run Prettier%. File is not properly formatted." \
-efm="[%tarn] %f %m" \
-efm="%E[%trror] %f: %m (%l:%c)" \
-efm="%C[error]%r" \
-efm="%Z[error]%r" \
-efm="%-G%r" \
-name="${INPUT_TOOL_NAME}" \
-reporter="${INPUT_REPORTER}" \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
-level="${INPUT_LEVEL}" \
"${INPUT_REVIEWDOG_FLAGS}"
fi

exit_code=$?
echo "::endgroup::"
exit $exit_code
30 changes: 26 additions & 4 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
name: CI Tests

on: [push, pull_request]
# Pull request will trigger when PR is opened or updated, and more (https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request)
# Thus limit push checks to master branch so things run after the PR is merged. Without the limit multiple actions run on each PR
on:
pull_request:
push:
branches:
- 'master'

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 30

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 20
- name: Enable Corepack
run: corepack enable
- name: Setup
run: npm ci
- name: Check Code Style
run: npm run lint
- name: Run eslint on changed files
# Always run both eslint and prettier
if: always()
uses: tj-actions/eslint-changed-files@v25
with:
config_path: 'eslint.config.mjs'
extra_args: '--max-warnings=0'
reporter: github-pr-review
- name: Run Prettier on changed files
# Always run both eslint and prettier
if: always()
uses: ./.github/actions/prettier
with:
fail_on_error: true
filter_mode: file
level: warning
prettier_flags: '**/*.{js,jsx,ts,tsx}'
reporter: github-pr-review
- name: Build
run: npm run ci-build
11 changes: 0 additions & 11 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,6 @@ export default [
},
},
},
{
files: ['src/**/*'],
rules: {
'no-restricted-imports': [
'error',
{
patterns: ['./*', '../*'],
},
],
},
},
{
files: ['src/**/*.js'],
rules: {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"scripts": {
"build": "npm run nearley && tsc -b && npm run webpack && npx tailwindcss -i ./src/client/css/stylesheet.css -o ./public/css/stylesheet.css",
"lint": "npm run prettier-base --check && npm run eslint -- --max-warnings 0",
"lint": "npx prettier --check $(npm run --silent list-files); npx eslint --max-warnings 0 $(npm run --silent list-files)",
"list-files": "bash -c 'shopt -s nullglob\n echo {jobs,scripts,src}/{**/*,*}.{js,jsx,ts,tsx} force_update.js webpack.*.mjs eslint.config.mjs babel.config.mjs'",
"bash": "bash",
"nodemon": "nodemon --max-old-space-size=8192 --ignore src/client --ignore public --ignore private --ignore temp --ignore dist/pages --ignore __tests__ src/app.js",
"webpack": "NODE_OPTIONS=--max_old_space-size=18192 webpack --mode production --config webpack.prod.mjs",
Expand Down Expand Up @@ -195,4 +196,4 @@
"webpack-node-externals": "^3.0.0"
},
"packageManager": "[email protected]"
}
}

0 comments on commit 75acdde

Please sign in to comment.