Skip to content

Commit 2a0c779

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

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

.pre-commit-config.yaml

+7
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
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import textwrap
2+
3+
import re
4+
from pathlib import Path
5+
6+
7+
def get_version(path: Path) -> str:
8+
"""Get version from _version.py"""
9+
versions = re.findall(r"__version__\s*=\s*['\"](.*?)['\"]", path.read_text(encoding="utf8"))
10+
if len(versions) != 1:
11+
raise ValueError(f"Expected one version, but found {versions}")
12+
return versions[0]
13+
14+
15+
def get_in_progress_header(path: Path) -> str:
16+
"""Get 'In progress' header from CHANGELOG.md"""
17+
in_progress_headers = re.findall(
18+
r"^## In progress: .*$", path.read_text(encoding="utf8"), re.MULTILINE | re.IGNORECASE
19+
)
20+
if len(in_progress_headers) != 1:
21+
raise ValueError(f"Expected single 'In progress' header, but found {in_progress_headers}")
22+
return in_progress_headers[0]
23+
24+
def main():
25+
root = Path.cwd()
26+
27+
version_path = root / "openeogeotrellis" / "_version.py"
28+
expected_version = get_version(version_path)
29+
expected_version = expected_version.partition("a")[0]
30+
expected_in_progress_header = f"## In progress: {expected_version}"
31+
32+
changelog_path = root / "CHANGELOG.md"
33+
in_progress_header = get_in_progress_header(path=changelog_path)
34+
35+
if in_progress_header.lower() != expected_in_progress_header.lower():
36+
print(
37+
textwrap.dedent(
38+
f"""\
39+
Version inconsistency in CHANGELOG.md.
40+
Expected:
41+
{expected_in_progress_header}
42+
but found:
43+
{in_progress_header}
44+
"""
45+
)
46+
)
47+
return 1
48+
return 0
49+
50+
51+
if __name__ == "__main__":
52+
raise SystemExit(main())

0 commit comments

Comments
 (0)