Skip to content

Commit

Permalink
Merge pull request #16 from adnikiforov/rubocop-versions-from-gemfile
Browse files Browse the repository at this point in the history
Add ability to parse Gemfile.lock for current rubocop version
  • Loading branch information
mgrachev authored Sep 6, 2020
2 parents 746658c + 89996d2 commit 0bc6647
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ FROM ruby:2.6-alpine
ENV REVIEWDOG_VERSION v0.10.2

SHELL ["/bin/ash", "-eo", "pipefail", "-c"]
RUN apk add --update --no-cache build-base git

# hadolint ignore=DL3018
RUN apk add --update --no-cache build-base git grep
RUN wget -O - -q https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh| sh -s -- -b /usr/local/bin/ $REVIEWDOG_VERSION

COPY entrypoint.sh /entrypoint.sh
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,24 @@ With `reporter: github-pr-review` a comment is added to the Pull Request Convers

### `rubocop_version`

Optional. Set rubocop version.
By default install latest version.
Optional. Set rubocop version. Possible values:
* empty or omit: install latest version
* `gemfile`: install version from Gemfile (`Gemfile.lock` should be presented, otherwise it will fallback to latest bundler version)
* version (e.g. `0.90.0`): install said version

### `rubocop_extensions`

Optional. Set list of rubocop extensions with versions.

By default install `rubocop-rails`, `rubocop-performance`, `rubocop-rspec`, `rubocop-i18n`, `rubocop-rake` with latest versions.
Provide desired version delimited by `:` (e.g. `rubocop-rails:1.7.1`)

Possible version values:
* empty or omit (`rubocop-rails rubocop-rspec`): install latest version
* `rubocop-rails:gemfile rubocop-rspec:gemfile`: install version from Gemfile (`Gemfile.lock` should be presented, otherwise it will fallback to latest bundler version)
* version (e.g. `rubocop-rails:1.7.1 rubocop-rspec:2.0.0`): install said version

You can combine `gemfile`, fixed and latest bundle version as you want to.

### `rubocop_flags`

Expand Down
55 changes: 53 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,60 @@ cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit 1

export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"

gem install -N rubocop $(version $INPUT_RUBOCOP_VERSION)
# if 'gemfile' rubocop version selected
if [[ $INPUT_RUBOCOP_VERSION = "gemfile" ]]; then
# if Gemfile.lock is here
if [[ -f 'Gemfile.lock' ]]; then
# grep for rubocop version
RUBOCOP_GEMFILE_VERSION=`cat Gemfile.lock | grep -oP '^\s{4}rubocop\s\(\K.*(?=\))'`

echo $INPUT_RUBOCOP_EXTENSIONS | xargs gem install -N
# if rubocop version found, then pass it to the gem install
# left it empty otherwise, so no version will be passed
if [[ -n "$RUBOCOP_GEMFILE_VERSION" ]]; then
RUBOCOP_VERSION=$RUBOCOP_GEMFILE_VERSION
else
printf "Cannot get the rubocop's version from Gemfile.lock. The latest version will be installed."
fi
else
printf 'Gemfile.lock not found. The latest version will be installed.'
fi
else
# set desired rubocop version
RUBOCOP_VERSION=$INPUT_RUBOCOP_VERSION
fi

gem install -N rubocop $(version $RUBOCOP_VERSION)

# Traverse over list of rubocop extensions
for extension in $INPUT_RUBOCOP_EXTENSIONS; do
# grep for name and version
INPUT_RUBOCOP_EXTENSION_NAME=`echo $extension | grep -oP '^rubocop-\w*'`
INPUT_RUBOCOP_EXTENSION_VERSION=`echo $extension | grep -oP '^rubocop-\w*:\K(.*)'`

# if version is 'gemfile'
if [[ $INPUT_RUBOCOP_EXTENSION_VERSION = "gemfile" ]]; then
# if Gemfile.lock is here
if [[ -f 'Gemfile.lock' ]]; then
# grep for rubocop extension version
RUBOCOP_EXTENSION_GEMFILE_VERSION=`cat Gemfile.lock | grep -oP "^\s{4}$INPUT_RUBOCOP_EXTENSION_NAME\s\(\K.*(?=\))"`

# if rubocop extension version found, then pass it to the gem install
# left it empty otherwise, so no version will be passed
if [[ -n "$RUBOCOP_EXTENSION_GEMFILE_VERSION" ]]; then
RUBOCOP_EXTENSION_VERSION=$RUBOCOP_EXTENSION_GEMFILE_VERSION
else
printf "Cannot get the rubocop extension version from Gemfile.lock. The latest version will be installed."
fi
else
printf 'Gemfile.lock not found. The latest version will be installed.'
fi
else
# set desired rubocop extension version
RUBOCOP_EXTENSION_VERSION=$INPUT_RUBOCOP_EXTENSION_VERSION
fi

gem install -N $INPUT_RUBOCOP_EXTENSION_NAME $(version $RUBOCOP_EXTENSION_VERSION)
done

rubocop ${INPUT_RUBOCOP_FLAGS} \
| reviewdog -f=rubocop \
Expand Down

0 comments on commit 0bc6647

Please sign in to comment.