Using Pronto to Introduce Style to Your Rails App
I believe most developers know that code style matters. It helps the code base feel clean and consistent. I’ve worked on a number of projects where teams are working without a style guide due to all of the style debt that’s been accrued. Thanks to Pronto, style guides can be adopted without forcing you to pay off that debt immediately.
Enter Pronto
Pronto is a tool that runs linters on “relevant” changes (lines you’ve added or changed) in your project. This means you can incrementally introduce a style guide since it only comments on new code and existing code that you’ve changed.
Pronto can be run locally, but really shines when run in CI. When run in CI pronto will comment on lines in pull requests with the output from the tool it’s running. This moves the responsibility of policing style from members of the team to Pronto during code review.
I’ve mainly used pronto to run Rubocop but it supports a number of other runners as well.
Setting up Style Linting in Rails
Here we’ll be adding Rubocop and Pronto with the Rubocop runner which sets
us up for success in the next step. Add the following to your Gemfile
.
group :development, :test do
gem 'rubocop', '~> 0.55.0', require: false
end
group :test do
gem 'pronto'
gem 'pronto-rubocop', require: false
end
It’s that easy! If you have an existing Rubocop config you can add it, if not I recommend starting with a blank slate and opening pull requests to change rules as you go.
If you want to give it a try, run bundle pronto run
and it will start linting
added files and changes.
Configuring for CircleCI
Unfortunately good documentation on setting up Pronto in CircleCI is hard to find. Here are the steps I followed to configure Pronto to work with CircleCI 2.0.
- Generate an OAuth token in Github
- Add a project level environment
variable
called
PRONTO_GITHUB_ACCESS_TOKEN
using the token created in the first step. - Add
sudo apt-get install cmake
as one of the steps in.circleci/config.yml
before runningbundle
. build a pronto dependency. - Add the configuration below to
.circleci/config.yml
to run pronto on the correct pull request.
- run: echo 'export PRONTO_PULL_REQUEST_ID=$CIRCLE_PR_NUMBER' >> $BASH_ENV
- run: if [ $PRONTO_PULL_REQUEST_ID != false ]; then pronto run -f github_pr_review -c origin/master; fi
Now whenever a pull request is opened pronto will run and use Github’s review feature to comment on style violations in the pull request.
Conclusion
Pronto is a great way to introduce a style guide to your team without having to spend a significant amount of time getting the existing code base to conform to it. It’s a great compromise between improving the code base and productivity.
If all of that sounds great but you can’t, or don’t want to setup Pronto HoundCI is a hosted alternative that provides the same functionality.