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.

24.8 KB
Raw Blame
import time
import dataclasses
from collections.abc import Iterator
from datetime import UTC, date, datetime, timedelta
from typing import Any, Optional
from urllib.parse import urlencode

import requests
from structlog.types import FilteringBoundLogger
from tenacity import retry_if_exception_type, stop_after_attempt, wait_exponential_jitter

from products.warehouse_sources.sources.sdk import (
    ResumableSourceManager,
    SourceResponse,
    make_tracked_session,
    retry_with_deadline,
)
from products.warehouse_sources.sources.squadcast.settings import SQUADCAST_ENDPOINTS, SquadcastEndpointConfig


@dataclasses.dataclass(frozen=True, kw_only=True, slots=True)
class RegionHosts:
    auth_host: str
    api_host: str


# Squadcast is region-sharded: a US account's refresh token only works against the US hosts and
# vice versa, so the user picks the region in the source form.
REGION_HOSTS: dict[str, RegionHosts] = {
    "us": RegionHosts(auth_host="https://auth.squadcast.com", api_host="https://api.squadcast.com"),
    "eu": RegionHosts(auth_host="https://auth.eu.squadcast.com", api_host="https://api.eu.squadcast.com"),
}

PAGE_SIZE = 100

# The postmortem list endpoint requires a `limit` and exposes no offset/page param, so we ask for
# a generous window and warn if the reported total exceeds what came back.
POSTMORTEM_LIMIT = 1000

# The incident export requires a start/end window; chunking it bounds each response's size on
# accounts with years of history and gives us natural resume checkpoints.
INCIDENT_EXPORT_WINDOW_DAYS = 30

# Window start used on full refresh / first sync (predates Squadcast's launch).
FULL_REFRESH_START = datetime(2015, 1, 1, tzinfo=UTC)

# Retry/throttle settings kept near the top for easy tuning.
RETRY_ATTEMPTS = 5
REQUEST_TIMEOUT_SECONDS = 60

# Re-exchange the refresh token this many seconds before the access token's `expires_at`.
TOKEN_REFRESH_LEEWAY_SECONDS = 120


class SquadcastRetryableError(Exception):
    pass


class SquadcastAuthError(Exception):
    """Raised when the refresh token exchange is rejected — a permanent credential failure."""


@dataclasses.dataclass(frozen=False)
class SquadcastResumeConfig:
    # Bookmark of the team currently being processed (fan-out endpoints). A stable team-ID
    # bookmark rather than a positional index, so teams added/removed between a crash and the
    # retry can't resume us into the wrong team. None for org-level endpoints.
    team_id: str | None = None
    # Position within the bookmarked team: an offset, a page cursor, or an ISO window start,
    # depending on the endpoint's pagination style. None means "start the team from the top".
    cursor: str | None = None


def _hosts_for_region(region: str | None) -> RegionHosts:
    return REGION_HOSTS.get((region or "us").lower(), REGION_HOSTS["us"])


def _format_datetime(value: Any) -> str:
    """Format a datetime as RFC 3339 with a Z suffix, which Squadcast's date filters expect."""
    if isinstance(value, datetime):
        utc_value = value.astimezone(UTC) if value.tzinfo else value.replace(tzinfo=UTC)
    elif isinstance(value, date):
        utc_value = datetime.combine(value, datetime.min.time(), tzinfo=UTC)
    else:
        return str(value)
    return utc_value.strftime("%Y-%m-%dT%H:%M:%SZ")


def _parse_datetime(value: Any) -> datetime:
    if isinstance(value, datetime):
        return value.astimezone(UTC) if value.tzinfo else value.replace(tzinfo=UTC)
    if isinstance(value, date):
        return datetime.combine(value, datetime.min.time(), tzinfo=UTC)
    return datetime.fromisoformat(str(value).replace("Z", "+00:00")).astimezone(UTC)


def _error_message(response: requests.Response) -> str:
    try:
        return response.json().get("meta", {}).get("error_message") or response.text
    except Exception:
        return response.text


