diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 61280dd..79f81c3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/README.md b/README.md index 1ae8817..07caf3d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/action.yml b/action.yml index 7cd76cc..d7e8b2f 100644 --- a/action.yml +++ b/action.yml @@ -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: @@ -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' diff --git a/script.sh b/script.sh index 81a06bc..f247c82 100755 --- a/script.sh +++ b/script.sh @@ -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}" @@ -85,9 +87,24 @@ else BUNDLE_EXEC="bundle exec " fi +if [ "${INPUT_ONLY_CHANGED}" = "true" ]; then + echo '::group:: Getting changed files list' + 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) + ) + 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}" \