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.

fix(data-warehouse): keep the completed event on a github webhook timestamp tie (#107539)

Julian Bez5364eefad999
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.py
index 9d6f646eab57..d01434404635 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
@@ -1732,6 +1732,10 @@ def _normalize_primary_key(primary_key: str | list[str]) -> list[str]:
return [primary_key] if isinstance(primary_key, str) else list(primary_key)
+# Lifecycle order of the status field on workflow runs, workflow jobs and check runs.
+_STATUS_STAGE = {"requested": 1, "waiting": 1, "pending": 1, "queued": 1, "in_progress": 2, "completed": 3}
+
+
def _make_webhook_dedupe_transformer(primary_key: str, version_keys: list[str]) -> Callable[[pa.Table], pa.Table]:
"""Collapse a webhook batch to one row per ``primary_key`` — the one ranking newest by
``version_keys`` (newest first, NULLs last). GitHub emits a single run/job as separate
@@ -1746,23 +1750,29 @@ def _make_webhook_dedupe_transformer(primary_key: str, version_keys: list[str])
ids = table.column(primary_key).to_pylist()
version_columns = [table.column(key).to_pylist() for key in present_version_keys]
+ statuses = table.column("status").to_pylist() if "status" in table.column_names else None
def rank(row_index: int) -> tuple[tuple[int, Any], ...]:
# A present value beats NULL (NULLS LAST); among present values a larger one is newer
# (ISO-8601 timestamps compare correctly as strings). The leading flag keeps NULLs from
# ever being order-compared against a real value.
- return tuple(
+ version = tuple(
(1, column[row_index]) if column[row_index] is not None else (0, "") for column in version_columns
)
+ if statuses is None:
+ return version
+ # GitHub timestamps are second-coarse, so a run that GitHub skips at once sends its
+ # in_progress and completed events with the same updated_at. GitHub does not deliver
+ # webhooks in order, so the stale in_progress event can arrive last. On a timestamp tie,
+ # the further lifecycle stage wins. Otherwise the row stays in_progress forever.
+ return (*version, (_STATUS_STAGE.get(statuses[row_index] or "", 0), ""))
best_index_by_id: dict[Any, int] = {}
for index, object_id in enumerate(ids):
if object_id is None:
continue
best = best_index_by_id.get(object_id)
- # On a tie (>=, not >) the later-arriving row wins. GitHub timestamps are second-coarse,
- # so a fast in_progress -> completed transition can share an updated_at; rows arrive in
- # chronological order (files read oldest-first), so the later index is the newer event.
+ # On a full tie (>=, not >) the later-arriving row wins, because files are read oldest-first.
if best is None or rank(index) >= rank(best):
best_index_by_id[object_id] = index
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.py
index 130d7e4910ac..b7e277cf6c3f 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
@@ -252,8 +252,10 @@ GITHUB_ENDPOINTS: dict[str, GithubEndpointConfig] = {
sort_mode="desc", # API always returns newest-first; sort/direction are ignored
response_data_path="workflow_runs",
# 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"],
+ # recency key so a completed run is never frozen by a stale earlier webhook event. A re-run
+ # keeps the run id and raises run_attempt, which ranks first: the new attempt's first event
+ # can share its second with the previous attempt's completion, and must still win.
+ version_keys=["run_attempt", "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
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_webhook_dedupe.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_webhook_dedupe.py
index 82db6fae2643..c1325d67f0f1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_webhook_dedupe.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_webhook_dedupe.py
@@ -73,13 +73,20 @@ def test_drops_rows_with_null_id() -> None:
assert rows == []
-def test_equal_version_keys_keep_later_arrival() -> None:
- # GitHub's second-coarse updated_at can be identical for a fast in_progress -> completed
- # transition; the later-arriving completed event must win, so a strict > would freeze the row.
+ "arrival_order",
+ [
+ ["in_progress", "completed"],
+ # Webhooks arrive out of order: a skipped run's in_progress event can land after its completed one.
+ ["completed", "in_progress"],
+ ["completed", "queued"],
+ ],
+)
+def test_equal_version_keys_keep_furthest_status(arrival_order: list[str]) -> None:
table = pa.table(
{
"id": [1, 1],
- "status": ["in_progress", "completed"],
+ "status": arrival_order,
"updated_at": ["2026-01-01T00:00:00Z", "2026-01-01T00:00:00Z"],
}
)
@@ -88,8 +95,21 @@ def test_equal_version_keys_keep_later_arrival() -> None:
assert out.column("status")[0].as_py() == "completed"
+def test_rerun_attempt_outranks_the_previous_attempts_completion() -> None:
+ table = pa.table(
+ {
+ "id": [1, 1],
+ "status": ["completed", "queued"],
+ "run_attempt": [1, 2],
+ "updated_at": ["2026-01-01T00:00:00Z", "2026-01-01T00:00:00Z"],
+ }
+ )
+ out = _make_webhook_dedupe_transformer("id", GITHUB_ENDPOINTS["workflow_runs"].version_keys or [])(table)
+ assert out.num_rows == 1
+ assert out.column("status")[0].as_py() == "queued"
+
+
def test_single_version_key_keeps_max() -> None:
- # workflow_runs ranks on updated_at alone.
table = pa.table(
{
"id": [1, 1],