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.

7.5 KB
Raw Blame
import dataclasses
from datetime import UTC, date, datetime
from typing import Any, Optional, cast

from products.warehouse_sources.sources.productboard.settings import FANOUT_PARENT_ENDPOINTS, PRODUCTBOARD_ENDPOINTS
from products.warehouse_sources.sources.sdk import (
    ClientConfig,
    JSONResponsePaginator,
    Resource,
    RESTAPIConfig,
    ResumableSourceManager,
    SourceResponse,
    build_dependent_resource,
    make_tracked_session,
    rest_api_resource,
    validate_via_probe,
)

PRODUCTBOARD_BASE_URL = "https://api.productboard.com/v2"


@dataclasses.dataclass(frozen=True)
class ProductboardResumeConfig:
    # Full URL of the next unfetched page — Productboard returns it in the `links.next` body field.
    next_url: Optional[str] = None
    # Fan-out tables checkpoint the shape `build_dependent_resource` emits: which parents finished
    # and where the current one stopped.
    fanout_state: Optional[dict[str, Any]] = None


def _non_secret_headers() -> dict[str, str]:
    # Bearer auth is supplied via the framework auth config so its value is redacted from logs and
    # error messages; only the non-secret Accept header is set here.
    return {"Accept": "application/json"}


def _format_incremental_value(value: Any) -> str:
    """Format an incremental cursor value as ISO 8601 with a `Z` suffix.

    Productboard's date/time filters expect ISO 8601 date-time strings; we normalise
    to UTC so naive watermarks from the DB compare correctly against tz-aware values.
    """
    if isinstance(value, datetime):
        dt = value
    elif isinstance(value, date):
        dt = datetime.combine(value, datetime.min.time())
    else:
        return str(value)

    dt = dt.replace(tzinfo=UTC) if dt.tzinfo is None else dt.astimezone(UTC)
    return dt.strftime("%Y-%m-%dT%H:%M:%SZ")


def _build_initial_params(
    endpoint: str,
    should_use_incremental_field: bool,
    db_incremental_field_last_value: Any,
    incremental_field: str | None,
) -> dict[str, Any]:
    config = PRODUCTBOARD_ENDPOINTS[endpoint]
    params: dict[str, Any] = {}

    if config.entity_type:
        # The `type[]` bracket key percent-encodes on the wire (`type%5B%5D=...`), which is
        # what Productboard's `/entities` filter expects.
        params["type[]"] = config.entity_type

    if config.supports_incremental and should_use_incremental_field and db_incremental_field_last_value:
        field = incremental_field or config.default_incremental_field
        filter_param = config.incremental_param_map.get(field) if field else None
        if filter_param:
            params[filter_param] = _format_incremental_value(db_incremental_field_last_value)

    return params


def _flatten_relationship_target(row: dict[str, Any]) -> dict[str, Any]:
    # The target id is part of the primary key, so it has to be a top-level column.
    target = row.pop("target", None) or {}
    row["target_id"] = target.get("id")
    row["target_type"] = target.get("type")
    return row


def productboard_source(
    access_token: str,
    endpoint: str,
    team_id: int,
    job_id: str,
    resumable_source_manager: ResumableSourceManager[ProductboardResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Optional[Any] = None,
    incremental_field: str | None = None,
) -> SourceResponse:
    config = PRODUCTBOARD_ENDPOINTS[endpoint]

    params = _build_initial_params(
        endpoint, should_use_incremental_field, db_incremental_field_last_value, incremental_field
    )

    client_config: ClientConfig = {
        "base_url": PRODUCTBOARD_BASE_URL,
        "headers": _non_secret_headers(),
        "auth": {"type": "bearer", "token": access_token},
        # `links.next` comes from the response body, so pin every request to the API host and refuse
        # redirects to keep the bearer token from being sent anywhere else.
        "allowed_hosts": [],
        "allow_redirects": False,
        # Next-page URL lives in the response body under `links.next`; absence of it ends the sync.
        "paginator": JSONResponsePaginator(next_url_path="links.next"),
    }

    resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None

    resource: Resource
    if config.fanout is not None:

        def save_fanout_checkpoint(state: Optional[dict[str, Any]]) -> None:
            if state:
                resumable_source_manager.save_state(ProductboardResumeConfig(fanout_state=state))

        fanout_resource = build_dependent_resource(
            endpoint_configs={**PRODUCTBOARD_ENDPOINTS, **FANOUT_PARENT_ENDPOINTS},
            child_endpoint=endpoint,
            fanout=config.fanout,
            client_config=client_config,
            path_format_values={},
            team_id=team_id,
            job_id=job_id,
            db_incremental_field_last_value=None,
            page_size_param=None,
            parent_endpoint_extra={"data_selector": "data"},
            child_endpoint_extra={"data_selector": "data"},
            resume_hook=save_fanout_checkpoint,
            initial_paginator_state=resume.fanout_state if resume else None,
            # Saves the parent page too, so the checkpoint keeps two pages of finished parents rather
            # than every parent of a large entity or note listing.
            resume_parent_pagination=True,
        )
        resource = cast(Resource, fanout_resource).add_map(_flatten_relationship_target)
    else:
        rest_config: RESTAPIConfig = {
            "client": client_config,
            "resources": [
                {
                    "name": endpoint,
                    "endpoint": {
                        "path": config.path,
                        "params": params,
                        "data_selector": "data",
                    },
                }
            ],
        }

        def save_checkpoint(state: Optional[dict[str, Any]]) -> None:
            # Persist only while a next page remains; save AFTER a page is yielded so a crash re-yields
            # the last page (merge dedupes on primary key) rather than skipping it.
            if state and state.get("next_url"):
                resumable_source_manager.save_state(ProductboardResumeConfig(next_url=state["next_url"]))

        # The incremental watermark is applied by `_build_initial_params` as a Productboard-specific server
        # filter param, so the framework's generic incremental injection is not used here.
        resource = rest_api_resource(
            rest_config,
            team_id,
            job_id,
            None,
            resume_hook=save_checkpoint,
            initial_paginator_state={"next_url": resume.next_url} if resume and resume.next_url else None,
        )

    return SourceResponse(
        name=endpoint,
        items=lambda: resource,
        primary_keys=config.primary_keys,
        sort_mode=config.sort_mode,
        partition_count=1,
        partition_size=1,
        partition_mode="datetime" if config.partition_key else None,
        partition_format="week" if config.partition_key else None,
        partition_keys=[config.partition_key] if config.partition_key else None,
        column_hints=resource.column_hints,
    )


def validate_credentials(access_token: str, path: str) -> tuple[bool, int | None]:
    """Probe a single Productboard endpoint. Returns (ok, status_code)."""
    return validate_via_probe(
        lambda: make_tracked_session(redact_values=(access_token,)),
        f"{PRODUCTBOARD_BASE_URL}{path}",
        headers={"Authorization": f"Bearer {access_token}", **_non_secret_headers()},
    )