Skip to content

Commit

Permalink
chore: handle upcoming bundle changes
Browse files Browse the repository at this point in the history
Fixes #1855 and #1856
  • Loading branch information
lengau committed Aug 27, 2024
1 parent d2ffef3 commit 982793d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
26 changes: 23 additions & 3 deletions charmcraft/application/commands/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import argparse
import collections
import dataclasses
import datetime
import os
import pathlib
import re
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
7 changes: 0 additions & 7 deletions tests/spread/store/name-registration/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 36 additions & 0 deletions tests/unit/commands/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 982793d

Please sign in to comment.