Tweak builderrors script for git hooks
What
Build error checks can be configured locally. We need to leverage builderrors
script and create a new check.
Current approach
Developers push to server and CI runner shares the build error report. Build error checks can be configured
Some might have configured eslint
, htmllint
etc. to run before commits but are in isolation. We need teams to adopt such practices and it should be easy to configure.
What I tried
- Configured my local machine with the instructions including cloning
monitor
repo. - In a project directory, ran
monitor/builderrors
. The script didn't ignorenode_modules
,assets
etc.
Proposed approach
- I'd like teams to configure
builderrors
script as agit
pre-commit hook. -
tweak builderrors
script to be run as a pre-commit hook. without any YAML configuration.
Alternative approach
pre-commit is a useful utility to configure script sanity checks via git hooks.
It relies on a .pre-commit-config.yaml
config where checks are spelt out. Sample configuration below:
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.0.0
hooks:
- id: trailing-whitespace
description: This hook trims trailing whitespace.
- id: end-of-file-fixer
description: Ensures that a file is either empty or ends with one newline.
- id: check-yaml
description: This hook checks yaml files for parseable syntax.
- id: check-added-large-files
description: Prevent giant files from being committed. Defaults to 500kB.
- id: pretty-format-json
description: This hook sets a standard for formatting JSON files.
args: [--indent=2, --no-sort-keys]
- id: flake8
description: This hook runs flake8.
Note that minimal working config is only 5 lines (repos
, repo
, rev
, hooks
, id
). This works well.
pre-commit configuration
- install pre-commit:
pip install pre-commit
- create
.pre-commit-config.yaml
with critical checks, in root directory of project - In terminal, run:
pre-commit install
. This will create apre-commit
file in.git/hooks/
directory..git/
is not version controlled.
By default, pre-commit
runs before every commit. To test,
- In terminal, run:
pre-commit run --all-files
. This will validate the config file.
- Change a file (introduce a bug, example add a
console
statement inJS
file) and run:git commit
. Commit should fail. Fix the bug and re-commit. Commit should succeed.