Skip to content

feat: seed deterministic project token for demo data#61016

Merged
joshsny merged 2 commits into
masterfrom
posthog-code/deterministic-demo-project-token
Jun 1, 2026
Merged

feat: seed deterministic project token for demo data#61016
joshsny merged 2 commits into
masterfrom
posthog-code/deterministic-demo-project-token

Conversation

@joshsny
Copy link
Copy Markdown
Contributor

@joshsny joshsny commented Jun 1, 2026

Problem

When you run generate_demo_data locally we seed the Hedgebox demo team and a fixed personal API key (DEV_API_KEY), but the project's API token (api_token, the phc_... value) is random on every run. That means anything that wants to send events into the demo project — local SDK setups, the hedgebox-dummy app, scripts — has to look up the freshly generated token each time instead of hardcoding a known value.

Changes

  • generate_demo_data now assigns the demo team a deterministic project API token, phc_localposthogprojecttoken, but only on the fresh-account path (not when targeting an existing --team-id or resetting the master project).
  • Because api_token is unique, the token is first released from any team that claimed it on a previous run (reassigned a random token), so re-running the command always hands the predictable token to the latest demo team.
  • Threaded an optional api_token argument through MatrixManager.ensure_account_and_savecreate_team; it defaults to None, so the signup and eval-data-setup callers are unaffected.
  • The completion output now prints the project API token alongside the login link and password.

How did you test this code?

I'm an agent. I syntax-checked the changed files; ruff and the app runtime aren't available in this environment, so I did not run the command or the test suite. The change is additive and gated behind the new-account branch — existing callers pass no api_token and keep the random default.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Docs update

🤖 Agent context

Authored by PostHog Code (Claude) at the request of the repo owner. The ask was to make the demo project token deterministic (phc_localposthogprojecttoken) only for generate_demo_data, mirroring how DEV_API_KEY is fixed locally.

Key decisions:

  • Scoped the deterministic token to the new-account branch only, leaving --team-id / master-project flows and the signup/eval callers on the random default by making the new api_token parameter optional.
  • Handled the unique-constraint collision on re-runs by releasing the token from the prior holder before creating the new team, rather than catching IntegrityError, so the freshest demo team always owns the predictable token.

Created with PostHog Code

Give the locally seeded demo team a fixed project API token
(phc_localposthogprojecttoken) when running generate_demo_data for a
fresh account, so SDKs and tools can target it without looking up the
random token. The token is released from any team that claimed it on a
previous run, and is only applied on the new-account path (not when
seeding into an existing project or the master project).

Generated-By: PostHog Code
Task-Id: 0fc47ef9-6dd7-40d6-a698-205ff5727773
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 1, 2026

Hey @joshsny! 👋

It looks like your git author email on this PR isn't your @posthog.com address (joshua@timenavi.com). Since you're on the PostHog team, it's worth pointing your local git author email at your @posthog.com address. Why it matters:

  • Consistent work identity in git history — internal tooling that attributes commits to team members keys off your @posthog.com address.
  • Keeps team contributions easy to tell apart from external community ones when scanning history.

You can fix it for this repo with:

git config user.email "you@posthog.com"

Or set it globally with git config --global user.email "you@posthog.com". No need to redo this PR — just a nudge for next time. 🙂

@joshsny joshsny requested a review from a team June 1, 2026 20:40
@joshsny joshsny marked this pull request as ready for review June 1, 2026 20:40
Copilot AI review requested due to automatic review settings June 1, 2026 20:40
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Jun 1, 2026

Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
posthog/management/commands/generate_demo_data.py:178-191
**Non-atomic token release and team creation**

The `Team.objects.filter(...).update(...)` that releases the old token runs outside the `transaction.atomic()` block inside `ensure_account_and_save`. If the process is killed — or `ensure_account_and_save` raises an unexpected (non-`ValidationError`) exception — between these two calls, `DEMO_PROJECT_API_TOKEN` is orphaned: no team holds it, and the newly created team will receive a random token instead. The next successful run will self-heal because the `filter` returns empty and the new team gets the deterministic token, but a partial failure on the current run leaves the dev environment in an inconsistent state until re-run.

Reviews (1): Last reviewed commit: "feat: seed deterministic project token f..." | Re-trigger Greptile

Comment thread posthog/management/commands/generate_demo_data.py Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes the locally seeded demo project easier to target by assigning a deterministic project API token (phc_localposthogprojecttoken) when generate_demo_data creates a fresh demo account, and it threads an optional api_token through MatrixManager.ensure_account_and_save into team creation.

Changes:

  • Add a fixed demo project API token and attempt to “release” it from any prior team before creating a new demo team.
  • Extend MatrixManager.ensure_account_and_save to accept an optional api_token and pass it through to create_team.
  • Print the project API token in the command’s completion output.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
posthog/management/commands/generate_demo_data.py Adds deterministic token assignment/release and prints token on completion.
posthog/demo/matrix/manager.py Threads optional api_token into demo team creation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread posthog/management/commands/generate_demo_data.py Outdated
Comment thread posthog/management/commands/generate_demo_data.py Outdated
Copy link
Copy Markdown
Member

@gantoine gantoine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Address code-review feedback on the deterministic demo project token:

- Gate the deterministic token to local dev (DEBUG and not TEST). In
  tests (Playwright setup seeds many workspaces) and shared/cloud
  environments, fall back to random tokens so runs don't fight over the
  unique token or mutate unrelated projects.
- Move the token release inside the existing atomic block in
  ensure_account_and_save, so a partial failure can't orphan the token.
- Print the team's actual api_token in the completion output instead of
  assuming the deterministic constant.

Generated-By: PostHog Code
Task-Id: 0fc47ef9-6dd7-40d6-a698-205ff5727773
@joshsny joshsny enabled auto-merge (squash) June 1, 2026 21:09
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 1, 2026

🎭 Playwright report · View test results →

⚠️ 2 flaky tests:

  • Inline editing insight title via compact card popover (chromium)
  • Save an insight, make changes, discard them, and save a copy (chromium)

These issues are not necessarily caused by your changes.
Annoyed by this comment? Help fix flakies and failures and it'll disappear!

@joshsny joshsny merged commit 28c87fb into master Jun 1, 2026
258 of 262 checks passed
@joshsny joshsny deleted the posthog-code/deterministic-demo-project-token branch June 1, 2026 21:33
@deployment-status-posthog
Copy link
Copy Markdown

deployment-status-posthog Bot commented Jun 1, 2026

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-06-01 21:57 UTC Run
prod-us ✅ Deployed 2026-06-02 10:18 UTC Run
prod-eu ✅ Deployed 2026-06-02 10:20 UTC Run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants