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.

ci: skip Core tests for data-import source PRs (#61409)

Raúl Negrón-Otero294ef37a4c81
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index f5a52ff906cb..f69032b978e1 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -86,6 +86,10 @@ jobs:
product_yamls: ${{ steps.filter.outputs.product_yamls || 'false' }}
product_yamls_files: ${{ steps.filter.outputs.product_yamls_files }}
timing_scripts: ${{ steps.filter.outputs.timing_scripts }}
+ # True only when every backend-relevant changed file is a data
+ # warehouse import source — lets build_django_matrix drop the
+ # Core/CorePOE/compat segments. Defaults false (full matrix).
+ data_import_sources_only: ${{ steps.sources.outputs.data_import_sources_only || 'false' }}
steps:
# For pull requests it's not necessary to checkout the code, but we
# also want this to run on master so we need to checkout
@@ -258,6 +262,44 @@ jobs:
- 'services/mcp/schema/generated-tool-definitions.json'
- 'services/mcp/schema/tool-definitions-all.json'
+ # Detect PRs whose backend changes are confined to data warehouse
+ # import sources that NO Django Core/CorePOE-collected code imports.
+ # Such a PR is exercised only by the Temporal segment (Core/CorePOE
+ # --ignore=posthog/temporal), so those segments add no coverage.
+ # The `coupled` sources are reverse-imported at runtime by Core tests
+ # (e.g. posthog/hogql/test/test_direct_postgres_query.py imports
+ # postgres) — a PR touching them must run the full matrix or it would
+ # silently skip those tests. `coupled` is kept complete by the guard
+ # test posthog/temporal/data_imports/sources/tests/test_ci_core_coupled_sources.py.
+ # Fails OPEN: empty/non-source/coupled file list → false → full matrix.
+ # PR events only; push and merge_group always run everything.
+ - name: Determine if only data warehouse sources changed
+ id: sources
+ if: github.event_name == 'pull_request'
+ env:
+ BACKEND_FILES: ${{ steps.filter.outputs.backend_files }}
+ run: |
+ prefix="posthog/temporal/data_imports/sources/"
+ # Space-separated, double-quoted, single line — parsed by the guard test.
+ coupled="common postgres salesforce stripe"
+ only_sources=false
+ if [ -n "$BACKEND_FILES" ]; then
+ only_sources=true
+ for f in $BACKEND_FILES; do
+ case "$f" in
+ "$prefix"*) ;;
+ *) only_sources=false; break ;;
+ esac
+ rest=${f#"$prefix"} # path within sources/
+ top=${rest%%/*} # vendor dir (or top-level filename)
+ case " $coupled " in
+ *" $top "*) only_sources=false; break ;;
+ esac
+ done
+ fi
+ echo "data_import_sources_only=$only_sources" >> "$GITHUB_OUTPUT"
+ echo "Only data warehouse import sources changed: $only_sources"
+
detect-snapshot-mode:
name: Detect snapshot mode
runs-on: ubuntu-latest
@@ -1484,6 +1526,7 @@ jobs:
OLDEST_SUPPORTED_IMAGE: ${{ needs.get_clickhouse_versions.outputs.oldest_supported_image }}
COMPAT_MATRIX_JSON: ${{ needs.get_clickhouse_versions.outputs.compat_matrix }}
DJANGO_SHARDS_JSON: ${{ needs.turbo-discover.outputs.django_shards }}
+ DATA_IMPORT_SOURCES_ONLY: ${{ needs.changes.outputs.data_import_sources_only }}
run: |
# Auto-shard counts from turbo-discover (Amdahl's law on .test_durations).
# Falls back to hardcoded defaults if turbo-discover failed or data is missing.
@@ -1553,12 +1596,19 @@ jobs:
]
')
- include=$(jq -cn \
- --argjson core "$core" \
- --argjson core_persons_on_events "$core_persons_on_events" \
- --argjson temporal "$temporal" \
- --argjson compat "$compat" \
- '$core + $core_persons_on_events + $temporal + $compat')
+ if [ "$DATA_IMPORT_SOURCES_ONLY" = "true" ]; then
+ # Only data warehouse import sources changed — the Temporal
+ # segment already covers posthog/temporal, so skip Core/CorePOE/compat.
+ echo "Only data warehouse import sources changed — running Temporal segment only"
+ include="$temporal"
+ else
+ include=$(jq -cn \
+ --argjson core "$core" \
+ --argjson core_persons_on_events "$core_persons_on_events" \
+ --argjson temporal "$temporal" \
+ --argjson compat "$compat" \
+ '$core + $core_persons_on_events + $temporal + $compat')
+ fi
echo "include=$include" >> "$GITHUB_OUTPUT"
echo "Django matrix size: $(jq -r 'length' <<< "$include")"
diff --git a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr
index 800a3fff4b5e..4d81dc6a2021 100644
--- a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr
+++ b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr
@@ -1122,7 +1122,7 @@
WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2020-12-29 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,
(SELECT nullIf(nullIf(events.`$session_id`, ''), 'null') AS session_id
FROM events
- WHERE and(equals(events.team_id, 99999), notEmpty(nullIf(nullIf(events.`$session_id`, ''), 'null')), lessOrEquals(events.timestamp, now64(6, 'UTC')), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.person_properties, 'email'), ''), 'null'), '^"|"$', ''), 'bla'), 0), greaterOrEquals(events.timestamp, toDateTime64('2020-12-28 00:00:00.000000', 6, 'UTC')))
+ WHERE and(equals(events.team_id, 99999), notEmpty(nullIf(nullIf(events.`$session_id`, ''), 'null')), lessOrEquals(events.timestamp, now64(6, 'UTC')), equals(events.mat_pp_email, 'bla'), greaterOrEquals(events.timestamp, toDateTime64('2020-12-28 00:00:00.000000', 6, 'UTC')))
GROUP BY nullIf(nullIf(events.`$session_id`, ''), 'null')
HAVING 1
ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESC
diff --git a/posthog/temporal/data_imports/sources/tests/test_ci_core_coupled_sources.py b/posthog/temporal/data_imports/sources/tests/test_ci_core_coupled_sources.py
new file mode 100644
index 000000000000..1f2c933fcf39
--- /dev/null
+++ b/posthog/temporal/data_imports/sources/tests/test_ci_core_coupled_sources.py
@@ -0,0 +1,105 @@
+import re
+import ast
+from pathlib import Path
+
+# Guards the `data_import_sources_only` optimization in .github/workflows/ci-backend.yml.
+# That step trims a sources-only PR to the Django Temporal segment, dropping Core/CorePOE.
+# That is only safe for sources NOT imported by Core/CorePOE-collected code. The workflow
+# excludes the coupled ones via its `coupled` list; this test fails if a Core/CorePOE file
+# gains a runtime import of a source missing from that list — which would otherwise let a
+# sources-only PR silently skip the Core test that exercises it.
+#
+# Deliberately uses stdlib ast over a path walk, NOT the repo's `grimp` dependency: grimp
+# does not descend posthog/temporal/data_imports/ (an implicit namespace package — no
+# __init__.py), so its graph contains zero source modules and the guard would pass blind.
+
+# Only leaf imports (sources.<vendor>...) are counted. A bare
+# `from ...sources import SourceRegistry / load_all_sources` is intentionally NOT flagged:
+# the registry is lazy, so importing it loads no vendor module — only calling
+# load_all_sources() does (at worker boot), which the Temporal registry-load test covers.
+SOURCES_PKG = "posthog.temporal.data_imports.sources."
+
+# Roots collected by the Django Core/CorePOE segments. posthog/temporal is excluded — it is
+# the Temporal segment, which always runs for a sources-only PR.
+_SCAN_ROOTS = ("posthog", "ee", "products/product_analytics")
+
+
+def _repo_root() -> Path:
+ for parent in Path(__file__).resolve().parents:
+ if (parent / ".github" / "workflows" / "ci-backend.yml").exists():
+ return parent
+ raise RuntimeError("repo root not found")
+
+
+def _is_type_checking(test: ast.expr) -> bool:
+ if isinstance(test, ast.Name):
+ return test.id == "TYPE_CHECKING"
+ if isinstance(test, ast.Attribute):
+ return test.attr == "TYPE_CHECKING"
+ return False
+
+
+def _runtime_imported_sources(tree: ast.AST) -> set[str]:
+ found: set[str] = set()
+
+ class Visitor(ast.NodeVisitor):
+ def visit_If(self, node: ast.If) -> None:
+ # Type-only imports don't affect runtime test behavior — skip the
+ # TYPE_CHECKING branch but still scan its else.
+ if _is_type_checking(node.test):
+ for stmt in node.orelse:
+ self.visit(stmt)
+ return
+ self.generic_visit(node)
+
+ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
+ module = node.module or ""
+ if module.startswith(SOURCES_PKG):
+ found.add(module[len(SOURCES_PKG) :].split(".")[0])
+
+ def visit_Import(self, node: ast.Import) -> None:
+ for alias in node.names:
+ if alias.name.startswith(SOURCES_PKG):
+ found.add(alias.name[len(SOURCES_PKG) :].split(".")[0])
+
+ Visitor().visit(tree)
+ return found
+
+
+def _workflow_coupled(root: Path) -> set[str]:
+ text = (root / ".github" / "workflows" / "ci-backend.yml").read_text()
+ match = re.search(r'coupled="([^"]*)"', text)
+ assert match, 'could not find `coupled="..."` in ci-backend.yml sources step'
+ coupled = set(match.group(1).split())
+ assert coupled, "parsed an empty `coupled` list from ci-backend.yml"
+ return coupled
+
+
+def test_core_coupled_sources_are_excluded_from_ci_trim():
+ root = _repo_root()
+ excluded = root / "posthog" / "temporal" # the Temporal segment; always runs
+
+ imported: set[str] = set()
+ for rel in _SCAN_ROOTS:
+ base = root / rel
+ if not base.exists():
+ continue
+ for file in base.rglob("*.py"):
+ if file.is_relative_to(excluded):
+ continue
+ text = file.read_text(errors="ignore")
+ if "data_imports.sources" not in text:
+ continue
+ try:
+ tree = ast.parse(text, filename=str(file))
+ except SyntaxError:
+ continue
+ imported |= _runtime_imported_sources(tree)
+
+ coupled = _workflow_coupled(root)
+ missing = imported - coupled
+ assert not missing, (
+ f"Core/CorePOE code imports warehouse sources {sorted(missing)} not in the CI "
+ f"`coupled` list {sorted(coupled)}. A sources-only PR touching them would skip "
+ f"their Core tests. Add them to `coupled` in .github/workflows/ci-backend.yml."
+ )