From 982793dff302fc7ad75a6f91dc843ed66158706e Mon Sep 17 00:00:00 2001 From: Alex Lowe Date: Tue, 27 Aug 2024 16:01:40 -0400 Subject: [PATCH] chore: handle upcoming bundle changes Fixes #1855 and #1856 --- charmcraft/application/commands/store.py | 26 ++++++++++++-- .../spread/store/name-registration/task.yaml | 7 ---- tests/unit/commands/test_store.py | 36 +++++++++++++++++++ 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/charmcraft/application/commands/store.py b/charmcraft/application/commands/store.py index ed30c4b32..96a7c4d3c 100644 --- a/charmcraft/application/commands/store.py +++ b/charmcraft/application/commands/store.py @@ -18,6 +18,7 @@ import argparse import collections import dataclasses +import datetime import os import pathlib import re @@ -70,6 +71,9 @@ class _ResourceType(typing.NamedTuple): ResourceType = _ResourceType() # the list of valid attenuations to restrict login credentials VALID_ATTENUATIONS = {getattr(attenuations, x) for x in dir(attenuations) if x.isupper()} +BUNDLE_REGISTRATION_REMOVAL_URL = ( + "https://discourse.charmhub.io/t/discontinuing-new-charmhub-bundle-registrations/15344" +) class LoginCommand(CharmcraftCommand): @@ -349,7 +353,7 @@ class RegisterBundleNameCommand(CharmcraftCommand): name = "register-bundle" help_msg = "Register a bundle name in the Store" overview = textwrap.dedent( - """ + f""" Register a bundle name in the Store. Claim a name for your bundle in Charmhub. Once you have registered @@ -368,18 +372,34 @@ class RegisterBundleNameCommand(CharmcraftCommand): https://discourse.charmhub.io/c/charm Registration will take you through login if needed. + + \u001b[31mWARNING:\u001b[0m Charmhub will stop accepting new bundle registrations on 2024-11-01. + For more information, see: + {BUNDLE_REGISTRATION_REMOVAL_URL} """ ) - def fill_parser(self, parser): + def fill_parser(self, parser: argparse.ArgumentParser): """Add own parameters to the general parser.""" parser.add_argument("name", help="The name to register in Charmhub") - def run(self, parsed_args): + def run(self, parsed_args: argparse.Namespace) -> int: """Run the command.""" + if datetime.date.today() >= datetime.date(2024, 11, 1): + emit.message( + "\u001b[31mERROR:\u001b[0m New bundle registration is discontinued as of 2024-11-01. For more " + f"information, see: {BUNDLE_REGISTRATION_REMOVAL_URL}" + ) + return 1 + emit.progress( + "\u001b[31mWARNING:\u001b[0m New bundle registration will stop working on 2024-11-01. For " + f"more information, see: {BUNDLE_REGISTRATION_REMOVAL_URL}", + permanent=True, + ) store = Store(env.get_store_config()) store.register_name(parsed_args.name, EntityType.bundle) emit.message(f"You are now the publisher of bundle {parsed_args.name!r} in Charmhub.") + return os.EX_OK class UnregisterNameCommand(CharmcraftCommand): diff --git a/tests/spread/store/name-registration/task.yaml b/tests/spread/store/name-registration/task.yaml index f329f5559..bd5d880fe 100644 --- a/tests/spread/store/name-registration/task.yaml +++ b/tests/spread/store/name-registration/task.yaml @@ -33,12 +33,5 @@ execute: | echo $package_type test $package_type == "charm" - charmcraft register-bundle $BUNDLE_NAME - package_type=$(charmcraft names --format=json | jq -r --arg bundle_name $BUNDLE_NAME '.[] | select(.name==$bundle_name) | .type') - echo $package_type - test $package_type == "bundle" - charmcraft unregister $CHARM_NAME - charmcraft unregister $BUNDLE_NAME charmcraft names | NOMATCH $CHARM_NAME - charmcraft names | NOMATCH $BUNDLE_NAME diff --git a/tests/unit/commands/test_store.py b/tests/unit/commands/test_store.py index 774b3b376..1d2164aa9 100644 --- a/tests/unit/commands/test_store.py +++ b/tests/unit/commands/test_store.py @@ -22,10 +22,12 @@ import craft_cli.pytest_plugin import craft_store +import freezegun import pytest from craft_store import models from charmcraft import errors, store +from charmcraft.application import commands from charmcraft.application.commands import SetResourceArchitecturesCommand from charmcraft.application.commands.store import FetchLibs, LoginCommand from charmcraft.application.main import APP_METADATA @@ -264,3 +266,37 @@ def test_fetch_libs_success( emitter.assert_progress("Getting library metadata from charmhub") emitter.assert_message("Downloaded 1 charm libraries.") + + +@freezegun.freeze_time("2024-10-31") +def test_register_bundle_warning(monkeypatch: pytest.MonkeyPatch, emitter): + mock_store = mock.Mock() + monkeypatch.setattr("charmcraft.application.commands.store.Store", mock_store) + + parsed_args = argparse.Namespace(name="name") + cmd = commands.RegisterBundleNameCommand(None) + cmd.run(parsed_args) + + emitter.assert_progress( + "\u001b[31mWARNING:\u001b[0m New bundle registration will stop working on 2024-11-01. For " + f"more information, see: {commands.store.BUNDLE_REGISTRATION_REMOVAL_URL}", + permanent=True, + ) + mock_store.assert_called() + + +@freezegun.freeze_time("2024-11-01") +def test_register_bundle_error(monkeypatch: pytest.MonkeyPatch, emitter): + mock_store = mock.Mock() + monkeypatch.setattr("charmcraft.application.commands.store.Store", mock_store) + + parsed_args = argparse.Namespace(name="name") + cmd = commands.RegisterBundleNameCommand(None) + + assert cmd.run(parsed_args) == 1 + + emitter.assert_message( + "\u001b[31mERROR:\u001b[0m New bundle registration is discontinued as of 2024-11-01. For " + f"more information, see: {commands.store.BUNDLE_REGISTRATION_REMOVAL_URL}", + ) + mock_store.assert_not_called()