Skip to content

Commit b1388d0

Browse files
fcollonvalpre-commit-ci[bot]blink1073
authored
Dynamically set the NPM tag (#510)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Steven Silvester <[email protected]>
1 parent 1fe6781 commit b1388d0

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

jupyter_releaser/cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ def extract_release(auth, dist_dir, dry_run, release_url):
600600

601601

602602
@main.command()
603+
@add_options(auth_options)
603604
@add_options(dist_dir_options)
604605
@click.option("--npm-token", help="A token for the npm release", envvar="NPM_TOKEN")
605606
@click.option(
@@ -626,30 +627,40 @@ def extract_release(auth, dist_dir, dry_run, release_url):
626627
envvar="TWINE_REPOSITORY_URL",
627628
default="https://upload.pypi.org/legacy/",
628629
)
630+
@click.option(
631+
"--npm-tag",
632+
help="The npm tag. It defaults to 'next' if it is a prerelease otherwise to 'latest'.",
633+
envvar="NPM_TAG",
634+
default="",
635+
)
629636
@add_options(dry_run_options)
630637
@add_options(python_packages_options)
631638
@add_options(release_url_options)
632639
@use_checkout_dir()
633640
def publish_assets(
641+
auth,
634642
dist_dir,
635643
npm_token,
636644
npm_cmd,
637645
twine_cmd,
638646
npm_registry,
639647
twine_repository_url,
648+
npm_tag,
640649
dry_run,
641650
release_url,
642651
python_packages,
643652
):
644653
"""Publish release asset(s)"""
645654
for python_package in python_packages:
646655
lib.publish_assets(
656+
auth,
647657
dist_dir,
648658
npm_token,
649659
npm_cmd,
650660
twine_cmd,
651661
npm_registry,
652662
twine_repository_url,
663+
npm_tag,
653664
dry_run,
654665
release_url,
655666
python_package,

jupyter_releaser/lib.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import shutil
99
import tempfile
1010
import uuid
11+
import warnings
1112
from datetime import datetime, timezone
1213
from glob import glob
1314
from pathlib import Path
@@ -315,12 +316,14 @@ def extract_release(auth, dist_dir, dry_run, release_url):
315316

316317

317318
def publish_assets( # noqa
319+
auth,
318320
dist_dir,
319321
npm_token,
320322
npm_cmd,
321323
twine_cmd,
322324
npm_registry,
323325
twine_repository_url,
326+
npm_tag,
324327
dry_run,
325328
release_url,
326329
python_package,
@@ -334,6 +337,22 @@ def publish_assets( # noqa
334337
npm.handle_npm_config(npm_token)
335338
if npm_token:
336339
util.run("npm whoami")
340+
# check if this is a prerelease
341+
match = util.parse_release_url(release_url)
342+
owner, repo = match["owner"], match["repo"]
343+
344+
gh = util.get_gh_object(dry_run=dry_run, owner=owner, repo=repo, token=auth)
345+
release = util.release_for_url(gh, release_url)
346+
is_prerelease = release.prerelease
347+
348+
if " --tag " not in npm_cmd:
349+
npm_tag = npm_tag or ("next" if is_prerelease else "latest")
350+
npm_cmd = f"{npm_cmd} --tag {npm_tag}"
351+
elif npm_tag:
352+
warnings.warn(
353+
f"The NPM tag '{npm_tag}' will be ignored as at tag option is set in NPM command; '{npm_cmd}'.",
354+
stacklevel=2,
355+
)
337356

338357
res = python_package.split(":")
339358
python_package_path = res[0]

jupyter_releaser/tests/test_cli.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def test_list_envvars(runner):
152152
npm-cmd: RH_NPM_COMMAND
153153
npm-install-options: RH_NPM_INSTALL_OPTIONS
154154
npm-registry: NPM_REGISTRY
155+
npm-tag: NPM_TAG
155156
npm-token: NPM_TOKEN
156157
post-version-message: RH_POST_VERSION_MESSAGE
157158
post-version-spec: RH_POST_VERSION_SPEC
@@ -593,21 +594,25 @@ def wrapped(cmd, **kwargs):
593594
assert "after-publish-assets" in log
594595

595596

596-
def test_publish_assets_npm(npm_dist, runner, mocker):
597+
def test_publish_assets_npm(npm_dist, runner, mocker, mock_github):
598+
# Create the release.
597599
dist_dir = npm_dist / util.CHECKOUT_NAME / "dist"
600+
release = create_draft_release("bar", glob(f"{dist_dir!s}/*.*"))
601+
602+
os.environ["RH_RELEASE_URL"] = release.html_url
603+
598604
orig_run = util.run
599605
called = 0
600606

601607
def wrapped(cmd, **kwargs):
602608
nonlocal called
603-
if cmd.startswith("npm publish --dry-run"):
609+
if cmd.startswith("npm publish --dry-run --tag next"):
604610
called += 1
605611
return orig_run(cmd, **kwargs)
606612

607613
mock_run = mocker.patch("jupyter_releaser.util.run", wraps=wrapped)
608614

609615
runner(["publish-assets", "--npm-cmd", "npm publish --dry-run", "--dist-dir", dist_dir])
610-
611616
assert called == 3, called
612617

613618

0 commit comments

Comments
 (0)