commit 12e3965730905404125c6f8ee7231c2e74e0dc42 Author: Corentin Séchet Date: Wed Nov 27 11:36:34 2024 +0100 feat: add filename convention pre-commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e385369 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +fc_hooks.egg-info diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..f06eaad --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,15 @@ +default_stages: [commit, merge-commit, push, manual] +default_install_hook_types: [commit-msg, pre-commit] +repos: + - repo: https://github.com/compilerla/conventional-pre-commit + rev: v2.3.0 + hooks: + - id: conventional-pre-commit + stages: [commit-msg] + args: [] + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.3.3 + hooks: + - id: ruff + args: [ --fix ] + - id: ruff-format diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..3195d0e --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,11 @@ +- id: fc-path-convention + name: Frog Collective path convention + description: Check paths follows Frog Collective's naming convention + entry: fc-hooks + language: python + types: [text] + exclude: | + (?x)( + ^.pre-commit-config.yaml + ) + stages: [pre-commit, pre-merge-commit, manual] diff --git a/README.md b/README.md new file mode 100644 index 0000000..9762b0b --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ +# Frog Collective's commit hooks + +## Use these hooks + +### Configure pre-commit + +Add this repository to your .pre-commit-config.yaml file : + +```yaml +repos: + - repo: https://git.frog-collective.com/frog-collective/commit-hooks.git + rev: 0.1 + hooks: + - id: fc-path-convention +``` + +### Available Hooks + +* fc-path-convention : Adri je te laisse mettre ici ce qu'il faut + +## Test hooks + +### Test the hooks command + +You can install the hook command and test it without calling pre-commit by +installing a virtualenv, and thise package in it : + +``` + $ virtualenv .venv + $ source .venv/bin/activate + $ pip install -e . +``` + +Then you can test the hooks by running the 'fc-hooks' command : + +``` +$ fc-hooks TestBadFileName.py +TestBadFileName.py doesn't follow the naming convention +``` + +### Test hooks inside a repository + +You can test the hooks in any repository, by using the pre-commit's 'try-repo' +command. + +First, clone this repository : + +``` $ git git@git.frog-collective.com:frog-collective/commit-hooks.git ``` + +Go to your repository, and try the hooks : + +``` +$ cd my-repo +$ pre-commit try-repo ../commit-hooks + +=============================================================================== +Using config: +=============================================================================== +repos: +- repo: ../commit-hooks + rev: 5ca01ce3117dfc15baa68e31cf580601a3f12c9b + hooks: + - id: fc-path-convention +=============================================================================== +[INFO] Installing environment for ../commit-hooks. +[INFO] Once installed this environment will be reused. +[INFO] This may take a few minutes... +Frog Collective path convention..........................................Passed +``` diff --git a/fc_hooks/__init__.py b/fc_hooks/__init__.py new file mode 100644 index 0000000..7124271 --- /dev/null +++ b/fc_hooks/__init__.py @@ -0,0 +1,33 @@ +import re +from pathlib import Path + +from click import argument, command, echo + + +def check_path(path: Path) -> bool: + str_path = str(path) + + if path.suffix == ".cs": + if re.search(r"^[a-zA-Z\\\/\._]*$", str_path) is None: + return False + else: + if re.search(r"^[a-z\\\/\._]*$", str_path) is None: + return False + + return True + + +@command +@argument( + "paths", + nargs=-1, +) +def main(paths: list[str]) -> None: + has_error = False + for path in paths: + if not check_path(Path(path)): + echo(f"{path} doesn't follows convention") + has_error = True + + if has_error: + exit(1) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..965a573 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,42 @@ +[project] +name = "fc-hooks" +version = "0.0.1" +description = "Frog Collective's commit hooks" +license = {text = "WTFPL"} +readme = "README.md" +dependencies = [ + "click", +] + +[project.scripts] +fc-hooks = "fc_hooks:main" + +[build-system] +requires = ["setuptools>=45"] + +[tool.setuptools.packages.find] +include = ["fc_hooks"] + +[tool.ruff] +line-length = 120 + +[tool.ruff.lint] +select = [ + "E", + "F", + "W", + "UP", + "B", + "SIM", + "I", + "N", + "U", + "YTT", + "ASYNC", + "RUF" +] + +[tool.mypy] +strict = true +packages = "fc_hooks" +