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): post hogbox preview as a ci report section (#110736)

Georges-Antoine Assi4658d2bb1179
diff --git a/.github/scripts/post-ci-sections.test.mjs b/.github/scripts/post-ci-sections.test.mjs
index 00415ac7554f..4203b521dd8c 100644
--- a/.github/scripts/post-ci-sections.test.mjs
+++ b/.github/scripts/post-ci-sections.test.mjs
@@ -3,6 +3,7 @@ import { describe, it } from 'node:test'
import { buildDocsPreviewSection } from './post-docs-preview-section.mjs'
import { buildHobbySection } from './post-hobby-section.mjs'
+import { buildHogboxPreviewSection } from './post-hogbox-preview-section.mjs'
import { buildTrunkLaneSection, postTrunkLaneSection } from './post-trunk-lane-section.mjs'
const commonHobby = {
@@ -123,6 +124,36 @@ describe('CI report section builders', () => {
assert.match(section.body, /actions\/runs\/42/)
})
+ for (const testCase of [
+ {
+ name: 'claims the PR frontend only when it was swapped in',
+ frontendSwapped: true,
+ expected: /\*\*and\*\* frontend/,
+ },
+ {
+ name: 'says the frontend is unchanged when it was not swapped in',
+ frontendSwapped: false,
+ expected: /frontend unchanged by this PR/,
+ },
+ ]) {
+ it(testCase.name, () => {
+ const section = buildHogboxPreviewSection({
+ state: 'ready',
+ sha: '1234567890abcdef',
+ runUrl: 'https://github.com/PostHog/posthog/actions/runs/42',
+ url: 'https://preview.example.com',
+ boxId: 'box-1',
+ penId: 'None',
+ consoleHost: 'console.example.com',
+ frontendSwapped: testCase.frontendSwapped,
+ })
+ assert.equal(section.status, 'ok')
+ assert.match(section.summary, /https:\/\/preview\.example\.com/)
+ assert.match(section.body, testCase.expected)
+ assert.doesNotMatch(section.body, /console\/fleet\/pens/)
+ })
+ }
+
it('moves a hobby preview through setup, ready, and failed states', () => {
const initial = buildHobbySection({ state: 'initial', ...commonHobby })
const ready = buildHobbySection({
diff --git a/.github/scripts/post-hogbox-preview-section.mjs b/.github/scripts/post-hogbox-preview-section.mjs
new file mode 100644
index 000000000000..7a7a287cd3e2
--- /dev/null
+++ b/.github/scripts/post-hogbox-preview-section.mjs
@@ -0,0 +1,105 @@
+#!/usr/bin/env node
+import { pathToFileURL } from 'node:url'
+
+import { postSection, updateSectionIfPresent } from '../../frontend/bin/ci-report/update-ci-report.mjs'
+
+const LEGACY_PREFIXES = ['<!-- hogbox-preview-comment -->']
+
+export function buildHogboxPreviewSection({
+ state,
+ sha = '',
+ runUrl,
+ url,
+ boxId,
+ penId,
+ consoleHost,
+ frontendSwapped,
+ readySeconds,
+}) {
+ const commit = sha.slice(0, 7)
+
+ if (state === 'building') {
+ return {
+ status: 'info',
+ summary: 'building',
+ body: [
+ "Spinning up PostHog for this PR on a hogland hogbox. This section updates in place when it's ready (usually a few minutes).",
+ '',
+ `<sub>commit \`${commit}\` &middot; <a href="${runUrl}">build log</a></sub>`,
+ ].join('\n'),
+ }
+ }
+
+ if (state === 'failed') {
+ return {
+ status: 'fail',
+ summary: 'build failed',
+ body: [
+ `The preview didn't come up for commit \`${commit}\`. See the **[build log](${runUrl})** for the failing step. It'll retry on the next push.`,
+ '',
+ '<sub>Previews are optional and never block merging. A failure here is often a hogland or tailnet hiccup rather than anything in your PR, so the check stays green and this section is the status.</sub>',
+ ].join('\n'),
+ }
+ }
+
+ if (state === 'torn-down') {
+ return {
+ status: 'info',
+ summary: 'torn down',
+ body: 'The preview for this PR was torn down (label removed or `no-preview` added). Re-add the `hogbox-preview` label to bring it back.',
+ }
+ }
+
+ if (state !== 'ready') {
+ throw new Error(`Unknown hogbox preview section state: ${state}`)
+ }
+
+ // A backend-only preview serves the :master SPA, so only claim the PR's frontend when it was swapped in.
+ const running = frontendSwapped
+ ? "this PR's backend **and** frontend, on the PostHog `:master` base"
+ : "this PR's backend on the PostHog `:master` base (frontend unchanged by this PR)"
+ // The CLI parser can surface a missing pen id as the literal string 'None'.
+ const adminUrl =
+ consoleHost && penId && penId !== 'None' ? `https://${consoleHost}/console/fleet/pens/${penId}` : null
+ const readyIn = readySeconds ? ` &middot; ready in ${readySeconds}s (push → usable)` : ''
+
+ return {
+ status: 'ok',
+ summary: `ready, <a href="${url}">open the preview</a>`,
+ body: [
+ `### [▶ Open the preview](${url})`,
+ '',
+ '| | |',
+ '|--|--|',
+ '| 🔑 **Login** | `[email protected]` / `12345678` (demo data) |',
+ `| 🧩 **Running** | ${running} |`,
+ '| 🔗 **Link** | **stable across rebuilds**: a re-push swaps the box underneath, the URL stays |',
+ '| 🔒 **Access** | tailnet only (PostHog VPN) |',
+ ...(adminUrl ? [`| 🛠️ **Admin** | [inspect & debug state in hogland](${adminUrl}) |`] : []),
+ '| 💤 **Idle** | sleeps after ~30 min idle (snapshot to S3, zero node cost) and wakes on your next visit in ~30s, behind a brief "waking up" screen |',
+ '',
+ `<sub>commit \`${commit}\` &middot; box \`${boxId}\`${readyIn} &middot; <a href="${runUrl}">build log</a> &middot; rebuilds on every push, torn down on close</sub>`,
+ ].join('\n'),
+ }
+}
+
+async function main() {
+ const [state] = process.argv.slice(2)
+ const section = buildHogboxPreviewSection({
+ state,
+ sha: process.env.SHA,
+ runUrl: `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`,
+ url: process.env.URL,
+ boxId: process.env.BOX,
+ penId: process.env.PEN,
+ consoleHost: process.env.CONSOLE_HOST,
+ frontendSwapped: process.env.SWAP === 'true',
+ readySeconds: process.env.READY_SECONDS,
+ })
+ const post = state === 'torn-down' ? updateSectionIfPresent : postSection
+ await post({ id: 'hogbox-preview', ...section }, { legacyPrefixes: LEGACY_PREFIXES })
+}
+
+if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
+ await main()
+}
diff --git a/.github/workflows/hogbox-preview-env.yml b/.github/workflows/hogbox-preview-env.yml
index 245837396a01..48daf5163e28 100644
--- a/.github/workflows/hogbox-preview-env.yml
+++ b/.github/workflows/hogbox-preview-env.yml
@@ -3,7 +3,7 @@
# Restores the pre-baked "preview golden" snapshot (PostHog already migrated +
# seeded on the :master image), mounts the PR's backend source over the image's
# /code, builds the PR's frontend, runs the delta migrations, and posts the
-# reachable URL on the PR (sticky comment + a GitHub Deployment).
+# reachable URL on the PR (a CI report section + a GitHub Deployment).
#
# This drives tools/hogbox-preview — the layer (hogland) is swappable; the stack
# recipe is not. See tools/hogbox-preview/README.md.
@@ -35,7 +35,7 @@
#
# --- LIFECYCLE ------------------------------------------------------------------
# - build/update: once `decide` says build, `announce` posts the in-progress
-# Deployment + comment, then `build-frontend` (untrusted, no credentials —
+# Deployment + CI report section, then `build-frontend` (untrusted, no credentials —
# see the permissions note below) and `deploy` (trusted, brings the box up)
# run IN PARALLEL. deploy no longer waits on the dist: it restores + mounts +
# migrates immediately, and only blocks on build-frontend right at the end,
@@ -46,7 +46,7 @@
# (preview-pr-<n>) is reaped by pr-cleanup.yml's existing deployment cleanup.
# - teardown fast path: the `teardown` job below fires when `hogbox-preview` is
# removed OR `no-preview` is added (the cron is the backstop); it also marks
-# the Deployment inactive and flips the sticky comment.
+# the Deployment inactive and flips the CI report section.
#
# --- PREREQS (tracked out of band) ----------------------------------------------
# - vars.TS_HOGLAND_CI_CLIENT_ID / TS_HOGLAND_CI_AUDIENCE (tailnet join)
@@ -61,7 +61,7 @@
# --- OPTIONAL BY DESIGN ---------------------------------------------------------
# A preview never gates a PR: all preview infra (tailnet, hogland, GitHub API, the
-# reporters) fails open and reports via the Deployment + sticky comment. Only
+# reporters) fails open and reports via the Deployment + CI report section. Only
# `build-frontend` (PR code — deploy reads its conclusion) and the fork guards still
# red. There is no "yellow" to use instead, and job-level `continue-on-error` still
# reds the PR, so fail-open means step-level. The name prefix is on the JOBS because
@@ -227,9 +227,9 @@ jobs:
: 'no hogbox-preview label and no frontend changes → skip');
# ----------------------------------------------------------------------------
- # Trusted, tiny, no-checkout: open a GitHub Deployment (in_progress) and the
- # sticky "building…" comment immediately, so the PR shows activity before the
- # multi-minute build + bring-up finishes. Runs github-script only — no PR
+ # Trusted and tiny: open a GitHub Deployment (in_progress) and the
+ # "building…" CI report section immediately, so the PR shows activity before the
+ # multi-minute build + bring-up finishes. Runs only default-branch scripts — no PR
# code — which is why it may hold write perms. Deliberately OUTSIDE the
# per-PR concurrency group: announcing must never cancel an in-flight
# deploy; build-frontend/deploy below do the coalescing.
@@ -241,14 +241,14 @@ jobs:
timeout-minutes: 5
permissions:
contents: read
- pull-requests: write # sticky preview comment
+ pull-requests: write # CI report section
deployments: write # GitHub Deployment for the PR's Environments UI
outputs:
sha: ${{ steps.start.outputs.sha }}
deployment_id: ${{ steps.start.outputs.deployment_id }}
steps:
# Cosmetic: on a blip sha/deployment_id go empty, which deploy already tolerates.
- - name: Announce (deployment + building comment)
+ - name: Announce (deployment)
id: start
continue-on-error: true
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
@@ -272,15 +272,22 @@ jobs:
...context.repo, deployment_id: dep.data.id, state: 'in_progress',
environment, log_url: runUrl, description: 'Building preview…',
});
- const marker = '<!-- hogbox-preview-comment -->';
- const body =
- `${marker}\n### 🦔 Hogbox preview &middot; 🔧 building…\n\n` +
- `Spinning up PostHog for this PR on a hogland hogbox — this comment updates in place when it's ready (usually a few minutes).\n\n` +
- `<sub>commit \`${sha.slice(0, 7)}\` &middot; <a href="${runUrl}">build log</a></sub>`;
- const { data: comments } = await github.rest.issues.listComments({ ...context.repo, issue_number: pr });
- const ex = comments.find(c => c.body.includes(marker));
- if (ex) await github.rest.issues.updateComment({ ...context.repo, comment_id: ex.id, body });
- else await github.rest.issues.createComment({ ...context.repo, issue_number: pr, body });
+
+ - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ continue-on-error: true
+ with:
+ ref: ${{ github.event.repository.default_branch }}
+ sparse-checkout: |
+ .github/scripts
+ frontend/bin/ci-report
+
+ - name: Post building section
+ continue-on-error: true
+ env:
+ GITHUB_TOKEN: ${{ github.token }}
+ PR_NUMBER: ${{ env.PR }}
+ SHA: ${{ steps.start.outputs.sha }}
+ run: node .github/scripts/post-hogbox-preview-section.mjs building
# ----------------------------------------------------------------------------
# UNTRUSTED build: checks out the PR and runs its pnpm lifecycle + turbo
@@ -431,7 +438,7 @@ jobs:
needs: [decide, announce]
# Runs whenever an eligible build was attempted — even if announce
# failed, so the failure reporter below still flips the Deployment +
- # sticky comment to failed. Skips only on cancellation (a newer push's
+ # CI report section to failed. Skips only on cancellation (a newer push's
# run took over the concurrency group).
if: ${{ !cancelled() && needs.decide.outputs.build == 'true' }}
concurrency:
@@ -443,7 +450,7 @@ jobs:
contents: read
id-token: write # mint OIDC for the tailnet join AND the hogplane token
actions: read # poll this run's jobs + artifacts to wait on build-frontend
- pull-requests: write # sticky preview comment
+ pull-requests: write # CI report section
deployments: write # flip the Deployment to success/failure
env:
SHA: ${{ needs.announce.outputs.sha }}
@@ -561,7 +568,7 @@ jobs:
# The box is healthy on the :master SPA. NOW block on build-frontend:
# poll THIS run's jobs for the `build PR frontend` job until it has a
# conclusion. failure/cancelled/timed_out → fail (the failure reporter
- # flips the comment + Deployment). success WITH the dist artifact →
+ # flips the CI report section + Deployment). success WITH the dist artifact →
# swap the PR frontend in. success WITHOUT the artifact, or skipped →
# the PR didn't touch the frontend, keep :master (no swap). By now the
# FE build has usually long finished, so this returns near-instantly.
@@ -632,7 +639,7 @@ jobs:
swap-frontend --frontend-dist "${RUNNER_TEMP}/fe-dist/frontend-dist.tgz"
# dist/swap are 'skipped' on a backend-only PR, which passes; 'failure'
- # would serve :master under a comment claiming the PR's frontend.
+ # would serve :master under a section claiming the PR's frontend.
- name: Decide preview outcome
id: outcome
if: ${{ !cancelled() }}
@@ -651,18 +658,14 @@ jobs:
echo "ok=$ok" >> "$GITHUB_OUTPUT"
# Reporting is auxiliary: an API blip must not red a preview that came up.
- - name: Report ready (deployment + comment)
+ - name: Report ready (deployment)
+ id: ready
if: steps.outcome.outputs.ok == 'true'
continue-on-error: true
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
- # SHA + DEP come from the job env (announce job outputs).
+ # DEP comes from the job env (announce job outputs).
env:
URL: ${{ steps.build.outputs.url }}
- BOX: ${{ steps.build.outputs.box_id }}
- PEN: ${{ steps.build.outputs.pen_id }}
- SWAP: ${{ steps.fe.outputs.swap }}
- # The hogland admin/console host (same value as the OIDC audience).
- CONSOLE_HOST: ${{ env.HOG_OIDC_AUDIENCE }}
with:
script: |
const pr = Number(process.env.PR);
@@ -675,49 +678,33 @@ jobs:
const run = await github.rest.actions.getWorkflowRun({ ...context.repo, run_id: context.runId });
const startedAt = run.data.run_started_at || run.data.created_at;
const secs = Math.max(0, Math.round((Date.now() - new Date(startedAt).getTime()) / 1000));
+ core.setOutput('secs', String(secs));
if (process.env.DEP) {
await github.rest.repos.createDeploymentStatus({
...context.repo, deployment_id: Number(process.env.DEP), state: 'success',
environment, environment_url: url, log_url: runUrl, description: 'Preview ready',
});
}
- // Only claim the PR's frontend when it was actually swapped
- // in; a backend-only preview serves the :master SPA, and
- // saying otherwise would be a correctness lie to the reviewer.
- const running = process.env.SWAP === 'true'
- ? "this PR's backend **and** frontend, on the PostHog `:master` base"
- : "this PR's backend on the PostHog `:master` base (frontend unchanged by this PR)";
- // Admin/console link for the pen — lets folks inspect and
- // debug box state in hogland. Only when both the host and pen
- // id are known.
- // Guard the literal 'None' too — the CLI parser can surface
- // it as a string, and it would otherwise be truthy here.
- const pen = process.env.PEN;
- const consoleHost = process.env.CONSOLE_HOST;
- const adminUrl = (consoleHost && pen && pen !== 'None')
- ? `https://${consoleHost}/console/fleet/pens/${pen}`
- : null;
- const marker = '<!-- hogbox-preview-comment -->';
- const body =
- `${marker}\n### 🦔 Hogbox preview &middot; ✅ ready\n\n` +
- `### [▶ Open the preview](${url})\n\n` +
- `| | |\n|--|--|\n` +
- `| 🔑 **Login** | \`[email protected]\` / \`12345678\` (demo data) |\n` +
- `| 🧩 **Running** | ${running} |\n` +
- `| 🔗 **Link** | **stable across rebuilds** — a re-push swaps the box underneath, the URL stays |\n` +
- `| 🔒 **Access** | tailnet only (PostHog VPN) |\n` +
- (adminUrl ? `| 🛠️ **Admin** | [inspect & debug state in hogland](${adminUrl}) |\n` : ``) +
- `| 💤 **Idle** | sleeps after ~30 min idle (snapshot to S3, zero node cost) and wakes on your next visit in ~30s, behind a brief "waking up" screen |\n\n` +
- `<sub>commit \`${(process.env.SHA || '').slice(0, 7)}\` &middot; box \`${process.env.BOX}\` &middot; ready in ${secs}s (push → usable) &middot; ` +
- `<a href="${runUrl}">build log</a> &middot; rebuilds on every push, torn down on close</sub>`;
- const { data: comments } = await github.rest.issues.listComments({ ...context.repo, issue_number: pr });
- const ex = comments.find(c => c.body.includes(marker));
- if (ex) await github.rest.issues.updateComment({ ...context.repo, comment_id: ex.id, body });
- else await github.rest.issues.createComment({ ...context.repo, issue_number: pr, body });
+
+ # SHA comes from the job env (announce job outputs).
+ - name: Post ready section
+ if: steps.outcome.outputs.ok == 'true'
+ continue-on-error: true
+ env:
+ GITHUB_TOKEN: ${{ github.token }}
+ PR_NUMBER: ${{ env.PR }}
+ URL: ${{ steps.build.outputs.url }}
+ BOX: ${{ steps.build.outputs.box_id }}
+ PEN: ${{ steps.build.outputs.pen_id }}
+ SWAP: ${{ steps.fe.outputs.swap }}
+ READY_SECONDS: ${{ steps.ready.outputs.secs }}
+ # The hogland admin/console host (same value as the OIDC audience).
+ CONSOLE_HOST: ${{ env.HOG_OIDC_AUDIENCE }}
+ run: node .github/scripts/post-hogbox-preview-section.mjs ready
# Load-bearing: runs on the very failures the chain above swallows, so an
# unguarded API call would hand the red X straight back.
- - name: Report failure (deployment + comment)
+ - name: Report failure (deployment)
if: steps.outcome.outputs.ok != 'true'
continue-on-error: true
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
@@ -732,17 +719,14 @@ jobs:
environment, log_url: runUrl, description: 'Preview build failed',
});
}
- const marker = '<!-- hogbox-preview-comment -->';
- const body =
- `${marker}\n### 🦔 Hogbox preview &middot; ❌ build failed\n\n` +
- `The preview didn't come up for commit \`${(process.env.SHA || '').slice(0, 7)}\`. ` +
- `See the **[build log](${runUrl})** for the failing step. It'll retry on the next push.\n\n` +
- `<sub>Previews are optional and never block merging. A failure here is often a hogland or tailnet ` +
- `hiccup rather than anything in your PR, so the check stays green and this comment is the status.</sub>`;
- const { data: comments } = await github.rest.issues.listComments({ ...context.repo, issue_number: pr });
- const ex = comments.find(c => c.body.includes(marker));
- if (ex) await github.rest.issues.updateComment({ ...context.repo, comment_id: ex.id, body });
- else await github.rest.issues.createComment({ ...context.repo, issue_number: pr, body });
+
+ - name: Post failure section
+ if: steps.outcome.outputs.ok != 'true'
+ continue-on-error: true
+ env:
+ GITHUB_TOKEN: ${{ github.token }}
+ PR_NUMBER: ${{ env.PR }}
+ run: node .github/scripts/post-hogbox-preview-section.mjs failed
# ----------------------------------------------------------------------------
# Fast-path teardown, decided by the `decide` job: either the `hogbox-preview`
@@ -766,7 +750,7 @@ jobs:
permissions:
contents: read
id-token: write # tailnet join + hogland token — no PR code in this job
- pull-requests: write # flip the sticky comment to torn-down
+ pull-requests: write # flip the CI report section to torn-down
deployments: write # mark the preview Deployment inactive
steps:
# Teardown runs the tool with the hogland token, so check it out from
@@ -812,11 +796,11 @@ jobs:
working-directory: tools/hogbox-preview
run: uv run --no-project --with posthog-hogland==0.3.0 --python 3.12 python -m hogbox_preview --host "$HOG_HOST" --name "preview-pr-${PR}" destroy
- # Don't leave a green "ready" Deployment + comment pointing at a box
+ # Don't leave a green "ready" Deployment + section pointing at a box
# that no longer exists: mark every deployment of this PR's
- # environment inactive and flip the sticky comment. (The close path
+ # environment inactive and flip the CI report section. (The close path
# gets the same treatment from pr-cleanup.yml's deployment reaper.)
- - name: Deactivate deployment + update comment
+ - name: Deactivate deployment
if: always()
continue-on-error: true
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
@@ -832,13 +816,11 @@ jobs:
environment, description: 'Preview torn down',
});
}
- const marker = '<!-- hogbox-preview-comment -->';
- const { data: comments } = await github.rest.issues.listComments({ ...context.repo, issue_number: pr });
- const ex = comments.find(c => c.body.includes(marker));
- if (ex) {
- const body =
- `${marker}\n### 🦔 Hogbox preview &middot; 💤 torn down\n\n` +
- `The preview for this PR was torn down (label removed or \`no-preview\` added). ` +
- `Re-add the \`hogbox-preview\` label to bring it back.`;
- await github.rest.issues.updateComment({ ...context.repo, comment_id: ex.id, body });
- }
+
+ - name: Post torn-down section
+ if: always()
+ continue-on-error: true
+ env:
+ GITHUB_TOKEN: ${{ github.token }}
+ PR_NUMBER: ${{ env.PR }}
+ run: node .github/scripts/post-hogbox-preview-section.mjs torn-down
diff --git a/frontend/bin/ci-report/update-ci-report.mjs b/frontend/bin/ci-report/update-ci-report.mjs
index 0f33b2ca469d..22b05e9f429b 100644
--- a/frontend/bin/ci-report/update-ci-report.mjs
+++ b/frontend/bin/ci-report/update-ci-report.mjs
@@ -40,6 +40,7 @@ export const SECTIONS = [
{ id: 'hogql-parser-rs', title: 'hogql-parser-rs version' },
{ id: 'replay-anonymizer-crate', title: 'posthog-replay-anonymizer version' },
{ id: 'generated-docs', title: 'Generated docs' },
+ { id: 'hogbox-preview', title: 'Hogbox preview' },
{ id: 'docs-preview', title: 'Docs preview' },
{ id: 'hobby-deploy', title: 'Hobby preview' },
{ id: 'survey-sdk', title: 'Survey SDK reminder' },
@@ -168,7 +169,8 @@ export async function gh(token, url, options = {}) {
}
// Resolve the PR-comment context from the Actions environment, or null (reason logged)
-// when this run cannot comment — missing env or not a pull_request event.
+// when this run cannot comment — missing env, or neither a pull_request event nor a
+// PR_NUMBER (a workflow_dispatch payload carries no pull_request, so its caller names the PR).
export function resolvePrContext(activity) {
const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN
const repo = process.env.GITHUB_REPOSITORY
@@ -177,9 +179,10 @@ export function resolvePrContext(activity) {
console.info(`Missing GitHub environment (token/repository/event) — skipping ${activity}.`)
return null
}
- const prNumber = JSON.parse(fs.readFileSync(eventPath, 'utf-8')).pull_request?.number
+ const prNumber =
+ JSON.parse(fs.readFileSync(eventPath, 'utf-8')).pull_request?.number || Number(process.env.PR_NUMBER) || null
if (!prNumber) {
- console.info(`Not a pull request event — skipping ${activity}.`)
+ console.info(`Not a pull request event and no PR_NUMBER — skipping ${activity}.`)
return null
}
return { token, repo, prNumber }
@@ -252,9 +255,17 @@ function isWriteConflict(err) {
* @param {{ id: string, summary: string, body: string }} section
* @param {{ legacyPrefixes?: string[] }} options
*/
-export async function clearSectionIfPresent({ id, summary, body }, options = {}) {
+export async function clearSectionIfPresent(section, options = {}) {
+ await updateSectionIfPresent({ ...section, status: 'ok' }, options)
+}
+
+/**
+ * @param {{ id: string, status: string, summary: string, body: string }} section
+ * @param {{ legacyPrefixes?: string[] }} options
+ */
+export async function updateSectionIfPresent({ id, status, summary, body }, options = {}) {
const { legacyPrefixes = [] } = options
- const context = resolvePrContext(`clearing "${id}"`)
+ const context = resolvePrContext(`updating "${id}"`)
if (!context) {
return
}
@@ -267,10 +278,10 @@ export async function clearSectionIfPresent({ id, summary, body }, options = {})
return
}
if (!reportComment || !parseSections(reportComment.body).has(id)) {
- console.info(`No existing "${id}" section — nothing to clear.`)
+ console.info(`No existing "${id}" section — nothing to update.`)
return
}
- await postSection({ id, status: 'ok', summary, body })
+ await postSection({ id, status, summary, body })
}
// Post or update this run's section into the shared comment. Fork PRs run with a
diff --git a/frontend/bin/ci-report/update-ci-report.test.ts b/frontend/bin/ci-report/update-ci-report.test.ts
index 0830d19ef57d..502f8c7747c6 100644
--- a/frontend/bin/ci-report/update-ci-report.test.ts
+++ b/frontend/bin/ci-report/update-ci-report.test.ts
@@ -9,6 +9,7 @@ import {
postSection,
renderComment,
STATUS_EMOJI,
+ updateSectionIfPresent,
upsertSection,
} from './update-ci-report.mjs'
@@ -323,6 +324,48 @@ describe('ci-report section helper', () => {
expect(github.comments.some((comment) => parseSections(comment.body).has('bundle-size'))).toBe(true)
})
+ it('posts to PR_NUMBER on a workflow_dispatch run, whose event has no pull_request', async () => {
+ const pullRequestEventPath = process.env.GITHUB_EVENT_PATH
+ const dispatchEventPath = path.join(tmpDir, 'dispatch-event.json')
+ fs.writeFileSync(dispatchEventPath, JSON.stringify({ inputs: { pr_number: '7' } }))
+ process.env.GITHUB_EVENT_PATH = dispatchEventPath
+ process.env.PR_NUMBER = '7'
+ try {
+ const github = fakeGitHub()
+ const fakeFetch = globalThis.fetch
+ const postUrls: string[] = []
+ globalThis.fetch = async (url: RequestInfo | URL, options: RequestInit = {}): Promise<Response> => {
+ if (options.method === 'POST') {
+ postUrls.push(String(url))
+ }
+ return fakeFetch(url, options)
+ }
+ await postSection({ id: 'hogbox-preview', status: 'info', summary: 'building', body: 'BOX' }, opts)
+ expect(postUrls).toEqual(['https://api.github.com/repos/PostHog/posthog/issues/7/comments'])
+ expect(parseSections(github.comments[0].body).has('hogbox-preview')).toBe(true)
+ } finally {
+ process.env.GITHUB_EVENT_PATH = pullRequestEventPath
+ delete process.env.PR_NUMBER
+ }
+ })
+
+ it.each([
+ {
+ name: 'leaves a report without the section untouched',
+ initial: [{ id: 'bundle-size', status: 'ok', summary: 'b', body: 'BUNDLE' }],
+ expected: undefined,
+ },
+ {
+ name: 'rewrites an existing section with the given status',
+ initial: [{ id: 'hogbox-preview', status: 'ok', summary: 'ready', body: 'READY' }],
+ expected: { status: 'info', summary: 'torn down', inner: 'GONE' },
+ },
+ ])('updateSectionIfPresent $name', async ({ initial, expected }) => {
+ const github = fakeGitHub([{ body: renderComment(build(initial)) }])
+ await updateSectionIfPresent({ id: 'hogbox-preview', status: 'info', summary: 'torn down', body: 'GONE' })
+ expect(parseSections(github.comments[0].body).get('hogbox-preview')).toEqual(expected)
+ })
+
it('removes a resolved legacy comment even when no report section exists yet', async () => {
const github = fakeGitHub([{ body: '## Old result\nNo longer relevant' }])
await clearSectionIfPresent(
diff --git a/tools/hogbox-preview/README.md b/tools/hogbox-preview/README.md
index 30dbcb763c87..d65f1aaf2902 100644
--- a/tools/hogbox-preview/README.md
+++ b/tools/hogbox-preview/README.md
@@ -107,8 +107,9 @@ build.
## Reporting & lifecycle
-- **PR comment** — a sticky comment (`<!-- hogbox-preview-comment -->`) staged
- building → ready → failed, with the URL, login, and what's running.
+- **PR comment** — a `Hogbox preview` section in the shared `🤖 CI report` comment
+ (`.github/scripts/post-hogbox-preview-section.mjs`), staged
+ building → ready → failed → torn down, with the URL, login, and what's running.
- **GitHub Deployment** — a `preview-pr-<n>` environment (in_progress → success
/failure + URL), so the preview shows in the PR's Deployments UI.
- **Teardown** — on PR close (`pr-closed.yml`) + on the fast path in