Repository navigation
add the flagsmith plugin #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package adapter | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/patch-it-dev/patch-it-plugins/fetch" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| // Variables, not constants, only so tests can point them at a local server. | ||
| var ( | ||
| flagsmithReleasesURL = "https://api.github.com/repos/Flagsmith/flagsmith/releases/latest" | ||
| // flagsmithSpecURLTemplate is the OpenAPI 3.1 description of Flagsmith's | ||
| // full REST API (the Admin/Management API and the SDK-facing endpoints | ||
| // together), committed at the root of Flagsmith/flagsmith as openapi.yaml | ||
| // (checked 2026-11-19). It's generated from the Django backend with | ||
| // drf-spectacular (`make generate-full-openapi`) and regenerated on every | ||
| // PR that touches api/** by a bot that commits the result back to the PR | ||
| // branch (.github/workflows/api-fix-docs-artefacts.yml), so a release tag | ||
| // always carries the spec for the code it actually ships. There's also a | ||
| // narrower sdk/openapi.yaml, filtered down to the SDK-only endpoints | ||
| // (openapi-filter-flagsmith-sdk.yml): this adapter reads the full file so | ||
| // Admin/Management API removals aren't missed. | ||
| flagsmithSpecURLTemplate = "https://raw.githubusercontent.com/Flagsmith/flagsmith/%s/openapi.yaml" | ||
| ) | ||
|
|
||
| // Flagsmith adapts Flagsmith's own OpenAPI spec for the openapi plugin. | ||
| type Flagsmith struct{} | ||
|
|
||
| var _ Adapter = Flagsmith{} | ||
|
|
||
| func (Flagsmith) API() string { return "flagsmith" } | ||
|
|
||
| // LatestTag asks GitHub for Flagsmith's newest release tag, e.g. "v2.293.0". | ||
| // Flagsmith tags a release on (almost) every merge to main via release-please, | ||
| // so releases/latest is always close to HEAD. | ||
| func (Flagsmith) LatestTag(ctx context.Context) (string, error) { | ||
| body, err := fetch.Get(ctx, flagsmithReleasesURL, githubHeaders()) | ||
| if err != nil { | ||
| return "", fmt.Errorf("looking up flagsmith's latest release: %w", err) | ||
| } | ||
| var release struct { | ||
| TagName string `json:"tag_name"` | ||
| } | ||
| if err := json.Unmarshal(body, &release); err != nil { | ||
| return "", fmt.Errorf("parsing github release response: %w", err) | ||
| } | ||
| if release.TagName == "" { | ||
| return "", fmt.Errorf("github release response had no tag_name") | ||
| } | ||
| return release.TagName, nil | ||
| } | ||
|
|
||
| // Fetch downloads the spec pinned to tag and converts it from YAML to JSON, | ||
| // since diff.Parse only reads JSON. This changes no provenance guarantee: | ||
| // normalize's excerpt already re-serializes whatever node it quotes rather | ||
| // than slicing the original bytes, for every adapter including Stripe's. | ||
| // | ||
| // Version is the release tag without its "v" ("2.293.0"), not info.version: | ||
| // that is a fixed "v1" in every release (checked 2026-11-19), so it can't | ||
| // tell two releases apart. The bare version is also what a self-hosted | ||
| // instance reports and what a customer tracks as "the Flagsmith version | ||
| // we're running". | ||
| func (Flagsmith) Fetch(ctx context.Context, tag string) (Spec, error) { | ||
| url := fmt.Sprintf(flagsmithSpecURLTemplate, tag) | ||
| body, err := fetch.Get(ctx, url, nil) // raw.githubusercontent.com needs no auth | ||
| if err != nil { | ||
| return Spec{}, fmt.Errorf("fetching flagsmith spec %s: %w", tag, err) | ||
| } | ||
|
|
||
| var doc any | ||
| if err := yaml.Unmarshal(body, &doc); err != nil { | ||
| return Spec{}, fmt.Errorf("parsing flagsmith spec %s as yaml: %w", tag, err) | ||
| } | ||
| content, err := json.Marshal(doc) | ||
| if err != nil { | ||
| return Spec{}, fmt.Errorf("converting flagsmith spec %s to json: %w", tag, err) | ||
| } | ||
|
|
||
| return Spec{ | ||
| Content: content, | ||
| Version: strings.TrimPrefix(tag, "v"), | ||
| SourceURL: url, | ||
| }, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package openapi | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/patch-it-dev/patch-it-plugins/adapter" | ||
| "github.com/patch-it-dev/patch-it-plugins/kit/kittest" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| // flagsmithTags are the saved copies under testdata/ (checked 2026-11-19): | ||
| // tag -> file and the full commit SHA each was copied from. | ||
| // | ||
| // v2.286.0 / v2.287.0 are release tags: "feat(API): Remove the experimental | ||
| // flag update endpoints" (#8736, commit 20cd1c2da) removed three whole | ||
| // operations (POST .../delete-segment-override/, .../update-flag-v1/, | ||
| // .../update-flag-v2/) between them, each marked "EXPERIMENTAL ENDPOINT - | ||
| // Subject to change without notice" in its own description, and listed under | ||
| // "Features" in the v2.287.0 release notes. | ||
| // | ||
| // desc-c3ef832 / desc-f504f16 are two commits, not tags (git log -S on | ||
| // openapi.yaml): "feat: rename admin api to management" (#8464, commit | ||
| // f504f160d, parent c3ef83244) only reworded two description strings ("the | ||
| // Admin API" to "the Management API"); nothing it names changed. The pinned | ||
| // drop test. | ||
| var flagsmithTags = map[string]string{ | ||
| "v2.286.0": "testdata/flagsmith-v2.286.0.yaml", | ||
| "v2.287.0": "testdata/flagsmith-v2.287.0.yaml", | ||
| "c3ef832": "testdata/flagsmith-desc-c3ef832.yaml", | ||
| "f504f16": "testdata/flagsmith-desc-f504f16.yaml", | ||
| } | ||
|
|
||
| func TestFlagsmithRegistersItself(t *testing.T) { | ||
| kittest.Registered(t, Plugin{Adapter: adapter.Flagsmith{}}) | ||
| } | ||
|
|
||
| // fakeFlagsmithAdapter serves the saved copies, cited by the real URL each | ||
| // was copied from, converted from YAML to JSON and the tag's leading "v" | ||
| // trimmed from Version the same way adapter.Flagsmith.Fetch does. | ||
| type fakeFlagsmithAdapter struct{} | ||
|
|
||
| func (fakeFlagsmithAdapter) API() string { return "flagsmith" } | ||
|
|
||
| func (fakeFlagsmithAdapter) LatestTag(context.Context) (string, error) { return "v2.287.0", nil } | ||
|
|
||
| func (fakeFlagsmithAdapter) Fetch(_ context.Context, tag string) (adapter.Spec, error) { | ||
| file, ok := flagsmithTags[tag] | ||
| if !ok { | ||
| return adapter.Spec{}, fmt.Errorf("no saved copy for %s", tag) | ||
| } | ||
| body, err := os.ReadFile(file) | ||
| if err != nil { | ||
| return adapter.Spec{}, err | ||
| } | ||
| var doc any | ||
| if err := yaml.Unmarshal(body, &doc); err != nil { | ||
| return adapter.Spec{}, err | ||
| } | ||
| content, err := json.Marshal(doc) | ||
| if err != nil { | ||
| return adapter.Spec{}, err | ||
| } | ||
| return adapter.Spec{ | ||
| Content: content, | ||
| Version: strings.TrimPrefix(tag, "v"), | ||
| SourceURL: "https://raw.githubusercontent.com/Flagsmith/flagsmith/" + tag + "/openapi.yaml", | ||
| }, nil | ||
| } | ||
|
|
||
| // The golden test and the replay fixture: the real removal of the three | ||
| // experimental flag-update endpoints between v2.286.0 and v2.287.0. | ||
| func TestFlagsmithGolden(t *testing.T) { | ||
| ctx := context.Background() | ||
| env := kittest.Env(t, nil) | ||
| sink := env.Baselines.(kittest.Sink) | ||
| p := Plugin{Adapter: fakeFlagsmithAdapter{}, Keep: requestSide} | ||
| if rejected, err := kittest.Replay(ctx, env, sink, p, "v2.286.0", "v2.287.0"); err != nil || rejected { | ||
| t.Fatalf("replay: rejected=%v err=%v", rejected, err) | ||
| } | ||
| kittest.Golden(t, "testdata/golden/flagsmith.json", kittest.Stored(t, sink)) | ||
| } | ||
|
|
||
| // The pinned drop test: a commit that only reworded two description strings | ||
| // ("the Admin API" to "the Management API") must produce no event. | ||
| func TestFlagsmithDropsADescriptionOnlyChange(t *testing.T) { | ||
| ctx := context.Background() | ||
| env := kittest.Env(t, nil) | ||
| sink := env.Baselines.(kittest.Sink) | ||
| p := Plugin{Adapter: fakeFlagsmithAdapter{}, Keep: requestSide} | ||
| if rejected, err := kittest.Replay(ctx, env, sink, p, "c3ef832", "f504f16"); err != nil || rejected { | ||
| t.Fatalf("replay: rejected=%v err=%v", rejected, err) | ||
| } | ||
| if got := kittest.Stored(t, sink); len(got) != 0 { | ||
| t.Errorf("%d events from a description-only change, want none", len(got)) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| openapi: 3.1.0 | ||
| info: | ||
| title: Flagsmith API | ||
| version: v1 | ||
| description: 'Flagsmith''s Core and SDK APIs. Check out <a href=''https://docs.flagsmith.com''>Flagsmith documentation</a>.' | ||
| contact: | ||
| email: [email protected] | ||
| license: | ||
| name: BSD License | ||
| identifier: BSD-3-Clause | ||
| servers: | ||
| - url: 'https://api.flagsmith.com' | ||
| description: Flagsmith API | ||
| paths: {} | ||
| components: | ||
| schemas: | ||
| TokenExchangeResponse: | ||
| type: object | ||
| properties: | ||
| access_token: | ||
| description: Short-lived access token for the Admin API. | ||
| type: string | ||
| token_type: | ||
| description: Always "Bearer". | ||
| type: string | ||
| expires_in: | ||
| description: Access token lifetime in seconds. | ||
| type: integer | ||
| required: | ||
| - access_token | ||
| securitySchemes: | ||
| Master API Key: | ||
| type: apiKey | ||
| in: header | ||
| name: Authorization | ||
| description: 'For Admin API endpoints. <a href=''https://docs.flagsmith.com/clients/rest#private-api-endpoints''>Find out more</a>.' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| openapi: 3.1.0 | ||
| info: | ||
| title: Flagsmith API | ||
| version: v1 | ||
| description: 'Flagsmith''s Core and SDK APIs. Check out <a href=''https://docs.flagsmith.com''>Flagsmith documentation</a>.' | ||
| contact: | ||
| email: [email protected] | ||
| license: | ||
| name: BSD License | ||
| identifier: BSD-3-Clause | ||
| servers: | ||
| - url: 'https://api.flagsmith.com' | ||
| description: Flagsmith API | ||
| paths: {} | ||
| components: | ||
| schemas: | ||
| TokenExchangeResponse: | ||
| type: object | ||
| properties: | ||
| access_token: | ||
| description: Short-lived access token for the Management API. | ||
| type: string | ||
| token_type: | ||
| description: Always "Bearer". | ||
| type: string | ||
| expires_in: | ||
| description: Access token lifetime in seconds. | ||
| type: integer | ||
| required: | ||
| - access_token | ||
| securitySchemes: | ||
| Master API Key: | ||
| type: apiKey | ||
| in: header | ||
| name: Authorization | ||
| description: 'For Management API endpoints. <a href=''https://docs.flagsmith.com/integrating-with-flagsmith/flagsmith-api-overview/management-api''>Find out more</a>.' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Golden doesn't match what production emits.
adapter.Flagsmith.FetchsetsVersion: strings.TrimPrefix(tag, "v")("2.287.0"), but this fake setsVersion: tag. As a resulttestdata/golden/flagsmith.jsonrecords"version": "v2.287.0"/"previous_version": "v2.286.0", which the real plugin will never produce. Vercel's fake mirrors its adapter on this point. Suggested fix (then regenerate the golden and say so in the PR):(This needs
"strings"in the imports.)