class SquadcastClient:
    """Minimal Squadcast API client that exchanges the long-lived refresh token for a short-lived
    bearer access token and transparently re-exchanges it before expiry."""

    def __init__(self, refresh_token: str, region: str | None, logger: FilteringBoundLogger) -> None:
        hosts = _hosts_for_region(region)
        self._auth_host = hosts.auth_host
        self.api_host = hosts.api_host
        self._refresh_token = refresh_token
        self._logger = logger
        # Sessions are reused across requests so urllib3 keeps the connection alive. The refresh
        # token is masked in logs/samples, and redirects are disabled so credentialed headers
        # can't be replayed to a redirect target. Both sessions set `capture=False`: the token
        # exchange response carries the minted tokens, and the API responses carry arbitrary
        # customer-authored operational data (incident descriptions, logs, postmortems, runbook
        # steps) the name-based scrubbers can't recognise, so neither belongs in HTTP samples.
        self._session = make_tracked_session(redact_values=(refresh_token,), allow_redirects=False, capture=False)
        self._auth_session = make_tracked_session(redact_values=(refresh_token,), allow_redirects=False, capture=False)
        self._access_token: Optional[str] = None
        self._token_expires_at: float = 0

    def _get_access_token(self) -> str:
        now = time.time()
        if self._access_token is None or now >= self._token_expires_at - TOKEN_REFRESH_LEEWAY_SECONDS:
            response = self._auth_session.get(
                f"{self._auth_host}/oauth/access-token",
                headers={"X-Refresh-Token": self._refresh_token},
                timeout=REQUEST_TIMEOUT_SECONDS,
            )
            if response.status_code == 429 or response.status_code >= 500:
                raise SquadcastRetryableError(
                    f"Squadcast auth error (retryable): status={response.status_code}, host={self._auth_host}"
                )
            if not response.ok:
                raise SquadcastAuthError(
                    f"Squadcast refresh token was rejected (status={response.status_code}): {_error_message(response)}"
                )

            data = response.json().get("data", {})
            access_token = data.get("access_token")
            if not access_token:
                raise SquadcastAuthError("Squadcast token exchange succeeded but returned no access token")

            self._access_token = access_token
            # `expires_at` is a unix timestamp; fall back to a conservative 10 minutes if absent.
            self._token_expires_at = float(data.get("expires_at") or (now + 600))

        assert self._access_token is not None
        return self._access_token

    @retry_with_deadline(
        retry=retry_if_exception_type((SquadcastRetryableError, requests.ReadTimeout, requests.ConnectionError)),
        stop=stop_after_attempt(RETRY_ATTEMPTS),
        wait=wait_exponential_jitter(initial=1, max=30),
        reraise=True,
    )
    def get(self, path: str, params: Optional[dict[str, Any]] = None) -> Any:
        url = f"{self.api_host}{path}"
        if params:
            url = f"{url}?{urlencode(params)}"

        response = self._session.get(
            url,
            headers={"Authorization": f"Bearer {self._get_access_token()}"},
            timeout=REQUEST_TIMEOUT_SECONDS,
        )

        if response.status_code == 429 or response.status_code >= 500:
            raise SquadcastRetryableError(f"Squadcast API error (retryable): status={response.status_code}, url={url}")

        if not response.ok:
            self._logger.error(f"Squadcast API error: status={response.status_code}, body={response.text}, url={url}")
            response.raise_for_status()

        return response.json()


# Elements are unvalidated upstream JSON, so this is honestly `list[Any]` — `_prepare_rows`
# guards against non-dict elements at runtime.
def _extract_rows(data: Any, envelope: tuple[str, ...]) -> list[Any]:
    current = data
    for key in envelope:
        if not isinstance(current, dict):
            return []
        current = current.get(key)
    return current if isinstance(current, list) else []


def _prepare_rows(rows: list[Any], config: SquadcastEndpointConfig, team_id: str | None = None) -> list[Any]:
    """Drop credential-bearing fields (live keys must never land in warehouse tables) and stamp
    the owning team onto fan-out rows — most Squadcast payloads don't carry it."""
    for row in rows:
        if not isinstance(row, dict):
            continue
        for sensitive_field in config.sensitive_fields:
            row.pop(sensitive_field, None)
        if team_id is not None and "team_id" not in row:
            row["team_id"] = team_id
    return rows


def _iter_offset_pages(
    client: SquadcastClient,
    config: SquadcastEndpointConfig,
    team_id: str,
    start_cursor: str | None,
    resumable_source_manager: ResumableSourceManager[SquadcastResumeConfig],
) -> Iterator[list[dict[str, Any]]]:
    offset = int(start_cursor) if start_cursor else 0
    while True:
        assert config.team_param is not None
        data = client.get(config.path, {config.team_param: team_id, "offset": offset, "limit": PAGE_SIZE})
        rows = _extract_rows(data, config.envelope)
        if not rows:
            break

        yield _prepare_rows(rows, config, team_id)

        if len(rows) < PAGE_SIZE:
            break
        offset += PAGE_SIZE
        # Save AFTER yielding so a crash re-fetches the last page; merge dedupes on primary key.
        resumable_source_manager.save_state(SquadcastResumeConfig(team_id=team_id, cursor=str(offset)))


