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.

5.8 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.netlify._config import NetlifySourceConfig
from products.warehouse_sources.sources.netlify.netlify import (
    NetlifyResumeConfig,
    netlify_source,
    validate_credentials as validate_netlify_credentials,
)
from products.warehouse_sources.sources.netlify.settings import ENDPOINTS, INCREMENTAL_FIELDS, NETLIFY_ENDPOINTS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    detect,
)


@SourceRegistry.register
class NetlifySource(ResumableSource[NetlifySourceConfig, NetlifyResumeConfig]):
    codebase_detection = detect(
        env=("^NETLIFY_",),
        npm=("@netlify/functions", "netlify", "netlify-cli", "@netlify/blobs"),
        go=(
            "github.com/netlify/open-api",
            "github.com/netlify/netlify-commons",
            "github.com/netlify/netlify-credential-helper",
        ),
    )

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    supported_versions = ("v1",)
    default_version = "v1"
    api_docs_url = "https://open-api.netlify.com/"

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.NETLIFY,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="Netlify",
            releaseStatus=ReleaseStatus.GA,
            caption="""Enter your Netlify personal access token to sync your Netlify data into the PostHog Data warehouse.

Create a personal access token under **User settings > Applications > Personal access tokens** in the [Netlify UI](https://app.netlify.com/user/applications). The token has full access to the resources your account can reach, so no extra scopes are needed.
""",
            iconPath="/static/services/netlify.svg",
            docsUrl="https://posthog.com/docs/cdp/sources/netlify",
            keywords=["hosting", "jamstack", "deploys", "web"],
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_token",
                        label="Personal access token",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="nfp_...",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.netlify.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # An invalid or revoked token surfaces as a requests HTTPError when `_fetch_page` calls
            # `raise_for_status()`. Retrying can never satisfy a credential problem, so stop the sync.
            # Match the stable status text and base host, not the per-request path/query.
            "401 Client Error: Unauthorized for url: https://api.netlify.com": "Your Netlify personal access token is invalid or has been revoked. Netlify invalidates tokens on password reset. Create a new token in your Netlify user settings, then reconnect.",
            "403 Client Error: Forbidden for url: https://api.netlify.com": "Your Netlify personal access token does not have access to this resource. Create a token with the required access, then reconnect.",
        }

    def get_schemas(
        self,
        config: NetlifySourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Netlify exposes no server-side timestamp filter, so every table is full refresh.
        schemas = [
            SourceSchema(
                name=endpoint,
                supports_incremental=False,
                supports_append=False,
                incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
                should_sync_default=NETLIFY_ENDPOINTS[endpoint].should_sync_default,
            )
            for endpoint in ENDPOINTS
        ]
        if names is not None:
            names_set = set(names)
            schemas = [s for s in schemas if s.name in names_set]
        return schemas

    def validate_credentials(
        self,
        config: NetlifySourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_netlify_credentials(config.api_token)

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

    def source_for_pipeline(
        self,
        config: NetlifySourceConfig,
        resumable_source_manager: ResumableSourceManager[NetlifyResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return netlify_source(
            api_token=config.api_token,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
            db_incremental_field_last_value=None,  # every Netlify endpoint is full refresh
        )