: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
chore(devex): flag github consumers that pick a project by installation order (#114308)
Julian Bez8e9efe98ec3d
diff --git a/.agents/skills/adding-inbound-webhooks/SKILL.md b/.agents/skills/adding-inbound-webhooks/SKILL.mdindex c06bbd83e195..b40d815569d2 100644--- a/.agents/skills/adding-inbound-webhooks/SKILL.md+++ b/.agents/skills/adding-inbound-webhooks/SKILL.md@@ -50,6 +50,7 @@ Rules that decide whether this works:- A handler that reads the database wraps the read in `bounded_statement_timeout(ms, models=...)` from `posthog.ingress.dispatch.database`, passing only the models the read actually uses. Opening an alias is itself unbounded, so naming one the read never touches can stall the delivery on connection setup.- An import-linter contract (`webhook consumers must only import facade`) holds the module to its own product's `facade/`. Reach product internals through the facade. `hogli product:lint` holds the same rule by AST for every product that has a `webhook_consumers.py`, including relative imports and products with no contract yet.- A consumer whose resources are split across regions declares `ownership=`, pointing at a facade function that returns a `DeliveryOwnership`. Ingress forwards the signed request when the answer is `ELSEWHERE`, and dispatches locally either way. The lookup runs inside the request, so bound it with `bounded_statement_timeout(ms, models=...)`. Let a transient error out of the lookup rather than answering `LOCAL` or `UNDECIDED` through it: a lookup that raises asks a provider with `retry_status` for the delivery again, and a guessed answer receipts a delivery the other region never sees.+- A GitHub consumer cannot get its project from `installation.id`, because one installation can link to several projects. Read [Picking the project](../../../posthog/ingress/github/README.md#picking-the-project) for the two allowed shapes.Tests: extend the product's existing webhook test module rather than starting a parallel one.`products/stamphog/backend/tests/test_webhook_consumers.py` is the shape: drive the real view with a signed `RequestFactory` request and assert the enqueue, plus the event type the app does not register, the bad signature, the unparseable body, the non-POST, and the missing secret.diff --git a/.semgrep/rules/devex/github-installation-first-team.py b/.semgrep/rules/devex/github-installation-first-team.pynew file mode 100644index 000000000000..7ccaa52e4211--- /dev/null+++ b/.semgrep/rules/devex/github-installation-first-team.py@@ -0,0 +1,103 @@+# Test cases for github-installation-first-team.+# ruff: noqa+++def first_candidate(payload):+ # ruleid: github-installation-first-team+ return installation_team_ids(payload)[0]+++def first_candidate_through_variable(payload):+ team_ids = installation_team_ids(payload)+ if not team_ids:+ return None+ # ruleid: github-installation-first-team+ return Team.objects.filter(pk=team_ids[0]).first()+++def first_candidate_through_next(payload):+ team_ids = installation_team_ids(payload)+ # ruleid: github-installation-first-team+ return next(iter(team_ids), None)+++def resolve_through_helper(payload):+ # ruleid: github-installation-first-team+ return _resolve_external_team(payload)+++def first_integration(installation_id):+ # ruleid: github-installation-first-team+ return Integration.objects.filter(kind="github", integration_id=installation_id).order_by("id").first()+++def first_integration_through_using(installation_id):+ # ruleid: github-installation-first-team+ return Integration.objects.using("default").filter(kind="github", integration_id=installation_id).first()+++def first_integration_installation_before_kind(installation_id):+ # ruleid: github-installation-first-team+ return Integration.objects.filter(integration_id=installation_id, kind=Integration.IntegrationKind.GITHUB).first()+++def first_slack_integration(slack_team_id):+ # ok: github-installation-first-team+ return Integration.objects.filter(kind="slack", integration_id=slack_team_id).first()+++def indexed_integration(installation_id):+ # ruleid: github-installation-first-team+ return Integration.objects.filter(kind="github", integration_id=installation_id)[0]+++def get_integration(installation_id):+ # ruleid: github-installation-first-team+ return Integration.objects.get(kind="github", integration_id=installation_id)+++def every_candidate(payload):+ # ok: github-installation-first-team+ for team_id in installation_team_ids(payload):+ notify(team_id)+++def candidate_set(payload, team_id):+ # ok: github-installation-first-team+ team_ids = set(installation_team_ids(payload))+ return team_id in team_ids+++def candidate_membership(payload, team_id):+ # ok: github-installation-first-team+ return team_id in installation_team_ids(payload)+++def team_scoped_integration(team_id, installation_id):+ # ok: github-installation-first-team+ return Integration.objects.filter(team_id=team_id, integration_id=installation_id).first()+++def team_scoped_integration_get(team, installation_id):+ # ok: github-installation-first-team+ return Integration.objects.get(team=team, kind="github", integration_id=installation_id)+++def team_scoped_integration_chained(team_id, installation_id):+ # ok: github-installation-first-team+ return Integration.objects.filter(team_id=team_id).filter(kind="github", integration_id=installation_id).first()+++def team_scoped_helper(team_id, installation_id):+ # ok: github-installation-first-team+ return Integration.objects.first_github_for_team_installation(team_id, installation_id)+++def every_integration(installation_id):+ # ok: github-installation-first-team+ return list(Integration.objects.filter(kind="github", integration_id=installation_id).order_by("team_id"))+++def justified_exception(installation_id):+ # nosemgrep: github-installation-first-team -- the installation has one project by construction here+ return Integration.objects.filter(kind="github", integration_id=installation_id).first()diff --git a/.semgrep/rules/devex/github-installation-first-team.yaml b/.semgrep/rules/devex/github-installation-first-team.yamlnew file mode 100644index 000000000000..b12ec7b33cd6--- /dev/null+++ b/.semgrep/rules/devex/github-installation-first-team.yaml@@ -0,0 +1,69 @@+rules:+ - id: github-installation-first-team+ message: |+ This code picks one project for a GitHub installation by order. One installation can link+ to several projects in a region, so the first or lowest-id project depends on when the+ projects connected, not on which project should get the event.+ Use one of the two allowed shapes instead:+ - Subscription: take every candidate from installation_team_ids(...) and keep only the+ projects that have the product's own row for the repository (a workflow, a loop+ trigger, an issue link, a monitored repository). Act once per kept project.+ - Single owner: when the product acts as one voice on a repository or pull request, add a+ unique constraint across projects on (installation id, repository), as+ StamphogRepoConfig does, and find the owner through that row.+ See posthog/ingress/github/README.md#picking-the-project. For a justified exception, add+ `# nosemgrep: github-installation-first-team -- <justification>`.+ Pre-existing findings are grandfathered; only newly introduced findings block CI.+ languages: [python]+ severity: WARNING+ pattern-either:+ # The first candidate from the installation's team list, directly or through a variable.+ - pattern: installation_team_ids(...)[$INDEX]+ - pattern: next(iter(installation_team_ids(...)), ...)+ - patterns:+ - pattern-inside: |+ $TEAM_IDS = installation_team_ids(...)+ ...+ - pattern-either:+ - pattern: $TEAM_IDS[$INDEX]+ - pattern: next(iter($TEAM_IDS), ...)+ - pattern: _resolve_external_team(...)+ # One GitHub Integration row out of an installation lookup that no project filter narrows.+ - patterns:+ - pattern-either:+ - pattern: Integration.objects. ... .filter(..., kind=$KIND, ..., integration_id=$ID, ...). ... .first()+ - pattern: Integration.objects. ... .filter(..., kind=$KIND, ..., integration_id=$ID, ...). ... .get(...)+ - pattern: Integration.objects. ... .get(..., kind=$KIND, ..., integration_id=$ID, ...)+ - patterns:+ - pattern: $QUERY[$INDEX]+ - metavariable-pattern:+ metavariable: $QUERY+ pattern: Integration.objects. ... .filter(..., kind=$KIND, ..., integration_id=$ID, ...)+ - metavariable-regex:+ metavariable: $KIND+ regex: (?i).*github.*+ - pattern-not: Integration.objects. ... .filter(..., team_id=$TEAM, ...). ... .filter(...). ... .first()+ - pattern-not: Integration.objects. ... .filter(..., team=$TEAM, ...). ... .filter(...). ... .first()+ - pattern-not: Integration.objects. ... .filter(..., team_id=$TEAM, ...). ... .first()+ - pattern-not: Integration.objects. ... .filter(..., team=$TEAM, ...). ... .first()+ - pattern-not: Integration.objects. ... .filter(..., team_id=$TEAM, ...). ... .get(...)+ - pattern-not: Integration.objects. ... .filter(..., team=$TEAM, ...). ... .get(...)+ - pattern-not: Integration.objects. ... .get(..., team_id=$TEAM, ...)+ - pattern-not: Integration.objects. ... .get(..., team=$TEAM, ...)+ - pattern-not: Integration.objects. ... .filter(..., team_id=$TEAM, ...). ... [$INDEX]+ - pattern-not: Integration.objects. ... .filter(..., team=$TEAM, ...). ... [$INDEX]+ paths:+ include:+ - ee/**/*.py+ - posthog/**/*.py+ - products/**/*.py+ exclude:+ - '**/test/**'+ - '**/tests/**'+ - '**/test_*.py'+ - '**/*_test.py'+ - '**/conftest*.py'+ metadata:+ category: correctness+ subcategory: audit+ confidence: MEDIUMdiff --git a/posthog/github/installations.py b/posthog/github/installations.pyindex a8941fc614e7..5667418af90d 100644--- a/posthog/github/installations.py+++ b/posthog/github/installations.py@@ -25,7 +25,8 @@ def installation_team_ids(payload: dict) -> list[int]:if external_id is None:return []- # One installation can map to multiple teams; order_by makes attribution deterministic.+ # One installation can link to several teams. The order only keeps results stable, so never treat+ # the first team as the owner. See posthog/ingress/github/README.md#picking-the-project.return list(Integration.objects.using(SCOPE_DB_ALIAS).filter(kind="github", integration_id=external_id)diff --git a/posthog/ingress/github/README.md b/posthog/ingress/github/README.mdindex ef6a46a2b3a0..d9536e737777 100644--- a/posthog/ingress/github/README.md+++ b/posthog/ingress/github/README.md@@ -52,5 +52,26 @@ It scans descriptions for direct issue links and fingerprint permalinks on the cRepeated links and deliveries reuse an existing reference.Removing a link from a later description does not remove the reference.+### Picking the project++One installation can link to several projects in a region, so `installation.id` never names one project.+A consumer picks its projects in one of two shapes.++**Subscriptions.**+Deliver the event to every project that has its own row for the repository, for example a workflow, a loop trigger, an issue link, or a monitored repository.+Each project set up that row, so each project gets the event.+`installation_team_ids` gives the candidate projects.+Filter them by the product's own row before you act.++**Single owner.**+Some products act as one voice on a repository or pull request: they post the review, approve, or push fixes.+Such a product needs exactly one project per repository.+Enforce this with a unique constraint across projects on (installation id, repository), as `StamphogRepoConfig` does.+Then find the owner through that row.++Never pick a project by order.+The first or lowest-id project on an installation depends on when the projects connected, not on which project should get the event.+The `github-installation-first-team` semgrep rule flags this pattern.+The [Endpoints table](../README.md#endpoints) lists the consumer names per event type.PR analytics shared by those consumers live in `posthog/github/`, see its README.