Skip to content

Commit 3a7b560

Browse files
committed
add pre-commit hook to ensure consistency of version in changelog
1 parent 1b51beb commit 3a7b560

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ repos:
1414
rev: v2.1.1
1515
hooks:
1616
- id: darker
17+
- repo: local
18+
hooks:
19+
- id: changelog-version-consistency
20+
name: "Ensure changelog 'in progress' version matches package version"
21+
language: system
22+
files: 'CHANGELOG\.md|openeogeotrellis/_version\.py'
23+
entry: python ./scripts/changelog-version-consistency.py
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# TODO: move this script to a repo that allow better reuse?
2+
3+
import re
4+
import textwrap
5+
from pathlib import Path
6+
7+
8+
def get_version(path: Path) -> str:
9+
"""Get version from _version.py"""
10+
versions = re.findall(r"__version__\s*=\s*['\"](.*?)['\"]", path.read_text(encoding="utf8"))
11+
if len(versions) != 1:
12+
raise ValueError(f"Expected one version, but found {versions}")
13+
return versions[0]
14+
15+
16+
def get_in_progress_header(path: Path) -> str:
17+
"""Get 'In progress' header from CHANGELOG.md"""
18+
in_progress_headers = re.findall(
19+
r"^## In progress: .*$", path.read_text(encoding="utf8"), re.MULTILINE | re.IGNORECASE
20+
)
21+
if len(in_progress_headers) != 1:
22+
raise ValueError(f"Expected single 'In progress' header, but found {in_progress_headers}")
23+
return in_progress_headers[0]
24+
25+
26+
def main():
27+
root = Path.cwd()
28+
29+
version_path = root / "openeogeotrellis" / "_version.py"
30+
expected_version = get_version(version_path)
31+
expected_version = expected_version.partition("a")[0]
32+
expected_in_progress_header = f"## In progress: {expected_version}"
33+
34+
changelog_path = root / "CHANGELOG.md"
35+
in_progress_header = get_in_progress_header(path=changelog_path)
36+
37+
if in_progress_header.lower() != expected_in_progress_header.lower():
38+
print(
39+
textwrap.dedent(
40+
f"""\
41+
Version inconsistency in CHANGELOG.md.
42+
Expected:
43+
{expected_in_progress_header}
44+
but found:
45+
{in_progress_header}
46+
"""
47+
)
48+
)
49+
return 1
50+
return 0
51+
52+
53+
if __name__ == "__main__":
54+
raise SystemExit(main())

0 commit comments

Comments
 (0)