Skip to content

Commit cb677f5

Browse files
Allow the tag format customization (#536)
* Allow the tag format customization * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 665b0e1 commit cb677f5

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

jupyter_releaser/cli.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ def main(force):
313313
),
314314
]
315315

316+
tag_format_options: t.Any = [
317+
click.option(
318+
"--tag-format",
319+
envvar="RH_TAG_FORMAT",
320+
default="v{version}",
321+
help="The format to use for the release tag",
322+
)
323+
]
324+
316325

317326
def add_options(options):
318327
"""Add extracted common options to a click command"""
@@ -358,13 +367,14 @@ def prep_git(ref, branch, repo, auth, username, git_url):
358367
@add_options(version_cmd_options)
359368
@add_options(changelog_path_options)
360369
@add_options(python_packages_options)
370+
@add_options(tag_format_options)
361371
@use_checkout_dir()
362-
def bump_version(version_spec, version_cmd, changelog_path, python_packages):
372+
def bump_version(version_spec, version_cmd, changelog_path, python_packages, tag_format):
363373
"""Prep git and env variables and bump version"""
364374
prev_dir = os.getcwd()
365375
for python_package in [p.split(":")[0] for p in python_packages]:
366376
os.chdir(python_package)
367-
lib.bump_version(version_spec, version_cmd, changelog_path)
377+
lib.bump_version(version_spec, version_cmd, changelog_path, tag_format)
368378
os.chdir(prev_dir)
369379

370380

@@ -406,6 +416,7 @@ def build_changelog(
406416
@add_options(changelog_path_options)
407417
@add_options(dry_run_options)
408418
@add_options(post_version_spec_options)
419+
@add_options(tag_format_options)
409420
@use_checkout_dir()
410421
def draft_changelog(
411422
version_spec,
@@ -419,6 +430,7 @@ def draft_changelog(
419430
dry_run,
420431
post_version_spec,
421432
post_version_message,
433+
tag_format,
422434
):
423435
"""Create a changelog entry PR"""
424436
lib.draft_changelog(
@@ -433,6 +445,7 @@ def draft_changelog(
433445
dry_run,
434446
post_version_spec,
435447
post_version_message,
448+
tag_format,
436449
)
437450

438451

@@ -511,12 +524,7 @@ def check_npm(dist_dir, npm_install_options):
511524
default="Publish {version}",
512525
help="The message to use for the release commit",
513526
)
514-
@click.option(
515-
"--tag-format",
516-
envvar="RH_TAG_FORMAT",
517-
default="v{version}",
518-
help="The format to use for the release tag",
519-
)
527+
@add_options(tag_format_options)
520528
@click.option(
521529
"--tag-message",
522530
envvar="RH_TAG_MESSAGE",
@@ -544,6 +552,7 @@ def tag_release(dist_dir, release_message, tag_format, tag_message, no_git_tag_w
544552
@add_options(release_url_options)
545553
@add_options(post_version_spec_options)
546554
@click.argument("assets", nargs=-1)
555+
@add_options(tag_format_options)
547556
@use_checkout_dir()
548557
def populate_release(
549558
ref,
@@ -558,6 +567,7 @@ def populate_release(
558567
post_version_spec,
559568
post_version_message,
560569
assets,
570+
tag_format,
561571
):
562572
"""Populate a release."""
563573
lib.populate_release(
@@ -573,6 +583,7 @@ def populate_release(
573583
post_version_spec,
574584
post_version_message,
575585
assets,
586+
tag_format,
576587
)
577588

578589

jupyter_releaser/lib.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from jupyter_releaser import changelog, npm, python, util
2323

2424

25-
def bump_version(version_spec, version_cmd, changelog_path):
25+
def bump_version(version_spec, version_cmd, changelog_path, tag_format):
2626
"""Bump the version and verify new version"""
2727
util.bump_version(version_spec, version_cmd=version_cmd, changelog_path=changelog_path)
2828

@@ -36,7 +36,7 @@ def bump_version(version_spec, version_cmd, changelog_path):
3636
raise ValueError(msg)
3737

3838
# Bail if tag already exists
39-
tag_name = f"v{version}"
39+
tag_name = tag_format.format(version=version)
4040
if tag_name in util.run("git --no-pager tag", quiet=True).splitlines():
4141
msg = f"Tag {tag_name} already exists!"
4242
msg += " To delete run: `git push --delete origin {tag_name}`"
@@ -57,12 +57,14 @@ def draft_changelog(
5757
dry_run,
5858
post_version_spec,
5959
post_version_message,
60+
tag_format,
6061
):
6162
"""Create a changelog entry PR"""
6263
repo = repo or util.get_repo()
6364
branch = branch or util.get_branch()
6465
version = util.get_version()
6566
prerelease = util.is_prerelease(version)
67+
tag_name = tag_format.format(version=version)
6668

6769
current_sha = util.run("git rev-parse HEAD")
6870

@@ -73,8 +75,8 @@ def draft_changelog(
7375
util.log(npm_versions)
7476

7577
tags = util.run("git --no-pager tag", quiet=True)
76-
if f"v{version}" in tags.splitlines():
77-
msg = f"Tag v{version} already exists"
78+
if tag_name in tags.splitlines():
79+
msg = f"Tag {tag_name} already exists"
7880
raise ValueError(msg)
7981

8082
current = changelog.extract_current(changelog_path)
@@ -110,7 +112,7 @@ def draft_changelog(
110112
json.dump(data, fid)
111113

112114
release = gh.create_release(
113-
f"v{version}", branch, f"v{version}", current, True, prerelease, files=[metadata_path]
115+
tag_name, branch, tag_name, current, True, prerelease, files=[metadata_path]
114116
)
115117

116118
# Remove draft releases over a day old
@@ -209,6 +211,7 @@ def populate_release(
209211
post_version_spec,
210212
post_version_message,
211213
assets,
214+
tag_format,
212215
):
213216
"""Populate release assets and push tags and commits"""
214217
branch = branch or util.get_branch()
@@ -221,7 +224,10 @@ def populate_release(
221224
# Bump to post version if given.
222225
if post_version_spec:
223226
post_version = bump_version(
224-
post_version_spec, version_cmd=version_cmd, changelog_path=changelog_path
227+
post_version_spec,
228+
version_cmd=version_cmd,
229+
changelog_path=changelog_path,
230+
tag_format=tag_format,
225231
)
226232
util.log(post_version_message.format(post_version=post_version))
227233
util.run(f'git commit -a -m "Bump to {post_version}"')

jupyter_releaser/tests/test_cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ def test_bump_version_tag_exists(py_package, runner):
137137
runner(["bump-version", "--version-spec", "1.0.1"], env=dict(GITHUB_ACTIONS=""))
138138

139139

140+
def test_bump_version_custom_tag_exists(py_package, runner):
141+
runner(["prep-git", "--git-url", py_package])
142+
run("git tag rev1.0.1", cwd=util.CHECKOUT_NAME)
143+
with pytest.raises(ValueError):
144+
runner(
145+
["bump-version", "--version-spec", "1.0.1"],
146+
env=dict(GITHUB_ACTIONS="", RH_TAG_FORMAT=r"rev{version}"),
147+
)
148+
149+
140150
def test_list_envvars(runner):
141151
result = runner(["list-envvars"])
142152
assert (

0 commit comments

Comments
 (0)