Nabla custom git hooks
This project provides custom Git hooks for code quality validation and is intended to be used by all Nabla products.
Note: This project is in maintenance mode. For new projects, we recommend using commitizen, commitlint, or opencommit with pre-commit hooks.
- Git Branches Check: Validates old stale and already merged branches
- Jenkinsfile Validation: Checks Jenkinsfile syntax and formatting
- JIRA Integration: Optional JIRA ticket validation in commit messages
- Pre-commit Hooks: Integrates with the pre-commit framework
Install using pip:
pip install nabla-hooksOr from source:
pip install git+https://github.com/AlbanAndrieu/nabla-hooks.git- Add to your
.pre-commit-config.yaml:
repos:
- repo: https://github.com/AlbanAndrieu/nabla-hooks.git
rev: v1.0.7
hooks:
- id: git-branches-check- Install pre-commit hooks:
pre-commit install- Run on all files:
pre-commit run --all-filesWe welcome contributions! Please see our Contributing Guidelines for details on:
- Setting up your development environment with Poetry
- Running tests and code quality checks
- Submitting pull requests
- GitHub workflows and CI/CD
For issues, feature requests, or questions, please use our GitHub issue templates.
// spell-checker:disable
- Features
- Quick Start
- Initialize
// spell-checker:enable
Using pipenv with Pipfile:
direnv allow
pyenv install 3.12.10
pyenv local 3.12.10
python -m pipenv install --dev --ignore-pipfile
direnv allow
pre-commit installIf you're migrating from Pipenv:
pip install -U poetry pipenv-poetry-migrate
pipenv-poetry-migrate -f Pipfile -t pyproject.toml --no-use-group-notationThis package requires the following to run:
- Python >= 3.9
- pre-commit
- jira (optional, for JIRA integration)
See requirements.txt for the complete list of Python dependencies.
Install Python 3.12 and virtualenv:
virtualenv --no-site-packages /opt/ansible/env312 -p python3.12
source /opt/ansible/env312/bin/activateInstall Python 3.12 with pyenv:
curl -L https://pyenv.run | bash
echo 'export PATH="~/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
source ~/.bashrc
pyenv install 3.12.10See pyenv with direnv integration for more details.
pipenv check
python -m pipenv install --dev
python -m pipenv install --dev --ignore-pipfilepip install nabla-hooks
pip install git+https://github.com/AlbanAndrieu/nabla-hooks.git
- Create
.pre-commit-config.yamlin your git project
Example .pre-commit-config.yaml:
repos:
- repo: https://github.com/AlbanAndrieu/nabla-hooks.git
rev: v1.0.7
hooks:
- id: git-branches-check
- id: jira-check # Validates commit messages contain JIRA tickets
stages: [commit-msg]JIRA Check Hook: Validates that commit messages contain JIRA ticket references (e.g., PROJ-123, TEST-456).
Features:
- Pattern matching:
[A-Z]{2,10}-[0-9]+(customizable via--pattern) - Auto-skips merge, revert, fixup, and squash commits
- No JIRA API access required - simple pattern validation
- User-friendly error messages with examples
Example valid commit messages:
PROJ-123 Add new feature[TEAM-456] Fix critical bugABC-789: Update documentation
Testing locally
repos:
- repo: local
hooks:
- id: git-branches-check
name: GIT branches check
description: Check for old stale and already merged branches from the current repo with user friendly messages and colors
entry: pre_commit_hooks/git-branches-check.sh
language: script
types: [shell]
always_run: true
verbose: true
args: [--max=1, --verbose]
- id: jira-check
name: JIRA ticket validation
entry: hooks/validate_jira.py
language: python
stages: [commit-msg]Test locally:
pre-commit try-repo . git-branches-check --verbose- Install pre-commit hooks in your repository
For commit-msg hooks (like JIRA check), also run:
pre-commit install --hook-type commit-msg
-
enjoy it
-
Run pre-commit on all files
pre-commit run --all-filesTo skip specific hooks:
SKIP=flake8 git commit -am 'Add key'
# Or bypass all hooks
git commit -am 'Add key' --no-verifyThis project now supports OpenCommit, an AI-powered tool that generates commit messages automatically.
- Install OpenCommit globally:
npm install -g opencommit
# or use it with npx
npx opencommit- Configure your AI provider (OpenAI, Claude, or local models via Ollama):
oco config set OCO_API_KEY=<your_api_key>
# For local models with Ollama:
# oco config set OCO_AI_PROVIDER=ollama
# oco config set OCO_MODEL=llama3:8b- Install as a git hook (optional, if you want AI-generated messages by default):
oco hook setThe nabla-hooks prepare-commit-msg hook is now compatible with OpenCommit:
- With OpenCommit hook installed: When you run
git commit, OpenCommit will generate a commit message, and nabla-hooks will validate it. - Manual AI generation: You can use
ococommand directly to generate and commit:
git add .
oco- Fallback to commitizen: If no commit message is provided, the hook will fallback to using commitizen for message generation.
The hook intelligently detects existing commit messages (from oco, manual entry, or other tools) and validates them without forcing regeneration.
See JIRA authentication documentation
export JIRA_USER=aandrieu
export JIRA_PASSWORD=XXX
export JIRA_URL=https://localhost/jira
export JIRA_CERT_PATH=/etc/ssl/certs/NABLA-CA-1.crt
# Or use system CA bundle
export JIRA_CERT_PATH=/etc/ssl/certs/ca-certificates.crtexport JIRA_USER=alban.andrieu@free.fr
export JIRA_PASSWORD=XXX # Your generated API token
export JIRA_URL=https://localhost/jiraexport JENKINS_URL=https://localhost/jenkins/
export JENKINS_USER=aandrieu
export JENKINS_USER_TOKEN=XXXSee implementing git hooks using Python
First time setup:
# Copy hooks to .git/hooks
cp -r hooks/* .git/hooks/
# Or create symbolic link
rm -Rf ./.git/hooks/ && ln -s ../hooks ./.git/hooks && git checkout repo hooks/Using auto_prepare_commit_message:
wget -O .git/hooks/prepare-commit-msg https://raw.githubusercontent.com/commitizen-tools/commitizen/master/hooks/prepare-commit-msg.py
chmod +x .git/hooks/prepare-commit-msg
wget -O .git/hooks/post-commit https://raw.githubusercontent.com/commitizen-tools/commitizen/master/hooks/post-commit.py
chmod +x .git/hooks/post-commitGit uses template directories to initialize new repositories. We have two relevant directories:
- Linux:
/usr/share/git-core/templates/ - Windows:
C:/Program Files (x86)/Git/share/git-core/templates/(orC:/Program Files/...on 32-bit)
The hooks are copied from [...]/share/git-core/templates/ directory to .git/hooks/ when initializing a new repository.
Note: If you change the templates directory, the hooks directory must be a subdirectory of the templates directory. Do not set the templates directory to the desired hooks directory.
Setup:
git config --global --get init.templatedir
rm -Rf .git/hooks
git config --global init.templatedir /workspace/users/albandrieu30/nabla-hooks/Build a source distribution (a tar archive of all the files needed to build and install the package):
python -m buildInstall locally:
pip install .
# Or in editable mode
pip install -e ./See PyPI API tokens
rm -Rf dist/
pip install setuptools
python3 setup.py sdist bdist_wheel
# Check package
twine check dist/*
nano $HOME/.pypirc
export TWINE_PASSWORD=pypi-
python3 -m twine upload --repository nabla-hooks dist/* --verbosePublished versions:
- Production: nabla-hooks on PyPI
- Test: nabla-hooks on Test PyPI
Python example:
from hooks import get_msg
match_msgUsing versioneer for version management:
versioneer install
# Check version
python setup.py version
python setup.py installUsing tox:
source deactivate
tox --notest
tox -e py # Run tox using the version of Python in PATH
tox -e py312Using pytest from the root directory:
pytest --cache-clear --setup-show hooks/tests/pytest_test.py
pytest --cache-clear --setup-show tests/test_package.pypoetry config pypi-token.pypi ${TWINE_PASSWORD} poetry publish --build
poetry check
poetry lock
For more details, see the [CONTRIBUTING.md](.github/CONTRIBUTING.md) guide.
### Pdm
Using [PDM](https://pdm.fming.dev/) for dependency management:
```bash
pdm init
pdm run flake8
This project uses GitHub Actions for continuous integration and deployment. The following workflows are configured:
-
Python Package (
.github/workflows/python.yml) -
CodeQL Analysis (
.github/workflows/codeql.yml)- Security scanning for code vulnerabilities
- Runs on: Push and Pull requests
-
Linter (
.github/workflows/linter.yml)- Comprehensive linting across the project
- Uses MegaLinter for multiple file types
-
Release (
.github/workflows/release.yml)- Automated release creation and publishing
-
Tests (
.github/workflows/tests.yml)- Dedicated test workflow
All pull requests must pass:
- ✅ Linting (flake8, pylint, bandit)
- ✅ Tests (pytest with >30% coverage)
- ✅ Code quality checks (SonarCloud)
- ✅ Security scanning (CodeQL)
Before pushing, run the same checks locally:
# Activate poetry environment
poetry shell
# Run linters
poetry run flake8 hooks tests
poetry run pylint hooks
poetry run bandit -r hooks
# Run tests
poetry run pytest --cov=hooks --cov-fail-under=30
# Run pre-commit hooks
poetry run pre-commit run --all-filesFor more details, see CONTRIBUTING.md.
Using markdown-toc to update the table of contents:
npm install --save markdown-toc
markdown-toc README.md -i
markdown-toc CHANGELOG.md -iWith pre-commit hooks:
pre-commit install
git add README.md
pre-commit run markdown-tocCheck markdown syntax with remark-lint:
npm run lint-mdGroovy linting and formatting for Jenkinsfile. Tested with Node.js 12 and 16 on Ubuntu 20 and 21 (not working with Node.js 11 and 14).
npm install -g npm-groovy-lint@8.2.0
npm-groovy-lint --format
ll .groovylintrc.jsonContributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch
- Make your changes with clear commit messages
- Add tests if applicable
- Run pre-commit hooks and tests locally
- Submit a pull request
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
This project uses several open-source tools and libraries. See requirements.txt for the complete list.