def _iter_cursor_pages(
    client: SquadcastClient,
    config: SquadcastEndpointConfig,
    team_id: str,
    start_cursor: str | None,
    resumable_source_manager: ResumableSourceManager[SquadcastResumeConfig],
) -> Iterator[list[dict[str, Any]]]:
    cursor = start_cursor
    while True:
        assert config.team_param is not None
        params: dict[str, Any] = {config.team_param: team_id, "pageSize": PAGE_SIZE}
        if cursor:
            params["cursor"] = cursor

        data = client.get(config.path, params)
        rows = _extract_rows(data, config.envelope)
        page_info = data.get("pageInfo", {}) if isinstance(data, dict) else {}
        next_cursor = page_info.get("nextCursor") if page_info.get("hasNext") else None

        if rows:
            yield _prepare_rows(rows, config, team_id)

        if not next_cursor:
            break
        cursor = next_cursor
        resumable_source_manager.save_state(SquadcastResumeConfig(team_id=team_id, cursor=cursor))


def _iter_incident_export_windows(
    client: SquadcastClient,
    config: SquadcastEndpointConfig,
    team_id: str,
    start_cursor: str | None,
    resumable_source_manager: ResumableSourceManager[SquadcastResumeConfig],
    db_incremental_field_last_value: Any,
) -> Iterator[list[dict[str, Any]]]:
    if start_cursor:
        window_start = _parse_datetime(start_cursor)
    elif db_incremental_field_last_value is not None:
        window_start = _parse_datetime(db_incremental_field_last_value)
    else:
        window_start = FULL_REFRESH_START

    now = datetime.now(UTC)
    while window_start < now:
        window_end = min(window_start + timedelta(days=INCIDENT_EXPORT_WINDOW_DAYS), now)
        assert config.team_param is not None
        data = client.get(
            config.path,
            {
                config.team_param: team_id,
                "type": "json",
                "start_time": _format_datetime(window_start),
                "end_time": _format_datetime(window_end),
            },
        )

        # The JSON export's envelope isn't documented; accept both a bare list and the
        # `{"data": [...]}` / `{"incidents": [...]}` wrappers used elsewhere in the API.
        if isinstance(data, list):
            rows = [row for row in data if isinstance(row, dict)]
        else:
            rows = _extract_rows(data, ("data",)) or _extract_rows(data, ("incidents",))

        if rows:
            yield _prepare_rows(rows, config, team_id)

        window_start = window_end
        if window_start < now:
            resumable_source_manager.save_state(
                SquadcastResumeConfig(team_id=team_id, cursor=_format_datetime(window_start))
            )


def _get_postmortem_rows(
    client: SquadcastClient,
    config: SquadcastEndpointConfig,
    team_id: str,
    logger: FilteringBoundLogger,
    db_incremental_field_last_value: Any,
) -> Iterator[list[dict[str, Any]]]:
    window_start = (
        _parse_datetime(db_incremental_field_last_value)
        if db_incremental_field_last_value is not None
        else FULL_REFRESH_START
    )
    assert config.team_param is not None
    data = client.get(
        config.path,
        {
            config.team_param: team_id,
            "fromDate": _format_datetime(window_start),
            "toDate": _format_datetime(datetime.now(UTC)),
            "limit": POSTMORTEM_LIMIT,
        },
    )

    rows: list[dict[str, Any]] = []
    total_count: Optional[int] = None
    for item in _extract_rows(data, ("data",)):
        rows.extend(row for row in item.get("result") or [] if isinstance(row, dict))
        for counter in item.get("total_count") or []:
            if isinstance(counter, dict) and counter.get("count") is not None:
                total_count = counter["count"]

    # The endpoint requires `limit` and has no offset param, so an unusually large backlog
    # can't be paged through — surface the truncation instead of hiding it.
    if total_count is not None and total_count > len(rows):
        logger.warning(
            f"Squadcast: postmortems for team {team_id} truncated ({len(rows)} of {total_count}); "
            f"the API exposes no pagination beyond limit={POSTMORTEM_LIMIT}"
        )

    if rows:
        yield _prepare_rows(rows, config, team_id)


