Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.5 KB
Raw Blame
from datetime import datetime

from products.wizard.backend.facade.contracts import (
    GitRepositoryWorkspace,
    LocalFolderWorkspace,
    WizardRunCreatorDTO,
    WizardRunDTO,
    WizardRunTaskDTO,
    WizardWorkspace,
)
from products.wizard.backend.facade.enums import (
    WizardRunEnvironment,
    WizardRunStage,
    WizardRunStatus,
    WizardTaskStatus,
    WizardWorkspaceType,
)
from products.wizard.backend.facade.validation import (
    validate_optional_workspace_metadata_value,
    validate_workspace_metadata_value,
)
from products.wizard.backend.logic.programs import program_from_mapping
from products.wizard.backend.models import WizardRun


def workspace_to_record(workspace: WizardWorkspace) -> tuple[WizardWorkspaceType, dict[str, object]]:
    match workspace:
        case LocalFolderWorkspace(project_name=project_name):
            return WizardWorkspaceType.LOCAL_FOLDER, {"project_name": project_name}
        case GitRepositoryWorkspace(repository=repository, branch=None):
            return WizardWorkspaceType.GIT_REPOSITORY, {"repository": repository}
        case GitRepositoryWorkspace(repository=repository, branch=branch):
            return WizardWorkspaceType.GIT_REPOSITORY, {"repository": repository, "branch": branch}


def record_to_workspace(workspace_type: str, metadata: object) -> WizardWorkspace:
    match WizardWorkspaceType(workspace_type):
        case WizardWorkspaceType.LOCAL_FOLDER:
            return LocalFolderWorkspace(project_name=validate_workspace_metadata_value(metadata, "project_name"))
        case WizardWorkspaceType.GIT_REPOSITORY:
            return GitRepositoryWorkspace(
                repository=validate_workspace_metadata_value(metadata, "repository"),
                branch=validate_optional_workspace_metadata_value(metadata, "branch"),
            )


def record_to_run(run: WizardRun) -> WizardRunDTO:
    creator = run.created_by

    return WizardRunDTO(
        id=run.id,
        team_id=run.team_id,
        created_by_id=run.created_by_id,
        environment=WizardRunEnvironment(run.environment),
        workspace=record_to_workspace(run.workspace_type, run.workspace),
        program=program_from_mapping(run.program, allow_latest_version=True),
        status=WizardRunStatus(run.status),
        error_code=run.error_code,
        error_message=run.error_message,
        stage=WizardRunStage(run.stage) if run.stage else None,
        created_at=run.created_at,
        updated_at=run.updated_at,
        started_at=run.started_at,
        finished_at=run.finished_at,
        deadline_at=run.deadline_at,
        created_by=(
            WizardRunCreatorDTO(
                id=creator.id,
                first_name=creator.first_name,
                last_name=creator.last_name,
                email=creator.email,
            )
            if creator is not None
            else None
        ),
        tasks=tuple(
            WizardRunTaskDTO(
                title=task["title"],
                status=WizardTaskStatus(task["status"]),
                created_at=datetime.fromisoformat(task["created_at"]),
                started_at=datetime.fromisoformat(task["started_at"]) if task["started_at"] else None,
                completed_at=datetime.fromisoformat(task["completed_at"]) if task["completed_at"] else None,
                failed_at=datetime.fromisoformat(task["failed_at"]) if task["failed_at"] else None,
                error_message=task["error_message"],
            )
            for task in run.tasks_snapshot or []
        ),
    )