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

feat: create-track command #2024

Merged
merged 9 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions charmcraft/application/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
UploadCommand,
ListRevisionsCommand,
# release process, and show status
CreateTrack,
ReleaseCommand,
PromoteBundleCommand,
StatusCommand,
Expand Down Expand Up @@ -88,6 +89,7 @@ def fill_command_groups(app: craft_application.Application) -> None:
UploadCommand,
ListRevisionsCommand,
# release process, and show status
CreateTrack,
ReleaseCommand,
PromoteBundleCommand,
StatusCommand,
Expand Down Expand Up @@ -142,6 +144,7 @@ def fill_command_groups(app: craft_application.Application) -> None:
"ListNamesCommand",
"UploadCommand",
"ListRevisionsCommand",
"CreateTrack",
"ReleaseCommand",
"PromoteBundleCommand",
"StatusCommand",
Expand Down
70 changes: 70 additions & 0 deletions charmcraft/application/commands/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2374,3 +2374,73 @@ def _get_architectures_from_bases(
for architecture in base.architectures:
architectures.add(architecture)
return sorted(architectures)


class CreateTrack(CharmcraftCommand):
dariuszd21 marked this conversation as resolved.
Show resolved Hide resolved
"""Create one or more tracks."""

name = "create-track"
help_msg = "Create one or more tracks for a charm on Charmhub"
overview = textwrap.dedent(
"""
Create one or more tracks for a charm on Charmhub. Returns
the full list of tracks for that charm.

For example:

$ charmcraft create-track my-charm track-1 track-2
Name Created at Automatic phasing percentage
--------- -------------------- ------------------------------
track-1 2024-12-10T23:48:40Z
track-2 2024-12-11T00:14:24Z
latest 2023-04-17T23:55:07Z
Copy link
Contributor

@dariuszd21 dariuszd21 Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to satisfy RST.
However, from what I know general suggestion is not to put examples in the overview

Suggested change
For example:
$ charmcraft create-track my-charm track-1 track-2
Name Created at Automatic phasing percentage
--------- -------------------- ------------------------------
track-1 2024-12-10T23:48:40Z
track-2 2024-12-11T00:14:24Z
latest 2023-04-17T23:55:07Z
For example::
$ charmcraft create-track my-charm track-1 track-2
Name Created at Automatic phasing percentage
--------- -------------------- ------------------------------
track-1 2024-12-10T23:48:40Z
track-2 2024-12-11T00:14:24Z
latest 2023-04-17T23:55:07Z

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe doing this will subsequently mess up command-line help messages and we'll have another task à la canonical/craft-cli#306. So maybe it would be easier to remove this altogether and move the example into docs if it's wanted

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I've adjusted instead to tell people how to request a track guardrail.

"""
)
format_option = True

def fill_parser(self, parser: argparse.ArgumentParser) -> None:
"""Add own parameters to the general parser."""
super().fill_parser(parser=parser)
parser.add_argument(
"name",
help="The store name onto which to create the track",
)
parser.add_argument(
"track",
nargs="+",
help="The track name to create",
)
parser.add_argument(
"--automatic-phasing-percentage",
type=int,
lengau marked this conversation as resolved.
Show resolved Hide resolved
default=None,
help="Automatic phasing percentage",
)

def run(self, parsed_args: argparse.Namespace) -> None:
"""Run the command."""
emit.progress(f"Creating {len(parsed_args.track)} tracks on the store")
pct = parsed_args.automatic_phasing_percentage
tracks = [
lengau marked this conversation as resolved.
Show resolved Hide resolved
{"name": track, "automatic-phasing-percentage": pct}
for track in parsed_args.track
]
output_tracks = self._services.store.create_tracks(
parsed_args.name,
*tracks, # type: ignore[arg-type] # false positive in mypy
)

if fmt := parsed_args.format:
emit.message(cli.format_content(tracks, fmt))
return
data = [
{
"Name": track["name"],
"Created at": utils.format_timestamp(
datetime.datetime.fromisoformat(track["created-at"])
),
"Automatic phasing percentage": track["automatic-phasing-percentage"],
}
for track in output_tracks
]
emit.message(tabulate(data, headers="keys"))
22 changes: 21 additions & 1 deletion charmcraft/services/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import craft_application
import craft_store
from craft_cli import emit
from craft_store import models
from craft_store import models, publishergateway
from overrides import override

from charmcraft import const, env, errors, store
Expand Down Expand Up @@ -193,6 +193,26 @@ def setup(self) -> None:
api_base_url=self._base_url,
storage_base_url=self._storage_url,
)
self._auth = craft_store.Auth(
application_name=self._app.name,
host=self._base_url,
environment_auth=self._environment_auth,
)
self._publisher = craft_store.publishergateway.PublisherGateway(
base_url=self._base_url,
namespace="charm",
auth=self._auth,
)

def create_tracks(
self, name: str, *tracks: publishergateway.CreateTrackRequest
) -> Sequence[publishergateway.TrackMetadata]:
"""Create tracks in the store."""
self._publisher.create_tracks(name, *tracks)

metadata = self._publisher.get_package_metadata(name)
# "or []" here because tracks could be None.
return metadata["tracks"] or []

def set_resource_revisions_architectures(
self, name: str, resource_name: str, updates: dict[int, list[str]]
Expand Down
4 changes: 2 additions & 2 deletions charmcraft/utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import sys
from collections.abc import Collection, Iterable
from dataclasses import dataclass
from typing import Literal, overload
from typing import Any, overload

import tabulate
from craft_cli import emit
Expand Down Expand Up @@ -182,7 +182,7 @@ class OutputFormat(enum.Enum):

@overload
def format_content(
content: dict[str, str], fmt: Literal[OutputFormat.TABLE, "table"]
content: dict[str, Any] | list[dict[str, Any]], fmt: OutputFormat | str | None
) -> str: ...


Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ dependencies = [
"craft-providers>=2.0.0",
"craft-platforms~=0.3",
"craft-providers>=2.0.0",
"craft-store>=3.0.0",
# "craft-store>=3.0.0",
"craft-store @ git+https://github.com/canonical/craft-store@work/charmcraft-1901",
lengau marked this conversation as resolved.
Show resolved Hide resolved
"distro>=1.7.0",
"docker>=7.0.0",
"humanize>=2.6.0",
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ craft-grammar==2.0.1
craft-parts==2.1.4
craft-platforms==0.4.0
craft-providers==2.0.4
craft-store==3.0.2
craft-store @ git+https://github.com/canonical/craft-store@work/charmcraft-1901
lengau marked this conversation as resolved.
Show resolved Hide resolved
cryptography==43.0.3
dill==0.3.9
distro==1.9.0
Expand Down
Loading
Loading