Skip to content

Commit

Permalink
Outsource retrieval of changed files to reusable action
Browse files Browse the repository at this point in the history
  • Loading branch information
Splines committed Dec 6, 2023
1 parent 5529bcf commit 92e329a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
39 changes: 39 additions & 0 deletions .github/actions/changed_files.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "Get changed files"
description: "Checks out the code and returns the filenames of files that have changed in the pull request"

inputs:
file-extensions:
# for example: "\.rb$" or something like "\.js$|\.js.erb$"
description: "Regex expressions for grep to filter for specific files"
required: true

outputs:
changed-files:
description: "A space-separated list of the files that have changed in the pull request"
value: ${{ steps.get-changed-files.outputs.files }}

runs:
# For a small tutorial on GitHub composite actions, see
# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
using: "composite"
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].
#
# 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
- name: Get changed files
id: get-changed-files
run: |
files_pretty=$(git diff --name-only --diff-filter=ACMR -r HEAD^1...HEAD | grep '${{inputs.file-extensions}}' || test $? = 1;)
printf "🎴 Changed files: \n$files_pretty"
echo "files=$(echo ${files_pretty} | xargs)" >> $GITHUB_ENV
30 changes: 12 additions & 18 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
name: RuboCop (Ruby)
runs-on: ubuntu-latest
steps:
- name: 'Checkout PR branch (with test merge-commit)'
- name: Checkout PR branch (with test merge-commit)
uses: actions/checkout@v4
with:
fetch-depth: 2 # to also fetch parent of PR
Expand Down Expand Up @@ -48,28 +48,22 @@ jobs:
name: ESLint (JS)
runs-on: ubuntu-latest
steps:
- name: 'Checkout PR branch'
uses: actions/checkout@v4
- name: Checkout code & get changed files
id: eslint-changed
uses: ./.github/actions/changed_files
with:
fetch-depth: 2 # to also fetch parent of PR
file-extensions: \.js$|\.js.erb$

# 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 != ''
- name: Setup Node.js
if: ${{ steps.eslint-changed.outputs.changed-files }} != ''
uses: actions/setup-node@v4
with:
node-version: '20' # End of Life (EOL): April 2026

- name: 'Install dependencies'
if: env.CHANGED_FILES != ''
- name: Install dependencies
if: ${{ steps.eslint-changed.outputs.changed-files }} != ''
run: yarn install

- name: 'Run ESLint'
if: env.CHANGED_FILES != ''
run: yarn run eslint --ignore-path .gitignore --max-warnings 0 $CHANGED_FILES
- name: Run ESLint
if: ${{ steps.eslint-changed.outputs.changed-files }} != ''
run: yarn run eslint --ignore-path .gitignore --max-warnings 0 ${{ steps.eslint-changed.outputs.changed-files }}

0 comments on commit 92e329a

Please sign in to comment.