Skip to content

Commit

Permalink
add only_changed input to run rubocop only against changed files
Browse files Browse the repository at this point in the history
Switch to bash for process substitution and working with arrays.

Ignore if there are more than 100. Protecting from possibly hitting the
command line length limit, it can be much higher and can be calculated
or also extracted as one more input if needed. The effect from limiting
number of files processed by rubocop is also smaller.

Fix ci workflow to fetch all commits for pr branch plus head commit of
base branch to be able to find changed files.

Co-authored-by: Oliver Günther <[email protected]>
  • Loading branch information
toy and oliverguenther committed May 8, 2024
1 parent 8d644c0 commit cd3cfda
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ jobs:
BUNDLE_GEMFILE: ${{ github.workspace }}/test/using_bundler/Gemfile
steps:
- uses: actions/checkout@v4
- name: Fetch all commits for PR branch plus head commit of base branch
run: |
# fetch all commits of the PR branch
git fetch --shallow-exclude "${{ github.base_ref }}" origin "${{ github.ref }}"
# fix for "fatal: error in object: unshallow"
git repack -d
# fetch head commit of base branch
git fetch --deepen 1 origin "${{ github.ref }}"
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ Optional. Do not install Rubocop or its extensions. Default: `false`.

Optional. Run Rubocop with bundle exec. Default: `false`.

### `only_changed`

Optional. Run Rubocop only on changed (and added) files, for speedup [`true`, `false`].
Default: `false`.

## Example usage

This action will use your [RuboCop Configuration](https://docs.rubocop.org/rubocop/configuration.html) automatically.
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ inputs:
use_bundler:
description: "Run Rubocop with bundle exec. Default: `false`"
default: 'false'
only_changed:
description: "Run Rubocop only on changed (and added) files, for speedup [`true`, `false`]"
default: 'false'
runs:
using: 'composite'
steps:
Expand All @@ -68,6 +71,9 @@ runs:
INPUT_WORKDIR: ${{ inputs.workdir }}
INPUT_SKIP_INSTALL: ${{ inputs.skip_install }}
INPUT_USE_BUNDLER: ${{ inputs.use_bundler }}
INPUT_ONLY_CHANGED: ${{ inputs.only_changed }}
BASE_REF: ${{ github.event.pull_request.base.sha }}
HEAD_REF: ${{ github.sha }}
branding:
icon: 'check-circle'
color: 'red'
33 changes: 31 additions & 2 deletions script.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/sh -e
#!/usr/bin/env bash

set -e

cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit
export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"
Expand Down Expand Up @@ -85,9 +87,36 @@ else
BUNDLE_EXEC="bundle exec "
fi

if [ "${INPUT_ONLY_CHANGED}" = "true" ]; then
echo '::group:: Getting changed files list'

# get intersection of changed files (excluding deleted) with target files for
# rubocop as an array
# shellcheck disable=SC2086
readarray -t CHANGED_FILES < <(
comm -12 \
<(git diff --diff-filter=d --name-only "${BASE_REF}..${HEAD_REF}" | sort) \
<(${BUNDLE_EXEC}rubocop --list-target-files | sort)
)

if (( ${#CHANGED_FILES[@]} == 0 )); then
echo "No relevant files for rubocop, skipping"
exit 0
fi

echo "${CHANGED_FILES[@]}"

if (( ${#CHANGED_FILES[@]} > 100 )); then
echo "More than 100 changed files (${#CHANGED_FILES[@]}), running rubocop on all files"
unset CHANGED_FILES
fi

echo '::endgroup::'
fi

echo '::group:: Running rubocop with reviewdog 🐶 ...'
# shellcheck disable=SC2086
${BUNDLE_EXEC}rubocop ${INPUT_RUBOCOP_FLAGS} --require ${GITHUB_ACTION_PATH}/rdjson_formatter/rdjson_formatter.rb --format RdjsonFormatter \
${BUNDLE_EXEC}rubocop ${INPUT_RUBOCOP_FLAGS} --require ${GITHUB_ACTION_PATH}/rdjson_formatter/rdjson_formatter.rb --format RdjsonFormatter "${CHANGED_FILES[@]}" \
| reviewdog -f=rdjson \
-name="${INPUT_TOOL_NAME}" \
-reporter="${INPUT_REPORTER}" \
Expand Down

0 comments on commit cd3cfda

Please sign in to comment.