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 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? 🛑
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.ymlconfiguration 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-commithooks can be bypassed with the--no-verify(or simply-n) option in yourgit 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) |
|---|---|
![]() |
![]() |
Screenshots or screen recordings
No visual changes
How to set up and validate locally
- Checkout the MR
- Run
bundle install. This will make surelefthookis installed. -
⚠ IMPOTRANT! Afterbundle install, you should explicitly runlefthook 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
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.


