Skip to content

add pre-commit hook to ensure consistency of version in changelog #1095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 54 additions & 0 deletions scripts/changelog-version-consistency.py
Original file line number Diff line number Diff line change
@@ -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())