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: add edit-registries command #5050

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions snapcraft/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
craft_cli.CommandGroup(
"Store Registries",
[
commands.StoreEditRegistriesCommand,
commands.StoreListRegistriesCommand,
],
),
Expand Down
3 changes: 2 additions & 1 deletion snapcraft/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
StoreRegisterCommand,
)
from .plugins import ListPluginsCommand, PluginsCommand
from .registries import StoreListRegistriesCommand
from .registries import StoreEditRegistriesCommand, StoreListRegistriesCommand
from .remote import RemoteBuildCommand
from .status import (
StoreListRevisionsCommand,
Expand All @@ -77,6 +77,7 @@
"SnapCommand",
"StoreCloseCommand",
"StoreEditValidationSetsCommand",
"StoreEditRegistriesCommand",
"StoreExportLoginCommand",
"StoreLegacyCreateKeyCommand",
"StoreLegacyGatedCommand",
Expand Down
38 changes: 38 additions & 0 deletions snapcraft/commands/registries.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,41 @@ def run(self, parsed_args: "argparse.Namespace"):
name=parsed_args.name,
output_format=parsed_args.format,
)


class StoreEditRegistriesCommand(craft_application.commands.AppCommand):
"""Edit a registries set."""

name = "edit-registries"
help_msg = "Edit registries"
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe something that's not the same as the option? "Edit a registries set" could work, or even better if you could change the word "edit" to something different.

overview = textwrap.dedent(
"""
Edit a registries set.

If the registries set does not exist, then a new registries set will be created.

The account ID of the authenticated account can be determined with the
``snapcraft whoami`` command.

Use the ``list-registries`` command to view existing registries.
"""
)
_services: services.SnapcraftServiceFactory # type: ignore[reportIncompatibleVariableOverride]

@override
def fill_parser(self, parser: "argparse.ArgumentParser") -> None:
parser.add_argument(
"account_id",
metavar="account-id",
help="The account ID of the registries set to edit",
)
parser.add_argument(
"name", metavar="name", help="Name of the registries set to edit"
)

@override
def run(self, parsed_args: "argparse.Namespace"):
self._services.registries.edit_assertion(
name=parsed_args.name,
account_id=parsed_args.account_id,
)
11 changes: 10 additions & 1 deletion snapcraft/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Data models for snapcraft."""

from .assertions import Assertion, RegistryAssertion
from .assertions import (
Assertion,
EditableAssertion,
EditableRegistryAssertion,
Registry,
RegistryAssertion,
)
from .manifest import Manifest
from .project import (
MANDATORY_ADOPTABLE_FIELDS,
Expand Down Expand Up @@ -43,12 +49,15 @@
"Component",
"ComponentProject",
"ContentPlug",
"EditableAssertion",
"EditableRegistryAssertion",
"GrammarAwareProject",
"Hook",
"Lint",
"Manifest",
"Platform",
"Project",
"Registry",
"RegistryAssertion",
"SnapcraftBuildPlanner",
"Socket",
Expand Down
74 changes: 63 additions & 11 deletions snapcraft/models/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,81 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Models for assertion sets."""
"""Assertion models."""

from typing import Any, Literal
from typing import Literal

from craft_application import models
from typing_extensions import Self


class RegistryAssertion(models.CraftBaseModel):
"""Data model for a registry assertion."""
class Registry(models.CraftBaseModel):
"""Access and data definitions for a specific facet of a snap or system."""

request: str | None = None
"""Optional dot-separated path to access the field."""

storage: str
"""Dot-separated storage path."""

access: Literal["read", "write", "read-write"] | None = None
"""Access permissions for the field."""

content: list[Self] | None = None
"""Optional nested rules."""


class Rules(models.CraftBaseModel):
"""A list of registries for a particular view."""

rules: list[Registry]


class EditableRegistryAssertion(models.CraftBaseModel):
"""Subset of a registries assertion that can be edited by the user."""

account_id: str
authority_id: str
body: dict[str, Any] | str | None = None
body_length: str | None = None
"""Issuer of the registry assertion and owner of the signing key."""

name: str
revision: int = 0
sign_key_sha3_384: str | None = None
summary: str | None = None
timestamp: str
revision: int | None = 0

views: dict[str, Rules]
"""A map of logical views of how the storage is accessed."""

body: str | None = None
"""A JSON schema that defines the storage structure."""


class RegistryAssertion(EditableRegistryAssertion):
"""A full registries assertion containing editable and non-editable fields."""

type: Literal["registry"]
views: dict[str, Any]

authority_id: str
"""Issuer of the registry assertion and owner of the signing key."""

timestamp: str
"""Timestamp of when the assertion was issued."""

body_length: str | None = None
"""Length of the body field."""

sign_key_sha3_384: str | None = None
"""Signing key ID."""


class RegistriesList(models.CraftBaseModel):
"""A list of registry assertions."""

registry_list: list[RegistryAssertion] = []


# this will be a union for validation sets and registries once
# validation sets are migrated from the legacy codebase
Assertion = RegistryAssertion

# this will be a union for editable validation sets and editable registries once
# validation sets are migrated from the legacy codebase
EditableAssertion = EditableRegistryAssertion
8 changes: 4 additions & 4 deletions snapcraft/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

"""Snapcraft services."""

from snapcraft.services.assertions import AssertionService
from snapcraft.services.assertions import Assertion
from snapcraft.services.lifecycle import Lifecycle
from snapcraft.services.package import Package
from snapcraft.services.provider import Provider
from snapcraft.services.registries import RegistriesService
from snapcraft.services.registries import Registries
from snapcraft.services.remotebuild import RemoteBuild
from snapcraft.services.service_factory import SnapcraftServiceFactory

__all__ = [
"AssertionService",
"Assertion",
"Lifecycle",
"Package",
"Provider",
"RegistriesService",
"Registries",
"RemoteBuild",
"SnapcraftServiceFactory",
]
Loading
Loading