Setup ESLint in order to lint JavaScript #2379
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Linting | |
# Trigger each time HEAD branch is updated in a pull request | |
# see https://github.com/orgs/community/discussions/26366 | |
on: | |
pull_request: | |
types: [opened, reopened, synchronize, ready_for_review] | |
jobs: | |
# Resources: | |
# number [1] being most important | |
# [1] https://github.com/actions/checkout/issues/520#issuecomment-1167205721 | |
# [2] https://robertfaldo.medium.com/commands-to-run-rubocop-and-specs-you-changed-in-your-branch-e6d2f2e4110b | |
# [3] https://community.atlassian.com/t5/Bitbucket-questions/Git-diff-show-different-files-than-PR-Pull-Request/qaq-p/2331786 | |
rubocop: | |
name: RuboCop (Ruby) | |
runs-on: ubuntu-latest | |
steps: | |
- name: 'Checkout PR branch (with test merge-commit)' | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 2 # to also fetch parent of PR | |
# Adapted from this great comment [1]. Git diff adapted from [2]. | |
# "|| test $? = 1;" is used to ignore the exit code of grep when no files | |
# are found matching the pattern. For the "three dots" ... syntax, see [3]. | |
- name: Get changed files | |
run: | | |
files=$(git diff --name-only --diff-filter=ACMR -r HEAD^1...HEAD | grep '\.rb$' || test $? = 1;) | |
printf "🎴 Changed ruby files: \n$files" | |
echo "CHANGED_FILES=$(echo ${files} | xargs)" >> $GITHUB_ENV | |
- name: Set up Ruby 3 | |
if: env.CHANGED_FILES != '' | |
uses: ruby/setup-ruby@v1 | |
with: | |
ruby-version: 3.1.4 | |
bundler-cache: true | |
- name: Run RuboCop | |
if: env.CHANGED_FILES != '' | |
run: | | |
echo "🚨 Running RuboCop version: $(bundle info rubocop | head -1)" | |
bundle exec rubocop --format github --fail-level 'convention' --force-exclusion -- $CHANGED_FILES | |
eslint: | |
name: ESLint (JS) | |
runs-on: ubuntu-latest | |
steps: | |
- name: 'Checkout PR branch' | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 2 # to also fetch parent of PR | |
# see notes above (in rubocop job) | |
- name: Get changed files | |
run: | | |
files=$(git diff --name-only --diff-filter=ACMR -r HEAD^1...HEAD | grep -e '\.js$' -e '\.js.erb$'|| test $? = 1;) | |
printf "🎴 Changed js files: \n$files" | |
echo "CHANGED_FILES=$(echo ${files} | xargs)" >> $GITHUB_ENV | |
- name: 'Setup Node.js' | |
if: env.CHANGED_FILES != '' | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' # End of Life (EOL): April 2026 | |
- name: 'Install dependencies' | |
if: env.CHANGED_FILES != '' | |
run: yarn install | |
- name: 'Run ESLint' | |
if: env.CHANGED_FILES != '' | |
run: yarn run eslint --ignore-path .gitignore --max-warnings 0 $CHANGED_FILES |