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.

8.3 KB
Raw Blame
from datetime import date
from typing import Optional, cast

from products.warehouse_sources.sources.gusto._config import GustoSourceConfig
from products.warehouse_sources.sources.gusto.gusto import (
    GUSTO_API_VERSION_2024_04_01,
    GUSTO_API_VERSION_2026_06_15,
    GustoResumeConfig,
    gusto_source,
    validate_credentials as validate_gusto_credentials,
)
from products.warehouse_sources.sources.gusto.settings import ENDPOINTS, GUSTO_ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceFieldSelectConfig,
    SourceFieldSelectConfigOption,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    VersionDeprecation,
    build_endpoint_schemas,
    detect,
)


@SourceRegistry.register
class GustoSource(ResumableSource[GustoSourceConfig, GustoResumeConfig]):
    codebase_detection = detect(
        env=("^GUSTO_", "^GUSTOEMBEDDED_"),
        npm=("@gusto/embedded-api", "@gusto/embedded-react-sdk"),
        pypi=("gusto-embedded",),
        rubygems=("gusto_embedded_client", "gusto_packs", "omniauth-gusto", "rubocop-gusto"),
    )

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs

    supported_versions = (GUSTO_API_VERSION_2024_04_01, GUSTO_API_VERSION_2026_06_15)
    default_version = GUSTO_API_VERSION_2026_06_15
    api_docs_url = "https://docs.gusto.com/embedded-payroll/reference"
    # Gusto's version upgrade guide lists 2024-04-01 as no longer supported since its final sunset on
    # 2026-06-15; calls pinned to it now return 406.
    deprecated_versions = (VersionDeprecation(version=GUSTO_API_VERSION_2024_04_01, sunset_at=date(2026, 6, 15)),)

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.GUSTO

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.GUSTO,
            category=DataWarehouseSourceCategory.HR___RECRUITING,
            label="Gusto",
            releaseStatus=ReleaseStatus.BETA,
            keywords=["payroll", "hris", "zenpayroll"],
            caption="""Pull your Gusto payroll and people data into the PostHog Data warehouse.

Company data is only readable through an authorization-code OAuth grant, so you bring your own Gusto developer app: register it in the [Gusto Developer Portal](https://dev.gusto.com), authorize it against your company as a payroll admin, then enter the app's client ID and secret along with the resulting refresh token.

Gusto rotates refresh tokens, so the token you paste here is exchanged on every sync. If syncs start failing with an authorization error, generate a fresh refresh token and update this connection.""",
            iconPath="/static/services/gusto.png",
            docsUrl="https://posthog.com/docs/cdp/sources/gusto",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldSelectConfig(
                        name="environment",
                        label="Environment",
                        required=True,
                        defaultValue="production",
                        options=[
                            SourceFieldSelectConfigOption(label="Production", value="production"),
                            SourceFieldSelectConfigOption(label="Demo", value="demo"),
                        ],
                    ),
                    SourceFieldInputConfig(
                        name="client_id",
                        label="Client ID",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="client_secret",
                        label="Client secret",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                    SourceFieldInputConfig(
                        name="refresh_token",
                        label="Refresh token",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.gusto.canonical_descriptions import (
            CANONICAL_DESCRIPTIONS,  # noqa: PLC0415
        )

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        # Both the production and demo hosts start `https://api.gusto`, so match on that prefix.
        return {
            "400 Client Error: Bad Request for url: https://api.gusto": "Gusto rejected these OAuth credentials. Refresh tokens rotate on use — generate a new one and reconnect.",
            "401 Client Error: Unauthorized for url: https://api.gusto": "Gusto authentication failed. Check your client ID and secret, then reconnect with a fresh refresh token.",
            "403 Client Error: Forbidden for url: https://api.gusto": "Your Gusto app is not authorized to read this data. Reconnect with a payroll admin authorization for the company.",
            # Gusto returns 406 once a pinned API version reaches end of life; retrying can't recover it.
            "406 Client Error: Not Acceptable for url: https://api.gusto": "This Gusto API version is no longer supported. Reconnect the source to move to the current version.",
        }

    def get_schemas(
        self,
        config: GustoSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Only the money-movement endpoints take a server-side date filter; the people and
        # configuration tables have no updated-since parameter, so they stay full refresh. Those
        # three are merge-only: a payroll is restated as it moves from calculated to processed, so
        # appending would leave duplicate rows behind.
        return build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names, merge_only=tuple(INCREMENTAL_FIELDS))

    def validate_credentials(
        self,
        config: GustoSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        # One OAuth grant covers every company-scoped endpoint, so a single probe validates them all.
        return validate_gusto_credentials(
            environment=config.environment,
            client_id=config.client_id,
            client_secret=config.client_secret,
            refresh_token=config.refresh_token,
            api_version=self.resolve_api_version(api_version),
        )

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[GustoResumeConfig]:
        return ResumableSourceManager[GustoResumeConfig](inputs, GustoResumeConfig)

    def source_for_pipeline(
        self,
        config: GustoSourceConfig,
        resumable_source_manager: ResumableSourceManager[GustoResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        if inputs.schema_name not in GUSTO_ENDPOINTS:
            raise ValueError(f"Unknown Gusto schema '{inputs.schema_name}'")

        return gusto_source(
            environment=config.environment,
            client_id=config.client_id,
            client_secret=config.client_secret,
            refresh_token=config.refresh_token,
            endpoint=inputs.schema_name,
            api_version=self.resolve_api_version(inputs.api_version),
            logger=inputs.logger,
            resumable_source_manager=resumable_source_manager,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value
            if inputs.should_use_incremental_field
            else None,
        )