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

from products.warehouse_sources.sources.codefresh._config import CodefreshSourceConfig
from products.warehouse_sources.sources.codefresh.codefresh import (
    ACCOUNT_LOOKUP_FAILED,
    ACCOUNT_LOOKUP_MESSAGE,
    CodefreshResumeConfig,
    codefresh_source,
    validate_credentials as validate_codefresh_credentials,
)
from products.warehouse_sources.sources.codefresh.settings import CODEFRESH_ENDPOINTS, ENDPOINTS, INCREMENTAL_FIELDS
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,
)


@SourceRegistry.register
class CodefreshSource(ResumableSource[CodefreshSourceConfig, CodefreshResumeConfig]):
    codebase_detection = detect(env=("^CODEFRESH_",), npm=("codefresh",), go=("github.com/codefresh-io/go-sdk",))

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    api_docs_url = "https://codefresh.io/docs/docs/integrations/codefresh-api/"

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.CODEFRESH,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="Codefresh",
            releaseStatus=ReleaseStatus.BETA,
            caption="""Enter your Codefresh API key to automatically pull your Codefresh CI/CD data into the PostHog Data warehouse.

You can create an API key in your [Codefresh user settings](https://g.codefresh.io/user/settings). Codefresh API keys are scoped per resource, so grant read access for the resources you want to sync:
- **Project** (projects)
- **Pipeline** (pipelines, triggers)
- **Build** (builds, images)
- **Step Type** (step types)
- **Environments V2** (environments)

The teams and users tables read your account's membership, which Codefresh does not put behind a resource scope.

Only the US SaaS host (`g.codefresh.io`) is supported. EU and self-hosted/on-prem installations are not yet supported.
""",
            iconPath="/static/services/codefresh.png",
            docsUrl="https://posthog.com/docs/cdp/sources/codefresh",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # An invalid or revoked Codefresh 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/query.
            "401 Client Error: Unauthorized for url: https://g.codefresh.io": "Your Codefresh API key is invalid or has been revoked. Create a new key in your Codefresh user settings, then reconnect.",
            "403 Client Error: Forbidden for url: https://g.codefresh.io": "Your Codefresh API key is missing the access scope needed to sync this data. Grant the required resource scopes to the key in your Codefresh user settings, then reconnect.",
            # The users table needs an account id, which is read from the teams the key can see. A
            # key that reaches no team can never resolve one, so retrying cannot help.
            ACCOUNT_LOOKUP_FAILED: ACCOUNT_LOOKUP_MESSAGE,
        }

    def get_schemas(
        self,
        config: CodefreshSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Codefresh exposes no server-side updated-since filter on any list endpoint, so every
        # table is full refresh only (INCREMENTAL_FIELDS is empty per endpoint) — an "incremental"
        # sync would re-page the whole resource anyway.
        return build_endpoint_schemas(
            ENDPOINTS,
            INCREMENTAL_FIELDS,
            names,
            should_sync_default={name: cfg.should_sync_default for name, cfg in CODEFRESH_ENDPOINTS.items()},
        )

    def validate_credentials(
        self,
        config: CodefreshSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        valid, error = validate_codefresh_credentials(config.api_key, schema_name=schema_name)
        if valid:
            return True, None
        return False, error or "Invalid Codefresh API key"

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

    def source_for_pipeline(
        self,
        config: CodefreshSourceConfig,
        resumable_source_manager: ResumableSourceManager[CodefreshResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return codefresh_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,
        )