Skip to content

Commit

Permalink
add only_changed input to run rubocop only against changed files (#103)
Browse files Browse the repository at this point in the history
* add only_changed input to run rubocop only against changed files

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]>

* automatically fetch the tip of the base branch if it is not available

* don't hide the name of the CI job

* test only_changed

---------

Co-authored-by: Oliver Günther <[email protected]>
  • Loading branch information
toy and oliverguenther authored Jul 14, 2024
1 parent 4e3af4b commit e44fb2c
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 3 deletions.
53 changes: 52 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: CI
on: [pull_request]
jobs:
test-skip-install-and-use-bundler:
name: runner / rubocop
runs-on: ubuntu-latest
defaults:
run:
Expand All @@ -22,3 +21,55 @@ jobs:
skip_install: 'true'
use_bundler: 'true'
- run: test "$(bundle exec rubocop --version)" == "1.18.1"
test-only_changed:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
env:
INPUT_ONLY_CHANGED: 'true'
steps:
- uses: actions/checkout@v4
- name: setup
run: |
git config user.email "[email protected]"
git config user.name "I am an automated workflow"
- name: Check when there are relevant files
run: |
git checkout ${{ github.sha }}
rm -f test/only_changed/reviewdog-was-called
cp test/only_changed/few_relevant/files/* .
git add *
git commit -m auto
export PATH=test/only_changed/few_relevant/mock_bins:test/only_changed/shared_mock_bins:$PATH
BASE_REF=$(git rev-parse HEAD~) HEAD_REF=$(git rev-parse HEAD) ./script.sh
[ -f test/only_changed/reviewdog-was-called ]
- name: Check when there are no relevant files
run: |
git checkout ${{ github.sha }}
rm -f test/only_changed/reviewdog-was-called
cp test/only_changed/nothing_relevant/files/* .
git add *
git commit -m auto
export PATH=test/only_changed/nothing_relevant/mock_bins:test/only_changed/shared_mock_bins:$PATH
BASE_REF=$(git rev-parse HEAD~) HEAD_REF=$(git rev-parse HEAD) ./script.sh
[ ! -f test/only_changed/reviewdog-was-called ]
- name: Check when there are too many relevant files
run: |
git checkout ${{ github.sha }}
rm -f test/only_changed/reviewdog-was-called
touch a{00..100}.rb
git add *
git commit -m auto
export PATH=test/only_changed/too_many_relevant/mock_bins:test/only_changed/shared_mock_bins:$PATH
BASE_REF=$(git rev-parse HEAD~) HEAD_REF=$(git rev-parse HEAD) ./script.sh
[ -f test/only_changed/reviewdog-was-called ]
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ 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`.

Will fetch the tip of the base branch with depth 1 from remote origin if it is not available.
If you use different remote name or customize the checkout otherwise, make the tip of the base branch available before this action

### `reporter`

Optional. Reporter of reviewdog command [`github-pr-check`, `github-check`, `github-pr-review`].
Expand Down
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ 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`].
Will fetch the tip of the base branch with depth 1 from remote origin if it is not available.
If you use different remote name or customize the checkout otherwise, make the tip of the base branch available before this action.
default: 'false'
reporter:
description: |
Reporter of reviewdog command [github-pr-check,github-check,github-pr-review].
Expand Down Expand Up @@ -61,6 +67,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 +77,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'
44 changes: 42 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,46 @@ else
BUNDLE_EXEC="bundle exec "
fi

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

# check if commit is present in repository, otherwise fetch it
if ! git cat-file -e "${BASE_REF}"; then
git fetch --depth 1 origin "${BASE_REF}"
fi

# 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 \
--fail-level error \
"${CHANGED_FILES[@]}" \
| reviewdog -f=rdjson \
-name="${INPUT_TOOL_NAME}" \
-reporter="${INPUT_REPORTER}" \
Expand Down
1 change: 1 addition & 0 deletions test/only_changed/few_relevant/files/a.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "Hello, " + "world!"
1 change: 1 addition & 0 deletions test/only_changed/few_relevant/files/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "Hello, " + "world!"
18 changes: 18 additions & 0 deletions test/only_changed/few_relevant/mock_bins/rubocop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

case ARGV
when %w[
--list-target-files
]
puts Dir['**/*.rb']
when %W[
--require #{ENV['GITHUB_ACTION_PATH']}/rdjson_formatter/rdjson_formatter.rb
--format RdjsonFormatter
--fail-level error
a.rb
]
puts 'Mock message for reviewdog'
else
abort "rubocop mock called with unexpected arguments:\n#{ARGV.join("\n")}"
end
1 change: 1 addition & 0 deletions test/only_changed/nothing_relevant/files/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "Hello, " + "world!"
11 changes: 11 additions & 0 deletions test/only_changed/nothing_relevant/mock_bins/rubocop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

case ARGV
when %w[
--list-target-files
]
puts Dir['**/*.rb']
else
abort "rubocop mock called with unexpected arguments:\n#{ARGV.join("\n")}"
end
9 changes: 9 additions & 0 deletions test/only_changed/shared_mock_bins/bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

if [ "$1" = "exec" ]; then
shift
eval "$@"
else
echo "Only 'exec' command is supported"
exit 1
fi
8 changes: 8 additions & 0 deletions test/only_changed/shared_mock_bins/curl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

arguments="$*"

if [ "$arguments" != "-sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh" ]; then
echo "curl mock got unexpected arguments: $arguments"
exit 1
fi
10 changes: 10 additions & 0 deletions test/only_changed/shared_mock_bins/reviewdog
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

touch test/only_changed/reviewdog-was-called

read -r input

if [ "$input" != "Mock message for reviewdog" ]; then
echo "reviewdog mock got unexpected input: $input"
exit 1
fi
17 changes: 17 additions & 0 deletions test/only_changed/too_many_relevant/mock_bins/rubocop
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

case ARGV
when %w[
--list-target-files
]
puts Dir['**/*.rb']
when %W[
--require #{ENV['GITHUB_ACTION_PATH']}/rdjson_formatter/rdjson_formatter.rb
--format RdjsonFormatter
--fail-level error
]
puts 'Mock message for reviewdog'
else
abort "rubocop mock called with unexpected arguments:\n#{ARGV.join("\n")}"
end

0 comments on commit e44fb2c

Please sign in to comment.