Pre-Commit Hooks


Dev tooling has come a long way over the last couple of years since I started this program. In our capstone project I recently set up a series of pre-commit hooks for increasing the quality of our code before sending it in to review. The goal was to enforce code style, linting rules, and branch rules for our dev team when checking in code to the repo.

Here’s an example of it in action.

For the sake of the example, I’ve set up a couple of linting and styling issues to fail. The hooks identify what code quality issues exist on staged files and don’t allow the git commit command to work before all the tests are passed. Therefore, code can’t be pushed the repo.

An example of a successful run looks like this:

Since we are using a monorepo for our project, it was challenging to set this up.

Project structure:

The front end is written in Typescript with dependency management handled by NPM and each service uses Python with Poetry. Utilities is a module which is used by the rest of the services. Given that our repo is using a bunch of different dependency management tools, I had to make a few architectural decisions.

  • Should the pre-commit package be installed in every service?

I decided to only install pre-commit in the services/utilities package since it would be redundant to do so in the other packages as well since our configuration file for the hooks would be the same. If this was not the case, it would make more sense to install pre-commit in every service so that a different configuration file could be used.

  • Where should the configuration file live?

My initial thought was the put the configuration file at the project root directory but unfortunately I couldn’t find a way to make pre-commit work like this. Therefore it lives in the services/utilities folder where pre-commit is installed.

I think the installation of this tool will help our team maintain standards going forward with the rest of the project and serves a de facto CI tool for our repo. Not only does it enforce standards, it also increases developer productivity through a defined workflow.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *