From 78ad9603f66dd9432dedcbb36199aff8dfc09a54 Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Wed, 20 Nov 2024 12:11:11 +0100 Subject: [PATCH] Improve check_release looking at pyproject.toml Also refactor it to not need templating. --- .../{check_release.py.j2 => check_release.py} | 70 +++++++++++++------ 1 file changed, 47 insertions(+), 23 deletions(-) rename templates/github/.ci/scripts/{check_release.py.j2 => check_release.py} (73%) diff --git a/templates/github/.ci/scripts/check_release.py.j2 b/templates/github/.ci/scripts/check_release.py similarity index 73% rename from templates/github/.ci/scripts/check_release.py.j2 rename to templates/github/.ci/scripts/check_release.py index 915d0af6..095dd739 100755 --- a/templates/github/.ci/scripts/check_release.py.j2 +++ b/templates/github/.ci/scripts/check_release.py @@ -1,35 +1,21 @@ #!/usr/bin/env python -{% include 'header.j2' %} - import argparse import re import os import tomllib import yaml +from pathlib import Path from tempfile import TemporaryDirectory from packaging.version import Version from git import Repo -UPSTREAM_REMOTE = "https://github.com/{{ github_org }}/{{ plugin_name }}.git" -DEFAULT_BRANCH = "{{ plugin_default_branch }}" RELEASE_BRANCH_REGEX = r"^([0-9]+)\.([0-9]+)$" Y_CHANGELOG_EXTS = [".feature", ".removal", ".deprecation"] Z_CHANGELOG_EXTS = [".bugfix", ".doc", ".misc"] -def current_version(repo): - try: - pyproject_toml = tomllib.loads(repo.git.show(f"{DEFAULT_BRANCH}:pyproject.toml")) - current_version = pyproject_toml["project"]["version"] - except Exception: - current_version = repo.git.grep( - "current_version", DEFAULT_BRANCH, "--", ".bumpversion.cfg" - ).split("=")[-1] - return Version(current_version) - - -def main(): +def options(): """Check which branches need a release.""" parser = argparse.ArgumentParser() parser.add_argument( @@ -39,17 +25,57 @@ def main(): "'supported'. Defaults to 'supported', see `supported_release_branches` in " "`plugin_template.yml`.", ) - opts = parser.parse_args() + return parser.parse_args() + + +def template_config(): + # Assume this script lies in .ci/scripts + path = Path(__file__).absolute().parent.parent.parent / "template_config.yml" + return yaml.safe_load(path.read_text()) + + +def current_version(repo, commitish): + try: + pyproject_toml = tomllib.loads(repo.git.show(f"{commitish}:pyproject.toml")) + current_version = pyproject_toml["project"]["version"] + except Exception: + current_version = repo.git.grep( + "current_version", commitish, "--", ".bumpversion.cfg" + ).split("=")[-1] + return Version(current_version) + +def check_pyproject_dependencies(repo, from_commit, to_commit): + try: + old_pyproject = tomllib.load(repo.show("{from_commit}:pyproject.toml")) + old_dependencies = set(old_pyproject["project"]["dependencies"]) + new_pyproject = tomllib.load(repo.show("{to_commit}:pyproject.toml")) + new_dependencies = set(new_pyproject["project"]["dependencies"]) + if old_dependencies != new_dependencies: + return ["dependencies"] + else: + return [] + except Exception as e: + print(f"WARNING: Comparing the dependencies in pyproject.toml failed. ({e})") + # Gathering more details failed. + return ["pyproject.toml changed somehow (PLEASE check if dependencies are affected)."] + + +def main(options, template_config): with TemporaryDirectory() as d: # Clone from upstream to ensure we have updated branches & main + GITHUB_ORG = template_config["github_org"] + PLUGIN_NAME = template_config["plugin_name"] + UPSTREAM_REMOTE = f"https://github.com/{GITHUB_ORG}/{PLUGIN_NAME}.git" + DEFAULT_BRANCH = template_config["plugin_default_branch"] + repo = Repo.clone_from(UPSTREAM_REMOTE, d, filter="blob:none") heads = [h.split("/")[-1] for h in repo.git.ls_remote("--heads").split("\n")] available_branches = [h for h in heads if re.search(RELEASE_BRANCH_REGEX, h)] available_branches.sort(key=lambda ver: Version(ver)) available_branches.append(DEFAULT_BRANCH) - branches = opts.branches + branches = options.branches if branches == "supported": with open(f"{d}/template_config.yml", mode="r") as f: tc = yaml.safe_load(f) @@ -97,9 +123,7 @@ def main(): f"{last_tag}", f"origin/{branch}", "--name-only", "--", "pyproject.toml" ) if pyproject_diff: - reasons.append( - "pyproject.toml changed (PLEASE check if dependencies are affected." - ) + reasons.extend(check_pyproject_dependencies(repo, last_tag, f"origin/{branch}")) if reasons: curr_version = Version(last_tag) @@ -122,7 +146,7 @@ def main(): if ext in Y_CHANGELOG_EXTS: # We don't put Y release bumps in the commit message, check file instead. # The 'current_version' is always the dev of the next version to release. - next_version = current_version(repo).base_version + next_version = current_version(repo, DEFAULT_BRANCH).base_version print(f"A new Y-release is needed! New Version: {next_version}") releases.append(next_version) break @@ -132,4 +156,4 @@ def main(): if __name__ == "__main__": - main() + main(options(), template_config())