From d953893b59aaa07db89b0969dac9d7e80821bade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Collonval?= Date: Fri, 17 Nov 2023 15:54:18 +0100 Subject: [PATCH 1/2] Allow the tag format customization --- jupyter_releaser/cli.py | 27 +++++++++++++++++++-------- jupyter_releaser/lib.py | 15 +++++++++------ jupyter_releaser/tests/test_cli.py | 7 +++++++ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/jupyter_releaser/cli.py b/jupyter_releaser/cli.py index 4928cffa..12825a55 100644 --- a/jupyter_releaser/cli.py +++ b/jupyter_releaser/cli.py @@ -313,6 +313,15 @@ def main(force): ), ] +tag_format_options: t.Any = [ + click.option( + "--tag-format", + envvar="RH_TAG_FORMAT", + default="v{version}", + help="The format to use for the release tag", + ) +] + def add_options(options): """Add extracted common options to a click command""" @@ -358,13 +367,14 @@ def prep_git(ref, branch, repo, auth, username, git_url): @add_options(version_cmd_options) @add_options(changelog_path_options) @add_options(python_packages_options) +@add_options(tag_format_options) @use_checkout_dir() -def bump_version(version_spec, version_cmd, changelog_path, python_packages): +def bump_version(version_spec, version_cmd, changelog_path, python_packages, tag_format): """Prep git and env variables and bump version""" prev_dir = os.getcwd() for python_package in [p.split(":")[0] for p in python_packages]: os.chdir(python_package) - lib.bump_version(version_spec, version_cmd, changelog_path) + lib.bump_version(version_spec, version_cmd, changelog_path, tag_format) os.chdir(prev_dir) @@ -406,6 +416,7 @@ def build_changelog( @add_options(changelog_path_options) @add_options(dry_run_options) @add_options(post_version_spec_options) +@add_options(tag_format_options) @use_checkout_dir() def draft_changelog( version_spec, @@ -419,6 +430,7 @@ def draft_changelog( dry_run, post_version_spec, post_version_message, + tag_format, ): """Create a changelog entry PR""" lib.draft_changelog( @@ -433,6 +445,7 @@ def draft_changelog( dry_run, post_version_spec, post_version_message, + tag_format, ) @@ -511,12 +524,7 @@ def check_npm(dist_dir, npm_install_options): default="Publish {version}", help="The message to use for the release commit", ) -@click.option( - "--tag-format", - envvar="RH_TAG_FORMAT", - default="v{version}", - help="The format to use for the release tag", -) +@add_options(tag_format_options) @click.option( "--tag-message", envvar="RH_TAG_MESSAGE", @@ -544,6 +552,7 @@ def tag_release(dist_dir, release_message, tag_format, tag_message, no_git_tag_w @add_options(release_url_options) @add_options(post_version_spec_options) @click.argument("assets", nargs=-1) +@add_options(tag_format_options) @use_checkout_dir() def populate_release( ref, @@ -558,6 +567,7 @@ def populate_release( post_version_spec, post_version_message, assets, + tag_format, ): """Populate a release.""" lib.populate_release( @@ -573,6 +583,7 @@ def populate_release( post_version_spec, post_version_message, assets, + tag_format, ) diff --git a/jupyter_releaser/lib.py b/jupyter_releaser/lib.py index ed176e5c..f52de7bf 100644 --- a/jupyter_releaser/lib.py +++ b/jupyter_releaser/lib.py @@ -22,7 +22,7 @@ from jupyter_releaser import changelog, npm, python, util -def bump_version(version_spec, version_cmd, changelog_path): +def bump_version(version_spec, version_cmd, changelog_path, tag_format): """Bump the version and verify new version""" util.bump_version(version_spec, version_cmd=version_cmd, changelog_path=changelog_path) @@ -36,7 +36,7 @@ def bump_version(version_spec, version_cmd, changelog_path): raise ValueError(msg) # Bail if tag already exists - tag_name = f"v{version}" + tag_name = tag_format.format(version=version) if tag_name in util.run("git --no-pager tag", quiet=True).splitlines(): msg = f"Tag {tag_name} already exists!" msg += " To delete run: `git push --delete origin {tag_name}`" @@ -57,12 +57,14 @@ def draft_changelog( dry_run, post_version_spec, post_version_message, + tag_format, ): """Create a changelog entry PR""" repo = repo or util.get_repo() branch = branch or util.get_branch() version = util.get_version() prerelease = util.is_prerelease(version) + tag_name = tag_format.format(version=version) current_sha = util.run("git rev-parse HEAD") @@ -73,8 +75,8 @@ def draft_changelog( util.log(npm_versions) tags = util.run("git --no-pager tag", quiet=True) - if f"v{version}" in tags.splitlines(): - msg = f"Tag v{version} already exists" + if tag_name in tags.splitlines(): + msg = f"Tag {tag_name} already exists" raise ValueError(msg) current = changelog.extract_current(changelog_path) @@ -110,7 +112,7 @@ def draft_changelog( json.dump(data, fid) release = gh.create_release( - f"v{version}", branch, f"v{version}", current, True, prerelease, files=[metadata_path] + tag_name, branch, tag_name, current, True, prerelease, files=[metadata_path] ) # Remove draft releases over a day old @@ -209,6 +211,7 @@ def populate_release( post_version_spec, post_version_message, assets, + tag_format, ): """Populate release assets and push tags and commits""" branch = branch or util.get_branch() @@ -221,7 +224,7 @@ def populate_release( # Bump to post version if given. if post_version_spec: post_version = bump_version( - post_version_spec, version_cmd=version_cmd, changelog_path=changelog_path + post_version_spec, version_cmd=version_cmd, changelog_path=changelog_path, tag_format=tag_format ) util.log(post_version_message.format(post_version=post_version)) util.run(f'git commit -a -m "Bump to {post_version}"') diff --git a/jupyter_releaser/tests/test_cli.py b/jupyter_releaser/tests/test_cli.py index ec13ccef..b74e612b 100644 --- a/jupyter_releaser/tests/test_cli.py +++ b/jupyter_releaser/tests/test_cli.py @@ -137,6 +137,13 @@ def test_bump_version_tag_exists(py_package, runner): runner(["bump-version", "--version-spec", "1.0.1"], env=dict(GITHUB_ACTIONS="")) +def test_bump_version_custom_tag_exists(py_package, runner): + runner(["prep-git", "--git-url", py_package]) + run("git tag rev1.0.1", cwd=util.CHECKOUT_NAME) + with pytest.raises(ValueError): + runner(["bump-version", "--version-spec", "1.0.1"], env=dict(GITHUB_ACTIONS="", RH_TAG_FORMAT=r"rev{version}")) + + def test_list_envvars(runner): result = runner(["list-envvars"]) assert ( From a998c2a9dc4ed9e8817e25c0658f093afa1a1422 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 17 Nov 2023 14:57:05 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- jupyter_releaser/lib.py | 5 ++++- jupyter_releaser/tests/test_cli.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/jupyter_releaser/lib.py b/jupyter_releaser/lib.py index f52de7bf..365619df 100644 --- a/jupyter_releaser/lib.py +++ b/jupyter_releaser/lib.py @@ -224,7 +224,10 @@ def populate_release( # Bump to post version if given. if post_version_spec: post_version = bump_version( - post_version_spec, version_cmd=version_cmd, changelog_path=changelog_path, tag_format=tag_format + post_version_spec, + version_cmd=version_cmd, + changelog_path=changelog_path, + tag_format=tag_format, ) util.log(post_version_message.format(post_version=post_version)) util.run(f'git commit -a -m "Bump to {post_version}"') diff --git a/jupyter_releaser/tests/test_cli.py b/jupyter_releaser/tests/test_cli.py index b74e612b..e8f4d6b7 100644 --- a/jupyter_releaser/tests/test_cli.py +++ b/jupyter_releaser/tests/test_cli.py @@ -141,7 +141,10 @@ def test_bump_version_custom_tag_exists(py_package, runner): runner(["prep-git", "--git-url", py_package]) run("git tag rev1.0.1", cwd=util.CHECKOUT_NAME) with pytest.raises(ValueError): - runner(["bump-version", "--version-spec", "1.0.1"], env=dict(GITHUB_ACTIONS="", RH_TAG_FORMAT=r"rev{version}")) + runner( + ["bump-version", "--version-spec", "1.0.1"], + env=dict(GITHUB_ACTIONS="", RH_TAG_FORMAT=r"rev{version}"), + ) def test_list_envvars(runner):