Flag Previews
Track 3 Build on Open Source Flagsmith Toggle Wars hackathon

A Flagsmith environment for every pull request.

Created when the PR opens, updated on each push, deleted when it closes. The flag differences are posted as one PR comment that is edited in place.

For teams that self-host Flagsmith and want to test a PR's flag changes without sharing one staging environment.

bash quickstart.sh

Measured: 64 s on one run, with the Flagsmith images already pulled. A first image pull adds download time that was not measured. Needs Python 3.10+, Docker (about 1 GiB free RAM) and curl.

The PR comment flagprev wrote: a table of three flags, banner_text, max_items and new_checkout, with baseline and this-PR values
The PR comment. A local HTML render of the Markdown file, not a screenshot of github.com, because the PR event is simulated.
The problem

One staging environment, five pull requests.

Today

Priya's PR turns on new_checkout. Sam's PR changes max_items.

Both test in the shared staging environment. Whoever saved last decides what the other sees.

Flagsmith does not create an environment per pull request by itself. So teams usually pick one of two paths:

  • Test every PR's flags in one shared staging environment, so PRs overwrite each other.
  • Skip flag testing before merge.
With Flag Previews

Each PR gets its own copy.

The copy starts from a baseline (default Development) and applies that PR's own overrides.

The PR's code runs against its own flag values, with its own client key. The baseline stays unchanged. When the PR closes, the copy is removed.

The people and tools above are examples to tell the story. They are not users of this project.

What the code sees

Same app, two keys, different answers.

The demo app reads the flags once with pr-42's client key and once with the baseline key. Output from the 29 Nov run in docs/DEMO-OUTPUT.txt.

3 of 3 flags differ (real Flagsmith, simulated PR event)

FlagBaselinepr-42
banner_texton, "Welcome"on, "Hello preview"
max_itemson, 10on, 25
new_checkoutoff, "v1"on, "v2"

The overrides file lives with the PR

A .flagprev/overrides.json in the PR's repo. Unknown flags and wrong value types are rejected before anything is created.

{"flags": {
  "new_checkout": {"enabled": true, "value": "v2"},
  "banner_text":  {"value": "Hello preview"}}}
Flagsmith dashboard on the pr-42 environment: banner_text Hello preview, max_items 25, new_checkout v2, all switched on
The pr-42 environment in Flagsmith after a simulated opened event. Real Flagsmith 2.295.2.
Flagsmith dashboard on the Development baseline: new_checkout is switched off
The Development baseline, unchanged: new_checkout off, "v1".
How it works

The PR's lifecycle drives the environment's lifecycle.

PR events become Flagsmith admin API calls. The same pr-<n> is re-synced on every push, so it never drifts from its baseline.

Pull request lifecycle against Flagsmith Three PR events flow to flagprev. Opened or reopened clones the baseline into pr-n and posts a comment. A push synchronize re-syncs the same pr-n and edits the comment. Closed deletes pr-n. GITHUB PR EVENT FLAGPREV FLAGSMITH (ADMIN API) opened / reopenedsigned webhook or CI synchronize (push)or flagprev event closedflagprev gc sweeps strays validate, then cloneupsert, per-PR lock re-sync to current baselineplus the PR's overrides delete, after checksonly ^pr-\d+$ + marker pr-42 createdclone of Development, new client key pr-42 updated, same environmentcomment edited, not duplicated pr-42 deletedcomment says so Baseline (Development) is only read, never changed. PR code reads flags with pr-42's client key.

opened / reopened

Clone the baseline into pr-<n>, apply the PR's overrides, comment the flag diff.

synchronize

Re-sync the same pr-<n> to the current baseline plus the PR's overrides. Edit the same comment.

closed

Delete pr-<n>. flagprev gc deletes strays: closed PRs and old previews.

Flagsmith features used

Only the admin API, standard library only.

Tested against Flagsmith 2.295.2, self-hosted. Flagsmith Cloud was not tried. It needs an admin-level API token, because cloning and deleting environments is only possible through the admin API.

