Skip to content

Commit 18281fc

Browse files
authored
ci: Run the Flagsmith SDK behavioural spec (#237)
1 parent 6df032a commit 18281fc

4 files changed

Lines changed: 105 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: java

‎renovate.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
"ignorePresets": [":semanticPrefixFixDepsChoreOthers"],
55
"semanticCommitType": "deps",
66
"git-submodules": { "enabled": true },
7+
"customManagers": [
8+
{
9+
"customType": "regex",
10+
"managerFilePatterns": ["/^\\.github/workflows/sdk-spec\\.yml$/"],
11+
"matchStrings": ["spec-ref: (?<currentValue>v[\\d.]+)"],
12+
"depNameTemplate": "Flagsmith/sdk-spec",
13+
"datasourceTemplate": "github-tags"
14+
}
15+
],
716
"packageRules": [
817
{
918
"matchUpdateTypes": [
@@ -23,6 +32,11 @@
2332
"matchManagers": ["git-submodules"],
2433
"versioning": "semver-coerced",
2534
"enabled": true
35+
},
36+
{
37+
"matchPackageNames": ["Flagsmith/sdk-spec"],
38+
"groupName": "SDK spec",
39+
"enabled": true
2640
}
2741
]
2842
}

‎sdk-spec/Dockerfile‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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/java .
3+
FROM docker.io/library/maven:3.9-eclipse-temurin-11 AS build
4+
5+
WORKDIR /sdk
6+
COPY pom.xml lombok.config ./
7+
COPY src/main ./src/main
8+
RUN mvn --batch-mode --no-transfer-progress \
9+
-Dmaven.test.skip=true -Dcheckstyle.skip=true -Djacoco.skip=true -Dmaven.source.skip=true \
10+
package dependency:copy-dependencies -DincludeScope=runtime -DoutputDirectory=target/lib \
11+
&& cp target/flagsmith-java-client-*.jar target/lib/
12+
13+
COPY --from=harness Harness.java /harness/Harness.java
14+
COPY sdk-spec/Steps.java ./sdk-spec/Steps.java
15+
RUN javac -cp "target/lib/*" -d target/classes-spec /harness/Harness.java sdk-spec/Steps.java
16+
17+
FROM docker.io/library/eclipse-temurin:11-jre
18+
19+
WORKDIR /sdk
20+
COPY --from=build /sdk/target/lib ./lib
21+
COPY --from=build /sdk/target/classes-spec ./classes
22+
COPY sdk-spec/Steps.java ./sdk-spec/Steps.java
23+
24+
ENTRYPOINT ["java", "-XX:TieredStopAtLevel=1", "-cp", "classes:lib/*", "Harness", "sdk-spec/Steps.java"]

‎sdk-spec/Steps.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import com.flagsmith.FlagsmithClient;
2+
import com.flagsmith.exceptions.FlagsmithClientError;
3+
import com.flagsmith.models.BaseFlag;
4+
import com.flagsmith.models.Flags;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* Steps for the Flagsmith SDK behavioural spec: https://github.com/Flagsmith/sdk-spec
11+
*
12+
* <p>Each method implements the Gherkin step in the comment above it.
13+
*/
14+
public class Steps {
15+
16+
/** A flag in the shape the spec expects. */
17+
public static class SpecFlag {
18+
public final String feature;
19+
public final boolean enabled;
20+
public final Object value;
21+
22+
SpecFlag(BaseFlag flag) {
23+
feature = flag.getFeatureName();
24+
enabled = flag.getEnabled();
25+
value = flag.getValue();
26+
}
27+
}
28+
29+
static List<SpecFlag> asSpecFlags(Flags flags) {
30+
return flags.getAllFlags().stream().map(SpecFlag::new).collect(Collectors.toList());
31+
}
32+
33+
// Create the SDK client
34+
static FlagsmithClient createClient(String environmentKey, String apiUrl) {
35+
return FlagsmithClient.newBuilder().setApiKey(environmentKey).withApiUrl(apiUrl).build();
36+
}
37+
38+
// When the application gets the environment flags
39+
static List<SpecFlag> getEnvironmentFlags(FlagsmithClient flagsmith)
40+
throws FlagsmithClientError {
41+
return asSpecFlags(flagsmith.getEnvironmentFlags());
42+
}
43+
44+
// When the application gets the flags for identity "{identifier}" with traits:
45+
static List<SpecFlag> getIdentityFlags(
46+
FlagsmithClient flagsmith, String identifier, Map<String, Object> traits)
47+
throws FlagsmithClientError {
48+
return asSpecFlags(flagsmith.getIdentityFlags(identifier, traits));
49+
}
50+
}

0 commit comments

Comments
 (0)