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.

1.9 KB
Raw Blame
from typing import Any
from uuid import UUID

from django.db import transaction

from posthog.exceptions_capture import capture_exception

from products.tasks.backend.facade.task_run_signals import TaskOriginProduct, connect_task_run_post_save


def connect() -> None:
    connect_task_run_post_save(open_docs_pull_request, dispatch_uid="knowledge_writer_docs_pull_request")


def open_docs_pull_request(sender: type, instance: Any, created: bool, **kwargs: Any) -> None:
    try:
        schedule_docs_pull_request(instance, created=created, update_fields=kwargs.get("update_fields"))
    except Exception as error:
        capture_exception(error)


def schedule_docs_pull_request(instance: Any, *, created: bool, update_fields: Any) -> None:
    if created or instance.origin_product != TaskOriginProduct.KNOWLEDGE_WRITER:
        return
    if instance.status != instance.Status.COMPLETED:
        return
    if update_fields is not None and "status" not in update_fields and "output" not in update_fields:
        return
    if not (instance.output or {}).get("head_branches"):
        return
    task_run_id, task_id, team_id = instance.id, instance.task_id, instance.team_id
    transaction.on_commit(lambda: start_publish_or_record_failure(task_run_id, task_id, team_id), robust=True)


def start_publish_or_record_failure(task_run_id: UUID, task_id: UUID, team_id: int) -> None:
    from .logic import pull_requests  # noqa: PLC0415 — keeps the GitHub client off the setup import path
    from .temporal import client as temporal_client  # noqa: PLC0415 — keeps temporalio off the setup import path

    pull_requests.claim_publication(task_id, team_id, task_run_id)
    try:
        temporal_client.start_publish(task_run_id, team_id=team_id)
    except Exception as error:
        capture_exception(error, {"team_id": team_id, "task_run_id": str(task_run_id)})
        pull_requests.record_pull_request_failure(task_id, team_id, "publish_not_started")