def _iter_schedule_rotations(
    client: SquadcastClient,
    config: SquadcastEndpointConfig,
    team_id: str,
    start_cursor: str | None,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[SquadcastResumeConfig],
) -> Iterator[list[dict[str, Any]]]:
    schedules_config = SQUADCAST_ENDPOINTS["schedules"]
    assert schedules_config.team_param is not None
    cursor = start_cursor
    while True:
        params: dict[str, Any] = {schedules_config.team_param: team_id, "pageSize": PAGE_SIZE}
        if cursor:
            params["cursor"] = cursor

        data = client.get(schedules_config.path, params)
        page_info = data.get("pageInfo", {}) if isinstance(data, dict) else {}
        next_cursor = page_info.get("nextCursor") if page_info.get("hasNext") else None

        # Stage the cursor of the current schedules page, not the next one: a resume re-reads
        # this page's rotations, and merge dedupes them on the primary key.
        resumable_source_manager.save_state(SquadcastResumeConfig(team_id=team_id, cursor=cursor))

        for schedule in _extract_rows(data, schedules_config.envelope):
            schedule_id = schedule.get("id") if isinstance(schedule, dict) else None
            if schedule_id is None:
                continue
            try:
                rotations = _extract_rows(client.get(config.path.format(schedule_id=schedule_id)), config.envelope)
            except requests.HTTPError as exc:
                # 404: the schedule was deleted after the list request. 403: the role can't read this
                # schedule. Either way skip only this schedule, so the team's other schedules still sync.
                if exc.response is not None and exc.response.status_code in (403, 404):
                    logger.warning(
                        f"Squadcast: cannot read rotations of schedule {schedule_id} for team {team_id} "
                        f"(status={exc.response.status_code}), skipping"
                    )
                    rotations = []
                else:
                    raise

            for rotation in rotations:
                if isinstance(rotation, dict):
                    rotation.setdefault("scheduleID", schedule_id)

            if rotations:
                yield _prepare_rows(rotations, config, team_id)
            else:
                resumable_source_manager.safe_point()

        if not next_cursor:
            break
        cursor = next_cursor


def _iter_team_rows(
    client: SquadcastClient,
    config: SquadcastEndpointConfig,
    team_id: str,
    start_cursor: str | None,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[SquadcastResumeConfig],
    db_incremental_field_last_value: Any,
) -> Iterator[list[dict[str, Any]]]:
    if config.pagination == "offset":
        yield from _iter_offset_pages(client, config, team_id, start_cursor, resumable_source_manager)
    elif config.pagination == "cursor":
        yield from _iter_cursor_pages(client, config, team_id, start_cursor, resumable_source_manager)
    elif config.pagination == "incident_export":
        yield from _iter_incident_export_windows(
            client, config, team_id, start_cursor, resumable_source_manager, db_incremental_field_last_value
        )
    elif config.pagination == "postmortems":
        yield from _get_postmortem_rows(client, config, team_id, logger, db_incremental_field_last_value)
    elif config.pagination == "schedule_rotations":
        yield from _iter_schedule_rotations(client, config, team_id, start_cursor, logger, resumable_source_manager)
    else:
        assert config.team_param is not None
        data = client.get(config.path, {config.team_param: team_id})
        rows = _extract_rows(data, config.envelope)

        # See settings.py: escalation policies and squads aren't paginated client-side, so check
        # the reported total or next-page flag for silent truncation.
        if isinstance(data, dict):
            total_count = data.get("meta", {}).get("total_count")
            has_next = bool((data.get("pageInfo") or {}).get("hasNext"))
            if has_next or (total_count is not None and total_count > len(rows)):
                logger.warning(
                    f"Squadcast: endpoint '{config.path}' for team {team_id} returned {len(rows)} "
                    f"of {total_count if total_count is not None else 'more'} rows; results may be truncated"
                )

        if rows:
            yield _prepare_rows(rows, config, team_id)


