Skip to content
Home » pre-commit Odoo apps: Step-by-Step Guide

pre-commit Odoo apps: Step-by-Step Guide

  • Odoo
pre-commit Odoo apps

First, pre-commit Odoo apps streamline code checks for Odoo modules by running hooks before each commit. Next, this tutorial shows you how to set up pre-commit, integrate OCA plugins like pylint-odoo and flake8-odoo, and automate lint in CI. Then, we include code examples and detailed steps. Finally, you will master pre-commit Odoo apps to maintain high code standards. Moreover, you can learn more at https://pre-commit.com/.

source : https://github.com/odooerpdevelopers/mis-addons-public

Prerequisites for pre-commit Odoo apps

Python environment for Odoo pre-commit hooks

First, you need Python 3.8 or later installed. Next, you create a virtual environment to isolate dependencies. Then, you activate the venv with a single command. After that, you upgrade pip and setuptools. Finally, you confirm python --version shows the right interpreter.

python3 -m venv .venv
source .venv/bin/activate       # macOS/Linux
# .venv\Scripts\activate        # Windows PowerShell
pip install --upgrade pip setuptools

Git setup for pre-commit Odoo modules

First, you install Git and configure user info. Next, you clone your Odoo project or OCA add‑ons repo. Then, you navigate into the repo folder. After that, you verify git status shows a clean tree. Finally, you ensure .git/hooks/pre-commit folder exists.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git clone https://github.com/your-org/your-odoo-repo.git
cd your-odoo-repo

Installing pre-commit and Odoo hook tools

First, you install pre-commit inside your virtual environment. Next, you verify installation with pre-commit --version. Then, you enable Git hooks via pre-commit install. After that, you prepare to add OCA hook repos. Finally, you confirm the .git/hooks/pre-commit script is executable.

pip install pre-commit
pre-commit --version           # e.g., pre-commit 2.20.0
pre-commit install
ls -l .git/hooks/pre-commit     # check execute flag

Configuring pre-commit Odoo apps with .pre-commit-config.yaml

First, you create a file named .pre-commit-config.yaml at your repo root. Next, you list external hook repositories and pin their rev:. Then, you add basic and OCA‑specific hooks. After that, you tune file patterns and extra dependencies. Finally, you save and commit the config.

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer

  - repo: https://github.com/OCA/pylint-odoo
    rev: 2.13.0
    hooks:
      - id: pylint-odoo
        args: ["--rcfile=.pylintrc"]

  - repo: https://github.com/OCA/flake8-odoo
    rev: 15.0.0
    hooks:
      - id: flake8-odoo
        additional_dependencies: ["flake8>=3.9.2"]

First, this config loads core fixes (whitespace, EOF). Next, it integrates pylint-odoo for manifest and code checks. Then, it adds flake8-odoo for style and complexity. After that, you can extend with repo: local hooks. Finally, you commit .pre-commit-config.yaml.

Setting up pylint‑odoo plugin for Odoo lint checks

First, you add a .pylintrc file at the repo root. Next, you load the pylint_odoo plugin. Then, you disable docstring warnings you don’t need. After that, you set your Odoo version. Finally, you save changes.

# .pylintrc
[MASTER]
load-plugins=pylint_odoo

[MESSAGES CONTROL]
disable=C0114,C0115,C0116  # missing module, class, function docstrings

[ODOO]
version=16.0

First, you load the plugin under [MASTER]. Next, you silence select messages in [MESSAGES CONTROL]. Then, you set version in the [ODOO] block. Finally, you keep this file in version control.

Configuring flake8‑odoo for Odoo style checks

First, you create a flake8.cfg at the project root. Next, you define a line‑length limit. Then, you exclude common build and cache folders. After that, you pick error codes to enforce. Finally, you ignore false positives.

# flake8.cfg

[flake8]

max-line-length = 120 exclude = .git, __pycache__, .venv select = C,E,F,W,B,B950 ignore = F403,F405

First, max-line-length matches OCA’s 120 chars. Next, exclude skips unwanted folders. Then, select limits checks to core codes. After that, ignore handles import quirks. Finally, you version this config.

Running pre-commit locally for Odoo modules

First, you run all hooks on every file with one command. Next, you review failures and auto‑fix where possible. Then, you commit the fixed files. Finally, you ensure no errors remain.

