Skip to content

Commit adddf47

Browse files
ci: Run the Flagsmith SDK behavioural spec (#263)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4fafcd9 commit adddf47

4 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎.github/workflows/sdk-spec.yml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: SDK spec
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
sdk-spec:
13+
# Keep the ref in `uses:` and `spec-ref` in sync.
14+
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.3.0
15+
with:
16+
spec-ref: v0.3.0
17+
language: python

‎renovate.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
"extends": ["config:recommended", ":semanticCommitScopeDisabled"],
44
"ignorePresets": [":semanticPrefixFixDepsChoreOthers"],
55
"semanticCommitType": "deps",
6+
"customManagers": [
7+
{
8+
"customType": "regex",
9+
"managerFilePatterns": ["/^\\.github/workflows/sdk-spec\\.yml$/"],
10+
"matchStrings": ["spec-ref: (?<currentValue>v[\\d.]+)"],
11+
"depNameTemplate": "Flagsmith/sdk-spec",
12+
"datasourceTemplate": "github-tags"
13+
}
14+
],
615
"packageRules": [
716
{
817
"matchManagers": ["pip_requirements", "pip-compile", "pep621", "poetry", "github-actions"],
@@ -22,6 +31,11 @@
2231
{
2332
"matchPackageNames": ["flagsmith-flag-engine"],
2433
"enabled": true
34+
},
35+
{
36+
"matchPackageNames": ["Flagsmith/sdk-spec"],
37+
"groupName": "SDK spec",
38+
"enabled": true
2539
}
2640
]
2741
}

‎sdk-spec/Dockerfile‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Runs this SDK, built from source, under the Flagsmith SDK behavioural spec.
2+
# docker build -f sdk-spec/Dockerfile --build-context harness=<sdk-spec>/harness/python .
3+
FROM python:3.12-slim
4+
5+
WORKDIR /sdk
6+
COPY pyproject.toml poetry.lock README.md ./
7+
COPY flagsmith ./flagsmith
8+
RUN pip install --no-cache-dir .
9+
10+
COPY --from=harness harness.py /harness/harness.py
11+
COPY sdk-spec/steps.py ./sdk-spec/steps.py
12+
13+
ENTRYPOINT ["python", "/harness/harness.py", "sdk-spec/steps.py"]

‎sdk-spec/steps.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Steps for the Flagsmith SDK behavioural spec: https://github.com/Flagsmith/sdk-spec
3+
4+
Each function implements the Gherkin step in the comment above it.
5+
"""
6+
7+
import typing
8+
9+
from flagsmith import Flagsmith
10+
from flagsmith.models import Flags
11+
from flagsmith.types import TraitMapping
12+
13+
SpecFlag = dict[str, typing.Any]
14+
15+
16+
def as_spec_flags(flags: Flags) -> list[SpecFlag]:
17+
return [
18+
{"feature": flag.feature_name, "enabled": flag.enabled, "value": flag.value}
19+
for flag in flags.all_flags()
20+
]
21+
22+
23+
# Create the SDK client
24+
def create_client(environment_key: str, api_url: str) -> Flagsmith:
25+
return Flagsmith(environment_key=environment_key, api_url=api_url)
26+
27+
28+
# When the application gets the environment flags
29+
def get_environment_flags(flagsmith: Flagsmith) -> list[SpecFlag]:
30+
return as_spec_flags(flagsmith.get_environment_flags())
31+
32+
33+
# When the application gets the flags for identity "{identifier}" with traits:
34+
def get_identity_flags(
35+
flagsmith: Flagsmith, identifier: str, traits: TraitMapping
36+
) -> list[SpecFlag]:
37+
return as_spec_flags(flagsmith.get_identity_flags(identifier, traits=traits))

0 commit comments

Comments
 (0)