: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
fix(warehouse-sources): make github workflow_runs webhook-only (#70634)
Julian Bez7eb6e393c817
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.pyindex 974b5a3fa806..019c3fdc8a49 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.py@@ -98,6 +98,14 @@ class WebhookSourceManager:return has_webhook_function+ async def schema_is_webhook(self) -> bool:+ from products.warehouse_sources.backend.models.external_data_schema import ExternalDataSchema++ schema = await database_sync_to_async_pool(_db_read_with_retry)(+ lambda: ExternalDataSchema.objects.get(id=self._inputs.schema_id, team_id=self._inputs.team_id)+ )+ return bool(schema.is_webhook)+async def _list_webhook_parquet_files(self) -> list[str]:prefix = self._get_webhook_s3_prefix()diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.pyindex f55737d7b40c..1b5db439f2ed 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py@@ -926,6 +926,23 @@ def github_source(if webhook_source_manager is not Noneelse False)+ # Effective webhook-only: the endpoint is webhook-first (zero lookback) AND this schema is+ # actually in webhook sync mode. webhook_enabled already implies schema.is_webhook; otherwise+ # confirm against the DB (only on the poll fallback, so no extra read when webhook is active).+ # Gating on the schema, not just the endpoint config, keeps a legacy poll-mode workflow_runs+ # schema correct on two fronts: it keeps polling (the poll no-op below stays off), and its+ # SourceResponse.webhook_only stays False so a reset still wipes+rebuilds (handle_reset_or_full_refresh+ # only preserves the table for genuinely webhook-only schemas).+ webhook_only_schema = bool(+ webhook_only+ and webhook_source_manager is not None+ and (webhook_enabled or async_to_sync(webhook_source_manager.schema_is_webhook)())+ )+ # A webhook-only schema whose webhook mode is inactive (no function yet, or reset_pipeline forced+ # the poll path) yields nothing rather than crawling the full REST history — otherwise the direct+ # workflow_runs poll ignores the zero-day marker and re-crawls all of /actions/runs. Matches the+ # Slack/Stripe webhook-only sources.+ webhook_only_poll_noop = webhook_only_schema and not webhook_enableddef items() -> Iterator[Any] | AsyncIterator[Any]:if webhook_enabled:@@ -954,6 +971,9 @@ def github_source()return webhook_source_manager.get_items(table_transformer=transformer)+ if webhook_only_poll_noop:+ return iter([])+return get_rows(personal_access_token=personal_access_token,repository=repository,@@ -976,7 +996,7 @@ def github_source(partition_mode="datetime" if endpoint_config.partition_key else None,partition_format="week" if endpoint_config.partition_key else None,partition_keys=[endpoint_config.partition_key] if endpoint_config.partition_key else None,- webhook_only=webhook_only,+ webhook_only=webhook_only_schema,)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/settings.pyindex f32c2919c9f3..357cc1cdd610 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/github/settings.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/settings.py@@ -203,6 +203,13 @@ GITHUB_ENDPOINTS: dict[str, GithubEndpointConfig] = {# workflow_run carries updated_at, which GitHub bumps on every status change — the natural# recency key so a completed run is never frozen by a stale earlier webhook event.version_keys=["updated_at"],+ # Webhook-only marker, like workflow_jobs and reviews: the webhook is the source of truth.+ # initial_lookback_days == 0 makes source.py report this schema as webhook_only, activates+ # webhook mode from the first sync (skips the initial_sync_complete gate in github.py), and+ # makes the poll path yield no rows instead of crawling the full /actions/runs history.+ # Crawling a busy repo's whole run history on connect is huge against a shared, rate-limited+ # budget. History, if wanted, is a deliberate one-off backfill.+ initial_lookback_days=0,),"workflow_jobs": GithubEndpointConfig(name="workflow_jobs",diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/source.pyindex e78a4fbc3c9a..6465169a7da4 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/github/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/source.py@@ -278,11 +278,11 @@ If automatic creation failed, your token needs webhook permissions — the **adm@staticmethoddef _schema_for_endpoint(endpoint: str) -> SourceSchema:webhook_capable = endpoint in GITHUB_WEBHOOK_RESOURCE_MAP- # An endpoint whose poll does no first-sync backfill (initial_lookback_days == 0,- # i.e. workflow_jobs and reviews) can only ever be populated by the webhook: the- # per-parent fan-out is too expensive to backfill at volume. Offer it as webhook-only- # so users can't pick a poll mode that would sync an empty table forever. workflow_runs- # keeps its poll backfill; the webhook just replaces re-polling for it.+ # An endpoint whose poll does no first-sync backfill (initial_lookback_days == 0:+ # workflow_jobs, workflow_runs, reviews) can only ever be populated by the webhook —+ # backfilling the full history is too expensive against a shared, rate-limited budget.+ # Offer it as webhook-only so users can't pick a poll mode that would sync an empty table+ # forever; the webhook replaces both re-polling and the initial history crawl.webhook_only = webhook_capable and GITHUB_ENDPOINTS[endpoint].initial_lookback_days == 0supports_poll = bool(INCREMENTAL_FIELDS.get(endpoint)) and not webhook_onlyreturn SourceSchema(diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_webhook_only.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_webhook_only.pynew file mode 100644index 000000000000..f8e2a453b6f7--- /dev/null+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_webhook_only.py@@ -0,0 +1,70 @@+from collections.abc import Iterable++import pytest+from unittest import mock++from products.warehouse_sources.backend.temporal.data_imports.sources.github import github+++def _no_resume() -> mock.Mock:+ manager = mock.Mock()+ manager.can_resume.return_value = False+ return manager++[email protected]("endpoint", ["workflow_runs", "workflow_jobs", "reviews"])+def test_webhook_only_poll_yields_no_rows_when_webhook_inactive(endpoint: str) -> None:+ webhook_source_manager = mock.Mock()+ webhook_source_manager.webhook_enabled = mock.AsyncMock(return_value=False)+ webhook_source_manager.schema_is_webhook = mock.AsyncMock(return_value=True)++ with mock.patch.object(github, "_fetch_page") as fetch_page:+ response = github.github_source(+ personal_access_token="tok",+ repository="acme/widgets",+ endpoint=endpoint,+ logger=mock.Mock(),+ resumable_source_manager=_no_resume(),+ webhook_source_manager=webhook_source_manager,+ )+ result = response.items()+ # The webhook-only fallback must return a sync, empty iterator — not the async webhook stream.+ assert isinstance(result, Iterable)+ rows = list(result)++ assert rows == []+ # An actual webhook-mode schema is webhook_only, so a reset preserves its table.+ assert response.webhook_only is True+ fetch_page.assert_not_called()+ webhook_source_manager.get_items.assert_not_called()+++def test_poll_mode_workflow_runs_still_polls() -> None:+ # A legacy workflow_runs schema still configured for poll sync (is_webhook False) must keep+ # polling, not get short-circuited to empty — otherwise it silently freezes once workflow_runs+ # becomes webhook-only.+ webhook_source_manager = mock.Mock()+ webhook_source_manager.webhook_enabled = mock.AsyncMock(return_value=False)+ webhook_source_manager.schema_is_webhook = mock.AsyncMock(return_value=False)++ empty_page = mock.Mock()+ empty_page.json.return_value = {"workflow_runs": []}+ empty_page.headers = {}++ with mock.patch.object(github, "_fetch_page", return_value=empty_page) as fetch_page:+ response = github.github_source(+ personal_access_token="tok",+ repository="acme/widgets",+ endpoint="workflow_runs",+ logger=mock.Mock(),+ resumable_source_manager=_no_resume(),+ webhook_source_manager=webhook_source_manager,+ )+ result = response.items()+ assert isinstance(result, Iterable) # poll path is a sync iterator+ list(result)++ fetch_page.assert_called()+ # A legacy poll-mode schema is NOT webhook_only, so a reset still wipes and rebuilds.+ assert response.webhook_only is False+ webhook_source_manager.get_items.assert_not_called()diff --git a/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/github/test_github_source.py b/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/github/test_github_source.pyindex 26969492517d..c77fa6ce81b8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/github/test_github_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/github/test_github_source.py@@ -548,22 +548,23 @@ class TestGithubSourceSortMode:assert response.sort_mode == expected_sort_mode-# A fresh webhook schema must not deadlock: workflow_jobs does no poll backfill-# (initial_lookback_days == 0), so webhook mode has to activate before the zero-row-# poll could mark initial_sync_complete — otherwise the gate never opens and queued-# webhook files never drain. workflow_runs (and the non-webhook endpoints) keep their-# historical poll backfill, so they must NOT skip that gate.+# A fresh webhook schema must not deadlock: the webhook-only endpoints (workflow_jobs,+# workflow_runs, reviews) do no poll backfill (initial_lookback_days == 0), so webhook mode+# has to activate before the zero-row poll could mark initial_sync_complete — otherwise the+# gate never opens and queued webhook files never drain. The non-webhook endpoints (issues,+# commits, ...) keep their historical poll backfill, so they must NOT skip that gate.class TestGithubWebhookActivationGate:def _make_webhook_manager(self, enabled: bool) -> mock.Mock:manager = mock.Mock()manager.webhook_enabled = mock.AsyncMock(return_value=enabled)+ manager.schema_is_webhook = mock.AsyncMock(return_value=True)manager.get_items = mock.Mock(return_value=iter([{"id": 1}]))return manager@parameterized.expand([("workflow_jobs_skips_gate", "workflow_jobs", True),- ("workflow_runs_keeps_gate", "workflow_runs", False),+ ("workflow_runs_skips_gate", "workflow_runs", True),("issues_keeps_gate", "issues", False),])@@ -1424,10 +1425,9 @@ class TestGithubWebhookSource:webhook_capable = {s.name for s in schemas if s.supports_webhooks}assert webhook_capable == {"workflow_jobs", "workflow_runs", "reviews"}- def test_workflow_jobs_is_webhook_only_but_workflow_runs_keeps_poll(self) -> None:- # workflow_jobs does no poll backfill (zero floor), so it must not be offered as a- # poll mode that would sync an empty table forever — it's webhook-only. workflow_runs- # still has a real poll backfill and stays incremental/append-capable.+ def test_workflow_runs_and_jobs_are_webhook_only(self) -> None:+ # workflow_jobs and workflow_runs both do no poll backfill (zero floor), so neither is+ # offered as a poll mode that would sync an empty table forever — both are webhook-only.by_name = {s.name: s for s in self.source.get_schemas(_pat_config(), team_id=1)}jobs = by_name["workflow_jobs"]@@ -1437,8 +1437,9 @@ class TestGithubWebhookSource:assert jobs.supports_webhooks is Trueruns = by_name["workflow_runs"]- assert runs.webhook_only is False- assert runs.supports_incremental is True+ assert runs.webhook_only is True+ assert runs.supports_incremental is False+ assert runs.supports_append is Falseassert runs.supports_webhooks is True@parameterized.expand(