DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Kanani Nirav
Kanani Nirav

Posted on

Github Pre-commit Hook Setup In Ruby On Rails for maintaining coding standards and productive.

In this article, we will set up GitHub pre-commit hook using the pre-commit gem in Ruby On Rails, and we will use Git Hooks to run Rubocop, Brakeman, and Rspec.

Image description

What is GitHub Pre-commit Hook?

Pre-commit hook runs before typing the commit message. It’s primarily being used for linting or running tests to make sure everything is working. Pre-commit hooks are very helpful in maintaining the coding standards and are productive also as many of the suggested lint changes can be fixed automatically. It plays an essential role when multiple people are working on the same repo. Everyone will have their own way of writing the code and formatting it. But as a project, you can’t afford to have too many different formattings. By using pre-commit hooks, a team can make sure everything goes to the repo passed the checks and balances that are put in place.

For the pre-commit hook, we will be using the pre-commit gem

Install the pre-commit gem ( https://github.com/jish/pre-commit )

$ gem install pre-commit
Enter fullscreen mode Exit fullscreen mode

Use the pre-commit command to generate a stub pre-commit hook

# In your git repo
$ pre-commit install
Enter fullscreen mode Exit fullscreen mode

This creates a .git/hooks/pre-commit script which will check your git config and run checks that are enabled.

Bundler

If you want to use Bundler to specify a version of RuboCop, add the following to Gemfile:

group :development do
  gem "pre-commit", require: false
  gem "rubocop", require: false
end
Enter fullscreen mode Exit fullscreen mode

And run the following to run pre-commit via Bundler:

$ git config pre-commit.ruby "bundle exec ruby"
Enter fullscreen mode Exit fullscreen mode

RVM

If you are using rvm you need to install pre-commit into the default gemset, because it does not use the current environment

$ rvm default do gem install pre-commit
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can configure pre-commit to use the current rvm gemset

$ git config pre-commit.ruby "rvm `rvm current` do ruby" # OR:
$ git config pre-commit.ruby `rvm wrapper current show ruby` # available in RVM 1.26.12
Enter fullscreen mode Exit fullscreen mode

Here we are creating a config file to manage our pre-commit checks.

pre-commit comes with 4 configuration providers:

  • default - basic settings, read-only

  • git - reads configuration from git config pre-commit.*, allow local update

  • yaml - reads configuration from /etc/pre_commit.yml, $HOME/.pre_commit.yml and config/pre_commit.yml, allows config/pre_commit.yml updates

  • env - reads configuration from environment variables

    default available checks list. you can add according to your requirement.
    before_all, ci, coffeelint, common, console_log, csslint, debugger, gemfile_path, go, go_build, go_fmt, jshint, jslint, json, local, merge_conflict, migration, nb_space, pry, rails, rspec_focus, rubocop, ruby, ruby_symbol_hashrockets, scss_lint, tabs, whitespace, yaml

Use pre-commit list to see the list of default and enabled checks and warnings.

Image description

If we are trying to commit with incorrect format code or debugger present in code then it will stop commit and display something like the below image.

Image description

For more information please check gem.

We will create a custom script according to the requirements. Here we are creating for RSpec and Brakeman.

It’s assumed that you already have a Rails app and use Brakeman to keep your app secure and Rspec to run your test cases.

Let’s start by creating a scripts directory in your app’s root folder:

$ cd rails-app
$ mkdir scripts
Enter fullscreen mode Exit fullscreen mode

Now we will create three different bash files for each command we want to run, in our case, it's Brakeman and Rspec. Below are files you can use and put inside the scripts folder:

Now, let’s create or edit pre-commit bash files which we will symlink into the .git folder where the hooks reside:

Next, we want to write a bash script to create the symlink between these files and the hooks:

All the files which we need are created, but before we run the install-hooks bash file to install our hooks, we need to make these files executables. This is done with the following command:

$ chmod +x scripts/*.bash
Enter fullscreen mode Exit fullscreen mode

Now we are ready to run install-hooks.bash:

$ ./scripts/install-hooks.bash
Installing hooks...
Done!
Enter fullscreen mode Exit fullscreen mode

And you’re all set! Try committing and pushing your code. Before committing, Brakeman and Rspec will run.

for your reference and information:
https://github.com/jish/pre-commit
https://github.com/baurine/githook

If this guide has been helpful to you and your team please share it with others!

Top comments (1)

Collapse
 
asianiuk profile image
Alexandr

Does anybody experience a timeout error with such solution? Does anybody know a workaround of the issue?

Now it's your turn.

Join DEV and share your story.