Skip to content

Commit f099390

Browse files
authored
Merge pull request #1 from MiraGeoscience/DEVOPS-420
DEVOPS-420 - Sharing pre-commit-hooks script in a python package
2 parents 19a3ae0 + 574a9bb commit f099390

11 files changed

Lines changed: 622 additions & 0 deletions

.codespellignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ba
2+
nd
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Create JIRA issue
2+
3+
on:
4+
issues:
5+
types: [opened]
6+
7+
jobs:
8+
call-workflow-create-jira-issue:
9+
uses: MiraGeoscience/CI-tools/.github/workflows/reusable-jira-issue_to_jira.yml@v1.0.0
10+
secrets: inherit
11+
with:
12+
project_key: 'GEOPY'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: Add JIRA issue summary
2+
3+
on:
4+
pull_request_target:
5+
types: [opened]
6+
7+
jobs:
8+
call-workflow-add-jira-issue-summary:
9+
uses: MiraGeoscience/CI-tools/.github/workflows/reusable-jira-pr_add_jira_summary.yml@v1.0.0
10+
secrets: inherit

.pre-commit-config.yaml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
default_language_version:
2+
python: python3
3+
exclude: ^docs/(conf.py|_ext/)
4+
default_stages: [commit,push]
5+
fail_fast: false
6+
7+
ci:
8+
skip: [pylint]
9+
autoupdate_branch: develop
10+
autoupdate_schedule: monthly
11+
12+
repos:
13+
- repo: https://github.com/python-poetry/poetry
14+
rev: 1.8.0
15+
hooks:
16+
- id: poetry-check
17+
args: [--lock]
18+
- repo: https://github.com/hadialqattan/pycln
19+
rev: v2.4.0
20+
hooks:
21+
- id: pycln
22+
args: [ --config=pyproject.toml ]
23+
- repo: https://github.com/astral-sh/ruff-pre-commit
24+
rev: v0.5.1
25+
hooks:
26+
- id: ruff
27+
args:
28+
- --fix
29+
- --exit-non-zero-on-fix
30+
# - --unsafe-fixes
31+
- id: ruff-format
32+
- repo: https://github.com/pre-commit/mirrors-mypy
33+
rev: v1.10.1
34+
hooks:
35+
- id: mypy
36+
additional_dependencies: [
37+
tomli, # to read config from pyproject.toml
38+
]
39+
- repo: https://github.com/codingjoe/relint
40+
rev: 3.1.1
41+
hooks:
42+
- id: relint
43+
args: [-W] # to fail on warnings
44+
- repo: https://github.com/MiraGeoscience/pre-commit-hooks
45+
rev: v1.0.0
46+
hooks:
47+
- id: check-copyright
48+
files: (^LICENSE|^README(|-dev).rst|\.py|\.pyi)$
49+
exclude: (^\.|^docs/)
50+
- id: prepare-commit-msg
51+
- id: check-commit-msg
52+
- repo: https://github.com/codespell-project/codespell
53+
rev: v2.3.0
54+
hooks:
55+
- id: codespell
56+
exclude: (\.lock|\.ipynb|^THIRD_PARTY_SOFTWARE\.rst)$
57+
entry: codespell -I .codespellignore
58+
- repo: https://github.com/pre-commit/pre-commit-hooks
59+
rev: v4.6.0
60+
hooks:
61+
- id: trailing-whitespace
62+
exclude: \.mdj$
63+
- id: check-json
64+
exclude_types: [jupyter]
65+
- id: check-toml
66+
- id: check-yaml
67+
- id: check-added-large-files # crashing on some configuration. To be investigated
68+
- id: check-case-conflict
69+
- id: check-merge-conflict
70+
- id: debug-statements
71+
- id: detect-private-key
72+
- id: end-of-file-fixer
73+
exclude: (^\.idea/.*\.xml|\.mdj)$
74+
- id: mixed-line-ending
75+
exclude: ^\.idea/.*\.xml$
76+
- id: name-tests-test
77+
- id: pretty-format-json
78+
args:
79+
- --autofix
80+
- --indent=4
81+
- --no-sort-keys
82+
exclude_types: [jupyter]
83+
exclude: ^docs/(.*/)?images/
84+
- repo: https://github.com/rstcheck/rstcheck
85+
rev: v6.2.4
86+
hooks:
87+
- id: rstcheck
88+
exclude: ^THIRD_PARTY_SOFTWARE.rst$
89+
additional_dependencies: [sphinx]
90+
- repo: https://github.com/pre-commit/pygrep-hooks
91+
rev: v1.10.0
92+
hooks:
93+
- id: rst-backticks
94+
exclude: ^THIRD_PARTY_SOFTWARE.rst$

.pre-commit-hooks.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
- id: check-copyright
2+
name: check-copyright
3+
description: check for valid copyright statements in given files
4+
entry: check_copyright
5+
language: python
6+
types: [text]
7+
8+
- id: prepare-commit-msg
9+
name: prepare commit message
10+
description: add the JIRA ID found in the branch name in case it is missing from the commit message
11+
entry: git_message_hook
12+
args: [--prepare]
13+
language: python
14+
stages: [prepare-commit-msg]
15+
16+
- id: check-commit-msg
17+
name: check commit message
18+
description: check the commit message for a valid JIRA ID
19+
entry: git_message_hook
20+
args: [--check]
21+
language: python
22+
stages: [commit-msg]

