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.0 KB
Raw Blame
from typing import Optional, cast

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,
)
from products.warehouse_sources.sources.trello._config import TrelloSourceConfig
from products.warehouse_sources.sources.trello.settings import ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.trello.trello import (
    TrelloResumeConfig,
    trello_source,
    validate_credentials as validate_trello_credentials,
)


@SourceRegistry.register
class TrelloSource(ResumableSource[TrelloSourceConfig, TrelloResumeConfig]):
    codebase_detection = detect(
        env=("^TRELLO_",),
        script_hosts=("p.trellocdn.com",),
        npm=("trello",),
        pypi=("py-trello",),
        rubygems=("ruby-trello",),
    )

    supported_versions = ("1",)
    default_version = "1"
    api_docs_url = "https://developer.atlassian.com/cloud/trello/rest/"

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

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.TRELLO,
            category=DataWarehouseSourceCategory.PRODUCTIVITY,
            label="Trello",
            releaseStatus=ReleaseStatus.GA,
            caption="""Enter your Trello API key and token to sync your boards, cards, lists and more into the PostHog Data warehouse.

Get your API key from [trello.com/power-ups/admin](https://trello.com/power-ups/admin) and generate a token with **read** access for your account.""",
            iconPath="/static/services/trello.png",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="Your Trello API key",
                        secret=True,
                    ),
                    SourceFieldInputConfig(
                        name="api_token",
                        label="API token",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="Your Trello API token",
                        secret=True,
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error": "Invalid Trello API key or token. Please check your credentials and reconnect.",
            "403 Client Error": "Your Trello token does not have the required permissions. Please grant read access and reconnect.",
            "invalid key": "Invalid Trello API key. Please check your credentials and reconnect.",
            "invalid token": "Invalid or expired Trello token. Please generate a new token and reconnect.",
        }

    def get_schemas(
        self,
        config: TrelloSourceConfig,
        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: TrelloSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_trello_credentials(config.api_key, config.api_token)

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

    def source_for_pipeline(
        self,
        config: TrelloSourceConfig,
        resumable_source_manager: ResumableSourceManager[TrelloResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return trello_source(
            api_key=config.api_key,
            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=inputs.db_incremental_field_last_value
            if inputs.should_use_incremental_field
            else None,
        )