Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 29, 2022 02:27 am GMT

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. Its 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 cant 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

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: falseend

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

    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.

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

Lets start by creating a scripts directory in your apps 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:

Now, lets 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

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

$ ./scripts/install-hooks.bashInstalling hooks...Done!

And youre 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!


Original Link: https://dev.to/kanani_nirav/github-pre-commit-hook-setup-in-ruby-on-rails-12m3

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To