Which Flagsmith endpoints, and where in the code
Flagsmith featureHow Flag Previews uses itCode
Admin REST API, token authSends Authorization: Token ... only. No SDK in the main code.src/flagprev/core.py
Environment clone and deletePOST /environments/{key}/clone/, PATCH and DELETE /environments/{key}/core.py
Feature statesReads and sets enabled state and value through /features/featurestates/core.py
Segment overridesReads, changes and creates overrides for segments that already existcore.py
Multivariate optionsSets the preview's variant split with percentagescore.py
Client flag evaluationThe demo app reads GET /flags/ with the clone's client key and with the baseline keydemo/app.py
  • Ownership marker. A preview's description holds a small flagprev:{...} marker. That is how an environment is known to be ours; there is no separate registry.
  • Safe deletion. The name must match ^pr-\d+$ and carry the marker. Development, Production and a hand-made pr-123 without the marker are refused (tested).
  • No keys in comments. The comment links to the project page, not the environment page, because Flagsmith's environment URL contains the environment key. Override values are escaped so a value cannot break the table or ping someone.
  • Webhook. HMAC-SHA256 signature check with a constant-time compare, 401 on a bad signature (nothing is created), answers 202 at once and works on a background thread.
  • What clone copies, from our own tests. Default values and enabled state, segment overrides, multivariate percentages. It does not copy identity overrides and it gives a new client key.
Results

Real Flagsmith, simulated pull requests.

Every Flagsmith call is real. The PR events are simulated by tools/simulate_pr_event.py, which sends a GitHub-shaped payload signed with the real HMAC scheme. There are no real users and no usage numbers.

67
tests pass on a fresh local stack: 49 offline, 18 live, 0 skipped (29 Nov 03:12 IST)
measured
3 of 3
flags differ between pr-42 and baseline in the demo
simulated PR event
64 s
for bash quickstart.sh from a clean copy, images already pulled
measured, one run
910 MiB
peak memory of the 4 containers, sampled every 3 s, so the true peak may be a little higher
measured, one run
Where the 64 seconds go

Where the 64 seconds go

Measured steps of one bash quickstart.sh run, 29 Nov 2026, macOS arm64. Bootstrap took under 1 s; it is drawn as a sliver.

Flagsmith healthy36 s
Install6 s
Bootstrap<1 s
Smoke check20 s
Demo2 s

Tested against real GitHub on 27 Nov with a temporary private repo and PR (comment created, edited in place, updated on close; real webhook deliveries accepted). That repo, webhook and tunnel were deleted the same day, so no evidence a judge can open remains. The workflow file has never run on GitHub.

What it does not do

Known gaps, stated up front.

No public repository, no package.Install is "take this folder". No PyPI package, no published Docker image, no Marketplace action.
The GitHub Actions workflow has never run on GitHub.The YAML parses and the flagprev event command it calls is unit-tested offline.
No screenshot of a real GitHub PR.The comment image is a local render of the Markdown. The Flagsmith dashboard screenshots are real.
No users besides the author.No adoption or usage numbers are claimed, and no experiment or A/B claims.
Identity overrides are not cloned.Flagsmith's clone does not copy them, and per-user data should not be copied into PR environments anyway.
Segments and multivariate values are not created.Segment overrides work for segments that exist. Variant values and per-segment multivariate splits cannot be edited.
Flagsmith Cloud and other versions are untested.Only 2.295.2, self-hosted.
Overlaps with other Track 3 entries.A flag diff on a PR already exists elsewhere, and FlagLens covers more of Flagsmith. The different part is a real environment per PR that the PR's code can run against, cleaned up automatically.

Locks are per machine: they do not reach across machines. The example workflow installs flagprev from the PR's own checkout, so in a repository that takes PRs from people you do not trust, install it from the base branch or a pinned version instead.

Run it

One command, then the demo.

Step by step with screenshots: Run it yourself. There is no public repository yet, so start from the project folder. macOS system Python is 3.9 and does not work; brew install python@3.11 fixes that.

Quick start

PYTHON=/path/to/python3.11 bash quickstart.sh
# PYTHON only if your python3 is 3.9
# starts Flagsmith, installs, bootstraps,
# smoke check, demo. Ends with: Done.
docker compose stop   # later; keeps the data

Flagsmith runs on port 8601 (FS_PORT), the webhook service on 8610 (FLAGPREV_PORT).

Tests

python tools/seed_flags.py && \
python -m unittest discover -s tests -t .

49 offline tests and 18 live tests. The live tests need a Flagsmith with the flags new_checkout, banner_text and max_items; seed_flags.py creates them. Without a Flagsmith they are skipped.

CLI

flagprev create --pr 42 --overrides demo/overrides.json
flagprev diff --pr 42
flagprev list
flagprev destroy --pr 42
flagprev gc --open-prs 7,9 --dry-run

Webhook or CI

flagprev serve   # needs WEBHOOK_SECRET
flagprev event   # reads $GITHUB_EVENT_PATH

See .github/workflows/flag-previews.yml for the example workflow.