39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from datetime import datetime, timezone
|
|
|
|
|
|
class DormantStatusProducer(RuntimeError):
|
|
pass
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class StatusRecord:
|
|
context: str
|
|
state: str
|
|
head_oid: str
|
|
observed_at: str
|
|
|
|
|
|
class MachineStatusProducer:
|
|
def __init__(self, *, enabled: bool = False, fixture: bool = False) -> None:
|
|
self.enabled = enabled
|
|
self.fixture = fixture
|
|
|
|
def produce(self, context: str, state: str, head_oid: str) -> StatusRecord:
|
|
if not self.fixture:
|
|
raise DormantStatusProducer("LIVE_STATUS_PUBLICATION_DENIED")
|
|
if self.enabled:
|
|
raise DormantStatusProducer("DORMANT_CANDIDATE_MUST_NOT_BE_ENABLED")
|
|
if context not in {
|
|
"aios/machine-gates", "aios/secret-scan", "aios/evidence-closure"
|
|
}:
|
|
raise ValueError("unknown status context")
|
|
if state not in {"success", "failure"} or len(head_oid) != 40:
|
|
raise ValueError("invalid status record")
|
|
return StatusRecord(
|
|
context, state, head_oid,
|
|
datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
|
|
)
|