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

from products.warehouse_sources.sources.heroku._config import HerokuSourceConfig
from products.warehouse_sources.sources.heroku.heroku import (
    HerokuResumeConfig,
    heroku_source,
    validate_credentials as validate_heroku_credentials,
)
from products.warehouse_sources.sources.heroku.settings import ENDPOINTS, HEROKU_ENDPOINTS, INCREMENTAL_FIELDS
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 HerokuSource(ResumableSource[HerokuSourceConfig, HerokuResumeConfig]):
    codebase_detection = detect(
        env=("^HEROKU_",),
        npm=("heroku-client",),
        go=("github.com/heroku/heroku-go", "github.com/heroku/heroku-http-adapter"),
        rubygems=(
            "platform-api",
            "heroku-bouncer",
            "heroku-runtime-api",
            "heroku_hatchet",
            "omniauth-heroku",
            "puma-heroku",
            "rails_on_heroku",
        ),
    )

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    supported_versions = ("3",)  # Accept header: application/vnd.heroku+json; version=3
    default_version = "3"
    api_docs_url = "https://devcenter.heroku.com/articles/platform-api-reference"

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.HEROKU,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="Heroku",
            releaseStatus=ReleaseStatus.GA,
            caption="""Enter your Heroku API key to pull your Heroku platform data (apps, releases, builds, dynos, add-ons, and more) into the PostHog Data warehouse.

You can find your API key in your [Heroku account settings](https://dashboard.heroku.com/account) under **API Key**, or create a long-lived token with the Heroku CLI: `heroku authorizations:create`.
""",
            iconPath="/static/services/heroku.svg",
            docsUrl="https://posthog.com/docs/cdp/sources/heroku",
            keywords=["paas", "deploys", "dynos"],
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="HRKU-...",
                        secret=True,
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # An invalid or revoked Heroku API key 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.
            "401 Client Error: Unauthorized for url: https://api.heroku.com": "Your Heroku API key is invalid or has been revoked. Generate a new key in your Heroku account settings, then reconnect.",
            "403 Client Error: Forbidden for url: https://api.heroku.com": "Your Heroku API key does not have access to this data. Check the token's scope (or your account's access to the resource), then reconnect.",
        }

    def get_schemas(
        self,
        config: HerokuSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        def _description(endpoint: str) -> str | None:
            if endpoint == "dynos":
                return "A point-in-time snapshot of the processes currently running on each app"
            if endpoint == "invoices":
                return "Invoices for your personal Heroku account; team invoices are not included"
            if endpoint == "teams":
                return "Only returns data for accounts that belong to Heroku Teams or Enterprise"
            if endpoint == "team_monthly_usage":
                return "Monthly usage for Heroku Enterprise teams, covering the last 12 months"
            return None

        def _build_schema(endpoint: str) -> SourceSchema:
            # Heroku's API has no server-side updated-since/created-since filters, so every
            # endpoint is full refresh only.
            return SourceSchema(
                name=endpoint,
                supports_incremental=False,
                supports_append=False,
                incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
                should_sync_default=HEROKU_ENDPOINTS[endpoint].should_sync_default,
                description=_description(endpoint),
            )

        schemas = [_build_schema(endpoint) 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: HerokuSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        if validate_heroku_credentials(config.api_key):
            return True, None

        return False, "Invalid Heroku API key"

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

    def source_for_pipeline(
        self,
        config: HerokuSourceConfig,
        resumable_source_manager: ResumableSourceManager[HerokuResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return heroku_source(
            api_key=config.api_key,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
        )