def get_rows(
    refresh_token: str,
    region: str | None,
    endpoint: str,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[SquadcastResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Any = None,
) -> Iterator[list[dict[str, Any]]]:
    config = SQUADCAST_ENDPOINTS[endpoint]
    client = SquadcastClient(refresh_token, region, logger)
    last_value = db_incremental_field_last_value if should_use_incremental_field else None

    if config.team_param is None:
        data = client.get(config.path)
        rows = _extract_rows(data, config.envelope)
        if rows:
            yield _prepare_rows(rows, config)
        return

    team_ids = [str(team["id"]) for team in _extract_rows(client.get("/v3/teams"), ("data",)) if team.get("id")]

    # Resolve the saved team-ID bookmark to the slice of teams still to process. If the
    # bookmarked team no longer exists, start over from the first team — merge dedupes the
    # re-pulled rows on the primary key. `resume_cursor` is consumed by the first team only.
    resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
    remaining = team_ids
    resume_cursor: str | None = None
    if resume is not None and resume.team_id is not None and resume.team_id in team_ids:
        remaining = team_ids[team_ids.index(resume.team_id) :]
        resume_cursor = resume.cursor
        logger.debug(f"Squadcast: resuming {endpoint} from team {resume.team_id}, cursor={resume_cursor}")

    for index, team_id in enumerate(remaining):
        start_cursor = resume_cursor
        resume_cursor = None  # only the resumed-into team uses the saved cursor

        try:
            yield from _iter_team_rows(
                client, config, team_id, start_cursor, logger, resumable_source_manager, last_value
            )
        except requests.HTTPError as exc:
            # The connecting user's role may not grant access to every team. Skip teams we
            # can't read rather than failing the whole sync. Any other HTTP error re-raises.
            if exc.response is not None and exc.response.status_code in (403, 404):
                logger.warning(
                    f"Squadcast: no access to team {team_id} for endpoint '{endpoint}' "
                    f"(status={exc.response.status_code}), skipping"
                )
            else:
                raise

        # Advance the bookmark to the next team so a crash between teams resumes correctly.
        if index + 1 < len(remaining):
            resumable_source_manager.save_state(SquadcastResumeConfig(team_id=remaining[index + 1], cursor=None))


def squadcast_source(
    refresh_token: str,
    region: str | None,
    endpoint: str,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[SquadcastResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Optional[Any] = None,
) -> SourceResponse:
    config = SQUADCAST_ENDPOINTS[endpoint]

    return SourceResponse(
        name=endpoint,
        items=lambda: get_rows(
            refresh_token=refresh_token,
            region=region,
            endpoint=endpoint,
            logger=logger,
            resumable_source_manager=resumable_source_manager,
            should_use_incremental_field=should_use_incremental_field,
            db_incremental_field_last_value=db_incremental_field_last_value,
        ),
        primary_keys=list(config.primary_keys),
        # Incremental endpoints fan out over teams, so rows are not globally ordered by the
        # incremental field; "desc" makes the pipeline persist the watermark only after the
        # whole job completes instead of checkpointing a possibly-unsafe per-batch maximum.
        sort_mode="desc" if config.incremental_fields else "asc",
        partition_count=1,
        partition_size=1,
        partition_mode="datetime" if config.partition_key else None,
        partition_format="month" if config.partition_key else None,
        partition_keys=[config.partition_key] if config.partition_key else None,
    )


def validate_credentials(
    refresh_token: str, region: str | None, endpoint: Optional[str] = None
) -> tuple[bool, int, str | None]:  # nosemgrep: tuple-return-prefer-dataclass -- pre-existing, file moved
    """Exchange the refresh token, then probe a cheap list endpoint.

    Returns ``(ok, status_code, error_message)``. ``status_code`` is 0 on transport failure.
    The caller decides how to treat 403 (valid token, missing access for the probed endpoint).
    """
    hosts = _hosts_for_region(region)
    # Same credential hardening as SquadcastClient: mask the refresh token, never follow
    # redirects with credentialed headers, and keep both the token exchange and the API probe
    # out of sample capture — the probe response body carries customer operational data.
    session = make_tracked_session(redact_values=(refresh_token,), allow_redirects=False, capture=False)
    auth_session = make_tracked_session(redact_values=(refresh_token,), allow_redirects=False, capture=False)

    try:
        auth_response = auth_session.get(
            f"{hosts.auth_host}/oauth/access-token",
            headers={"X-Refresh-Token": refresh_token},
            timeout=10,
        )
    except requests.exceptions.RequestException as e:
        return False, 0, str(e)

    if not auth_response.ok:
        if auth_response.status_code in (400, 401):
            return False, 401, "Invalid Squadcast refresh token"
        return False, auth_response.status_code, _error_message(auth_response)

    access_token = auth_response.json().get("data", {}).get("access_token")
    if not access_token:
        return False, 0, "Squadcast token exchange returned no access token"

    # Team-scoped endpoints require query params a probe can't guess, so probe /v3/teams for
    # them (the fan-out needs it anyway); org-level endpoints are probed directly.
    config = SQUADCAST_ENDPOINTS.get(endpoint) if endpoint else None
    probe_path = config.path if config is not None and config.team_param is None else "/v3/teams"

    try:
        response = session.get(
            f"{hosts.api_host}{probe_path}",
            headers={"Authorization": f"Bearer {access_token}"},
            timeout=10,
        )
    except requests.exceptions.RequestException as e:
        return False, 0, str(e)

    if response.status_code == 200:
        return True, 200, None
    if response.status_code == 401:
        return False, 401, "Invalid Squadcast refresh token"
    if response.status_code == 403:
        return False, 403, "Your Squadcast account does not have access to this resource"

    return False, response.status_code, _error_message(response)