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.

6.2 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.productboard._config import ProductboardSourceConfig
from products.warehouse_sources.sources.productboard.productboard import (
    ProductboardResumeConfig,
    productboard_source,
    validate_credentials as validate_productboard_credentials,
)
from products.warehouse_sources.sources.productboard.settings import (
    ENDPOINTS,
    FANOUT_PARENT_ENDPOINTS,
    INCREMENTAL_FIELDS,
    PRODUCTBOARD_ENDPOINTS,
)
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
    detect,
)


def _probe_path(schema_name: Optional[str]) -> str:
    """Path used to validate credentials. With a schema, probe its endpoint to confirm
    scope; otherwise hit a cheap workspace-scoped endpoint to confirm the token."""
    if schema_name is None:
        return "/members"

    config = PRODUCTBOARD_ENDPOINTS[schema_name]
    if config.fanout is not None:
        # Child paths need a parent id; the parent listing needs the same scope.
        return {**PRODUCTBOARD_ENDPOINTS, **FANOUT_PARENT_ENDPOINTS}[config.fanout.parent_name].path
    if config.entity_type:
        return f"/entities?type[]={config.entity_type}"
    return config.path


@SourceRegistry.register
class ProductboardSource(ResumableSource[ProductboardSourceConfig, ProductboardResumeConfig]):
    codebase_detection = detect(env=("^PRODUCTBOARD_",))

    supported_versions = ("v2",)
    default_version = "v2"
    api_docs_url = "https://developer.productboard.com"

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

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

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url: https://api.productboard.com": "Your Productboard access token is invalid or expired. Please generate a new token and reconnect.",
            "403 Client Error: Forbidden for url: https://api.productboard.com": "Your Productboard access token is missing the required scope for this resource. Please grant access and reconnect.",
        }

    def get_schemas(
        self,
        config: ProductboardSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        return build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names)

    def validate_credentials(
        self,
        config: ProductboardSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        ok, status = validate_productboard_credentials(config.access_token, _probe_path(schema_name))
        if ok:
            return True, None

        if status == 401:
            return False, "Invalid Productboard access token"

        # A valid token may legitimately lack scope for endpoints the user isn't syncing,
        # so accept 403 at source-create. Re-raise it only when validating a specific schema.
        if status == 403:
            if schema_name is None:
                return True, None
            return False, "Your access token is missing the required scope for this resource"

        return False, "Failed to validate Productboard credentials"

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

    def source_for_pipeline(
        self,
        config: ProductboardSourceConfig,
        resumable_source_manager: ResumableSourceManager[ProductboardResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return productboard_source(
            access_token=config.access_token,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
            should_use_incremental_field=inputs.should_use_incremental_field,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value
            if inputs.should_use_incremental_field
            else None,
            incremental_field=inputs.incremental_field,
        )

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.PRODUCTBOARD,
            category=DataWarehouseSourceCategory.PRODUCTIVITY,
            label="Productboard",
            caption="""Enter your Productboard public API access token to sync your Productboard data into the PostHog Data warehouse.

You can create an access token in your Productboard [workspace settings](https://app.productboard.com/) under **Integrations → Public API**. A Pro plan or higher is required.

Grant read access for the resources you want to sync — for example `entities:read`, `notes:read`, `members:read`, and `teams:read`.""",
            iconPath="/static/services/productboard.png",
            docsUrl="https://posthog.com/docs/cdp/sources/productboard",
            releaseStatus=ReleaseStatus.GA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="access_token",
                        label="Access token",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )