Skip to content

Commit

Permalink
Merge pull request #27 from openrewrite/support-multiple-sdks
Browse files Browse the repository at this point in the history
Support multiple SDKs and rebrand to rewrite-feature-flags
  • Loading branch information
shanman190 authored Aug 5, 2024
2 parents 5b4c2c4 + 3dae65c commit 13d2c9b
Show file tree
Hide file tree
Showing 39 changed files with 1,212 additions and 177 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
![Logo](https://github.com/openrewrite/rewrite/raw/main/doc/logo-oss.png)
### Migrate LaunchDarkly. Automatically.
### Migrate feature flags. Automatically.

[![ci](https://github.com/openrewrite/rewrite-launchdarkly/actions/workflows/ci.yml/badge.svg)](https://github.com/openrewrite/rewrite-launchdarkly/actions/workflows/ci.yml)
[![Apache 2.0](https://img.shields.io/github/license/openrewrite/rewrite-launchdarkly.svg)](https://www.apache.org/licenses/LICENSE-2.0)
[![Maven Central](https://img.shields.io/maven-central/v/org.openrewrite.recipe/rewrite-launchdarkly.svg)](https://mvnrepository.com/artifact/org.openrewrite.recipe/rewrite-launchdarkly)
[![ci](https://github.com/openrewrite/rewrite-feature-flags/actions/workflows/ci.yml/badge.svg)](https://github.com/openrewrite/rewrite-feature-flags/actions/workflows/ci.yml)
[![Apache 2.0](https://img.shields.io/github/license/openrewrite/rewrite-feature-flags.svg)](https://www.apache.org/licenses/LICENSE-2.0)
[![Maven Central](https://img.shields.io/maven-central/v/org.openrewrite.recipe/rewrite-feature-flags.svg)](https://mvnrepository.com/artifact/org.openrewrite.recipe/rewrite-feature-flags)
[![Revved up by Develocity](https://img.shields.io/badge/Revved%20up%20by-Develocity-06A0CE?logo=Gradle&labelColor=02303A)](https://ge.openrewrite.org/scans)

### What is this?

This project implements a [Rewrite module](https://github.com/openrewrite/rewrite) that performs common tasks when migrating to new version of LaunchDarkly.
This project implements a [Rewrite module](https://github.com/openrewrite/rewrite) that performs common tasks related to feature flags, or migrating to new versions of LaunchDarkly.

Browse [a selection of recipes available through this module in the recipe catalog](https://docs.openrewrite.org/recipes/launchdarkly).
Browse [a selection of recipes available through this module in the recipe catalog](https://docs.openrewrite.org/recipes/featureflags).

## Contributing

Expand Down
6 changes: 5 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "org.openrewrite.recipe"
description = "LaunchDarkly Migration"
description = "Feature flag migration"

val rewriteVersion = rewriteRecipe.rewriteVersion.get()
dependencies {
Expand All @@ -21,6 +21,10 @@ dependencies {

testImplementation("org.junit.jupiter:junit-jupiter-engine:latest.release")

testImplementation("dev.openfeature:sdk:latest.release")
testImplementation("io.getunleash:unleash-client-java:latest.release")
testImplementation("org.ff4j:ff4j-core:2.0.0") // 2.1.x requires Java 21

testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release")
}

Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rootProject.name = "rewrite-launchdarkly"
rootProject.name = "rewrite-feature-flags"

plugins {
id("com.gradle.develocity") version "latest.release"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.launchdarkly;
package org.openrewrite.featureflags;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.analysis.constantfold.ConstantFold;
import org.openrewrite.analysis.util.CursorUtil;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
Expand All @@ -34,24 +32,25 @@
import org.openrewrite.staticanalysis.RemoveUnusedPrivateFields;
import org.openrewrite.staticanalysis.SimplifyConstantIfBranchExecution;

import java.util.Optional;

@Value
@EqualsAndHashCode(callSuper = false)
public class RemoveBoolVariation extends Recipe {

private static final String METHOD_PATTERN_BOOLVARIATION = "com.launchdarkly.sdk.server.LDClient boolVariation(String, com.launchdarkly.sdk.*, boolean)";
public class RemoveBooleanFlag extends Recipe {

@Override
public String getDisplayName() {
return "Remove `boolVariation` for feature key";
return "Remove a boolean feature flag for feature key";
}

@Override
public String getDescription() {
return "Replace `boolVariation` invocations for feature key with value, and simplify constant if branch execution.";
return "Replace method invocations for feature key with value, and simplify constant if branch execution.";
}

@Option(displayName = "Method pattern",
description = "A method pattern to match against. The first argument must be the feature key as `String`.",
example = "dev.openfeature.sdk.Client getBooleanValue(String, Boolean)")
String methodPattern;

@Option(displayName = "Feature flag key",
description = "The key of the feature flag to remove.",
example = "flag-key-123abc")
Expand All @@ -62,18 +61,9 @@ public String getDescription() {
example = "true")
Boolean replacementValue;

@Option(displayName = "Method pattern",
description = "A method pattern to match against. If not specified, will match `LDClient` `boolVariation`. " +
"The first argument must be the feature key as `String`.",
example = METHOD_PATTERN_BOOLVARIATION,
required = false)
@Nullable
String methodPattern;

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
String pattern = Optional.ofNullable(methodPattern).filter(StringUtils::isNotEmpty).orElse(METHOD_PATTERN_BOOLVARIATION);
final MethodMatcher methodMatcher = new MethodMatcher(pattern, true);
final MethodMatcher methodMatcher = new MethodMatcher(methodPattern, true);
JavaVisitor<ExecutionContext> visitor = new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/org/openrewrite/featureflags/ff4j/RemoveCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.featureflags.ff4j;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.featureflags.RemoveBooleanFlag;

import java.util.Collections;
import java.util.List;

@Value
@EqualsAndHashCode(callSuper = false)
public class RemoveCheck extends Recipe {

@Option(displayName = "Feature flag key",
description = "The key of the feature flag to remove.",
example = "flag-key-123abc")
String featureKey;

@Option(displayName = "Replacement value",
description = "The value to replace the feature flag check with.",
example = "true")
Boolean replacementValue;

@Override
public String getDisplayName() {
return "Remove FF4j's `check` for feature key";
}

@Override
public String getDescription() {
return "Replace `check()` invocations for `featureKey` with `replacementValue`, and simplify constant if branch execution.";
}

@Override
public List<Recipe> getRecipeList() {
return Collections.singletonList(new RemoveBooleanFlag(
"org.ff4j.FF4j check(String)",
featureKey, replacementValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
@NonNullApi
@NonNullFields
package org.openrewrite.launchdarkly.search;
package org.openrewrite.featureflags.ff4j;

import org.openrewrite.internal.lang.NonNullApi;
import org.openrewrite.internal.lang.NonNullFields;
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.featureflags.ff4j.search;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.internal.lang.Nullable;

import java.util.Collections;
import java.util.List;

@Value
@EqualsAndHashCode(callSuper = false)
public class FindFeatureFlag extends Recipe {

@Option(displayName = "Feature key",
description = "The unique key for the feature flag.",
example = "flag-key-123abc",
required = false)
@Nullable
String featureKey;

@Override
public String getDisplayName() {
return "Find a FF4j feature flag";
}

@Override
public String getDescription() {
return "Find a FF4j feature flag.";
}

@Override
public List<Recipe> getRecipeList() {
return Collections.singletonList(new org.openrewrite.featureflags.search.FindFeatureFlag(
"org.ff4j.FF4j check(String, ..)", featureKey));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@NonNullApi
@NonNullFields
package org.openrewrite.featureflags.ff4j.search;

import org.openrewrite.internal.lang.NonNullApi;
import org.openrewrite.internal.lang.NonNullFields;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.launchdarkly;
package org.openrewrite.featureflags.launchdarkly;

import lombok.EqualsAndHashCode;
import lombok.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.launchdarkly;
package org.openrewrite.featureflags.launchdarkly;

import lombok.EqualsAndHashCode;
import lombok.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.featureflags.launchdarkly;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.featureflags.RemoveBooleanFlag;

import java.util.Collections;
import java.util.List;

@Value
@EqualsAndHashCode(callSuper = false)
public class RemoveBoolVariation extends Recipe {

@Override
public String getDisplayName() {
return "Remove LaunchDarkly's `boolVariation` for feature key";
}

@Override
public String getDescription() {
return "Replace `boolVariation` invocations for feature key with value, and simplify constant if branch execution.";
}

@Option(displayName = "Feature flag key",
description = "The key of the feature flag to remove.",
example = "flag-key-123abc")
String featureKey;

@Option(displayName = "Replacement value",
description = "The value to replace the feature flag check with.",
example = "true")
Boolean replacementValue;

@Override
public List<Recipe> getRecipeList() {
return Collections.singletonList(new RemoveBooleanFlag(
"com.launchdarkly.sdk.server.LDClient boolVariation(String, com.launchdarkly.sdk.*, boolean)",
featureKey, replacementValue));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@NonNullApi
@NonNullFields
package org.openrewrite.featureflags.launchdarkly;

import org.openrewrite.internal.lang.NonNullApi;
import org.openrewrite.internal.lang.NonNullFields;
Loading

0 comments on commit 13d2c9b

Please sign in to comment.