diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c362e20ad..a78784566 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,3 +14,10 @@ repos: rev: v2.1.1 hooks: - id: darker + - repo: local + hooks: + - id: changelog-version-consistency + name: "Ensure changelog 'in progress' version matches package version" + language: system + files: 'CHANGELOG\.md|openeogeotrellis/_version\.py' + entry: python ./scripts/changelog-version-consistency.py diff --git a/scripts/changelog-version-consistency.py b/scripts/changelog-version-consistency.py new file mode 100644 index 000000000..0d4b0fd4a --- /dev/null +++ b/scripts/changelog-version-consistency.py @@ -0,0 +1,54 @@ +# TODO: move this script to a repo that allow better reuse? + +import re +import textwrap +from pathlib import Path + + +def get_version(path: Path) -> str: + """Get version from _version.py""" + versions = re.findall(r"__version__\s*=\s*['\"](.*?)['\"]", path.read_text(encoding="utf8")) + if len(versions) != 1: + raise ValueError(f"Expected one version, but found {versions}") + return versions[0] + + +def get_in_progress_header(path: Path) -> str: + """Get 'In progress' header from CHANGELOG.md""" + in_progress_headers = re.findall( + r"^## In progress: .*$", path.read_text(encoding="utf8"), re.MULTILINE | re.IGNORECASE + ) + if len(in_progress_headers) != 1: + raise ValueError(f"Expected single 'In progress' header, but found {in_progress_headers}") + return in_progress_headers[0] + + +def main(): + root = Path.cwd() + + version_path = root / "openeogeotrellis" / "_version.py" + expected_version = get_version(version_path) + expected_version = expected_version.partition("a")[0] + expected_in_progress_header = f"## In progress: {expected_version}" + + changelog_path = root / "CHANGELOG.md" + in_progress_header = get_in_progress_header(path=changelog_path) + + if in_progress_header.lower() != expected_in_progress_header.lower(): + print( + textwrap.dedent( + f"""\ + Version inconsistency in CHANGELOG.md. + Expected: + {expected_in_progress_header} + but found: + {in_progress_header} + """ + ) + ) + return 1 + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())