.relint.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- name: No import os.path
2+
pattern: 'from os import\b.*\bpath\b'
3+
hint: Use from pathlib import Path
4+
filePattern: .*\.pyi?
5+
error: false
6+
- name: No os.path
7+
pattern: '\bos\.path\b'
8+
hint: Use pathlib.Path
9+
filePattern: .*\.pyi?
10+
error: false

README.rst

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
MiraGeoscience.Pre-Commit Hooks
2+
===============================
3+
4+
This repository provides a collection of pre-commit hooks to automate
5+
essential checks and streamline your development workflow. By utilizing
6+
these hooks, you can maintain code quality and consistency across your
7+
projects, fostering a more efficient and collaborative development
8+
environment.
9+
10+
Included Hooks
11+
^^^^^^^^^^^^^^
12+
13+
- ``check-copyright``: Checks for valid copyright statements in files.
14+
- ``prepare-commit-msg``: Adds the JIRA ID found in the branch name
15+
in case it is missing from the commit message.
16+
- ``check-commit-message``: Checks that the branch name or the commit
17+
message starts with a reference to JIRA, and if the message meets the
18+
minimum required length for the summary line. Also checks that the JIRA ID in
19+
the commit message is consistEnt with the one extracted from the
20+
branch name (if any).
21+
22+
Usage
23+
^^^^^
24+
25+
Example of ``.pre-commit-config.yamnl``:
26+
27+
.. code:: yaml
28+
repos:
29+
- repo: http://github.com/MiraGeoscience/pre-commit-hooks
30+
rev: <release>
31+
hooks:
32+
- id: check-copyright
33+
types: [text]
34+
files: (^LICENSE|^README(|-dev).rst|\.py|\.pyi)$
35+
exclude: (^\.|^docs/)
36+
- id: prepare-commit-msg
37+
- id: check-commit-msg
38+
39+
License
40+
^^^^^^^
41+
42+
MIT License
43+
44+
Copyright (c) 2024 Mira Geoscience
45+
46+
Permission is hereby granted, free of charge, to any person obtaining a copy
47+
of this software and associated documentation files (the "Software"), to deal
48+
in the Software without restriction, including without limitation the rights
49+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
50+
copies of the Software, and to permit persons to whom the Software is
51+
furnished to do so, subject to the following conditions:
52+
53+
The above copyright notice and this permission notice shall be included in all
54+
copies or substantial portions of the Software.
55+
56+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
59+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
60+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
62+
SOFTWARE.
63+
64+
Copyright
65+
^^^^^^^^^
66+
Copyright (c) 2024 Mira Geoscience Ltd.

mirageoscience/hooks/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
2+
# Copyright (c) 2024 Mira Geoscience Ltd. '
3+
# '
4+
# This file is part of mirageoscience.pre-commit-hooks package. '
5+
# '
6+
# All rights reserved. '
7+
# ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
8+
9+
__version__ = "1.0.0"
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
3+
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
4+
# Copyright (c) 2024 Mira Geoscience Ltd. '
5+
# '
6+
# This file is part of mirageoscience.pre-commit-hooks package. '
7+
# '
8+
# mirageoscience_pre_commit_hooks is distributed under the terms and conditions of '
9+
# the MIT License (see LICENSE file at the root of this source code package). '
10+
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
11+
12+
from __future__ import annotations
13+
14+
import re
15+
import sys
16+
from datetime import date
17+
18+
19+
def main(args=None):
20+
"""Checks for valid copyright statements in given files.
21+
22+
This function scans the specified files for copyright notices and reports
23+
any files that either lack a copyright statement or have an invalid year.
24+
25+
Args:
26+
args (list, optional): A list of filenames to be checked. Defaults to
27+
`sys.argv[1:]` if not provided.
28+
29+
Raises:
30+
SystemExit: Exits the program with an exit code of 1 if any
31+
files have missing or invalid copyright statements.
32+
"""
33+
current_year = date.today().year
34+
copyright_re = re.compile(
35+
rf"\bcopyright \(c\) (:?\d{{4}}-|)\b{current_year}\b", re.IGNORECASE
36+
)
37+
files = sys.argv[1:]
38+
max_lines = 10
39+
report_files = []
40+
for f in files:
41+
with open(f, encoding="utf-8") as file:
42+
count = 0
43+
has_dated_copyright = False
44+
for line in file:
45+
count += 1
46+
if count >= max_lines and not (
47+
f.endswith("README.rst") or f.endswith("README-dev.rst")
48+
):
49+
break
50+
if re.search(copyright_re, line):
51+
has_dated_copyright = True
52+
break
53+
54+
if not has_dated_copyright:
55+
report_files.append(f)
56+
57+
if len(report_files) > 0:
58+
for f in report_files:
59+
sys.stderr.write(f"{f}: No copyright or invalid year\n")
60+
exit(1)
61+
62+
63+
# readonly CURRENT_YEAR=$(date +"%Y")
64+
65+
# if ! grep -e "Copyright (c) .*$CURRENT_YEAR" $(head -10 $f) 2>&1 1>/dev/null; then
66+
# echo "File '$f' has no copyright or an invalid year"
67+
# exit 1
68+
# fi

0 commit comments

Comments
 (0)