diff --git a/src/main/java/org/openrewrite/gitlab/core/AddComponent.java b/src/main/java/org/openrewrite/gitlab/core/AddComponent.java new file mode 100644 index 0000000..060de1e --- /dev/null +++ b/src/main/java/org/openrewrite/gitlab/core/AddComponent.java @@ -0,0 +1,78 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.gitlab.core; + +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.openrewrite.Option; +import org.openrewrite.Recipe; +import org.openrewrite.yaml.MergeYaml; + +import java.util.Collections; +import java.util.List; + +@Value +@EqualsAndHashCode(callSuper = false) +public class AddComponent extends Recipe { + @Option(displayName = "Component", + description = "Name of the component to use add.", + example = "$CI_SERVER_FQDN/components/opentofu/full-pipeline") + String newComponent; + + @Option(displayName = "Version", + description = "Version of the component to add.", + example = "0.10.0") + String version; + + @Option(displayName = "Inputs", + description = "The set of inputs to provide", + example = "opentofu_version: 1.6.1") + List inputs; + + @Override + public String getDisplayName() { + return "Change GitLab template"; + } + + @Override + public String getDescription() { + return "Change a GitLab template in use."; + } + + @Override + public List getRecipeList() { + StringBuilder includeBlock = new StringBuilder() + .append("include:\n") + .append(" - component: ") + .append(newComponent) + .append("@") + .append(version) + .append("\n") + .append(" inputs:\n"); + + inputs.forEach(input -> includeBlock.append(" ").append(input).append("\n")); + + return Collections.singletonList( + new MergeYaml( + "$", + //language=yml + includeBlock.toString(), + false, + "component", + ".gitlab-ci.yml") + ); + } +} diff --git a/src/test/java/org/openrewrite/gitlab/core/AddComponentTest.java b/src/test/java/org/openrewrite/gitlab/core/AddComponentTest.java new file mode 100644 index 0000000..29e1e38 --- /dev/null +++ b/src/test/java/org/openrewrite/gitlab/core/AddComponentTest.java @@ -0,0 +1,96 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.gitlab.core; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import java.util.List; + +import static org.openrewrite.yaml.Assertions.yaml; + +class AddComponentTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe( + new AddComponent( + "$CI_SERVER_FQDN/components/opentofu/full-pipeline", + "0.10.0", + List.of("version: 0.10.0", "opentofu_version: 1.6.1"))); + } + + @DocumentExample + @Test + void addToExistingList() { + //language=yaml + rewriteRun( + yaml( + """ + include: + - template: Gradle.gitlab-ci.yml + """, + """ + include: + - template: Gradle.gitlab-ci.yml + - component: $CI_SERVER_FQDN/components/opentofu/full-pipeline@0.10.0 + inputs: + version: 0.10.0 + opentofu_version: 1.6.1 + """, + source -> source.path(".gitlab-ci.yml") + ) + ); + } + + @Test + void addNewWhereNoneExist() { + //language=yaml + rewriteRun( + yaml( + "", + """ + include: + - component: $CI_SERVER_FQDN/components/opentofu/full-pipeline@0.10.0 + inputs: + version: 0.10.0 + opentofu_version: 1.6.1 + """, + source -> source.path(".gitlab-ci.yml") + ) + ); + } + + @Test + void noopWhenAlreadyPresent() { + //language=yaml + rewriteRun( + yaml( + """ + include: + - template: Gradle.gitlab-ci.yml + - component: $CI_SERVER_FQDN/components/opentofu/full-pipeline@0.10.0 + inputs: + version: 0.10.0 + opentofu_version: 1.6.1 + """, + source -> source.path(".gitlab-ci.yml") + ) + ); + } +}