Skip to content
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

feat: Added recipe to comment deprecated and removed properties witho… #635

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.java.spring;
ashakirin marked this conversation as resolved.
Show resolved Hide resolved

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jetbrains.annotations.Nullable;
import org.openrewrite.*;

import java.util.List;

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

@Override
public String getDisplayName() {
return "Comment Spring properties";
}

@Override
public String getDescription() {
return "Add inline comments to specified spring properties.";
}

@Option(displayName = "Property keys list",
description = "The list of names of the property keys to comment.",
example = "management.metrics.binders.files.enabled")
List<String> propertyKeys;

@Option(displayName = "Inline comment",
description = "Inline comment to be inserted",
example = "This property is deprecated and no longer applicable starting from Spring Boot 3.0.x")
String comment;

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
String inlineComment = " # " + comment;
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
Tree processingTree = tree;
for (String key : propertyKeys) {
String regex = "(?<!" + inlineComment + ")$";
ChangeSpringPropertyValue changeSpringPropertyValue = new ChangeSpringPropertyValue(key, inlineComment, regex, true, null);
processingTree = changeSpringPropertyValue.getVisitor().visit(processingTree, ctx);
}
return processingTree;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.java.spring;
ashakirin marked this conversation as resolved.
Show resolved Hide resolved

import org.junit.jupiter.api.Test;
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RewriteTest;

import java.util.List;

import static org.openrewrite.properties.Assertions.properties;
import static org.openrewrite.yaml.Assertions.yaml;

class InlineCommentSpringPropertiesTest implements RewriteTest {

@DocumentExample
@Test
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
void shouldInsertInlineCommentsIntoProperties() {
rewriteRun(
spec -> spec.recipe(new InlineCommentSpringProperties(List.of("test.propertyKey1", "test.propertyKey2"), "my comment")),
//language=yaml
yaml(
"""
test.propertyKey1: xxx
test.propertyKey2: yyy
test.propertyKey3: zzz
""",
"""
test.propertyKey1: xxx # my comment
test.propertyKey2: yyy # my comment
test.propertyKey3: zzz
""",
spec -> spec.path("application.yaml")
),
//language=properties
properties(
"""
test.propertyKey1=xxx
test.propertyKey2=yyy
test.propertyKey3=zzz
""",
"""
test.propertyKey1=xxx # my comment
test.propertyKey2=yyy # my comment
test.propertyKey3=zzz
""",
spec -> spec.path("application.properties")
)
);
}
}