: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from typing import Optional, cast
from products.warehouse_sources.sources.deepsource._config import DeepsourceSourceConfig
from products.warehouse_sources.sources.deepsource.deepsource import (
DeepsourceResumeConfig,
deepsource_source,
validate_credentials as validate_deepsource_credentials,
)
from products.warehouse_sources.sources.deepsource.settings import DEEPSOURCE_ENDPOINTS, ENDPOINTS
from products.warehouse_sources.sources.sdk import (
CanonicalDescriptions,
DataWarehouseSourceCategory,
ExternalDataSourceType,
FieldType,
ReleaseStatus,
ResumableSource,
ResumableSourceManager,
SourceConfig,
SourceFieldInputConfig,
SourceFieldInputConfigType,
SourceFieldSelectConfig,
SourceFieldSelectConfigOption,
SourceInputs,
SourceRegistry,
SourceResponse,
SourceSchema,
detect,
)
_ENDPOINT_DESCRIPTIONS: dict[str, str] = {
"repositories": "All repositories in the account, including ones not activated on DeepSource",
"analysis_runs": "One row per analysis run with its status and occurrence summary",
"checks": "One row per analyzer check inside an analysis run, with the issues it introduced and resolved",
"pull_requests": "Pull requests analyzed by DeepSource, with the issues and vulnerabilities each one raised",
"issues": "Currently open issue types per repository with category, severity, and occurrence count",
"issue_occurrences": "Every open occurrence of an issue with its file path and position",
"vulnerability_occurrences": "Dependency vulnerabilities (SCA) with CVSS/EPSS scores per repository",
"analyzers": "Every analyzer DeepSource offers, resolving the shortcodes carried on issues and checks",
"metrics": "Latest value per code-quality metric and language (coverage, duplication, and more)",
"reports": "Current value and status of each compliance and trend report (OWASP Top 10, code health, and more)",
}
@SourceRegistry.register
class DeepsourceSource(ResumableSource[DeepsourceSourceConfig, DeepsourceResumeConfig]):
codebase_detection = detect(env=("^DEEPSOURCE_",))
lists_tables_without_credentials = True # static endpoint catalog — safe for public docs
api_docs_url = "https://docs.deepsource.com/docs/developers/api"
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.DEEPSOURCE
@property
def connection_host_fields(self) -> list[str]:
# The PAT is sent to api.deepsource.com scoped to <account_login>/<vcs_provider>, so
# retargeting the account must force re-entry of the token — otherwise an editor who
# cannot read the PAT could point it at another account the owner can access.
return ["account_login", "vcs_provider"]
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.DEEPSOURCE,
category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
label="DeepSource",
releaseStatus=ReleaseStatus.BETA,
caption="""Import your DeepSource static-analysis data — repositories, analysis runs, issues, code-quality metrics, and reports — into the PostHog Data warehouse.
Generate a personal access token from the **Tokens** tab in your [DeepSource user settings](https://app.deepsource.com/). The token is scoped to your account, so it can read every account and repository you have access to.
The account login is the organization or user name exactly as it appears in DeepSource (for a GitHub organization, this is the organization's login).
""",
iconPath="/static/services/deepsource.png",
docsUrl="https://posthog.com/docs/cdp/sources/deepsource",
fields=cast(
list[FieldType],
[
SourceFieldInputConfig(
name="api_token",
label="Personal access token",
type=SourceFieldInputConfigType.PASSWORD,
required=True,
placeholder="",
secret=True,
),
SourceFieldInputConfig(
name="account_login",
label="Account login",
type=SourceFieldInputConfigType.TEXT,
required=True,
placeholder="my-organization",
secret=False,
),
SourceFieldSelectConfig(
name="vcs_provider",
label="VCS provider",
required=True,
defaultValue="GITHUB",
options=[
SourceFieldSelectConfigOption(label="GitHub", value="GITHUB"),
SourceFieldSelectConfigOption(label="GitLab", value="GITLAB"),
SourceFieldSelectConfigOption(label="Bitbucket", value="BITBUCKET"),
SourceFieldSelectConfigOption(label="GitHub Enterprise", value="GITHUB_ENTERPRISE"),
SourceFieldSelectConfigOption(label="Bitbucket Data Center", value="BITBUCKET_DATACENTER"),
SourceFieldSelectConfigOption(label="Azure DevOps Services", value="ADS"),
SourceFieldSelectConfigOption(label="Google Source Repositories", value="GSR"),
],
),
],
),
)
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.deepsource.canonical_descriptions import CANONICAL_DESCRIPTIONS
return CANONICAL_DESCRIPTIONS
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
# An invalid or revoked PAT surfaces as a 401/403 raised from the GraphQL transport.
# Match the stable status text plus the fixed API host, never the variable detail.
"401 Client Error: Unauthorized for url: https://api.deepsource.com": "Your DeepSource personal access token is invalid or has been revoked. Generate a new token in your DeepSource user settings, then reconnect.",
"403 Client Error: Forbidden for url: https://api.deepsource.com": "Your DeepSource personal access token does not have access to this data. Check the token's account access, then reconnect.",
# A wrong account login / VCS provider resolves the account to null — retrying can
# never fix the configuration.
"DeepSource account not found": "The configured DeepSource account could not be found. Check the account login and VCS provider, and that your token has access to it.",
}
def get_schemas(
self,
config: DeepsourceSourceConfig,
team_id: int,
with_counts: bool = False,
names: list[str] | None = None,
force_refresh: bool = False,
api_version: str | None = None,
) -> list[SourceSchema]:
# No DeepSource connection accepts a server-side timestamp filter (Relay cursor args
# only), so every schema is full refresh — see settings.py.
schemas = [
SourceSchema(
name=endpoint,
supports_incremental=False,
supports_append=False,
incremental_fields=[],
should_sync_default=DEEPSOURCE_ENDPOINTS[endpoint].should_sync_default,
description=_ENDPOINT_DESCRIPTIONS.get(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: DeepsourceSourceConfig,
team_id: int,
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
return validate_deepsource_credentials(config.api_token, config.account_login, config.vcs_provider)
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[DeepsourceResumeConfig]:
return ResumableSourceManager[DeepsourceResumeConfig](inputs, DeepsourceResumeConfig)
def source_for_pipeline(
self,
config: DeepsourceSourceConfig,
resumable_source_manager: ResumableSourceManager[DeepsourceResumeConfig],
inputs: SourceInputs,
) -> SourceResponse:
return deepsource_source(
api_token=config.api_token,
account_login=config.account_login,
vcs_provider=config.vcs_provider,
endpoint=inputs.schema_name,
logger=inputs.logger,
resumable_source_manager=resumable_source_manager,
)