-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Steven Tompkins
committed
Oct 11, 2024
1 parent
ffd8389
commit 957a030
Showing
2 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/main/java/org/openrewrite/gitlab/core/AddComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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.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<String> inputs; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Change GitLab template"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Change a GitLab template in use."; | ||
} | ||
|
||
@Override | ||
public List<Recipe> 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") | ||
); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/test/java/org/openrewrite/gitlab/core/AddComponentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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.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/[email protected] | ||
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/[email protected] | ||
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/[email protected] | ||
inputs: | ||
version: 0.10.0 | ||
opentofu_version: 1.6.1 | ||
""", | ||
source -> source.path(".gitlab-ci.yml") | ||
) | ||
); | ||
} | ||
} |