: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
feat(ci): enforce desktop/backend separation in PRs (#76384)
Charles Vien6d94d7b1c0fe
diff --git a/.github/scripts/desktop/check-pr-backend-coupling.sh b/.github/scripts/desktop/check-pr-backend-coupling.shnew file mode 100755index 000000000000..c18ffe817122--- /dev/null+++ b/.github/scripts/desktop/check-pr-backend-coupling.sh@@ -0,0 +1,85 @@+#!/usr/bin/env bash+# Fails any PR that changes both products/desktop/** and backend code that+# ships in production (the Django image or a rust service): desktop releases+# auto-update users on their own schedule, with no orchestration against+# backend deploys, so a coupled PR can ship a client that calls endpoints that+# are not deployed yet.+set -euo pipefail++REPOSITORY="${REPOSITORY:?}"+PR_NUMBER="${PR_NUMBER:?}"++# Deploy-relevant subset of ci-backend.yml's `backend` filter (paths that ship+# in the Django image) plus rust/** (rust services deploy separately and also+# serve clients). Tooling/test entries (pyproject.toml, uv.lock, conftest.py,+# docker-compose*, ...) are deliberately excluded so they cannot block an+# unrelated desktop PR.+is_backend_path() {+ case "$1" in+ *.md | *.mdx) return 1 ;;+ products/desktop/*) return 1 ;;+ posthog/*) return 0 ;;+ rust/*) return 0 ;;+ ee/frontend/*) return 1 ;;+ ee/*) return 0 ;;+ common/__init__.py | common/hogql_parser/* | common/hogvm/* | common/ingestion/* | common/migration_utils/* | common/plugin_transpiler/*) return 0 ;;+ products/*/backend/* | products/*.py) return 0 ;;+ frontend/src/products.json) return 0 ;;+ *) return 1 ;;+ esac+}++is_desktop_path() {+ case "$1" in+ products/desktop/*) return 0 ;;+ *) return 1 ;;+ esac+}++# A GitHub API failure must fail the check rather than read as an empty file list.+fetch_api() {+ if ! gh api "$@"; then+ echo "::error::GitHub API request failed for $*; cannot verify desktop/backend separation" >&2+ return 1+ fi+}++touched_desktop=false+touched_backend=false+files=$(fetch_api --paginate "repos/$REPOSITORY/pulls/$PR_NUMBER/files" | jq -r '.[].filename') || exit 1+while IFS= read -r path; do+ [ -n "$path" ] || continue+ if is_desktop_path "$path"; then touched_desktop=true; fi+ if is_backend_path "$path"; then touched_backend=true; fi+done <<<"$files"++if [ "$touched_desktop" = false ]; then+ echo "No products/desktop changes; nothing to check."+ exit 0+fi++# Labels come from the API, not the event payload, so applying the skip label+# and re-running is enough; label changes do not retrigger pull_request runs.+pr_json=$(fetch_api "repos/$REPOSITORY/pulls/$PR_NUMBER") || exit 1+if jq -e '[.labels[]?.name] | index("skip-desktop-backend-check") != null' <<<"$pr_json" >/dev/null; then+ echo "::notice::PR #$PR_NUMBER carries skip-desktop-backend-check; skipping the coupling check."+ exit 0+fi++if [ "$touched_backend" = true ]; then+ echo "::error::This PR contains both products/desktop and backend changes. These must be separated into different PRs."+ echo ""+ echo "Why? Desktop releases auto-update users and are not orchestrated with backend"+ echo "deploys. If desktop code depends on a backend change in the same PR, a release"+ echo "can ship a client that calls endpoints that are not deployed yet."+ echo ""+ echo "Solution: split into two PRs:"+ echo " 1. First PR: backend changes only (merge and wait for the deploy)."+ echo " 2. Second PR: desktop changes (merge once the backend is live)."+ echo ""+ echo "If the two halves are independent (safe to ship in either order), apply the"+ echo "'skip-desktop-backend-check' label and re-run this check."+ exit 1+fi++echo "No desktop/backend coupling detected."diff --git a/.github/scripts/desktop/check-pr-backend-coupling.test.sh b/.github/scripts/desktop/check-pr-backend-coupling.test.shnew file mode 100755index 000000000000..64f48ae7eae8--- /dev/null+++ b/.github/scripts/desktop/check-pr-backend-coupling.test.sh@@ -0,0 +1,111 @@+#!/usr/bin/env bash+set -euo pipefail++checker="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/check-pr-backend-coupling.sh"++workdir="$(mktemp -d)"+trap 'rm -rf "$workdir"' EXIT++# gh stub: prints the fixture registered for the requested path, fails on a+# .fail marker and 404s on anything unregistered. jq stays real.+fake_bin="$workdir/bin"+fixtures="$workdir/fixtures"+mkdir -p "$fake_bin" "$fixtures"+cat >"$fake_bin/gh" <<'EOF'+#!/usr/bin/env bash+path=""+for arg in "$@"; do+ case "$arg" in+ api | --*) ;;+ *)+ path="$arg"+ break+ ;;+ esac+done+slug=$(printf '%s' "${path:?}" | tr '/?&=' '____')+if [ -f "$GH_FIXTURE_DIR/$slug.fail" ]; then+ echo "stubbed API failure for $path" >&2+ exit 1+elif [ -f "$GH_FIXTURE_DIR/$slug.json" ]; then+ cat "$GH_FIXTURE_DIR/$slug.json"+else+ echo "stubbed 404 for $path" >&2+ exit 1+fi+EOF+chmod +x "$fake_bin/gh"++register_pr() {+ local number="$1" labels="$2"+ shift 2+ jq -n --argjson labels "$labels" '{labels: $labels}' \+ >"$fixtures/repos_PostHog_posthog_pulls_${number}.json"+ printf '%s\n' "$@" | jq -R '{filename: .}' | jq -s . \+ >"$fixtures/repos_PostHog_posthog_pulls_${number}_files.json"+}++run_checker() {+ PATH="$fake_bin:$PATH" GH_FIXTURE_DIR="$fixtures" \+ REPOSITORY="PostHog/posthog" PR_NUMBER="$1" "$checker"+}++assert_result() {+ local name="$1" pr="$2" expected_status="$3" needle="$4" output status+ set +e+ output=$(run_checker "$pr" 2>&1)+ status=$?+ set -e+ if [ "$status" -ne "$expected_status" ] || ! grep -Fq "$needle" <<<"$output"; then+ echo "FAIL: $name (exit $status, expected $expected_status)"+ awk '{print " | " $0}' <<<"$output"+ exit 1+ fi+ echo "ok: $name"+}++register_pr 1 "[]" products/desktop/apps/foo.ts products/desktop/README.md+assert_result "desktop-only PR passes" 1 0 "No desktop/backend coupling detected."++register_pr 2 "[]" posthog/models.py ee/billing/models.py+assert_result "backend-only PR is out of scope" 2 0 "No products/desktop changes"++register_pr 3 "[]" products/desktop/apps/foo.ts posthog/models.py+assert_result "coupled PR fails with split guidance" 3 1 "must be separated into different PRs"++register_pr 4 '[{"name": "skip-desktop-backend-check"}]' products/desktop/apps/foo.ts posthog/models.py+assert_result "skip label suppresses the check" 4 0 "skipping the coupling check"++# `yes` marks paths that count as backend and must fail the PR.+pr=100+while IFS='|' read -r path gated; do+ pr=$((pr + 1))+ register_pr "$pr" "[]" products/desktop/apps/foo.ts "$path"+ if [ "$gated" = yes ]; then+ assert_result "classifier arm $path is backend" "$pr" 1 "must be separated into different PRs"+ else+ assert_result "classifier arm $path is not backend" "$pr" 0 "No desktop/backend coupling detected."+ fi+done <<'CASES'+posthog/api/insight.py|yes+posthog/README.md|no+rust/capture/src/main.rs|yes+rust/capture/README.md|no+ee/billing/models.py|yes+ee/frontend/exports.ts|no+common/hogql_parser/parser.cpp|yes+products/llm_analytics/backend/api.py|yes+frontend/src/products.json|yes+pyproject.toml|no+frontend/src/scenes/App.tsx|no+CASES++register_pr 5 "[]" products/desktop/apps/foo.ts+touch "$fixtures/repos_PostHog_posthog_pulls_5_files.fail"+assert_result "file listing API failure fails closed" 5 1 "GitHub API request failed"++register_pr 6 "[]" products/desktop/apps/foo.ts+touch "$fixtures/repos_PostHog_posthog_pulls_6.fail"+assert_result "label lookup API failure fails closed" 6 1 "GitHub API request failed"++echo "PR backend coupling check regression cases passed."diff --git a/.github/workflows/desktop-ci.yml b/.github/workflows/desktop-ci.ymlindex cbf51d85d031..3c6815d40875 100644--- a/.github/workflows/desktop-ci.yml+++ b/.github/workflows/desktop-ci.yml@@ -5,10 +5,10 @@ name: Desktop CI# workflows counts once, so the whole desktop suite costs one slot per push.## The suites themselves stay unfiltered here on purpose: each child gates its-# own work on an internal `changes` job, so a non-desktop PR pays four short-# detector jobs and skips everything else. A trigger-level `paths:` filter-# would stop the `Desktop * Pass` checks from ever reporting, leaving any PR-# that requires them stuck waiting for status.+# own work on an internal `changes` job, so a non-desktop PR pays a handful of+# short detector jobs and skips everything else. A trigger-level `paths:`+# filter would stop the `Desktop * Pass` checks from ever reporting, leaving+# any PR that requires them stuck waiting for status.on:pull_request:@@ -55,12 +55,41 @@ jobs:POSTHOG_CODE_E2E_GATEWAY_URL: ${{ secrets.POSTHOG_CODE_E2E_GATEWAY_URL }}TRUNK_API_TOKEN: ${{ secrets.TRUNK_API_TOKEN }}+ # Desktop counterpart of the migration/service separation check (see the+ # script header for why). Lives here rather than pr-housekeeping so the+ # existing Desktop Tests Pass required check covers it.+ backend-coupling:+ name: Desktop backend coupling+ # merge_group has no pull_request payload; the check already ran on the PR+ if: github.event_name == 'pull_request'+ runs-on: ubuntu-latest+ timeout-minutes: 5+ steps:+ - name: Checkout gate scripts+ uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2+ with:+ sparse-checkout: .github/scripts/desktop+ persist-credentials: false+ - uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1+ id: app-token+ # forks can't read org secrets; fall back to github.token+ if: github.event.pull_request.head.repo.full_name == github.repository+ with:+ client-id: ${{ secrets.GH_APP_POSTHOG_PATHS_FILTER_APP_ID }}+ private-key: ${{ secrets.GH_APP_POSTHOG_PATHS_FILTER_PRIVATE_KEY }}+ - name: Check desktop/backend separation+ env:+ GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }}+ REPOSITORY: ${{ github.repository }}+ PR_NUMBER: ${{ github.event.pull_request.number }}+ run: .github/scripts/desktop/check-pr-backend-coupling.sh+# Umbrella gate for the whole desktop suite: the one check name to require in# the master ruleset. Adding or removing a suite only changes this needs list,# not branch protection.tests-pass:name: Desktop Tests Pass- needs: [build, quality, typecheck, test]+ needs: [build, quality, typecheck, test, backend-coupling]if: always()runs-on: ubuntu-latesttimeout-minutes: 5@@ -83,4 +112,8 @@ jobs:echo "Desktop tests did not succeed (result: ${{ needs.test.result }})."exit 1fi+ if [[ "${{ needs.backend-coupling.result }}" != "success" && "${{ needs.backend-coupling.result }}" != "skipped" ]]; then+ echo "Desktop backend coupling check did not succeed (result: ${{ needs.backend-coupling.result }})."+ exit 1+ fiecho "All desktop suites passed or were skipped."diff --git a/.github/workflows/shellcheck.yml b/.github/workflows/shellcheck.ymlindex 7ebe8492bc31..ea85beee8c5f 100644--- a/.github/workflows/shellcheck.yml+++ b/.github/workflows/shellcheck.yml@@ -24,6 +24,11 @@ jobs:.github/scripts/check-windows-paths.test.sh.github/scripts/check-windows-paths.sh+ - name: Check desktop backend coupling script+ run: |+ shellcheck .github/scripts/desktop/*.sh+ .github/scripts/desktop/check-pr-backend-coupling.test.sh+# Runs here, not in a path-gated ci-* job, so AGENTS/CLAUDE changes# anywhere in the repo are validated on every PR.- name: Check AGENTS.md / CLAUDE.md symlinksdiff --git a/products/desktop/MIGRATION.md b/products/desktop/MIGRATION.mdindex d2319b0e461d..057b981eba5a 100644--- a/products/desktop/MIGRATION.md+++ b/products/desktop/MIGRATION.md@@ -118,7 +118,7 @@ separate project.| Source (.github/workflows/) | Port | Notes beyond the standard transforms || --- | --- | --- |-| _(none — monorepo-native)_ | desktop-ci.yml | single `pull_request:`/`merge_group:` dispatch that calls the four gating workflows; see transform rule 7 |+| _(none — monorepo-native)_ | desktop-ci.yml | single `pull_request:`/`merge_group:` dispatch that calls the four gating workflows plus the `backend-coupling` separation check (`.github/scripts/desktop/check-pr-backend-coupling.sh`); see transform rule 7 || build.yml | desktop-build.yml | gating: `workflow_call` child of desktop-ci.yml + `Desktop Build Pass` || warm-caches.yml | desktop-warm-caches.yml | seeds every cache the restore-only desktop PR workflows use; pnpm-store caching is explicit (`desktop-pnpm-*` keys) instead of setup-node auto-cache so PR restores share the namespace || agent-release-verify.yml | desktop-agent-release-verify.yml | restore-only pnpm store |