pre-commit run --all-files
# To auto‑fix supported hooks:
pre-commit run --all-files --show-diff-on-failure --color always

First, this command scans your entire repo. Next, hooks like end-of-file-fixer auto‑apply. Then, you stage changes with git add. Finally, you commit without errors.

Automating Odoo pre-commit hooks in CI pipelines

GitHub Actions for Odoo pre-commit hooks

First, you create .github/workflows/pre-commit.yml. Next, you trigger on pull requests affecting Python or config files. Then, you install Python and pre-commit. After that, you run pre-commit run --all-files. Finally, you block merges on lint failures.

# .github/workflows/pre-commit.yml
name: CI / pre-commit checks
on:
  pull_request:
    paths:
      - '**/*.py'
      - '.pre-commit-config.yaml'

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'
      - name: Install pre-commit
        run: pip install pre-commit
      - name: Run hooks
        run: pre-commit run --all-files

First, this workflow runs on each PR. Next, it ensures consistent lint across branches. Then, it prevents merges when checks fail. Finally, it reports errors directly in the PR.

GitLab CI for Odoo pre-commit hooks

First, you edit .gitlab-ci.yml. Next, you define a job that installs pre-commit. Then, you run hooks on merge requests. After that, you cache pip installs. Finally, you enforce code quality on every MR.

# .gitlab-ci.yml
precommit:
  image: python:3.9
  before_script:
    - pip install pre-commit
  script:
    - pre-commit run --all-files
  only:
    - merge_requests

First, this job runs in a clean container. Next, it installs the required tool. Then, it checks all files. Finally, it fails the pipeline on errors.

Best practices for OCA pre-commit integration

First, pin each rev: to avoid unexpected breaks. Next, use YAML anchors to share common settings. Then, limit hook scope with files: or exclude: patterns. After that, document setup in CONTRIBUTING.md. Finally, review hook updates periodically.

# Example: reuse common patterns with anchors
defaults: &defaults
  files: \.py$
  exclude: tests/fixtures/

repos:
  - repo: https://github.com/OCA/pylint-odoo
    rev: 2.13.0
    hooks:
      - id: pylint-odoo
        <<: *defaults
  - repo: https://github.com/OCA/flake8-odoo
    rev: 15.0.0
    hooks:
      - id: flake8-odoo
        <<: *defaults

Troubleshooting pre-commit Odoo apps issues

First, if pre-commit install fails, check hook permissions with chmod +x .git/hooks/pre-commit. Next, if you see dependency conflicts, isolate hooks with language: python3. Then, if hooks run slowly, narrow the scope to --hook-stage. After that, to skip hooks temporarily, use git commit --no-verify. Finally, avoid long‑term skips and fix root causes.

Conclusion: Mastering pre-commit Odoo apps

First, combining pre-commit with OCA plugins boosts code quality. Next, integrating hooks into CI ensures consistency across your team. Then, following best practices maintains a stable setup. After that, you can focus on building features instead of fixing style issues. Finally, adopting pre-commit Odoo apps leads to cleaner code and faster reviews.

Additional Resources

  • Official pre-commit docs: https://pre-commit.com/
  • OCA pylint-odoo repo: https://github.com/OCA/pylint-odoo
  • OCA flake8-odoo repo: https://github.com/OCA/flake8-odoo
  • Odoo developer docs: https://www.odoo.com/documentation

FAQ

What is pre-commit?

First, pre-commit is a framework for managing and running Git hooks. Next, it helps you automate code checks before commits. Finally, it supports many tools via a simple YAML.

Why use pre-commit for Odoo?

First, Odoo modules require manifest and style checks. Next, OCA plugins enforce community standards. Finally, pre-commit automates these checks.

How do I skip hooks?

First, use git commit --no-verify to bypass hooks. Next, fix underlying issues and re-enable checks. Finally, avoid long‑term skips.

How do I update hook versions?

First, change the rev: in .pre-commit-config.yaml to the new tag. Next, run pre-commit autoupdate. Finally, test and commit the updated config.


Discover more from teguhteja.id

Subscribe to get the latest posts sent to your email.

Leave a Reply

WP Twitter Auto Publish Powered By : XYZScripts.com