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(eng-analytics): keep team reads and quarantines in the shown repo (#115516)

Julian Bezf31dc25d6d43
diff --git a/products/engineering_analytics/backend/facade/api.py b/products/engineering_analytics/backend/facade/api.py
index c085a5de5368..4119149850d8 100644
--- a/products/engineering_analytics/backend/facade/api.py
+++ b/products/engineering_analytics/backend/facade/api.py
@@ -606,10 +606,11 @@ def list_team_ci_health(
limit: int | None = None,
owner_team: str | None = None,
source_id: str | None = None,
+ repo: str | None = None,
user_access_control: "UserAccessControl | None" = None,
) -> TeamCIHealthList:
return logic.build_team_ci_health(
- curated=_authorized_source(team, source_id, user_access_control),
+ curated=_authorized_source(team, source_id, user_access_control, repo=repo),
date_from=date_from,
date_to=date_to,
min_failed_prs=min_failed_prs,
@@ -626,10 +627,11 @@ def get_team_ci_activity(
date_to: str | None = None,
test_limit: int | None = None,
source_id: str | None = None,
+ repo: str | None = None,
user_access_control: "UserAccessControl | None" = None,
) -> TeamCIActivity:
return logic.build_team_ci_activity(
- curated=_authorized_source(team, source_id, user_access_control),
+ curated=_authorized_source(team, source_id, user_access_control, repo=repo),
owner_team=owner_team,
date_from=date_from,
date_to=date_to,
@@ -644,10 +646,11 @@ def get_team_merge_trend(
date_from: str | None = None,
date_to: str | None = None,
source_id: str | None = None,
+ repo: str | None = None,
user_access_control: "UserAccessControl | None" = None,
) -> TeamMergeTrend:
return logic.build_team_merge_trend(
- curated=_authorized_source(team, source_id, user_access_control),
+ curated=_authorized_source(team, source_id, user_access_control, repo=repo),
owner_team=owner_team,
date_from=date_from,
date_to=date_to,
diff --git a/products/engineering_analytics/backend/logic/sources.py b/products/engineering_analytics/backend/logic/sources.py
index 3dbea7b27031..a07593b61c79 100644
--- a/products/engineering_analytics/backend/logic/sources.py
+++ b/products/engineering_analytics/backend/logic/sources.py
@@ -374,10 +374,13 @@ def resolve_trunk_quarantined_tests_source(
# job_inputs is an EncryptedJSONField and can hold any JSON shape.
inputs = source.job_inputs if isinstance(source.job_inputs, dict) else {}
resolved = TrunkQuarantineSource(table=table.name, org_url_slug=inputs.get("org_url_slug") or None)
- source_repo = f"{inputs.get('repo_owner', '')}/{inputs.get('repo_name', '')}"
- if source_repo.lower() == repository.lower():
+ owner, name = inputs.get("repo_owner") or "", inputs.get("repo_name") or ""
+ if f"{owner}/{name}".lower() == repository.lower():
return resolved
- fallback = fallback or resolved
+ # A source that names another repository holds that repository's quarantines, so only a
+ # legacy source with no repository keys may stand in.
+ if not owner and not name:
+ fallback = fallback or resolved
return fallback
diff --git a/products/engineering_analytics/backend/presentation/views/teams.py b/products/engineering_analytics/backend/presentation/views/teams.py
index cb30ee9ca9de..bb78261d783d 100644
--- a/products/engineering_analytics/backend/presentation/views/teams.py
+++ b/products/engineering_analytics/backend/presentation/views/teams.py
@@ -15,6 +15,7 @@ from products.engineering_analytics.backend.presentation.serializers.teams impor
)
from products.engineering_analytics.backend.presentation.views._base import (
_DATE_TO,
+ _REPO,
_SOURCE_ID,
EngineeringAnalyticsViewSetBase,
_bad_request,
@@ -64,6 +65,7 @@ class TeamActionsMixin(EngineeringAnalyticsViewSetBase):
"read a single team's rollup.",
),
_SOURCE_ID,
+ _REPO,
],
responses={
200: TeamCIHealthListSerializer,
@@ -96,10 +98,11 @@ class TeamActionsMixin(EngineeringAnalyticsViewSetBase):
limit=_optional_int_param(request, "limit"),
owner_team=request.query_params.get("owner_team") or None,
source_id=request.query_params.get("source_id") or None,
+ repo=request.query_params.get("repo") or None,
user_access_control=self.user_access_control,
)
except ValueError as exc:
- return _bad_request(exc, fallback="Invalid date, threshold, limit, or source_id")
+ return _bad_request(exc, fallback="Invalid date, threshold, limit, source_id, or repo")
return Response(TeamCIHealthListSerializer(instance=result).data)
@extend_schema(
@@ -131,6 +134,7 @@ class TeamActionsMixin(EngineeringAnalyticsViewSetBase):
description="Maximum number of per-test signal rows to return (1-100). Defaults to 25.",
),
_SOURCE_ID,
+ _REPO,
],
responses={
200: TeamCIActivitySerializer,
@@ -155,10 +159,11 @@ class TeamActionsMixin(EngineeringAnalyticsViewSetBase):
date_to=request.query_params.get("date_to") or None,
test_limit=_optional_int_param(request, "test_limit"),
source_id=request.query_params.get("source_id") or None,
+ repo=request.query_params.get("repo") or None,
user_access_control=self.user_access_control,
)
except ValueError as exc:
- return _bad_request(exc, fallback="Invalid owner_team, date, test_limit, or source_id")
+ return _bad_request(exc, fallback="Invalid owner_team, date, test_limit, source_id, or repo")
return Response(TeamCIActivitySerializer(instance=result).data)
@extend_schema(
@@ -183,6 +188,7 @@ class TeamActionsMixin(EngineeringAnalyticsViewSetBase):
),
_DATE_TO,
_SOURCE_ID,
+ _REPO,
],
responses={
200: TeamMergeTrendSerializer,
@@ -207,8 +213,9 @@ class TeamActionsMixin(EngineeringAnalyticsViewSetBase):
date_from=request.query_params.get("date_from") or None,
date_to=request.query_params.get("date_to") or None,
source_id=request.query_params.get("source_id") or None,
+ repo=request.query_params.get("repo") or None,
user_access_control=self.user_access_control,
)
except ValueError as exc:
- return _bad_request(exc, fallback="Invalid owner_team, date, or source_id")
+ return _bad_request(exc, fallback="Invalid owner_team, date, source_id, or repo")
return Response(TeamMergeTrendSerializer(instance=result).data)
diff --git a/products/engineering_analytics/backend/tests/_github_fixtures.py b/products/engineering_analytics/backend/tests/_github_fixtures.py
index 0f8e69a10b10..b1cbcdacb03b 100644
--- a/products/engineering_analytics/backend/tests/_github_fixtures.py
+++ b/products/engineering_analytics/backend/tests/_github_fixtures.py
@@ -66,7 +66,11 @@ def create_github_source(
def create_trunk_source(
- team: Team, *, prefix: str = "trunkprefix_", source_id: str = "trunk-source"
+ team: Team,
+ *,
+ prefix: str = "trunkprefix_",
+ source_id: str = "trunk-source",
+ job_inputs: dict[str, Any] | None = None,
) -> ExternalDataSource:
return ExternalDataSource.objects.create(
team=team,
@@ -75,7 +79,7 @@ def create_trunk_source(
status=ExternalDataSource.Status.COMPLETED,
source_type=ExternalDataSourceType.TRUNKIO,
prefix=prefix,
- job_inputs={},
+ job_inputs=job_inputs or {},
)
diff --git a/products/engineering_analytics/backend/tests/test_views.py b/products/engineering_analytics/backend/tests/test_views.py
index baa33cfe9fc5..377191df5f0c 100644
--- a/products/engineering_analytics/backend/tests/test_views.py
+++ b/products/engineering_analytics/backend/tests/test_views.py
@@ -3,6 +3,8 @@ from typing import Any
from posthog.test.base import BaseTest, ClickhouseTestMixin
+from parameterized import parameterized
+
from posthog.hogql.query import execute_hogql_query
from posthog.constants import AvailableFeature
@@ -11,8 +13,10 @@ from products.access_control.backend.facade.user_access_control import UserAcces
from products.access_control.backend.models.access_control import AccessControl
from products.engineering_analytics.backend.logic.sources import (
TRUNK_MERGE_QUEUE_SCHEMA,
+ TRUNK_QUARANTINED_TESTS_SCHEMA,
list_github_sources,
resolve_trunk_merge_queue_table,
+ resolve_trunk_quarantined_tests_source,
)
from products.engineering_analytics.backend.logic.views import (
ci_job_history,
@@ -108,6 +112,24 @@ class TestListGithubSourcesAccessControl(BaseTest):
AccessControl.objects.create(team=self.team, resource="external_data_source", access_level="none")
assert resolve_trunk_merge_queue_table(self.team, UserAccessControl(user=self.user, team=self.team)) is None
+ @parameterized.expand(
+ [
+ ("same_repo", {"repo_owner": "PostHog", "repo_name": "posthog"}, True),
+ ("legacy_without_repo_keys", {}, True),
+ ("other_repo", {"repo_owner": "PostHog", "repo_name": "posthog-js"}, False),
+ ]
+ )
+ def test_trunk_quarantine_resolver_never_serves_another_repos_source(
+ self, _name: str, job_inputs: dict[str, str], resolves: bool
+ ) -> None:
+ source = create_trunk_source(self.team, job_inputs=job_inputs)
+ table = create_warehouse_table_row(self.team, name="trunkprefix_quarantined_tests", source=source)
+ link_schema(self.team, source, name=TRUNK_QUARANTINED_TESTS_SCHEMA, table=table)
+
+ resolved = resolve_trunk_quarantined_tests_source(self.team, "posthog/posthog")
+
+ assert (resolved.table if resolved else None) == (table.name if resolves else None)
+
class TestEngineeringAnalyticsViews(ClickhouseTestMixin, BaseTest):
"""The curated query builders, exercised as inline subqueries over real
diff --git a/products/engineering_analytics/frontend/generated/api.schemas.ts b/products/engineering_analytics/frontend/generated/api.schemas.ts
index 4b5d83dbec8d..0873e7b3a054 100644
--- a/products/engineering_analytics/frontend/generated/api.schemas.ts
+++ b/products/engineering_analytics/frontend/generated/api.schemas.ts
@@ -3282,6 +3282,10 @@ export type EngineeringAnalyticsTeamCiActivityParams = {
* Owning team slug to scope to (as returned by team_ci_health), e.g. 'team-replay', or the literal 'unowned' for tests with no ownership stamp.
*/
owner_team: string
+ /**
+ * 'owner/name' repository to scope to when the selected source syncs several repositories (from the `sources` list). Defaults to the source's first repository.
+ */
+ repo?: string
/**
* Connected GitHub data warehouse source to read from. Defaults to the oldest connected GitHub source when the team has more than one.
*/
@@ -3313,6 +3317,10 @@ export type EngineeringAnalyticsTeamCiHealthParams = {
* Restrict the roster to one owning team slug (or 'unowned'). The cheap way to read a single team's rollup.
*/
owner_team?: string
+ /**
+ * 'owner/name' repository to scope to when the selected source syncs several repositories (from the `sources` list). Defaults to the source's first repository.
+ */
+ repo?: string
/**
* Connected GitHub data warehouse source to read from. Defaults to the oldest connected GitHub source when the team has more than one.
*/
@@ -3332,6 +3340,10 @@ export type EngineeringAnalyticsTeamMergeTrendParams = {
* Team slug to scope to (as returned by team_ci_health), matched against the GitHub org team slug of the source's team_members snapshot. The literal 'unowned' names an ownership gap, not an org team, and has no merge trend.
*/
owner_team: string
+ /**
+ * 'owner/name' repository to scope to when the selected source syncs several repositories (from the `sources` list). Defaults to the source's first repository.
+ */
+ repo?: string
/**
* Connected GitHub data warehouse source to read from. Defaults to the oldest connected GitHub source when the team has more than one.
*/
diff --git a/products/engineering_analytics/mcp/tools.yaml b/products/engineering_analytics/mcp/tools.yaml
index eb5ae749e951..f63795b689e6 100644
--- a/products/engineering_analytics/mcp/tools.yaml
+++ b/products/engineering_analytics/mcp/tools.yaml
@@ -171,14 +171,14 @@ tools:
each with its source id, repo (owner/name), table prefix, and a synced flag. A source can sync several
repositories, so entries can share an id and differ only by repo. Call this first when a team may have more
than one source or repo, then pass a chosen entry's id as source_id AND its repo as repo to the
- pull-requests, workflow-health, ci-cards, or pr-lifecycle tools to read that specific repo: omitting repo on
- a multi-repo source reads the source's default repo, not the one you picked. With a single source/repo you
- can skip it: the other tools default to the oldest connected source. The prefix also names the underlying
- warehouse tables, which you can query directly with SQL or save as SQL insights and subscriptions when the
- named tools don't fit. The source's original repo uses bare names (prefix + github_pull_requests /
- github_workflow_runs / github_workflow_jobs); every other repo of a multi-repo source is repo-qualified: the
- name `owner/repo.endpoint` is flattened for the table (each '/' becomes '_' and each '.' becomes '__',
- lower-cased), e.g. posthog/posthog.com's pull_requests is prefix +
+ pull-requests, workflow-health, ci-cards, pr-lifecycle, or team-ci-health tools to read that specific repo:
+ omitting repo on a multi-repo source reads the source's default repo, not the one you picked. With a single
+ source/repo you can skip it: the other tools default to the oldest connected source. The prefix also names
+ the underlying warehouse tables, which you can query directly with SQL or save as SQL insights and
+ subscriptions when the named tools don't fit. The source's original repo uses bare names (prefix +
+ github_pull_requests / github_workflow_runs / github_workflow_jobs); every other repo of a multi-repo source
+ is repo-qualified: the name `owner/repo.endpoint` is flattened for the table (each '/' becomes '_' and each
+ '.' becomes '__', lower-cased), e.g. posthog/posthog.com's pull_requests is prefix +
github_posthog_posthog__com__pull_requests: so pick the table for the repo you mean (timestamps land as
strings; parse with parseDateTimeBestEffort).
feature_flag: engineering-analytics
diff --git a/products/engineering_analytics/skills/diagnosing-ci-and-merge-bottlenecks/SKILL.md b/products/engineering_analytics/skills/diagnosing-ci-and-merge-bottlenecks/SKILL.md
index 528f5fcdef73..e0d6fff995d1 100644
--- a/products/engineering_analytics/skills/diagnosing-ci-and-merge-bottlenecks/SKILL.md
+++ b/products/engineering_analytics/skills/diagnosing-ci-and-merge-bottlenecks/SKILL.md
@@ -63,7 +63,7 @@ to take one failing test or red run to a verdict, switch to the `investigating-c
- **`engineering-analytics-sources`**: the team's connected GitHub sources and repos. With more than one of
either, call it first and pass the chosen entry's `source_id` **and** `repo` to `pull-requests`,
- `workflow-health`, and `pr-lifecycle`. Passing only `source_id` reads that source's default repo, not the one
+ `workflow-health`, `pr-lifecycle`, and `team-ci-health`. Passing only `source_id` reads that source's default repo, not the one
you picked. With a single source and repo the tools default to it.
There is no aggregate time-to-merge tool and no "counts" tool — derive those from `pull-requests` (the stuck/failing
diff --git a/services/mcp/schema/generated-tool-definitions.json b/services/mcp/schema/generated-tool-definitions.json
index 7735da8b2b50..68fb95b72ce6 100644
--- a/services/mcp/schema/generated-tool-definitions.json
+++ b/services/mcp/schema/generated-tool-definitions.json
@@ -4402,7 +4402,7 @@
"feature_flag": "engineering-analytics"
},
"engineering-analytics-sources": {
- "description": "The team's selectable GitHub repositories, oldest source first: one entry per repository a source syncs, each with its source id, repo (owner/name), table prefix, and a synced flag. A source can sync several repositories, so entries can share an id and differ only by repo. Call this first when a team may have more than one source or repo, then pass a chosen entry's id as source_id AND its repo as repo to the pull-requests, workflow-health, ci-cards, or pr-lifecycle tools to read that specific repo: omitting repo on a multi-repo source reads the source's default repo, not the one you picked. With a single source/repo you can skip it: the other tools default to the oldest connected source. The prefix also names the underlying warehouse tables, which you can query directly with SQL or save as SQL insights and subscriptions when the named tools don't fit. The source's original repo uses bare names (prefix + github_pull_requests / github_workflow_runs / github_workflow_jobs); every other repo of a multi-repo source is repo-qualified: the name `owner/repo.endpoint` is flattened for the table (each '/' becomes '_' and each '.' becomes '__', lower-cased), e.g. posthog/posthog.com's pull_requests is prefix + github_posthog_posthog__com__pull_requests: so pick the table for the repo you mean (timestamps land as strings; parse with parseDateTimeBestEffort).",
+ "description": "The team's selectable GitHub repositories, oldest source first: one entry per repository a source syncs, each with its source id, repo (owner/name), table prefix, and a synced flag. A source can sync several repositories, so entries can share an id and differ only by repo. Call this first when a team may have more than one source or repo, then pass a chosen entry's id as source_id AND its repo as repo to the pull-requests, workflow-health, ci-cards, pr-lifecycle, or team-ci-health tools to read that specific repo: omitting repo on a multi-repo source reads the source's default repo, not the one you picked. With a single source/repo you can skip it: the other tools default to the oldest connected source. The prefix also names the underlying warehouse tables, which you can query directly with SQL or save as SQL insights and subscriptions when the named tools don't fit. The source's original repo uses bare names (prefix + github_pull_requests / github_workflow_runs / github_workflow_jobs); every other repo of a multi-repo source is repo-qualified: the name `owner/repo.endpoint` is flattened for the table (each '/' becomes '_' and each '.' becomes '__', lower-cased), e.g. posthog/posthog.com's pull_requests is prefix + github_posthog_posthog__com__pull_requests: so pick the table for the repo you mean (timestamps land as strings; parse with parseDateTimeBestEffort).",
"category": "Engineering analytics",
"feature": "engineering_analytics",
"summary": "List connected GitHub sources",
diff --git a/services/mcp/schema/tool-definitions-all.json b/services/mcp/schema/tool-definitions-all.json
index 1ff8f8300bc9..0dbb9aac1271 100644
--- a/services/mcp/schema/tool-definitions-all.json
+++ b/services/mcp/schema/tool-definitions-all.json
@@ -4431,7 +4431,7 @@
"feature_flag": "engineering-analytics"
},
"engineering-analytics-sources": {
- "description": "The team's selectable GitHub repositories, oldest source first: one entry per repository a source syncs, each with its source id, repo (owner/name), table prefix, and a synced flag. A source can sync several repositories, so entries can share an id and differ only by repo. Call this first when a team may have more than one source or repo, then pass a chosen entry's id as source_id AND its repo as repo to the pull-requests, workflow-health, ci-cards, or pr-lifecycle tools to read that specific repo: omitting repo on a multi-repo source reads the source's default repo, not the one you picked. With a single source/repo you can skip it: the other tools default to the oldest connected source. The prefix also names the underlying warehouse tables, which you can query directly with SQL or save as SQL insights and subscriptions when the named tools don't fit. The source's original repo uses bare names (prefix + github_pull_requests / github_workflow_runs / github_workflow_jobs); every other repo of a multi-repo source is repo-qualified: the name `owner/repo.endpoint` is flattened for the table (each '/' becomes '_' and each '.' becomes '__', lower-cased), e.g. posthog/posthog.com's pull_requests is prefix + github_posthog_posthog__com__pull_requests: so pick the table for the repo you mean (timestamps land as strings; parse with parseDateTimeBestEffort).",
+ "description": "The team's selectable GitHub repositories, oldest source first: one entry per repository a source syncs, each with its source id, repo (owner/name), table prefix, and a synced flag. A source can sync several repositories, so entries can share an id and differ only by repo. Call this first when a team may have more than one source or repo, then pass a chosen entry's id as source_id AND its repo as repo to the pull-requests, workflow-health, ci-cards, pr-lifecycle, or team-ci-health tools to read that specific repo: omitting repo on a multi-repo source reads the source's default repo, not the one you picked. With a single source/repo you can skip it: the other tools default to the oldest connected source. The prefix also names the underlying warehouse tables, which you can query directly with SQL or save as SQL insights and subscriptions when the named tools don't fit. The source's original repo uses bare names (prefix + github_pull_requests / github_workflow_runs / github_workflow_jobs); every other repo of a multi-repo source is repo-qualified: the name `owner/repo.endpoint` is flattened for the table (each '/' becomes '_' and each '.' becomes '__', lower-cased), e.g. posthog/posthog.com's pull_requests is prefix + github_posthog_posthog__com__pull_requests: so pick the table for the repo you mean (timestamps land as strings; parse with parseDateTimeBestEffort).",
"category": "Engineering analytics",
"feature": "engineering_analytics",
"summary": "List connected GitHub sources",
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 1ba82f5cb27e..7ec275887e6f 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -122538,6 +122538,10 @@ export namespace Schemas {
* Owning team slug to scope to (as returned by team_ci_health), e.g. 'team-replay', or the literal 'unowned' for tests with no ownership stamp.
*/
owner_team: string;
+ /**
+ * 'owner/name' repository to scope to when the selected source syncs several repositories (from the `sources` list). Defaults to the source's first repository.
+ */
+ repo?: string;
/**
* Connected GitHub data warehouse source to read from. Defaults to the oldest connected GitHub source when the team has more than one.
*/
@@ -122569,6 +122573,10 @@ export namespace Schemas {
* Restrict the roster to one owning team slug (or 'unowned'). The cheap way to read a single team's rollup.
*/
owner_team?: string;
+ /**
+ * 'owner/name' repository to scope to when the selected source syncs several repositories (from the `sources` list). Defaults to the source's first repository.
+ */
+ repo?: string;
/**
* Connected GitHub data warehouse source to read from. Defaults to the oldest connected GitHub source when the team has more than one.
*/
@@ -122588,6 +122596,10 @@ export namespace Schemas {
* Team slug to scope to (as returned by team_ci_health), matched against the GitHub org team slug of the source's team_members snapshot. The literal 'unowned' names an ownership gap, not an org team, and has no merge trend.
*/
owner_team: string;
+ /**
+ * 'owner/name' repository to scope to when the selected source syncs several repositories (from the `sources` list). Defaults to the source's first repository.
+ */
+ repo?: string;
/**
* Connected GitHub data warehouse source to read from. Defaults to the oldest connected GitHub source when the team has more than one.
*/
diff --git a/services/mcp/src/generated/engineering_analytics/api.ts b/services/mcp/src/generated/engineering_analytics/api.ts
index 170c10bcab3d..52b55899c63d 100644
--- a/services/mcp/src/generated/engineering_analytics/api.ts
+++ b/services/mcp/src/generated/engineering_analytics/api.ts
@@ -262,6 +262,12 @@ export const EngineeringAnalyticsTeamCiHealthQueryParams = () => zod.object({
.describe(
"Restrict the roster to one owning team slug (or 'unowned'). The cheap way to read a single team's rollup."
),
+ repo: zod
+ .string()
+ .optional()
+ .describe(
+ "'owner\/name' repository to scope to when the selected source syncs several repositories (from the `sources` list). Defaults to the source's first repository."
+ ),
source_id: zod
.string()
.optional()
diff --git a/services/mcp/src/tools/generated/engineering_analytics.ts b/services/mcp/src/tools/generated/engineering_analytics.ts
index 091a25a6fb1c..fd535498bd83 100644
--- a/services/mcp/src/tools/generated/engineering_analytics.ts
+++ b/services/mcp/src/tools/generated/engineering_analytics.ts
@@ -180,6 +180,7 @@ const engineeringAnalyticsTeamCiHealth = (): ToolBase<
limit: params.limit,
min_failed_prs: params.min_failed_prs,
owner_team: params.owner_team,
+ repo: params.repo,
source_id: params.source_id,
},
})