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.

chore(ci): skip backend coverage when test matrices skip (#126818)

posthog[bot]b42e1ea011a7
diff --git a/.depot/workflows/ci-backend.yml b/.depot/workflows/ci-backend.yml
index bc945f51b2b6..32abdc4396b4 100644
--- a/.depot/workflows/ci-backend.yml
+++ b/.depot/workflows/ci-backend.yml
@@ -4528,7 +4528,7 @@ jobs:
backend-coverage-report:
name: Backend coverage report
- needs: [changes, turbo-tests, django_tests, turbo-discover]
+ needs: [changes, django, turbo-tests, django_tests, turbo-discover]
runs-on: depot-ubuntu-24.04
continue-on-error: true
timeout-minutes: 10
@@ -4540,7 +4540,7 @@ jobs:
github.event.pull_request.draft == false &&
needs.turbo-discover.outputs.mode != 'selected' &&
github.event.pull_request.head.repo.full_name == 'PostHog/posthog' &&
- (needs.turbo-tests.result != 'skipped' || needs.django_tests.result != 'skipped')
+ (needs.turbo-tests.result != 'skipped' || needs.django.result != 'skipped')
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
@@ -4623,7 +4623,7 @@ jobs:
calculate-running-time:
name: Calculate running time
- needs: [changes, django_tests]
+ needs: [changes, django, turbo-tests, django_tests]
runs-on: depot-ubuntu-24.04
timeout-minutes: 5
# Run on pull requests to PostHog/posthog + on PostHog/posthog outside of PRs - but never on forks or Dependabot (no secrets access)
@@ -4635,13 +4635,26 @@ jobs:
permissions:
contents: read
steps:
+ - name: Check whether stale backend coverage can be cleared
+ id: coverage-cleanup
+ if: >-
+ github.event_name == 'pull_request' &&
+ github.event.pull_request.draft == false &&
+ needs.changes.outputs.backend == 'false' &&
+ needs.django.result == 'skipped' &&
+ needs.turbo-tests.result == 'skipped' &&
+ needs.django_tests.result == 'success'
+ run: echo "needed=true" >> "$GITHUB_OUTPUT"
# Depot evaluates github.run_attempt as empty in a job-level `if`, so the attempt guard sits on the steps.
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
id: checkout
- if: github.run_attempt == '1'
+ if: github.run_attempt == '1' || steps.coverage-cleanup.outputs.needed == 'true'
continue-on-error: true
with:
- sparse-checkout: .github/scripts/capture_depot_running_time.py
+ sparse-checkout: |
+ .github/scripts/capture_depot_running_time.py
+ frontend/bin/ci-report/update-ci-report.mjs
+ .nvmrc
sparse-checkout-cone-mode: false
- uses: depot/setup-action@15c09a5f77a0840ad4bce955686522a257853461 # v1.7.1
id: setup-depot
@@ -4656,3 +4669,27 @@ jobs:
POSTHOG_DEVEX_PROJECT_API_TOKEN: ${{ secrets.POSTHOG_DEVEX_PROJECT_API_TOKEN }}
STATUS_JOB: django_tests
run: python3 .github/scripts/capture_depot_running_time.py
+ - name: Get app token for coverage cleanup
+ id: coverage-cleanup-token
+ if: steps.coverage-cleanup.outputs.needed == 'true' && steps.checkout.outcome == 'success' && hashFiles('frontend/bin/ci-report/update-ci-report.mjs') != ''
+ continue-on-error: true
+ uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1
+ with:
+ client-id: ${{ secrets.GH_APP_POSTHOG_TESTS_APP_ID }}
+ private-key: ${{ secrets.GH_APP_POSTHOG_TESTS_PRIVATE_KEY }}
+ permission-pull-requests: write
+ - name: Set up Node.js for coverage cleanup
+ if: steps.coverage-cleanup-token.outcome == 'success'
+ id: coverage-cleanup-node
+ continue-on-error: true
+ uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
+ with:
+ node-version-file: .nvmrc
+ token: ${{ steps.coverage-cleanup-token.outputs.token }}
+ - name: Clear stale backend coverage warning
+ if: steps.coverage-cleanup-node.outcome == 'success' && hashFiles('frontend/bin/ci-report/update-ci-report.mjs') != ''
+ continue-on-error: true
+ env:
+ GITHUB_TOKEN: ${{ steps.coverage-cleanup-token.outputs.token }}
+ run: |
+ node --input-type=module -e "import { clearSectionIfPresent } from './frontend/bin/ci-report/update-ci-report.mjs'; await clearSectionIfPresent({ id: 'backend-coverage', summary: 'no backend files changed', body: 'No backend coverage report is needed for this change.' })"
diff --git a/tools/workflow-plan/tests/workflows.test.ts b/tools/workflow-plan/tests/workflows.test.ts
index d53ca53722e7..00862ecd2bb8 100644
--- a/tools/workflow-plan/tests/workflows.test.ts
+++ b/tools/workflow-plan/tests/workflows.test.ts
@@ -6,7 +6,7 @@ import { readdirSync } from 'node:fs'
import path from 'node:path'
import { describe, expect, it } from 'vitest'
-import { evaluateValue, planFunctions } from '../src/expressions.ts'
+import { evaluateCondition, evaluateValue, planFunctions } from '../src/expressions.ts'
import {
type Outcome,
type Scenario,
@@ -190,7 +190,7 @@ const EXPECTATIONS: Expectation[] = [
),
depotBackend(
{ name: 'ready PR, superseded and cancelled', cancelled: true },
- { results: { django_tests: 'cancelled' } }
+ { results: { django_tests: 'cancelled', 'backend-coverage-report': 'cancelled' } }
),
depotBackend(
{ name: 'draft PR', github: pullRequest({ draft: true }) },
@@ -282,7 +282,7 @@ const EXPECTATIONS: Expectation[] = [
},
},
},
- { runs: ['turbo-tests', 'django_tests'], skipped: ['django'] }
+ { runs: ['turbo-tests', 'django_tests', 'backend-coverage-report'], skipped: ['django'] }
),
depotBackend(
{ name: 'draft PR labeled no-ci', github: pullRequest({ draft: true, labels: ['no-ci'] }) },
@@ -307,6 +307,7 @@ const EXPECTATIONS: Expectation[] = [
'get_clickhouse_versions',
'build_django_matrix',
'sdk-major-guard',
+ 'backend-coverage-report',
],
}
),
@@ -730,6 +731,139 @@ describe('CI workflow run plans', () => {
).toEqual(checks === 'full' ? validationSteps : checks === 'typecheck' ? ['Typecheck'] : [])
})
+ it.each([
+ ['both skipped', 'skipped', 'skipped', 'full', false],
+ ['core only', 'success', 'skipped', 'full', true],
+ ['products only', 'skipped', 'success', 'full', true],
+ ['failed core shards', 'failure', 'skipped', 'full', true],
+ ['failed product shards', 'skipped', 'failure', 'full', true],
+ ['selected tests', 'success', 'success', 'selected', false],
+ ])('backend coverage reporting with %s', (_name, django, products, mode, runs) => {
+ expect(
+ evaluateCondition(
+ workflow(DEPOT_BACKEND).jobs['backend-coverage-report']!.if,
+ {
+ github: pullRequest(),
+ needs: {
+ django: { result: django },
+ 'turbo-tests': { result: products },
+ django_tests: { result: 'success' },
+ 'turbo-discover': { outputs: { mode } },
+ },
+ },
+ planFunctions({
+ dependenciesSucceeded: django === 'success' && products === 'success',
+ dependenciesFailed: django === 'failure' || products === 'failure',
+ cancelled: false,
+ })
+ )
+ ).toBe(runs)
+ })
+
+ it.each([
+ {
+ name: 'frontend-only ready PR',
+ backend: 'false',
+ django: 'skipped',
+ products: 'skipped',
+ gate: 'success',
+ runs: true,
+ },
+ {
+ name: 'backend changes',
+ backend: 'true',
+ django: 'skipped',
+ products: 'skipped',
+ gate: 'success',
+ runs: false,
+ },
+ {
+ name: 'core matrix ran',
+ backend: 'false',
+ django: 'success',
+ products: 'skipped',
+ gate: 'success',
+ runs: false,
+ },
+ {
+ name: 'product matrix ran',
+ backend: 'false',
+ django: 'skipped',
+ products: 'success',
+ gate: 'success',
+ runs: false,
+ },
+ {
+ name: 'upstream failure',
+ backend: 'false',
+ django: 'skipped',
+ products: 'skipped',
+ gate: 'failure',
+ runs: false,
+ },
+ {
+ name: 'unknown change result',
+ backend: '',
+ django: 'skipped',
+ products: 'skipped',
+ gate: 'success',
+ runs: false,
+ },
+ ])('stale backend coverage cleanup on $name', ({ backend, django, products, gate, runs }) => {
+ const step = workflow(DEPOT_BACKEND).jobs['calculate-running-time']!.steps!.find(
+ (step) => step.id === 'coverage-cleanup'
+ )!
+ expect(
+ evaluateCondition(
+ step.if,
+ {
+ github: pullRequest(),
+ needs: {
+ changes: { outputs: { backend } },
+ django: { result: django },
+ 'turbo-tests': { result: products },
+ django_tests: { result: gate },
+ },
+ },
+ planFunctions({ dependenciesSucceeded: true, dependenciesFailed: false, cancelled: false })
+ )
+ ).toBe(runs)
+ })
+
+ it.each([
+ { name: 'ready PR', github: pullRequest(), cancelled: false, runs: true },
+ { name: 'draft PR', github: pullRequest({ draft: true }), cancelled: false, runs: false },
+ {
+ name: 'draft PR labeled no-ci',
+ github: pullRequest({ draft: true, labels: ['no-ci'] }),
+ cancelled: false,
+ runs: false,
+ },
+ { name: 'fork PR', github: pullRequest({ fork: true }), cancelled: false, runs: false },
+ { name: 'cancelled PR', github: pullRequest(), cancelled: true, runs: false },
+ { name: 'master push', github: push(), cancelled: false, runs: false },
+ ])('stale backend coverage cleanup policy on $name', ({ name, github, cancelled, runs }) => {
+ const plan = planWorkflow(workflow(DEPOT_BACKEND), {
+ name,
+ github,
+ cancelled,
+ steps: {
+ ...allFiltersChanged(workflow(DEPOT_BACKEND)),
+ ...backendSelectors,
+ ...frontendOnlyFilters,
+ 'calculate-running-time': { 'coverage-cleanup': { outputs: { needed: 'true' } } },
+ },
+ })
+ expect(plan.errors).toEqual([])
+ expect(
+ plan.jobs['calculate-running-time']?.steps.find((step) => step.id === 'coverage-cleanup')?.runs ?? false
+ ).toBe(runs)
+ const cleanup = plan.jobs['calculate-running-time']?.steps.find(
+ (step) => step.name === 'Clear stale backend coverage warning'
+ )
+ expect(cleanup?.runs ?? false).toBe(runs)
+ })
+
it.each([
{ result: 'success', exitCode: 0 },
{ result: 'failure', exitCode: 1 },