Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically set the NPM tag #510

Merged
merged 7 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions jupyter_releaser/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ def extract_release(auth, dist_dir, dry_run, release_url):


@main.command()
@add_options(auth_options)
@add_options(dist_dir_options)
@click.option("--npm-token", help="A token for the npm release", envvar="NPM_TOKEN")
@click.option(
Expand All @@ -626,30 +627,40 @@ def extract_release(auth, dist_dir, dry_run, release_url):
envvar="TWINE_REPOSITORY_URL",
default="https://upload.pypi.org/legacy/",
)
@click.option(
"--npm-tag",
help="The npm tag. It defaults to 'next' if it is a prerelease otherwise to 'latest'.",
envvar="NPM_TAG",
default="",
)
@add_options(dry_run_options)
@add_options(python_packages_options)
@add_options(release_url_options)
@use_checkout_dir()
def publish_assets(
auth,
dist_dir,
npm_token,
npm_cmd,
twine_cmd,
npm_registry,
twine_repository_url,
npm_tag,
dry_run,
release_url,
python_packages,
):
"""Publish release asset(s)"""
for python_package in python_packages:
lib.publish_assets(
auth,
dist_dir,
npm_token,
npm_cmd,
twine_cmd,
npm_registry,
twine_repository_url,
npm_tag,
dry_run,
release_url,
python_package,
Expand Down
19 changes: 19 additions & 0 deletions jupyter_releaser/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import shutil
import tempfile
import uuid
import warnings
from datetime import datetime, timezone
from glob import glob
from pathlib import Path
Expand Down Expand Up @@ -315,12 +316,14 @@ def extract_release(auth, dist_dir, dry_run, release_url):


def publish_assets( # noqa
auth,
dist_dir,
npm_token,
npm_cmd,
twine_cmd,
npm_registry,
twine_repository_url,
npm_tag,
dry_run,
release_url,
python_package,
Expand All @@ -334,6 +337,22 @@ def publish_assets( # noqa
npm.handle_npm_config(npm_token)
if npm_token:
util.run("npm whoami")
# check if this is a prerelease
match = util.parse_release_url(release_url)
owner, repo = match["owner"], match["repo"]

gh = util.get_gh_object(dry_run=dry_run, owner=owner, repo=repo, token=auth)
release = util.release_for_url(gh, release_url)
is_prerelease = release.prerelease

if " --tag " not in npm_cmd:
npm_tag = npm_tag or ("next" if is_prerelease else "latest")
npm_cmd = f"{npm_cmd} --tag {npm_tag}"
elif npm_tag:
warnings.warn(
f"The NPM tag '{npm_tag}' will be ignored as at tag option is set in NPM command; '{npm_cmd}'.",
stacklevel=2,
)

res = python_package.split(":")
python_package_path = res[0]
Expand Down
11 changes: 8 additions & 3 deletions jupyter_releaser/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def test_list_envvars(runner):
npm-cmd: RH_NPM_COMMAND
npm-install-options: RH_NPM_INSTALL_OPTIONS
npm-registry: NPM_REGISTRY
npm-tag: NPM_TAG
npm-token: NPM_TOKEN
post-version-message: RH_POST_VERSION_MESSAGE
post-version-spec: RH_POST_VERSION_SPEC
Expand Down Expand Up @@ -593,21 +594,25 @@ def wrapped(cmd, **kwargs):
assert "after-publish-assets" in log


def test_publish_assets_npm(npm_dist, runner, mocker):
def test_publish_assets_npm(npm_dist, runner, mocker, mock_github):
# Create the release.
dist_dir = npm_dist / util.CHECKOUT_NAME / "dist"
release = create_draft_release("bar", glob(f"{dist_dir!s}/*.*"))

os.environ["RH_RELEASE_URL"] = release.html_url

orig_run = util.run
called = 0

def wrapped(cmd, **kwargs):
nonlocal called
if cmd.startswith("npm publish --dry-run"):
if cmd.startswith("npm publish --dry-run --tag next"):
called += 1
return orig_run(cmd, **kwargs)

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

runner(["publish-assets", "--npm-cmd", "npm publish --dry-run", "--dist-dir", dist_dir])

assert called == 3, called


Expand Down