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 <mail@oliverguenther.de>
  • Loading branch information
toy and oliverguenther committed May 31, 2024
1 parent 6c175bc commit c83f927
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ jobs:
BUNDLE_GEMFILE: ${{ github.workspace }}/test/using_bundler/Gemfile
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2 # we are comparing PR merge head with base
- 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 @@ -47,6 +47,11 @@ Default is `added`.
Optional. Report level for reviewdog [`info`, `warning`, `error`].
It's same as `-level` flag of reviewdog.

### `only_changed`

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

### `reporter`

Optional. Reporter of reviewdog command [`github-pr-check`, `github-check`, `github-pr-review`].
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ inputs:
level:
description: 'Report level for reviewdog [info,warning,error]'
default: 'error'
only_changed:
description: "Run Rubocop only on changed (and added) files, for speedup [`true`, `false`]"
default: 'false'
reporter:
description: |
Reporter of reviewdog command [github-pr-check,github-check,github-pr-review].
Expand Down Expand Up @@ -61,6 +64,7 @@ runs:
INPUT_FILTER_MODE: ${{ inputs.filter_mode }}
INPUT_GITHUB_TOKEN: ${{ inputs.github_token }}
INPUT_LEVEL: ${{ inputs.level }}
INPUT_ONLY_CHANGED: ${{ inputs.only_changed }}
INPUT_REPORTER: ${{ inputs.reporter }}
INPUT_REVIEWDOG_FLAGS: ${{ inputs.reviewdog_flags }}
INPUT_RUBOCOP_EXTENSIONS: ${{ inputs.rubocop_extensions }}
Expand All @@ -70,6 +74,8 @@ runs:
INPUT_TOOL_NAME: ${{ inputs.tool_name }}
INPUT_USE_BUNDLER: ${{ inputs.use_bundler }}
INPUT_WORKDIR: ${{ inputs.workdir }}
BASE_REF: ${{ github.event.pull_request.base.sha }}
HEAD_REF: ${{ github.sha }}
branding:
icon: 'check-circle'
color: 'red'
34 changes: 32 additions & 2 deletions script.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/sh -e
#!/usr/bin/env bash

set -e
set -o pipefail

cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit
export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"
Expand Down Expand Up @@ -85,9 +88,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 || kill $$) \
<(${BUNDLE_EXEC}rubocop --list-target-files | sort || kill $$)
)

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

printf '%s\n' "${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 c83f927

Please sign in to comment.