: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.discourse._config import DiscourseSourceConfig
from products.warehouse_sources.sources.discourse.discourse import (
DiscourseResumeConfig,
discourse_source,
validate_credentials as validate_discourse_credentials,
)
from products.warehouse_sources.sources.discourse.settings import 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 DiscourseSource(ResumableSource[DiscourseSourceConfig, DiscourseResumeConfig]):
codebase_detection = detect(
env=("^DISCOURSE_",),
rubygems=(
"discourse_api",
"discourse-ember-source",
"discourse-emojis",
"discourse-fonts",
"discourse-omniauth-jwt",
"discourse-qunit-rails",
"discourse-seed-fu",
"discourse-systray",
"discourse_ai-tokenizers",
"discourse_dev",
"discourse_dev_assets",
"discourse_image_optim",
"discourse_mail_receiver",
"discourse_math_bundle",
"discourse_theme",
"discourse_voice_assets",
"discourse_zendesk_api",
"email_reply_parser-discourse",
"fastimage_discourse",
"omniauth-browserid-discourse",
"omniauth-github-discourse",
"rails_multisite_discourse",
"rubocop-discourse",
"rubocop-discourse-base",
"ruby-readability-discourse",
"seed-fu-discourse",
"sidekiq-failures-discourse",
"therubyracer-discourse",
),
)
lists_tables_without_credentials = True # static endpoint catalog — safe for public docs
api_docs_url = "https://docs.discourse.org"
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.DISCOURSE
@property
def connection_host_fields(self) -> list[str]:
# `base_url` is where the stored API key is sent, and `api_username` selects the identity an
# All Users key acts as — retargeting either while the key is preserved must re-require the key.
return ["base_url", "api_username"]
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.DISCOURSE,
category=DataWarehouseSourceCategory.COMMUNICATION,
label="Discourse",
releaseStatus=ReleaseStatus.BETA,
keywords=["forum", "community"],
caption="""Connect your Discourse community forum to sync categories, topics, posts, tags, groups, group members, badges, and user activity into the PostHog Data warehouse.
Generate an Admin API key under **Admin > API > Keys** on your Discourse instance (a key scoped to "Global" access, or scoped to read the tables below, both work). The API username must be an admin account, because the admin users and badges tables read admin-only endpoints. The instance URL is your forum's address, e.g. `https://yourforum.discourse.group`.
The user activity table is fetched once per member, so it takes longer to sync than the others.""",
iconPath="/static/services/discourse.png",
docsUrl="https://posthog.com/docs/cdp/sources/discourse",
fields=cast(
list[FieldType],
[
SourceFieldInputConfig(
name="base_url",
label="Instance URL",
type=SourceFieldInputConfigType.TEXT,
required=True,
placeholder="https://yourforum.discourse.group",
secret=False,
),
SourceFieldInputConfig(
name="api_key",
label="Admin API key",
type=SourceFieldInputConfigType.PASSWORD,
required=True,
placeholder="",
secret=True,
),
SourceFieldInputConfig(
name="api_username",
label="API username",
type=SourceFieldInputConfigType.TEXT,
required=True,
placeholder="system",
secret=False,
),
],
),
)
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.discourse.canonical_descriptions import CANONICAL_DESCRIPTIONS
return CANONICAL_DESCRIPTIONS
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
"403 Client Error": "Your Discourse API key or username is invalid, or does not have permission to access this resource. Please check the key and username and reconnect.",
"invalid_access": "Your Discourse API key or username is invalid, or does not have permission to access this resource. Please check the key and username and reconnect.",
}
def get_schemas(
self,
config: DiscourseSourceConfig,
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: DiscourseSourceConfig,
team_id: int,
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
return validate_discourse_credentials(
config.base_url, config.api_key, config.api_username, schema_name, team_id
)
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[DiscourseResumeConfig]:
return ResumableSourceManager[DiscourseResumeConfig](inputs, DiscourseResumeConfig)
def source_for_pipeline(
self,
config: DiscourseSourceConfig,
resumable_source_manager: ResumableSourceManager[DiscourseResumeConfig],
inputs: SourceInputs,
) -> SourceResponse:
if inputs.schema_name not in ENDPOINTS:
raise ValueError(f"Unknown Discourse schema '{inputs.schema_name}'")
return discourse_source(
base_url=config.base_url,
api_key=config.api_key,
api_username=config.api_username,
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,
incremental_field=inputs.incremental_field,
)