Github Pre-commit Hook Setup In Ruby On Rails for maintaining coding standards and productive.
Tags: ruby, rails, git, tutorial
Published: 2022-08-29 02:27:00

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrhmm50fjpqfgvypt5t1.png)

### **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](https://github.com/jish/pre-commit) )

    $ gem install pre-commit

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

    # In your git repo
    $ pre-commit install

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

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

    $ git config pre-commit.ruby "bundle exec ruby"

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

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

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
{% gist https://gist.github.com/kananinirav/1887a713a6116d075de17b957a33a915.js %}
> *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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3yj9l3dogra5hisbka8m.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vy6ef4qjuk9wy5jek954.png)

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](https://github.com/presidentbeef/brakeman) to keep your app secure and [Rspec](https://rspec.info/) to run your test cases.

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

    $ cd rails-app
    $ mkdir scripts

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:
{% gist https://gist.github.com/kananinirav/093f38da22aebd4b9ccb5205244d3905.js %}{% gist https://gist.github.com/kananinirav/4bd322b2515bf4b971e007715a4249c6.js %}Now, let’s create or edit `pre-commit` bash files which we will symlink into the `.git` folder where the hooks reside:
{% gist https://gist.github.com/kananinirav/9d5496c043de44240aaf048bb1691c61.js %}Next, we want to write a bash script to create the symlink between these files and the hooks:
{% gist https://gist.github.com/kananinirav/65ce64fe550860fef891043629ee8057.js %}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

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

    $ ./scripts/install-hooks.bash
    Installing hooks...
    Done!

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/jish/pre-commit)
- [https://github.com/baurine/githook](https://github.com/baurine/githook)*

If You are using [Medium](https://medium.com/) Please support and follow me for interesting articles. **[Medium Profile](https://medium.com/@kanani-nirav)**

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