Skip to content

Commit

Permalink
Allow kind to set set with API
Browse files Browse the repository at this point in the history
The POST and PATCH endpoints for editions now allow the kind to be set.
  • Loading branch information
jonathansick committed May 22, 2024
1 parent e9cc584 commit 708ce6e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
15 changes: 15 additions & 0 deletions keeper/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,21 @@ def mode_name(self) -> str:
else:
return self.default_mode_name

def set_kind(self, kind: str) -> None:
"""Set the edition kind.
Parameters
----------
kind : `str`
Kind identifier. Validated to be one defined in `EditionKind`.
Raises
------
ValidationError
Raised if `kind` is unknown.
"""
self.kind = EditionKind[kind]

@property
def kind_name(self) -> str:
"""Name of the kind (`str`).
Expand Down
6 changes: 6 additions & 0 deletions keeper/services/createedition.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def create_edition(
autoincrement_slug: Optional[bool] = False,
tracked_ref: Optional[str] = "main",
build: Optional[Build] = None,
kind: Optional[str] = None,
) -> Edition:
"""Create a new edition.
Expand Down Expand Up @@ -50,6 +51,8 @@ def create_edition(
is ``"git_refs"`` or ``"git_ref"``.
build : Build, optional
The build to initially publish with this edition.
kind : str, optional
The kind of the edition.
Returns
-------
Expand Down Expand Up @@ -83,6 +86,9 @@ def create_edition(
edition.tracked_ref = tracked_ref
edition.tracked_refs = [tracked_ref]

if kind is not None:
edition.set_kind(kind)

db.session.add(edition)
db.session.commit()

Expand Down
4 changes: 4 additions & 0 deletions keeper/services/updateedition.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def update_edition(
tracking_mode: Optional[str] = None,
tracked_ref: Optional[str] = None,
pending_rebuild: Optional[bool] = None,
kind: Optional[str] = None,
) -> Edition:
"""Update the metadata of an existing edititon or to point at a new
build.
Expand Down Expand Up @@ -63,6 +64,9 @@ def update_edition(
)
edition.pending_rebuild = pending_rebuild

if kind is not None:
edition.set_kind(kind)

db.session.add(edition)
db.session.commit()

Expand Down
30 changes: 29 additions & 1 deletion keeper/v2api/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from keeper.editiontracking import EditionTrackingModes
from keeper.exceptions import ValidationError
from keeper.models import OrganizationLayoutMode
from keeper.models import EditionKind, OrganizationLayoutMode
from keeper.utils import (
format_utc_datetime,
validate_path_slug,
Expand Down Expand Up @@ -664,6 +664,9 @@ class EditionPostRequest(BaseModel):
mode: str = "git_refs"
"""Tracking mode."""

kind: Optional[str] = None
"""The edition kind."""

tracked_ref: Optional[str] = None
"""Git ref being tracked if mode is ``git_ref``."""

Expand Down Expand Up @@ -711,6 +714,17 @@ def check_tracked_refs(
raise ValueError('tracked_ref must be set if mode is "git_ref"')
return v

@validator("kind")
def check_kind(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return None

# Get all known kinds from the EditionKind enum
kind_names = [kind.name for kind in EditionKind]
if v not in kind_names:
raise ValueError(f"Kind {v!r} is not known.")
return v


class EditionPatchRequest(BaseModel):
"""The model for a PATCH /editions/:id request."""
Expand All @@ -734,6 +748,9 @@ class EditionPatchRequest(BaseModel):
mode: Optional[str] = None
"""The edition tracking mode."""

kind: Optional[str] = None
"""The edition kind."""

build_url: Optional[HttpUrl] = None
"""URL of the build to initially publish with the edition, if available.
"""
Expand All @@ -759,6 +776,17 @@ def check_mode(cls, v: Optional[str]) -> Optional[str]:
raise ValueError(f"Tracking mode {v!r} is not known.")
return v

@validator("kind")
def check_kind(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return None

# Get all known kinds from the EditionKind enum
kind_names = [kind.name for kind in EditionKind]
if v not in kind_names:
raise ValueError(f"Kind {v!r} is not known.")
return v


class QueuedResponse(BaseModel):
"""Response that contains only a URL for the background task's status."""
Expand Down
2 changes: 2 additions & 0 deletions keeper/v2api/editions.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def post_edition(org: str, project: str) -> Tuple[str, int, Dict[str, str]]:
slug=request_data.slug,
autoincrement_slug=request_data.autoincrement,
tracked_ref=request_data.tracked_ref,
kind=request_data.kind,
build=build,
)
except Exception:
Expand Down Expand Up @@ -130,6 +131,7 @@ def patch_edition(
slug=request_data.slug,
tracking_mode=request_data.mode,
tracked_ref=request_data.tracked_ref,
kind=request_data.kind,
pending_rebuild=request_data.pending_rebuild,
)
except Exception:
Expand Down

0 comments on commit 708ce6e

Please sign in to comment.