Implement task 006 site config versioning and audit events
This commit is contained in:
@@ -16,6 +16,11 @@ from src.domain.contracts import (
|
||||
)
|
||||
|
||||
|
||||
_VERSION_CREATED_EVENT_TYPE = "SCRIPT_CONFIG_VERSION_CREATED"
|
||||
_VERSION_ACTIVATED_EVENT_TYPE = "SCRIPT_CONFIG_VERSION_ACTIVATED"
|
||||
_VERSION_ROLLBACK_EVENT_TYPE = "SCRIPT_CONFIG_VERSION_ROLLBACK"
|
||||
|
||||
|
||||
def create_target_site(
|
||||
repository: object,
|
||||
request: TargetSiteConfigCreateRequest,
|
||||
@@ -40,6 +45,14 @@ def create_target_site(
|
||||
return TargetSiteConfigResponse(site=site)
|
||||
|
||||
|
||||
def get_target_site(
|
||||
repository: object,
|
||||
site_id: UUID,
|
||||
) -> TargetSiteConfigResponse:
|
||||
site = repository.target_sites.get_by_id(site_id)
|
||||
return TargetSiteConfigResponse(site=site)
|
||||
|
||||
|
||||
def update_target_site(
|
||||
repository: object,
|
||||
site_id: UUID,
|
||||
@@ -82,6 +95,16 @@ def create_script_config_version(
|
||||
request: ScriptConfigVersionCreateRequest,
|
||||
) -> ScriptConfigVersionResponse:
|
||||
repository.target_sites.get_by_id(site_id)
|
||||
|
||||
if request.rollback_target_version_id is not None:
|
||||
rollback_target = repository.script_config_versions.get_by_id(
|
||||
request.rollback_target_version_id
|
||||
)
|
||||
if str(rollback_target["target_site_id"]) != str(site_id):
|
||||
raise LookupError(
|
||||
f"Rollback target version is not for site: {request.rollback_target_version_id}"
|
||||
)
|
||||
|
||||
existing_versions = repository.script_config_versions.list_for_site(site_id)
|
||||
version = request.version or _next_version(existing_versions)
|
||||
now = _now()
|
||||
@@ -112,6 +135,24 @@ def create_script_config_version(
|
||||
target_site_id=site_id,
|
||||
version_id=version_id,
|
||||
activated_at=now,
|
||||
rollback_target_version_id=request.rollback_target_version_id,
|
||||
)
|
||||
repository.script_config_version_events.create(
|
||||
target_site_id=site_id,
|
||||
version_id=version_id,
|
||||
event_type=_VERSION_ACTIVATED_EVENT_TYPE,
|
||||
actor_user_id=current_user.id,
|
||||
payload={"action": "activate", "version": version},
|
||||
created_at=now,
|
||||
)
|
||||
else:
|
||||
repository.script_config_version_events.create(
|
||||
target_site_id=site_id,
|
||||
version_id=version_id,
|
||||
event_type=_VERSION_CREATED_EVENT_TYPE,
|
||||
actor_user_id=current_user.id,
|
||||
payload={"action": "create", "version": version},
|
||||
created_at=now,
|
||||
)
|
||||
|
||||
return ScriptConfigVersionResponse(version=_script_version_summary(row))
|
||||
@@ -122,6 +163,7 @@ def activate_script_config_version(
|
||||
*,
|
||||
site_id: UUID,
|
||||
version_id: UUID,
|
||||
current_user: CurrentUser,
|
||||
) -> ScriptConfigVersionResponse:
|
||||
repository.target_sites.get_by_id(site_id)
|
||||
row = repository.script_config_versions.activate(
|
||||
@@ -129,6 +171,54 @@ def activate_script_config_version(
|
||||
version_id=version_id,
|
||||
activated_at=_now(),
|
||||
)
|
||||
repository.script_config_version_events.create(
|
||||
target_site_id=site_id,
|
||||
version_id=version_id,
|
||||
event_type=_VERSION_ACTIVATED_EVENT_TYPE,
|
||||
actor_user_id=current_user.id,
|
||||
payload={"action": "activate", "version_id": str(version_id)},
|
||||
created_at=_now(),
|
||||
)
|
||||
return ScriptConfigVersionResponse(version=_script_version_summary(row))
|
||||
|
||||
|
||||
def rollback_script_config_version(
|
||||
repository: object,
|
||||
*,
|
||||
site_id: UUID,
|
||||
version_id: UUID,
|
||||
current_user: CurrentUser,
|
||||
) -> ScriptConfigVersionResponse:
|
||||
target_site = repository.target_sites.get_by_id(site_id)
|
||||
version_row = repository.script_config_versions.get_by_id(version_id)
|
||||
if str(version_row["target_site_id"]) != str(site_id):
|
||||
raise LookupError(
|
||||
f"Script config version not found for site: {site_id} {version_id}"
|
||||
)
|
||||
|
||||
rollback_target_id = target_site.active_script_config_version_id
|
||||
if rollback_target_id == version_id:
|
||||
rollback_target_id = None
|
||||
|
||||
row = repository.script_config_versions.activate(
|
||||
target_site_id=site_id,
|
||||
version_id=version_id,
|
||||
activated_at=_now(),
|
||||
rollback_target_version_id=rollback_target_id,
|
||||
)
|
||||
repository.script_config_version_events.create(
|
||||
target_site_id=site_id,
|
||||
version_id=version_id,
|
||||
event_type=_VERSION_ROLLBACK_EVENT_TYPE,
|
||||
actor_user_id=current_user.id,
|
||||
payload={
|
||||
"action": "rollback",
|
||||
"rollback_target_version_id": (
|
||||
str(rollback_target_id) if rollback_target_id is not None else None
|
||||
),
|
||||
},
|
||||
created_at=_now(),
|
||||
)
|
||||
return ScriptConfigVersionResponse(version=_script_version_summary(row))
|
||||
|
||||
|
||||
@@ -147,10 +237,13 @@ def _script_version_summary(row: dict[str, object]) -> ScriptConfigVersionSummar
|
||||
target_site_id=row["target_site_id"],
|
||||
version=row["version"],
|
||||
status=row["status"],
|
||||
diff=row["diff"],
|
||||
rollback_target_version_id=row["rollback_target_version_id"],
|
||||
publishing_yaml_hash=row["publishing_yaml_hash"],
|
||||
transform_script_hash=row["transform_script_hash"],
|
||||
created_by=row["created_by"],
|
||||
created_at=row["created_at"],
|
||||
activated_at=row["activated_at"],
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -113,10 +113,13 @@ class ScriptConfigVersionSummary(ContractModel):
|
||||
target_site_id: UUID
|
||||
version: int = Field(ge=1)
|
||||
status: ScriptConfigVersionStatus
|
||||
diff: JsonObject = Field(default_factory=dict)
|
||||
publishing_yaml_hash: str = Field(min_length=1)
|
||||
transform_script_hash: str = Field(min_length=1)
|
||||
rollback_target_version_id: UUID | None = None
|
||||
created_by: UUID
|
||||
created_at: datetime
|
||||
activated_at: datetime | None = None
|
||||
|
||||
|
||||
class ScriptConfigVersionCreateRequest(ContractModel):
|
||||
|
||||
@@ -37,6 +37,7 @@ class BackendRepository:
|
||||
self.target_sites = TargetSitesRepository(self)
|
||||
self.articles = ArticlesRepository(self)
|
||||
self.script_config_versions = ScriptConfigVersionsRepository(self)
|
||||
self.script_config_version_events = ScriptConfigVersionAuditEventsRepository(self)
|
||||
self.schema = SchemaRepository(self)
|
||||
|
||||
def setup(self) -> None:
|
||||
@@ -719,6 +720,7 @@ class ScriptConfigVersionsRepository:
|
||||
target_site_id: UUID,
|
||||
version_id: UUID,
|
||||
activated_at: datetime,
|
||||
rollback_target_version_id: UUID | None = None,
|
||||
) -> dict[str, Any]:
|
||||
placeholder = self._repository.placeholder()
|
||||
with self._repository.connection() as connection:
|
||||
@@ -756,13 +758,15 @@ class ScriptConfigVersionsRepository:
|
||||
SET
|
||||
status = {placeholder},
|
||||
activated_at = {placeholder},
|
||||
updated_at = {placeholder}
|
||||
updated_at = {placeholder},
|
||||
rollback_target_version_id = COALESCE({placeholder}, rollback_target_version_id)
|
||||
WHERE id = {placeholder}
|
||||
""",
|
||||
(
|
||||
ScriptConfigVersionStatus.ACTIVE.value,
|
||||
_datetime_value(activated_at),
|
||||
_datetime_value(activated_at),
|
||||
_uuid_value(rollback_target_version_id),
|
||||
str(version_id),
|
||||
),
|
||||
)
|
||||
@@ -795,6 +799,97 @@ class ScriptConfigVersionsRepository:
|
||||
return [_plain_row(row) for row in rows]
|
||||
|
||||
|
||||
class ScriptConfigVersionAuditEventsRepository:
|
||||
def __init__(self, repository: BackendRepository) -> None:
|
||||
self._repository = repository
|
||||
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
target_site_id: UUID,
|
||||
version_id: UUID,
|
||||
event_type: str,
|
||||
actor_user_id: UUID | None,
|
||||
payload: JsonObject,
|
||||
created_at: datetime,
|
||||
) -> dict[str, Any]:
|
||||
placeholder = self._repository.placeholder()
|
||||
sql = f"""
|
||||
INSERT INTO script_config_version_events (
|
||||
id,
|
||||
target_site_id,
|
||||
version_id,
|
||||
event_type,
|
||||
actor_user_id,
|
||||
payload,
|
||||
created_at
|
||||
)
|
||||
VALUES (
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder}
|
||||
)
|
||||
"""
|
||||
params = (
|
||||
str(uuid4()),
|
||||
str(target_site_id),
|
||||
str(version_id),
|
||||
event_type,
|
||||
_uuid_value(actor_user_id),
|
||||
_json_value(payload),
|
||||
_datetime_value(created_at),
|
||||
)
|
||||
with self._repository.connection() as connection:
|
||||
connection.execute(sql, params)
|
||||
|
||||
return self.get(event_type=event_type, target_site_id=target_site_id, version_id=version_id)
|
||||
|
||||
def list_for_site(self, target_site_id: UUID) -> list[dict[str, Any]]:
|
||||
placeholder = self._repository.placeholder()
|
||||
with self._repository.connection() as connection:
|
||||
rows = connection.execute(
|
||||
f"""
|
||||
SELECT id, target_site_id, version_id, event_type, actor_user_id, payload, created_at
|
||||
FROM script_config_version_events
|
||||
WHERE target_site_id = {placeholder}
|
||||
ORDER BY created_at
|
||||
""",
|
||||
(str(target_site_id),),
|
||||
).fetchall()
|
||||
|
||||
return [_script_config_version_event_from_row(row) for row in rows]
|
||||
|
||||
def get(
|
||||
self,
|
||||
*,
|
||||
target_site_id: UUID,
|
||||
version_id: UUID,
|
||||
event_type: str,
|
||||
) -> dict[str, Any]:
|
||||
placeholder = self._repository.placeholder()
|
||||
with self._repository.connection() as connection:
|
||||
row = connection.execute(
|
||||
f"""
|
||||
SELECT id, target_site_id, version_id, event_type, actor_user_id, payload, created_at
|
||||
FROM script_config_version_events
|
||||
WHERE target_site_id = {placeholder}
|
||||
AND version_id = {placeholder}
|
||||
AND event_type = {placeholder}
|
||||
""",
|
||||
(str(target_site_id), str(version_id), event_type),
|
||||
).fetchone()
|
||||
|
||||
if row is None:
|
||||
raise LookupError(
|
||||
f"Script config version event not found: {target_site_id} {version_id} {event_type}"
|
||||
)
|
||||
return _script_config_version_event_from_row(row)
|
||||
|
||||
|
||||
class SchemaRepository:
|
||||
def __init__(self, repository: BackendRepository) -> None:
|
||||
self._repository = repository
|
||||
@@ -920,6 +1015,18 @@ def _workflow_event_from_row(row: Any) -> WorkflowEventSummary:
|
||||
)
|
||||
|
||||
|
||||
def _script_config_version_event_from_row(row: Any) -> dict[str, Any]:
|
||||
return {
|
||||
"id": _row_value(row, "id"),
|
||||
"target_site_id": _row_value(row, "target_site_id"),
|
||||
"version_id": _row_value(row, "version_id"),
|
||||
"event_type": _row_value(row, "event_type"),
|
||||
"actor_user_id": _row_value(row, "actor_user_id"),
|
||||
"payload": _json_from_row(row, "payload"),
|
||||
"created_at": _row_value(row, "created_at"),
|
||||
}
|
||||
|
||||
|
||||
def _plain_row(row: Any) -> dict[str, Any]:
|
||||
if isinstance(row, sqlite3.Row):
|
||||
result = dict(row)
|
||||
|
||||
@@ -285,6 +285,17 @@ POSTGRES_SCHEMA_STATEMENTS: tuple[str, ...] = (
|
||||
UNIQUE (prompt_key, version)
|
||||
)
|
||||
""",
|
||||
f"""
|
||||
CREATE TABLE IF NOT EXISTS script_config_version_events (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
target_site_id UUID NOT NULL REFERENCES target_sites(id) ON DELETE CASCADE,
|
||||
version_id UUID NOT NULL REFERENCES script_config_versions(id) ON DELETE CASCADE,
|
||||
event_type TEXT NOT NULL,
|
||||
actor_user_id UUID REFERENCES users(id),
|
||||
payload JSONB NOT NULL DEFAULT '{{}}'::jsonb,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
)
|
||||
""",
|
||||
"CREATE INDEX IF NOT EXISTS idx_target_sites_slug ON target_sites (slug)",
|
||||
"""
|
||||
CREATE INDEX IF NOT EXISTS idx_articles_dashboard
|
||||
@@ -314,6 +325,10 @@ POSTGRES_SCHEMA_STATEMENTS: tuple[str, ...] = (
|
||||
CREATE INDEX IF NOT EXISTS idx_publish_commits_article
|
||||
ON publish_commits (article_id, created_at)
|
||||
""",
|
||||
"""
|
||||
CREATE INDEX IF NOT EXISTS idx_script_config_version_events_site
|
||||
ON script_config_version_events (target_site_id, created_at)
|
||||
""",
|
||||
)
|
||||
|
||||
|
||||
@@ -550,6 +565,17 @@ SQLITE_SCHEMA_STATEMENTS: tuple[str, ...] = (
|
||||
UNIQUE (prompt_key, version)
|
||||
)
|
||||
""",
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS script_config_version_events (
|
||||
id TEXT PRIMARY KEY,
|
||||
target_site_id TEXT NOT NULL,
|
||||
version_id TEXT NOT NULL,
|
||||
event_type TEXT NOT NULL,
|
||||
actor_user_id TEXT,
|
||||
payload TEXT NOT NULL DEFAULT '{}',
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
""",
|
||||
"CREATE INDEX IF NOT EXISTS idx_target_sites_slug ON target_sites (slug)",
|
||||
"""
|
||||
CREATE INDEX IF NOT EXISTS idx_articles_dashboard
|
||||
@@ -579,6 +605,10 @@ SQLITE_SCHEMA_STATEMENTS: tuple[str, ...] = (
|
||||
CREATE INDEX IF NOT EXISTS idx_publish_commits_article
|
||||
ON publish_commits (article_id, created_at)
|
||||
""",
|
||||
"""
|
||||
CREATE INDEX IF NOT EXISTS idx_script_config_version_events_site
|
||||
ON script_config_version_events (target_site_id, created_at)
|
||||
""",
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,9 @@ from src.application.site_config import (
|
||||
activate_script_config_version,
|
||||
create_script_config_version,
|
||||
create_target_site,
|
||||
get_target_site,
|
||||
list_script_config_versions,
|
||||
rollback_script_config_version,
|
||||
update_target_site,
|
||||
)
|
||||
from src.domain.auth import ADMIN_ROLES, EDITOR_OR_ADMIN_ROLES
|
||||
@@ -36,6 +38,18 @@ def list_sites(
|
||||
return [TargetSiteConfigResponse(site=site) for site in repository.target_sites.list()]
|
||||
|
||||
|
||||
@router.get("/sites/{site_id}", response_model=TargetSiteConfigResponse)
|
||||
def get_site(
|
||||
site_id: UUID,
|
||||
_: CurrentUser = Depends(require_roles(EDITOR_OR_ADMIN_ROLES)),
|
||||
repository: BackendRepository = Depends(get_repository),
|
||||
) -> TargetSiteConfigResponse:
|
||||
try:
|
||||
return get_target_site(repository, site_id)
|
||||
except LookupError as error:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Not Found") from error
|
||||
|
||||
|
||||
@router.post(
|
||||
"/sites",
|
||||
response_model=TargetSiteConfigResponse,
|
||||
@@ -107,7 +121,7 @@ def post_script_config_version(
|
||||
def post_activate_script_config_version(
|
||||
site_id: UUID,
|
||||
version_id: UUID,
|
||||
_: CurrentUser = Depends(require_roles(ADMIN_ROLES)),
|
||||
current_user: CurrentUser = Depends(require_roles(ADMIN_ROLES)),
|
||||
repository: BackendRepository = Depends(get_repository),
|
||||
) -> ScriptConfigVersionResponse:
|
||||
try:
|
||||
@@ -115,6 +129,28 @@ def post_activate_script_config_version(
|
||||
repository,
|
||||
site_id=site_id,
|
||||
version_id=version_id,
|
||||
current_user=current_user,
|
||||
)
|
||||
except LookupError as error:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Not Found") from error
|
||||
|
||||
|
||||
@router.post(
|
||||
"/sites/{site_id}/publishing-config/versions/{version_id}/rollback",
|
||||
response_model=ScriptConfigVersionResponse,
|
||||
)
|
||||
def post_rollback_script_config_version(
|
||||
site_id: UUID,
|
||||
version_id: UUID,
|
||||
current_user: CurrentUser = Depends(require_roles(ADMIN_ROLES)),
|
||||
repository: BackendRepository = Depends(get_repository),
|
||||
) -> ScriptConfigVersionResponse:
|
||||
try:
|
||||
return rollback_script_config_version(
|
||||
repository,
|
||||
site_id=site_id,
|
||||
version_id=version_id,
|
||||
current_user=current_user,
|
||||
)
|
||||
except LookupError as error:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Not Found") from error
|
||||
|
||||
@@ -121,6 +121,35 @@ class AuthAuthorizationPublicApiTest(unittest.TestCase):
|
||||
version_activate_response.text,
|
||||
)
|
||||
|
||||
def test_editor_cannot_rollback_script_config_version(self) -> None:
|
||||
site_response = self.client.post(
|
||||
"/api/sites",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_ADMIN_EMAIL},
|
||||
json=self._site_payload("rollback_denied_blog"),
|
||||
)
|
||||
self.assertEqual(201, site_response.status_code, site_response.text)
|
||||
site_id = site_response.json()["site"]["id"]
|
||||
|
||||
first_version = self.client.post(
|
||||
f"/api/sites/{site_id}/publishing-config/versions",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_ADMIN_EMAIL},
|
||||
json=self._script_version_payload(),
|
||||
)
|
||||
self.assertEqual(201, first_version.status_code, first_version.text)
|
||||
version_id = first_version.json()["version"]["id"]
|
||||
|
||||
activate_response = self.client.post(
|
||||
f"/api/sites/{site_id}/publishing-config/versions/{version_id}/activate",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
|
||||
)
|
||||
rollback_response = self.client.post(
|
||||
f"/api/sites/{site_id}/publishing-config/versions/{version_id}/rollback",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
|
||||
)
|
||||
|
||||
self.assertEqual(403, activate_response.status_code, activate_response.text)
|
||||
self.assertEqual(403, rollback_response.status_code, rollback_response.text)
|
||||
|
||||
def test_admin_can_create_and_activate_script_config_version(self) -> None:
|
||||
site_response = self.client.post(
|
||||
"/api/sites",
|
||||
@@ -147,6 +176,121 @@ class AuthAuthorizationPublicApiTest(unittest.TestCase):
|
||||
self.assertEqual(200, activate_response.status_code, activate_response.text)
|
||||
self.assertEqual("ACTIVE", activate_response.json()["version"]["status"])
|
||||
|
||||
def test_admin_can_get_site_by_id(self) -> None:
|
||||
site_response = self.client.post(
|
||||
"/api/sites",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_ADMIN_EMAIL},
|
||||
json=self._site_payload("admin_get_site_by_id_blog"),
|
||||
)
|
||||
self.assertEqual(201, site_response.status_code, site_response.text)
|
||||
site_id = site_response.json()["site"]["id"]
|
||||
|
||||
get_response = self.client.get(
|
||||
f"/api/sites/{site_id}",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
|
||||
)
|
||||
|
||||
self.assertEqual(200, get_response.status_code, get_response.text)
|
||||
self.assertEqual(site_id, get_response.json()["site"]["id"])
|
||||
|
||||
def test_admin_can_create_version_with_rollback_target(self) -> None:
|
||||
site_response = self.client.post(
|
||||
"/api/sites",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_ADMIN_EMAIL},
|
||||
json=self._site_payload("rollback_target_blog"),
|
||||
)
|
||||
self.assertEqual(201, site_response.status_code, site_response.text)
|
||||
site_id = site_response.json()["site"]["id"]
|
||||
|
||||
baseline = self.client.post(
|
||||
f"/api/sites/{site_id}/publishing-config/versions",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_ADMIN_EMAIL},
|
||||
json=self._script_version_payload(diff={"summary": "baseline"}),
|
||||
)
|
||||
self.assertEqual(201, baseline.status_code, baseline.text)
|
||||
baseline_id = baseline.json()["version"]["id"]
|
||||
|
||||
create_response = self.client.post(
|
||||
f"/api/sites/{site_id}/publishing-config/versions",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_ADMIN_EMAIL},
|
||||
json=self._script_version_payload(
|
||||
diff={"summary": "rollback-aware"},
|
||||
rollback_target_version_id=baseline_id,
|
||||
activate=True,
|
||||
),
|
||||
)
|
||||
self.assertEqual(201, create_response.status_code, create_response.text)
|
||||
created = create_response.json()["version"]
|
||||
|
||||
self.assertEqual("ACTIVE", created["status"])
|
||||
self.assertEqual("rollback-aware", created["diff"].get("summary"))
|
||||
self.assertEqual(baseline_id, created["rollback_target_version_id"])
|
||||
|
||||
versions_response = self.client.get(
|
||||
f"/api/sites/{site_id}/publishing-config/versions",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
|
||||
)
|
||||
self.assertEqual(200, versions_response.status_code, versions_response.text)
|
||||
self.assertEqual(2, len(versions_response.json()["versions"]))
|
||||
|
||||
def test_admin_can_rollback_script_config_version(self) -> None:
|
||||
site_response = self.client.post(
|
||||
"/api/sites",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_ADMIN_EMAIL},
|
||||
json=self._site_payload("rollback_action_blog"),
|
||||
)
|
||||
self.assertEqual(201, site_response.status_code, site_response.text)
|
||||
site_id = site_response.json()["site"]["id"]
|
||||
|
||||
active = self.client.post(
|
||||
f"/api/sites/{site_id}/publishing-config/versions",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_ADMIN_EMAIL},
|
||||
json=self._script_version_payload(
|
||||
diff={"summary": "initial active"},
|
||||
activate=True,
|
||||
),
|
||||
)
|
||||
self.assertEqual(201, active.status_code, active.text)
|
||||
active_id = active.json()["version"]["id"]
|
||||
|
||||
next_version = self.client.post(
|
||||
f"/api/sites/{site_id}/publishing-config/versions",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_ADMIN_EMAIL},
|
||||
json=self._script_version_payload(diff={"summary": "next version"}),
|
||||
)
|
||||
self.assertEqual(201, next_version.status_code, next_version.text)
|
||||
next_id = next_version.json()["version"]["id"]
|
||||
|
||||
self.client.post(
|
||||
f"/api/sites/{site_id}/publishing-config/versions/{next_id}/activate",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_ADMIN_EMAIL},
|
||||
)
|
||||
|
||||
rollback_response = self.client.post(
|
||||
f"/api/sites/{site_id}/publishing-config/versions/{active_id}/rollback",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_ADMIN_EMAIL},
|
||||
)
|
||||
self.assertEqual(200, rollback_response.status_code, rollback_response.text)
|
||||
self.assertEqual(active_id, rollback_response.json()["version"]["id"])
|
||||
self.assertEqual(
|
||||
"ACTIVE",
|
||||
rollback_response.json()["version"]["status"],
|
||||
)
|
||||
self.assertEqual(
|
||||
next_id,
|
||||
rollback_response.json()["version"]["rollback_target_version_id"],
|
||||
)
|
||||
|
||||
get_response = self.client.get(
|
||||
f"/api/sites/{site_id}",
|
||||
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
|
||||
)
|
||||
self.assertEqual(200, get_response.status_code, get_response.text)
|
||||
self.assertEqual(
|
||||
active_id,
|
||||
get_response.json()["site"]["active_script_config_version_id"],
|
||||
)
|
||||
|
||||
def test_editor_can_create_articles_and_approve_plan_review(self) -> None:
|
||||
sites_response = self.client.get(
|
||||
"/api/sites",
|
||||
@@ -215,12 +359,22 @@ class AuthAuthorizationPublicApiTest(unittest.TestCase):
|
||||
},
|
||||
}
|
||||
|
||||
def _script_version_payload(self) -> dict[str, object]:
|
||||
return {
|
||||
def _script_version_payload(
|
||||
self,
|
||||
diff: dict[str, object] | None = None,
|
||||
rollback_target_version_id: str | None = None,
|
||||
activate: bool = False,
|
||||
) -> dict[str, object]:
|
||||
payload = {
|
||||
"publishing_yaml": "target: editorial_ops\nrepository:\n branch: main\n",
|
||||
"transform_script": "export function transformArticle(article) { return article; }\n",
|
||||
"diff": {"summary": "Task 004 authorization fixture."},
|
||||
"diff": diff or {"summary": "Task 004 authorization fixture."},
|
||||
}
|
||||
if rollback_target_version_id is not None:
|
||||
payload["rollback_target_version_id"] = rollback_target_version_id
|
||||
if activate:
|
||||
payload["activate"] = True
|
||||
return payload
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import sys
|
||||
from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
from uuid import UUID
|
||||
|
||||
BACKEND_ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(BACKEND_ROOT))
|
||||
|
||||
|
||||
@contextmanager
|
||||
def repository_dsn() -> Iterator[str]:
|
||||
configured = os.environ.get("PIPELINE_TEST_DATABASE_DSN")
|
||||
if configured:
|
||||
yield configured
|
||||
return
|
||||
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
yield f"sqlite:///{Path(directory) / 'pipeline-audit.db'}"
|
||||
|
||||
|
||||
class ScriptConfigAuditTest(unittest.TestCase):
|
||||
def test_rollback_writes_audit_event(self) -> None:
|
||||
with repository_dsn() as dsn:
|
||||
from src.application.auth import get_current_user_by_email
|
||||
from src.application.seed_data import seed_reference_data
|
||||
from src.application.site_config import (
|
||||
create_script_config_version,
|
||||
create_target_site,
|
||||
rollback_script_config_version,
|
||||
)
|
||||
from src.domain.contracts import (
|
||||
PublishingRules,
|
||||
ScriptConfigVersionCreateRequest,
|
||||
TargetSiteConfigCreateRequest,
|
||||
)
|
||||
from src.infrastructure.repositories import open_backend_repository
|
||||
|
||||
repository = open_backend_repository(dsn)
|
||||
repository.setup()
|
||||
seed_reference_data(repository)
|
||||
admin = get_current_user_by_email(repository, "admin@example.com")
|
||||
site = create_target_site(
|
||||
repository,
|
||||
TargetSiteConfigCreateRequest(
|
||||
name="Audit Test Site",
|
||||
slug="audit-site",
|
||||
publishing_type="git_next",
|
||||
default_language="en",
|
||||
brand_voice="Audit-focused publishing profile.",
|
||||
audience="QA operations team.",
|
||||
seo_rules={"primary_keyword_required": True},
|
||||
visual_rules={"hero_style": "technical"},
|
||||
source_rules={"minimum_sources": 1},
|
||||
publishing_rules=PublishingRules(
|
||||
repository_url="git@github.com:example/audit-site.git",
|
||||
production_branch="main",
|
||||
content_format="mdx",
|
||||
content_path_template="content/articles/{slug}.mdx",
|
||||
asset_path_template="public/articles/{slug}/{filename}",
|
||||
frontmatter_mapping={"title": "title"},
|
||||
dry_run_renderer="next-mdx",
|
||||
),
|
||||
active_script_config_version_id=None,
|
||||
),
|
||||
).site
|
||||
|
||||
initial = create_script_config_version(
|
||||
repository,
|
||||
site_id=site.id,
|
||||
current_user=admin,
|
||||
request=self._script_version_payload(summary="initial", activate=True),
|
||||
).version
|
||||
candidate = create_script_config_version(
|
||||
repository,
|
||||
site_id=site.id,
|
||||
current_user=admin,
|
||||
request=self._script_version_payload(
|
||||
summary="candidate",
|
||||
activate=True,
|
||||
rollback_target_version_id=initial.id,
|
||||
),
|
||||
).version
|
||||
|
||||
rolled_back = rollback_script_config_version(
|
||||
repository,
|
||||
site_id=site.id,
|
||||
version_id=initial.id,
|
||||
current_user=admin,
|
||||
)
|
||||
self.assertEqual(str(initial.id), str(rolled_back.version.id))
|
||||
|
||||
rollback_events = [
|
||||
event
|
||||
for event in repository.script_config_version_events.list_for_site(site.id)
|
||||
if event["event_type"] == "SCRIPT_CONFIG_VERSION_ROLLBACK"
|
||||
]
|
||||
self.assertEqual(1, len(rollback_events))
|
||||
rollback_event = rollback_events[0]
|
||||
self.assertEqual(str(site.id), str(rollback_event["target_site_id"]))
|
||||
self.assertEqual(str(initial.id), str(rollback_event["version_id"]))
|
||||
self.assertEqual(str(admin.id), str(rollback_event["actor_user_id"]))
|
||||
self.assertEqual(
|
||||
str(candidate.id),
|
||||
rollback_event["payload"]["rollback_target_version_id"],
|
||||
)
|
||||
|
||||
active_site = repository.target_sites.get_by_id(site.id)
|
||||
self.assertEqual(initial.id, active_site.active_script_config_version_id)
|
||||
|
||||
def _script_version_payload(
|
||||
self,
|
||||
summary: str,
|
||||
activate: bool = False,
|
||||
rollback_target_version_id: UUID | None = None,
|
||||
) -> ScriptConfigVersionCreateRequest:
|
||||
payload = ScriptConfigVersionCreateRequest(
|
||||
publishing_yaml="target:\n repo: audit\n",
|
||||
transform_script="export function transformArticle(article) { return article; }",
|
||||
diff={"summary": summary},
|
||||
activate=activate,
|
||||
)
|
||||
if rollback_target_version_id is not None:
|
||||
payload.rollback_target_version_id = rollback_target_version_id
|
||||
return payload
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -6,7 +6,7 @@
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"test:roles": "node tests/role-navigation.test.mjs",
|
||||
"test:ui": "node tests/role-navigation.test.mjs && node tests/article_dashboard.test.mjs && node tests/article_form_validation.test.mjs",
|
||||
"test:ui": "node tests/role-navigation.test.mjs && node tests/article_dashboard.test.mjs && node tests/article_form_validation.test.mjs && node tests/admin_script_versions.model.test.mjs",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
import AdminScriptsPage from "@/pages/admin-scripts";
|
||||
|
||||
export { AdminScriptsPage as default };
|
||||
@@ -0,0 +1,3 @@
|
||||
import AdminSitesPage from "@/pages/admin-sites";
|
||||
|
||||
export default AdminSitesPage;
|
||||
@@ -149,18 +149,68 @@ main {
|
||||
min-height: 120px;
|
||||
}
|
||||
|
||||
.codeArea {
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
||||
min-height: 120px;
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
.fieldError {
|
||||
margin: 0;
|
||||
color: #b42318;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.formSuccess {
|
||||
margin: 0;
|
||||
color: #027a48;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.formError {
|
||||
margin: 4px 0 0;
|
||||
color: #b42318;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.inlineLinks {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.inlineLinks a {
|
||||
text-decoration: none;
|
||||
border: 1px solid #d0d5dd;
|
||||
border-radius: 6px;
|
||||
padding: 8px 12px;
|
||||
background: #ffffff;
|
||||
display: inline-flex;
|
||||
}
|
||||
|
||||
.inlineLinks .activeLink {
|
||||
border-color: #1f4b99;
|
||||
color: #1f4b99;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.table button {
|
||||
border-radius: 6px;
|
||||
border: 1px solid #1d2733;
|
||||
background: #ffffff;
|
||||
color: #1d2733;
|
||||
min-height: 34px;
|
||||
padding: 0 10px;
|
||||
margin-right: 6px;
|
||||
}
|
||||
|
||||
.tableAction {
|
||||
background: #eceff5 !important;
|
||||
}
|
||||
|
||||
.formActions {
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import type {
|
||||
ScriptConfigVersionCreateRequest,
|
||||
ScriptConfigVersionSummary,
|
||||
} from "@pipeline/shared";
|
||||
|
||||
export type AdminScriptVersionRow = {
|
||||
id: string;
|
||||
version: number;
|
||||
status: string;
|
||||
createdBy: string;
|
||||
createdAt: string;
|
||||
activatedAt: string | null;
|
||||
publishingYamlHash: string;
|
||||
transformScriptHash: string;
|
||||
isActive: boolean;
|
||||
rollbackTargetVersionId: string | null;
|
||||
diff: string;
|
||||
};
|
||||
|
||||
export type NewAdminScriptVersionDraft = {
|
||||
publishingYaml: string;
|
||||
transformScript: string;
|
||||
diff: string;
|
||||
activate: boolean;
|
||||
rollbackTargetVersionId: string;
|
||||
};
|
||||
|
||||
export type DraftValidationErrors = Partial<Record<keyof NewAdminScriptVersionDraft, string>>;
|
||||
|
||||
const DEFAULT_DIFF = '{\n "summary": "manual publish pipeline change"\n}';
|
||||
const DEFAULT_PUBLISHING_YAML = "target: site\nrepository:\n branch: main\n";
|
||||
const DEFAULT_TRANSFORM_SCRIPT = "export function transformArticle(article) {\n return article;\n}\n";
|
||||
|
||||
export function buildAdminScriptVersionRows(
|
||||
versions: readonly ScriptConfigVersionSummary[],
|
||||
): AdminScriptVersionRow[] {
|
||||
return versions.map((version) => ({
|
||||
id: version.id,
|
||||
version: version.version,
|
||||
status: version.status,
|
||||
createdBy: version.created_by,
|
||||
createdAt: version.created_at,
|
||||
activatedAt: version.activated_at ?? null,
|
||||
publishingYamlHash: version.publishing_yaml_hash,
|
||||
transformScriptHash: version.transform_script_hash,
|
||||
isActive: version.status === "ACTIVE",
|
||||
rollbackTargetVersionId: version.rollback_target_version_id ?? null,
|
||||
diff: JSON.stringify(version.diff),
|
||||
}));
|
||||
}
|
||||
|
||||
export function defaultScriptVersionDraft(): NewAdminScriptVersionDraft {
|
||||
return {
|
||||
publishingYaml: DEFAULT_PUBLISHING_YAML,
|
||||
transformScript: DEFAULT_TRANSFORM_SCRIPT,
|
||||
diff: DEFAULT_DIFF,
|
||||
activate: false,
|
||||
rollbackTargetVersionId: "",
|
||||
};
|
||||
}
|
||||
|
||||
export function buildCreateScriptVersionPayload(
|
||||
draft: NewAdminScriptVersionDraft,
|
||||
): ScriptConfigVersionCreateRequest {
|
||||
const parsedDiff = parseJsonObject(draft.diff) ?? {};
|
||||
|
||||
return {
|
||||
publishing_yaml: draft.publishingYaml,
|
||||
transform_script: draft.transformScript,
|
||||
diff: parsedDiff,
|
||||
activate: draft.activate,
|
||||
rollback_target_version_id: draft.rollbackTargetVersionId
|
||||
? (draft.rollbackTargetVersionId as string)
|
||||
: null,
|
||||
version: null,
|
||||
};
|
||||
}
|
||||
|
||||
export function parseJsonObject(value: string): Record<string, unknown> | null {
|
||||
if (!value.trim()) {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
if (
|
||||
parsed &&
|
||||
typeof parsed === "object" &&
|
||||
!Array.isArray(parsed) &&
|
||||
parsed !== null
|
||||
) {
|
||||
return parsed as Record<string, unknown>;
|
||||
}
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo, useState, type FormEvent } from "react";
|
||||
|
||||
import type { ScriptConfigVersionSummary, TargetSiteConfig } from "@pipeline/shared";
|
||||
|
||||
import { ApiError, DEMO_ADMIN_EMAIL, activateScriptConfigVersion, createScriptConfigVersion, fetchScriptConfigVersions, rollbackScriptConfigVersion } from "@/shared/pipeline-api";
|
||||
import {
|
||||
buildAdminScriptVersionRows,
|
||||
buildCreateScriptVersionPayload,
|
||||
defaultScriptVersionDraft,
|
||||
parseJsonObject,
|
||||
type DraftValidationErrors,
|
||||
type NewAdminScriptVersionDraft,
|
||||
} from "./model";
|
||||
|
||||
type AdminScriptVersionsPanelProps = {
|
||||
site: TargetSiteConfig;
|
||||
initialVersions: readonly ScriptConfigVersionSummary[];
|
||||
};
|
||||
|
||||
function parseOptionalError(value: string): string | null {
|
||||
if (value.trim().length === 0) {
|
||||
return "Required value";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function AdminScriptVersionsPanel({
|
||||
site,
|
||||
initialVersions,
|
||||
}: AdminScriptVersionsPanelProps) {
|
||||
const [versions, setVersions] = useState<readonly ScriptConfigVersionSummary[]>(
|
||||
initialVersions,
|
||||
);
|
||||
const [draft, setDraft] = useState<NewAdminScriptVersionDraft>(
|
||||
defaultScriptVersionDraft(),
|
||||
);
|
||||
const [errors, setErrors] = useState<DraftValidationErrors>({});
|
||||
const [formError, setFormError] = useState("");
|
||||
const [actionMessage, setActionMessage] = useState("");
|
||||
const [isSubmitting, setSubmitting] = useState(false);
|
||||
|
||||
const rows = useMemo(
|
||||
() =>
|
||||
buildAdminScriptVersionRows(versions),
|
||||
[versions],
|
||||
);
|
||||
|
||||
const hasVersions = rows.length > 0;
|
||||
const canSubmit = !isSubmitting;
|
||||
const rollbackCandidates = rows.filter((row) => row.id !== site.active_script_config_version_id);
|
||||
|
||||
async function refreshVersions() {
|
||||
const latest = await fetchScriptConfigVersions(
|
||||
site.id,
|
||||
DEMO_ADMIN_EMAIL,
|
||||
);
|
||||
setVersions(latest.versions);
|
||||
}
|
||||
|
||||
function updateField<K extends keyof NewAdminScriptVersionDraft>(
|
||||
field: K,
|
||||
value: NewAdminScriptVersionDraft[K],
|
||||
) {
|
||||
setDraft((current) => ({ ...current, [field]: value }));
|
||||
}
|
||||
|
||||
function clearMessages() {
|
||||
setFormError("");
|
||||
setActionMessage("");
|
||||
}
|
||||
|
||||
async function handleCreateVersion(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
const nextErrors: DraftValidationErrors = {};
|
||||
|
||||
const publishingYamlError = parseOptionalError(draft.publishingYaml);
|
||||
const transformScriptError = parseOptionalError(draft.transformScript);
|
||||
const diffObject = parseJsonObject(draft.diff);
|
||||
|
||||
if (publishingYamlError) {
|
||||
nextErrors.publishingYaml = publishingYamlError;
|
||||
}
|
||||
if (transformScriptError) {
|
||||
nextErrors.transformScript = transformScriptError;
|
||||
}
|
||||
if (diffObject === null) {
|
||||
nextErrors.diff = "Invalid JSON object";
|
||||
}
|
||||
|
||||
if (draft.rollbackTargetVersionId) {
|
||||
const validRollback = rows.some((row) => row.id === draft.rollbackTargetVersionId);
|
||||
if (!validRollback) {
|
||||
nextErrors.rollbackTargetVersionId = "Select a valid version ID";
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(nextErrors).length > 0) {
|
||||
setErrors(nextErrors);
|
||||
return;
|
||||
}
|
||||
|
||||
setErrors({});
|
||||
clearMessages();
|
||||
setSubmitting(true);
|
||||
|
||||
try {
|
||||
const payload = buildCreateScriptVersionPayload(draft);
|
||||
await createScriptConfigVersion(site.id, payload, DEMO_ADMIN_EMAIL);
|
||||
setActionMessage("New version created");
|
||||
await refreshVersions();
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError) {
|
||||
setFormError(error.message || "Request failed");
|
||||
} else {
|
||||
setFormError("Unable to create publishing version");
|
||||
}
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleActivate(versionId: string) {
|
||||
setSubmitting(true);
|
||||
clearMessages();
|
||||
try {
|
||||
await activateScriptConfigVersion(site.id, versionId, DEMO_ADMIN_EMAIL);
|
||||
await refreshVersions();
|
||||
setActionMessage("Version activated");
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError) {
|
||||
setFormError(error.message || "Request failed");
|
||||
} else {
|
||||
setFormError("Unable to activate version");
|
||||
}
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleRollback(versionId: string) {
|
||||
setSubmitting(true);
|
||||
clearMessages();
|
||||
try {
|
||||
await rollbackScriptConfigVersion(site.id, versionId, DEMO_ADMIN_EMAIL);
|
||||
await refreshVersions();
|
||||
setActionMessage("Rollback applied");
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError) {
|
||||
setFormError(error.message || "Request failed");
|
||||
} else {
|
||||
setFormError("Unable to rollback version");
|
||||
}
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<section>
|
||||
<h2>{site.name} script versions</h2>
|
||||
{formError ? <p className="formError">Error: {formError}</p> : null}
|
||||
{actionMessage ? <p className="formSuccess">{actionMessage}</p> : null}
|
||||
|
||||
<h3>Create new version</h3>
|
||||
<form className="articleForm" onSubmit={handleCreateVersion}>
|
||||
<label htmlFor="publishingYaml">Publishing YAML</label>
|
||||
<textarea
|
||||
id="publishingYaml"
|
||||
className="codeArea"
|
||||
value={draft.publishingYaml}
|
||||
onChange={(event) => updateField("publishingYaml", event.target.value)}
|
||||
/>
|
||||
{errors.publishingYaml ? <p className="fieldError">{errors.publishingYaml}</p> : null}
|
||||
|
||||
<label htmlFor="transformScript">Transform script</label>
|
||||
<textarea
|
||||
id="transformScript"
|
||||
className="codeArea"
|
||||
value={draft.transformScript}
|
||||
onChange={(event) => updateField("transformScript", event.target.value)}
|
||||
/>
|
||||
{errors.transformScript ? (
|
||||
<p className="fieldError">{errors.transformScript}</p>
|
||||
) : null}
|
||||
|
||||
<label htmlFor="diff">Diff payload (JSON)</label>
|
||||
<textarea
|
||||
id="diff"
|
||||
className="codeArea"
|
||||
value={draft.diff}
|
||||
onChange={(event) => updateField("diff", event.target.value)}
|
||||
/>
|
||||
{errors.diff ? <p className="fieldError">{errors.diff}</p> : null}
|
||||
|
||||
<label htmlFor="activate">
|
||||
<input
|
||||
id="activate"
|
||||
type="checkbox"
|
||||
checked={draft.activate}
|
||||
onChange={(event) => updateField("activate", event.target.checked)}
|
||||
/>
|
||||
Activate on create
|
||||
</label>
|
||||
|
||||
<label htmlFor="rollbackTargetVersionId">Rollback target version (optional)</label>
|
||||
<select
|
||||
id="rollbackTargetVersionId"
|
||||
value={draft.rollbackTargetVersionId}
|
||||
onChange={(event) => updateField("rollbackTargetVersionId", event.target.value)}
|
||||
>
|
||||
<option value="">None</option>
|
||||
{rollbackCandidates.map((candidate) => (
|
||||
<option key={candidate.id} value={candidate.id}>
|
||||
v{candidate.version}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
{errors.rollbackTargetVersionId ? (
|
||||
<p className="fieldError">{errors.rollbackTargetVersionId}</p>
|
||||
) : null}
|
||||
|
||||
<div className="formActions">
|
||||
<button type="submit" disabled={!canSubmit}>
|
||||
{isSubmitting ? "Saving..." : "Create version"}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<h3>Version history</h3>
|
||||
{!hasVersions ? (
|
||||
<p className="emptyState">No versions yet.</p>
|
||||
) : (
|
||||
<table className="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Version</th>
|
||||
<th>Status</th>
|
||||
<th>Created</th>
|
||||
<th>Activated</th>
|
||||
<th>Author</th>
|
||||
<th>Hashes</th>
|
||||
<th>Rollback target</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{rows.map((version) => (
|
||||
<tr key={version.id}>
|
||||
<td>
|
||||
{version.version} {version.isActive ? "(active)" : null}
|
||||
<div style={{ marginTop: 6 }}>
|
||||
<strong>diff</strong>: {version.diff}
|
||||
</div>
|
||||
</td>
|
||||
<td>{version.status}</td>
|
||||
<td>{new Date(version.createdAt).toLocaleString()}</td>
|
||||
<td>
|
||||
{version.activatedAt
|
||||
? new Date(version.activatedAt).toLocaleString()
|
||||
: "—"}
|
||||
</td>
|
||||
<td>{version.createdBy}</td>
|
||||
<td>
|
||||
<div>YAML: {version.publishingYamlHash.slice(0, 8)}</div>
|
||||
<div>Script: {version.transformScriptHash.slice(0, 8)}</div>
|
||||
</td>
|
||||
<td>{version.rollbackTargetVersionId ?? "—"}</td>
|
||||
<td>
|
||||
{!version.isActive ? (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleActivate(version.id)}
|
||||
disabled={isSubmitting}
|
||||
>
|
||||
Activate
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleRollback(version.id)}
|
||||
disabled={isSubmitting}
|
||||
className="tableAction"
|
||||
>
|
||||
Rollback
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<span>Current</span>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo, useState, type FormEvent } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import type { TargetSiteConfigCreateRequest } from "@pipeline/shared";
|
||||
|
||||
import { ApiError, createTargetSite, DEMO_ADMIN_EMAIL } from "@/shared/pipeline-api";
|
||||
|
||||
type AdminSiteDraft = {
|
||||
name: string;
|
||||
slug: string;
|
||||
defaultLanguage: string;
|
||||
brandVoice: string;
|
||||
audience: string;
|
||||
publishingType: string;
|
||||
repositoryUrl: string;
|
||||
productionBranch: string;
|
||||
contentFormat: string;
|
||||
contentPathTemplate: string;
|
||||
assetPathTemplate: string;
|
||||
dryRunRenderer: string;
|
||||
frontmatterMapping: string;
|
||||
seoRules: string;
|
||||
visualRules: string;
|
||||
sourceRules: string;
|
||||
};
|
||||
|
||||
type ValidationErrors = Partial<Record<keyof AdminSiteDraft, string>>;
|
||||
|
||||
const defaultDraft = (): AdminSiteDraft => ({
|
||||
name: "",
|
||||
slug: "",
|
||||
defaultLanguage: "en",
|
||||
brandVoice: "Practical and helpful editorial guidance.",
|
||||
audience: "Content and marketing teams.",
|
||||
publishingType: "git_next",
|
||||
repositoryUrl: "",
|
||||
productionBranch: "main",
|
||||
contentFormat: "mdx",
|
||||
contentPathTemplate: "content/articles/{slug}.mdx",
|
||||
assetPathTemplate: "public/articles/{slug}/{filename}",
|
||||
dryRunRenderer: "next-mdx",
|
||||
frontmatterMapping: JSON.stringify(
|
||||
{
|
||||
title: "title",
|
||||
description: "description",
|
||||
publishedAt: "published_at",
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
seoRules: JSON.stringify({}, null, 2),
|
||||
visualRules: JSON.stringify({}, null, 2),
|
||||
sourceRules: JSON.stringify({}, null, 2),
|
||||
});
|
||||
|
||||
function parseJsonField(value: string): Record<string, unknown> | null {
|
||||
if (!value.trim()) {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
||||
return parsed as Record<string, unknown>;
|
||||
}
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function AdminSiteCreateForm() {
|
||||
const router = useRouter();
|
||||
const [values, setValues] = useState<AdminSiteDraft>(defaultDraft());
|
||||
const [errors, setErrors] = useState<ValidationErrors>({});
|
||||
const [formError, setFormError] = useState("");
|
||||
const [isSubmitting, setSubmitting] = useState(false);
|
||||
|
||||
const canSubmit = useMemo(
|
||||
() =>
|
||||
!isSubmitting &&
|
||||
values.name.trim() !== "" &&
|
||||
values.slug.trim() !== "" &&
|
||||
values.repositoryUrl.trim() !== "" &&
|
||||
values.brandVoice.trim() !== "" &&
|
||||
values.audience.trim() !== "",
|
||||
[isSubmitting, values],
|
||||
);
|
||||
|
||||
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
|
||||
const nextErrors: ValidationErrors = {};
|
||||
const parsedFrontmatter = parseJsonField(values.frontmatterMapping);
|
||||
const parsedSeo = parseJsonField(values.seoRules);
|
||||
const parsedVisual = parseJsonField(values.visualRules);
|
||||
const parsedSource = parseJsonField(values.sourceRules);
|
||||
|
||||
if (parsedFrontmatter === null) {
|
||||
nextErrors.frontmatterMapping = "Invalid JSON object";
|
||||
}
|
||||
if (parsedSeo === null) {
|
||||
nextErrors.seoRules = "Invalid JSON object";
|
||||
}
|
||||
if (parsedVisual === null) {
|
||||
nextErrors.visualRules = "Invalid JSON object";
|
||||
}
|
||||
if (parsedSource === null) {
|
||||
nextErrors.sourceRules = "Invalid JSON object";
|
||||
}
|
||||
if (!values.slug.trim()) {
|
||||
nextErrors.slug = "Slug is required";
|
||||
}
|
||||
if (!values.name.trim()) {
|
||||
nextErrors.name = "Name is required";
|
||||
}
|
||||
if (!values.repositoryUrl.trim()) {
|
||||
nextErrors.repositoryUrl = "Repository URL is required";
|
||||
}
|
||||
if (!values.brandVoice.trim()) {
|
||||
nextErrors.brandVoice = "Brand voice is required";
|
||||
}
|
||||
if (!values.audience.trim()) {
|
||||
nextErrors.audience = "Audience is required";
|
||||
}
|
||||
if (Object.keys(nextErrors).length > 0) {
|
||||
setErrors(nextErrors);
|
||||
return;
|
||||
}
|
||||
|
||||
setErrors({});
|
||||
setFormError("");
|
||||
setSubmitting(true);
|
||||
try {
|
||||
const payload: TargetSiteConfigCreateRequest = {
|
||||
name: values.name,
|
||||
slug: values.slug,
|
||||
publishing_type: values.publishingType,
|
||||
default_language: values.defaultLanguage,
|
||||
brand_voice: values.brandVoice,
|
||||
audience: values.audience,
|
||||
seo_rules: parsedSeo ?? {},
|
||||
visual_rules: parsedVisual ?? {},
|
||||
source_rules: parsedSource ?? {},
|
||||
publishing_rules: {
|
||||
repository_url: values.repositoryUrl,
|
||||
production_branch: values.productionBranch,
|
||||
content_format: values.contentFormat,
|
||||
content_path_template: values.contentPathTemplate,
|
||||
asset_path_template: values.assetPathTemplate,
|
||||
frontmatter_mapping: parsedFrontmatter ?? {},
|
||||
dry_run_renderer: values.dryRunRenderer || null,
|
||||
},
|
||||
};
|
||||
|
||||
await createTargetSite(payload, DEMO_ADMIN_EMAIL);
|
||||
router.push("/admin/sites");
|
||||
router.refresh();
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError) {
|
||||
setFormError(error.message || "Request failed");
|
||||
} else {
|
||||
setFormError("Unable to create site config");
|
||||
}
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
function updateField<K extends keyof AdminSiteDraft>(
|
||||
field: K,
|
||||
value: AdminSiteDraft[K],
|
||||
) {
|
||||
setValues((current) => ({ ...current, [field]: value }));
|
||||
}
|
||||
|
||||
return (
|
||||
<form className="articleForm" onSubmit={handleSubmit}>
|
||||
<h2>Create target site config</h2>
|
||||
{formError ? <p className="formError">Error: {formError}</p> : null}
|
||||
<label htmlFor="name">Name</label>
|
||||
<input
|
||||
id="name"
|
||||
value={values.name}
|
||||
onChange={(event) => updateField("name", event.target.value)}
|
||||
placeholder="Editorial blog"
|
||||
/>
|
||||
{errors.name ? <p className="fieldError">{errors.name}</p> : null}
|
||||
|
||||
<label htmlFor="slug">Slug</label>
|
||||
<input
|
||||
id="slug"
|
||||
value={values.slug}
|
||||
onChange={(event) => updateField("slug", event.target.value)}
|
||||
placeholder="editorial-blog"
|
||||
/>
|
||||
{errors.slug ? <p className="fieldError">{errors.slug}</p> : null}
|
||||
|
||||
<label htmlFor="defaultLanguage">Default language</label>
|
||||
<input
|
||||
id="defaultLanguage"
|
||||
value={values.defaultLanguage}
|
||||
onChange={(event) => updateField("defaultLanguage", event.target.value)}
|
||||
/>
|
||||
|
||||
<label htmlFor="publishingType">Publishing type</label>
|
||||
<input
|
||||
id="publishingType"
|
||||
value={values.publishingType}
|
||||
onChange={(event) => updateField("publishingType", event.target.value)}
|
||||
/>
|
||||
|
||||
<label htmlFor="brandVoice">Brand voice</label>
|
||||
<input
|
||||
id="brandVoice"
|
||||
value={values.brandVoice}
|
||||
onChange={(event) => updateField("brandVoice", event.target.value)}
|
||||
placeholder="Confident and practical"
|
||||
/>
|
||||
{errors.brandVoice ? <p className="fieldError">{errors.brandVoice}</p> : null}
|
||||
|
||||
<label htmlFor="audience">Audience</label>
|
||||
<input
|
||||
id="audience"
|
||||
value={values.audience}
|
||||
onChange={(event) => updateField("audience", event.target.value)}
|
||||
placeholder="Marketing and editorial teams"
|
||||
/>
|
||||
{errors.audience ? <p className="fieldError">{errors.audience}</p> : null}
|
||||
|
||||
<label htmlFor="repositoryUrl">Repository URL</label>
|
||||
<input
|
||||
id="repositoryUrl"
|
||||
value={values.repositoryUrl}
|
||||
onChange={(event) => updateField("repositoryUrl", event.target.value)}
|
||||
placeholder="git@github.com:org/repo.git"
|
||||
/>
|
||||
{errors.repositoryUrl ? <p className="fieldError">{errors.repositoryUrl}</p> : null}
|
||||
|
||||
<label htmlFor="productionBranch">Production branch</label>
|
||||
<input
|
||||
id="productionBranch"
|
||||
value={values.productionBranch}
|
||||
onChange={(event) => updateField("productionBranch", event.target.value)}
|
||||
placeholder="main"
|
||||
/>
|
||||
|
||||
<label htmlFor="contentFormat">Content format</label>
|
||||
<input
|
||||
id="contentFormat"
|
||||
value={values.contentFormat}
|
||||
onChange={(event) => updateField("contentFormat", event.target.value)}
|
||||
placeholder="mdx"
|
||||
/>
|
||||
|
||||
<label htmlFor="contentPathTemplate">Content path template</label>
|
||||
<input
|
||||
id="contentPathTemplate"
|
||||
value={values.contentPathTemplate}
|
||||
onChange={(event) => updateField("contentPathTemplate", event.target.value)}
|
||||
placeholder="content/articles/{slug}.mdx"
|
||||
/>
|
||||
|
||||
<label htmlFor="assetPathTemplate">Asset path template</label>
|
||||
<input
|
||||
id="assetPathTemplate"
|
||||
value={values.assetPathTemplate}
|
||||
onChange={(event) => updateField("assetPathTemplate", event.target.value)}
|
||||
placeholder="public/articles/{slug}/{filename}"
|
||||
/>
|
||||
|
||||
<label htmlFor="dryRunRenderer">Dry-run renderer</label>
|
||||
<input
|
||||
id="dryRunRenderer"
|
||||
value={values.dryRunRenderer}
|
||||
onChange={(event) => updateField("dryRunRenderer", event.target.value)}
|
||||
/>
|
||||
|
||||
<label htmlFor="frontmatterMapping">Frontmatter mapping (JSON)</label>
|
||||
<textarea
|
||||
id="frontmatterMapping"
|
||||
className="codeArea"
|
||||
value={values.frontmatterMapping}
|
||||
onChange={(event) => updateField("frontmatterMapping", event.target.value)}
|
||||
/>
|
||||
{errors.frontmatterMapping ? (
|
||||
<p className="fieldError">{errors.frontmatterMapping}</p>
|
||||
) : null}
|
||||
|
||||
<label htmlFor="seoRules">SEO rules (JSON)</label>
|
||||
<textarea
|
||||
id="seoRules"
|
||||
className="codeArea"
|
||||
value={values.seoRules}
|
||||
onChange={(event) => updateField("seoRules", event.target.value)}
|
||||
/>
|
||||
{errors.seoRules ? <p className="fieldError">{errors.seoRules}</p> : null}
|
||||
|
||||
<label htmlFor="visualRules">Visual rules (JSON)</label>
|
||||
<textarea
|
||||
id="visualRules"
|
||||
className="codeArea"
|
||||
value={values.visualRules}
|
||||
onChange={(event) => updateField("visualRules", event.target.value)}
|
||||
/>
|
||||
{errors.visualRules ? <p className="fieldError">{errors.visualRules}</p> : null}
|
||||
|
||||
<label htmlFor="sourceRules">Source rules (JSON)</label>
|
||||
<textarea
|
||||
id="sourceRules"
|
||||
className="codeArea"
|
||||
value={values.sourceRules}
|
||||
onChange={(event) => updateField("sourceRules", event.target.value)}
|
||||
/>
|
||||
{errors.sourceRules ? <p className="fieldError">{errors.sourceRules}</p> : null}
|
||||
|
||||
<div className="formActions">
|
||||
<button type="submit" disabled={!canSubmit}>
|
||||
{isSubmitting ? "Creating..." : "Create site"}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { TargetSiteConfig } from "@pipeline/shared";
|
||||
|
||||
export type AdminSiteRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
publishingRepository: string;
|
||||
productionBranch: string;
|
||||
activeVersionId: string | null;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export function buildAdminSiteRows(
|
||||
sites: readonly TargetSiteConfig[],
|
||||
): AdminSiteRow[] {
|
||||
return sites.map((site) => ({
|
||||
id: site.id,
|
||||
name: site.name,
|
||||
slug: site.slug,
|
||||
publishingRepository: site.publishing_rules.repository_url,
|
||||
productionBranch: site.publishing_rules.production_branch,
|
||||
activeVersionId: site.active_script_config_version_id ?? null,
|
||||
createdAt: site.created_at,
|
||||
updatedAt: site.updated_at,
|
||||
}));
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import Link from "next/link";
|
||||
|
||||
import type { AdminSiteRow } from "./model";
|
||||
|
||||
type AdminSiteConfigTableProps = {
|
||||
rows: readonly AdminSiteRow[];
|
||||
};
|
||||
|
||||
export function AdminSiteConfigTable({ rows }: AdminSiteConfigTableProps) {
|
||||
if (rows.length === 0) {
|
||||
return <p className="emptyState">No target sites yet.</p>;
|
||||
}
|
||||
|
||||
return (
|
||||
<table className="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Slug</th>
|
||||
<th>Repository</th>
|
||||
<th>Branch</th>
|
||||
<th>Active config version</th>
|
||||
<th>Revisions</th>
|
||||
<th>Updated</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{rows.map((row) => (
|
||||
<tr key={row.id}>
|
||||
<td>{row.name}</td>
|
||||
<td>{row.slug}</td>
|
||||
<td>{row.publishingRepository}</td>
|
||||
<td>{row.productionBranch}</td>
|
||||
<td>{row.activeVersionId ?? "none"}</td>
|
||||
<td>
|
||||
<Link href={`/admin/scripts?siteId=${row.id}`}>Versions</Link>
|
||||
</td>
|
||||
<td>{new Date(row.updatedAt).toLocaleString()}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import Link from "next/link";
|
||||
|
||||
import { DEMO_ADMIN_EMAIL, fetchScriptConfigVersions, fetchTargetSitesAsUser, fetchTargetSite } from "@/shared/pipeline-api";
|
||||
import { RoleNavigation } from "@/widgets/role-navigation";
|
||||
import { AdminScriptVersionsPanel } from "@/features/admin-scripts/ui";
|
||||
|
||||
type SearchParams = {
|
||||
siteId?: string;
|
||||
};
|
||||
|
||||
type AdminScriptsPageProps = {
|
||||
searchParams?: SearchParams;
|
||||
};
|
||||
|
||||
export default async function AdminScriptsPage({
|
||||
searchParams,
|
||||
}: AdminScriptsPageProps) {
|
||||
const sitesResponse = await fetchTargetSitesAsUser(DEMO_ADMIN_EMAIL);
|
||||
const siteOptions = sitesResponse.map((site) => site.site);
|
||||
|
||||
if (siteOptions.length === 0) {
|
||||
return (
|
||||
<main>
|
||||
<header className="pageHeader">
|
||||
<div>
|
||||
<h1>Admin Script Versions</h1>
|
||||
<p>No target sites configured yet.</p>
|
||||
</div>
|
||||
<Link href="/admin/sites">Open site configuration</Link>
|
||||
</header>
|
||||
<RoleNavigation role="ADMIN" />
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
const selectedSiteId =
|
||||
searchParams?.siteId ?? siteOptions[0]?.id;
|
||||
const selectedSite =
|
||||
siteOptions.find((site) => site.id === selectedSiteId) ?? siteOptions[0];
|
||||
|
||||
const selectedResponse = await fetchTargetSite(selectedSite.id, DEMO_ADMIN_EMAIL);
|
||||
const activeSite = selectedResponse.site;
|
||||
const versionsResponse = await fetchScriptConfigVersions(
|
||||
selectedSite.id,
|
||||
DEMO_ADMIN_EMAIL,
|
||||
);
|
||||
|
||||
return (
|
||||
<main>
|
||||
<header className="pageHeader">
|
||||
<div>
|
||||
<h1>Admin Script Versions</h1>
|
||||
<p>Manage publishing YAML + transform scripts for each site.</p>
|
||||
</div>
|
||||
<Link href="/admin/sites">Open site configuration</Link>
|
||||
</header>
|
||||
<RoleNavigation role="ADMIN" />
|
||||
<section className="panel">
|
||||
<h2>Select site</h2>
|
||||
<ul className="inlineLinks">
|
||||
{siteOptions.map((site) => {
|
||||
const isActive = site.id === activeSite.id;
|
||||
return (
|
||||
<li key={site.id}>
|
||||
<Link
|
||||
href={`/admin/scripts?siteId=${site.id}`}
|
||||
className={isActive ? "activeLink" : undefined}
|
||||
>
|
||||
{site.name}
|
||||
</Link>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</section>
|
||||
<section className="panel">
|
||||
<AdminScriptVersionsPanel
|
||||
site={activeSite}
|
||||
initialVersions={versionsResponse.versions}
|
||||
/>
|
||||
</section>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import Link from "next/link";
|
||||
|
||||
import { DEMO_ADMIN_EMAIL } from "@/shared/pipeline-api";
|
||||
import { AdminSiteCreateForm } from "@/features/admin-site-config-create/ui";
|
||||
import { buildAdminSiteRows } from "@/features/admin-site-config/model";
|
||||
import { AdminSiteConfigTable } from "@/features/admin-site-config/ui";
|
||||
import { RoleNavigation } from "@/widgets/role-navigation";
|
||||
import { fetchTargetSitesAsUser } from "@/shared/pipeline-api";
|
||||
|
||||
export default async function AdminSitesPage() {
|
||||
const sitesResponse = await fetchTargetSitesAsUser(DEMO_ADMIN_EMAIL);
|
||||
const sites = sitesResponse.map((site) => site.site);
|
||||
const rows = buildAdminSiteRows(sites);
|
||||
|
||||
return (
|
||||
<main>
|
||||
<header className="pageHeader">
|
||||
<div>
|
||||
<h1>Admin Site Config</h1>
|
||||
<p>Manage target site publishing settings.</p>
|
||||
</div>
|
||||
<Link href="/">Back to dashboard</Link>
|
||||
<span className="roleBadge">Admin demo</span>
|
||||
</header>
|
||||
<RoleNavigation role="ADMIN" />
|
||||
<section className="panel">
|
||||
<h2>Existing sites</h2>
|
||||
<AdminSiteConfigTable rows={rows} />
|
||||
</section>
|
||||
<section className="panel">
|
||||
<AdminSiteCreateForm />
|
||||
</section>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -4,12 +4,21 @@ import type {
|
||||
ArticleDetailResponse,
|
||||
ArticleListResponse,
|
||||
CurrentUserResponse,
|
||||
ScriptConfigVersionCreateRequest,
|
||||
ScriptConfigVersionListResponse,
|
||||
ScriptConfigVersionResponse,
|
||||
TargetSiteConfigResponse,
|
||||
TargetSiteConfigCreateRequest,
|
||||
} from "@pipeline/shared";
|
||||
|
||||
export const DEMO_EDITOR_EMAIL = "editor@example.com";
|
||||
export const DEMO_ADMIN_EMAIL = "admin@example.com";
|
||||
const DEFAULT_LOCAL_BACKEND_URL = "http://localhost:8000";
|
||||
|
||||
type ApiRequestConfig = {
|
||||
userEmail?: string;
|
||||
};
|
||||
|
||||
export type ApiErrorPayload = {
|
||||
status: number;
|
||||
message: string;
|
||||
@@ -66,15 +75,17 @@ async function parseBody(response: Response): Promise<unknown> {
|
||||
async function requestJson<T>(
|
||||
path: string,
|
||||
options: RequestInit = {},
|
||||
userEmail?: string,
|
||||
): Promise<T> {
|
||||
const isFormData = options.body instanceof FormData;
|
||||
const effectiveUserEmail = userEmail ?? DEMO_EDITOR_EMAIL;
|
||||
const response = await fetch(buildApiUrl(path), {
|
||||
...options,
|
||||
headers: {
|
||||
...(!isFormData
|
||||
? { "Content-Type": "application/json" }
|
||||
: {}),
|
||||
"X-Demo-User-Email": DEMO_EDITOR_EMAIL,
|
||||
"X-Demo-User-Email": effectiveUserEmail,
|
||||
...(options.headers ?? {}),
|
||||
},
|
||||
cache: options.cache ?? "no-store",
|
||||
@@ -96,38 +107,124 @@ async function requestJson<T>(
|
||||
return body as T;
|
||||
}
|
||||
|
||||
export function apiGet<T>(path: string): Promise<T> {
|
||||
return requestJson<T>(path, { method: "GET" });
|
||||
export function apiGet<T>(
|
||||
path: string,
|
||||
options?: ApiRequestConfig,
|
||||
): Promise<T> {
|
||||
return requestJson<T>(path, { method: "GET" }, options?.userEmail);
|
||||
}
|
||||
|
||||
export function apiPost<TRequest, TResponse>(
|
||||
path: string,
|
||||
body: TRequest,
|
||||
body: TRequest | undefined,
|
||||
options?: ApiRequestConfig,
|
||||
): Promise<TResponse> {
|
||||
return requestJson<TResponse>(path, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const requestBody = body === undefined ? undefined : JSON.stringify(body);
|
||||
return requestJson<TResponse>(
|
||||
path,
|
||||
{
|
||||
method: "POST",
|
||||
...(requestBody === undefined ? {} : { body: requestBody }),
|
||||
},
|
||||
options?.userEmail,
|
||||
);
|
||||
}
|
||||
|
||||
export function apiPatch<TRequest, TResponse>(
|
||||
path: string,
|
||||
body: TRequest,
|
||||
options?: ApiRequestConfig,
|
||||
): Promise<TResponse> {
|
||||
return requestJson<TResponse>(path, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
return requestJson<TResponse>(
|
||||
path,
|
||||
{
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
options?.userEmail,
|
||||
);
|
||||
}
|
||||
|
||||
export function fetchCurrentUser(): Promise<CurrentUserResponse> {
|
||||
return apiGet("/api/me");
|
||||
return apiGet<CurrentUserResponse>("/api/me");
|
||||
}
|
||||
|
||||
export function fetchTargetSites(): Promise<TargetSiteConfigResponse[]> {
|
||||
return apiGet<TargetSiteConfigResponse[]>("/api/sites");
|
||||
}
|
||||
|
||||
export function fetchTargetSitesAsUser(
|
||||
userEmail: string,
|
||||
): Promise<TargetSiteConfigResponse[]> {
|
||||
return apiGet<TargetSiteConfigResponse[]>("/api/sites", { userEmail });
|
||||
}
|
||||
|
||||
export function fetchTargetSite(
|
||||
siteId: string,
|
||||
userEmail = DEMO_EDITOR_EMAIL,
|
||||
): Promise<TargetSiteConfigResponse> {
|
||||
return apiGet<TargetSiteConfigResponse>(`/api/sites/${siteId}`, {
|
||||
userEmail,
|
||||
});
|
||||
}
|
||||
|
||||
export function createTargetSite(
|
||||
request: TargetSiteConfigCreateRequest,
|
||||
userEmail = DEMO_ADMIN_EMAIL,
|
||||
): Promise<TargetSiteConfigResponse> {
|
||||
return apiPost<TargetSiteConfigCreateRequest, TargetSiteConfigResponse>(
|
||||
"/api/sites",
|
||||
request,
|
||||
{ userEmail },
|
||||
);
|
||||
}
|
||||
|
||||
export function fetchScriptConfigVersions(
|
||||
siteId: string,
|
||||
userEmail = DEMO_EDITOR_EMAIL,
|
||||
): Promise<ScriptConfigVersionListResponse> {
|
||||
return apiGet<ScriptConfigVersionListResponse>(
|
||||
`/api/sites/${siteId}/publishing-config/versions`,
|
||||
{ userEmail },
|
||||
);
|
||||
}
|
||||
|
||||
export function createScriptConfigVersion(
|
||||
siteId: string,
|
||||
request: ScriptConfigVersionCreateRequest,
|
||||
userEmail = DEMO_ADMIN_EMAIL,
|
||||
): Promise<ScriptConfigVersionResponse> {
|
||||
return apiPost<ScriptConfigVersionCreateRequest, ScriptConfigVersionResponse>(
|
||||
`/api/sites/${siteId}/publishing-config/versions`,
|
||||
request,
|
||||
{ userEmail },
|
||||
);
|
||||
}
|
||||
|
||||
export function activateScriptConfigVersion(
|
||||
siteId: string,
|
||||
versionId: string,
|
||||
userEmail = DEMO_ADMIN_EMAIL,
|
||||
): Promise<ScriptConfigVersionResponse> {
|
||||
return apiPost<unknown, ScriptConfigVersionResponse>(
|
||||
`/api/sites/${siteId}/publishing-config/versions/${versionId}/activate`,
|
||||
undefined,
|
||||
{ userEmail },
|
||||
);
|
||||
}
|
||||
|
||||
export function rollbackScriptConfigVersion(
|
||||
siteId: string,
|
||||
versionId: string,
|
||||
userEmail = DEMO_ADMIN_EMAIL,
|
||||
): Promise<ScriptConfigVersionResponse> {
|
||||
return apiPost<unknown, ScriptConfigVersionResponse>(
|
||||
`/api/sites/${siteId}/publishing-config/versions/${versionId}/rollback`,
|
||||
undefined,
|
||||
{ userEmail },
|
||||
);
|
||||
}
|
||||
|
||||
export function fetchArticles(): Promise<ArticleListResponse> {
|
||||
return apiGet<ArticleListResponse>("/api/articles");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import ts from "typescript";
|
||||
|
||||
const testDir = path.dirname(fileURLToPath(import.meta.url));
|
||||
const sourcePath = path.resolve(
|
||||
testDir,
|
||||
"../src/features/admin-scripts/model.ts",
|
||||
);
|
||||
const source = readFileSync(sourcePath, "utf8");
|
||||
const compiled = ts.transpileModule(source, {
|
||||
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
|
||||
});
|
||||
|
||||
const moduleExports = {};
|
||||
new Function("exports", compiled.outputText)(moduleExports);
|
||||
|
||||
const {
|
||||
buildAdminScriptVersionRows,
|
||||
defaultScriptVersionDraft,
|
||||
buildCreateScriptVersionPayload,
|
||||
parseJsonObject,
|
||||
} = moduleExports;
|
||||
|
||||
const rows = buildAdminScriptVersionRows([
|
||||
{
|
||||
id: "11111111-1111-1111-1111-111111111101",
|
||||
target_site_id: "22222222-2222-2222-2222-222222222222",
|
||||
version: 1,
|
||||
status: "ACTIVE",
|
||||
diff: { summary: "seed" },
|
||||
publishing_yaml_hash: "abc",
|
||||
transform_script_hash: "def",
|
||||
rollback_target_version_id: null,
|
||||
created_by: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
|
||||
created_at: "2025-01-01T00:00:00Z",
|
||||
activated_at: "2025-01-01T01:00:00Z",
|
||||
},
|
||||
]);
|
||||
|
||||
assert.equal(rows.length, 1);
|
||||
assert.equal(rows[0].isActive, true);
|
||||
assert.equal(rows[0].diff, '{"summary":"seed"}');
|
||||
|
||||
const payload = buildCreateScriptVersionPayload(defaultScriptVersionDraft());
|
||||
assert.equal(payload.activate, false);
|
||||
assert.ok(typeof payload.publishing_yaml === "string");
|
||||
assert.equal(payload.rollback_target_version_id, null);
|
||||
|
||||
assert.deepEqual(parseJsonObject('{"a":1}'), { a: 1 });
|
||||
assert.equal(parseJsonObject("[]"), null);
|
||||
assert.deepEqual(parseJsonObject(""), {});
|
||||
Reference in New Issue
Block a user