Introduce the pre-commit Git hook

What does this MR do and why?

Using the Lefthook available in GitLab, this MR extends the existing hooks with the pre-commit hook that makes sure the staged files are:

For the FE files:

  • ESLint compliant
  • StyleLint compliant
  • Prettified

For the BE files:

  • RuboCop compliant

Why pre-commit?

The changes introduced here are operating only on the staged files, not the whole repo or branch which makes the execution time for each commit absolutely manageable. At the same time, to avoid duplicating the job, we can unload pre-push hook to not run the works, already introduced in pre-commit which makes this hook a tad faster. The only duplicated step is RuboCop for now. Just to make sue 😅 But we can remove it from pre-push as well should we need that

What are the Git Hooks?

Git hooks are hooks to run command-line commands at different life-cycle points of the git development like pre-commit, pre-push, etc. More info: https://githooks.com/

Why do we need the hooks at all?

Very often, at least on the FE side of the things, we end up pushing code that fails jobs like static-analysis in the pipeline purely because of the code/formatting/linting not following our configurations. To prevent this, we, the FE developers, have to always remember to explicitly prettify, check ESLint-compliance on the files, etc. Using Git Hooks will make sure that every commit conforms to our configurations before they get to the pipeline.

Also, keep in mind that pipelines failing due to the reasons that could have been caught earlier is a waste of computing resources for GitLab and time for the MR authors

🛑 What if I don't want to follow this practice? 🛑

Opt-out

If you would like to opt-out from the staged files being checked on every commit (and it's totally fine if you don't), you have several options:

  • The MR introduces lefthook-local.yml configuration file with some instructions on how to skip the checks. You can do that or write your own configuration that will be local for your instance and won't be picked up by git
  • If you're committing from the command line, the pre-commit hooks can be bypassed with the --no-verify (or simply -n) option in your git commit
  • If your IDE supports that, you, probably, should be able to turn off the "Run Git Hooks" options somewhere in the interface. VSCode requires "Git: Allow No Verify Commit" setting to be enabled, RubyMine shows the option right away if the hooks are enabled in the repo. But in general, here's how the option looks like at the end:
RubyMine VSCode (with "Git: Allow No Verify Commit" enabled)
Screenshot_2021-11-25_at_11.05.12 Screenshot_2021-11-25_at_11.12.37

Screenshots or screen recordings

No visual changes

How to set up and validate locally

  • Checkout the MR
  • Run bundle install. This will make sure lefthook is installed.
  • IMPOTRANT! After bundle install, you should explicitly run lefthook install -f. This is a run-and-forget command that one doesn't need to re-run unless the whole GDK gets nuked. This step will update the standard repository hooks (in .git/hooks/) to use Lefthook. Without this step, the Lefthook config won't be picked up automatically.
  • Open a file and edit it deliberately breaking ESLint rule, for example.
  • Add the file with git add
  • Try to commit the file with git commit: the commit won't happen and will fail outputting the cause of the error into the console

Keep in mind that even though ESLint prevents the commit from happening (in accordance with our ESLint config), Prettier, for example, will simply fix the files silently without a notification, Which usually is totally fine

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Denys Mishunov

Merge request reports

Loading