Skip to content

Commit 4c85a22

Browse files
div-bargaligithub-actions[bot]timtebeek
authored
Fix Recipe to remove build cache configuration for Develocity (#6465)
* Add failing tests for Develocity build cache connector configurations * Add option to remove buildCache configuration in RemoveDevelocityConfiguration * Update rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/RemoveDevelocityTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/RemoveDevelocityTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/RemoveDevelocityConfiguration.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Refactor RemoveDevelocityConfiguration to enhance functionality and remove buildCache configurations * Apply suggestions * Inline nested recipe to just using the visitor; update tests to run top level * Condense logic * Apply automated suggestions --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent dcb1cba commit 4c85a22

2 files changed

Lines changed: 331 additions & 1 deletion

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/RemoveDevelocityConfiguration.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,74 @@
1515
*/
1616
package org.openrewrite.gradle.plugins;
1717

18+
import org.jspecify.annotations.Nullable;
19+
import org.openrewrite.ExecutionContext;
20+
import org.openrewrite.Preconditions;
1821
import org.openrewrite.Recipe;
22+
import org.openrewrite.TreeVisitor;
1923
import org.openrewrite.gradle.RemoveExtension;
24+
import org.openrewrite.groovy.GroovyIsoVisitor;
25+
import org.openrewrite.internal.ListUtils;
26+
import org.openrewrite.java.MethodMatcher;
27+
import org.openrewrite.java.search.UsesMethod;
28+
import org.openrewrite.java.tree.J;
29+
import org.openrewrite.java.tree.Statement;
2030

2131
import java.util.Arrays;
2232
import java.util.List;
2333

34+
import static java.util.Collections.singletonList;
35+
2436
public class RemoveDevelocityConfiguration extends Recipe {
37+
38+
protected static final MethodMatcher BUILD_CACHE_MATCHER = new MethodMatcher("*..* buildCache(..)");
39+
2540
@Override
2641
public String getDisplayName() {
2742
return "Remove Develocity configuration";
2843
}
2944

3045
@Override
3146
public String getDescription() {
32-
return "Remove Develocity configuration from a Gradle build.";
47+
return "Remove the Develocity Gradle plugin and associated configuration.";
48+
}
49+
50+
@Override
51+
public TreeVisitor<?, ExecutionContext> getVisitor() {
52+
return Preconditions.check(
53+
new UsesMethod<>(BUILD_CACHE_MATCHER),
54+
new GroovyIsoVisitor<ExecutionContext>() {
55+
@Override
56+
public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
57+
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
58+
59+
if (BUILD_CACHE_MATCHER.matches(m) &&
60+
m.getArguments().size() == 1 &&
61+
m.getArguments().get(0) instanceof J.Lambda) {
62+
J.Lambda lambda = (J.Lambda) m.getArguments().get(0);
63+
J.Block body = (J.Block) lambda.getBody();
64+
65+
List<Statement> filteredStatements = ListUtils.filter(body.getStatements(),
66+
stmt -> {
67+
if (stmt instanceof J.Return && ((J.Return) stmt).getExpression() instanceof J.MethodInvocation) {
68+
// Unpack J.Return wrapping last statement in closure
69+
stmt = (J.MethodInvocation) ((J.Return) stmt).getExpression();
70+
}
71+
if (!(stmt instanceof J.MethodInvocation)) {
72+
return true;
73+
}
74+
// Remove only 'remote' method invocations
75+
return !"remote".equals(((J.MethodInvocation) stmt).getSimpleName());
76+
});
77+
if (filteredStatements.isEmpty()) {
78+
return null;
79+
}
80+
return m.withArguments(singletonList(lambda.withBody(body.withStatements(filteredStatements))));
81+
}
82+
83+
return m;
84+
}
85+
});
3386
}
3487

3588
@Override

rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/RemoveDevelocityTest.java

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.test.RecipeSpec;
2121
import org.openrewrite.test.RewriteTest;
22+
import org.openrewrite.test.SourceSpec;
2223

2324
import static org.openrewrite.gradle.Assertions.settingsGradle;
2425
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
2526

2627
class RemoveDevelocityTest implements RewriteTest {
28+
2729
@Override
2830
public void defaults(RecipeSpec spec) {
2931
spec
@@ -92,4 +94,279 @@ void removeDevelocity() {
9294
)
9395
);
9496
}
97+
98+
@Test
99+
void removeRemoteCacheWithLocalPreserved() {
100+
rewriteRun(
101+
settingsGradle(
102+
"""
103+
plugins {
104+
id 'com.gradle.develocity' version '3.17'
105+
}
106+
107+
develocity {
108+
server = 'https://ge.example.com'
109+
}
110+
111+
buildCache {
112+
local {
113+
enabled = true
114+
directory = file('build-cache')
115+
}
116+
remote(develocity.buildCache) {
117+
enabled = true
118+
}
119+
}
120+
""",
121+
"""
122+
123+
124+
buildCache {
125+
local {
126+
enabled = true
127+
directory = file('build-cache')
128+
}
129+
}
130+
""",
131+
SourceSpec::noTrim
132+
)
133+
);
134+
}
135+
136+
@Test
137+
void removeEntireBuildCacheWhenOnlyRemoteExists() {
138+
rewriteRun(
139+
settingsGradle(
140+
"""
141+
plugins {
142+
id 'com.gradle.develocity' version '3.17'
143+
}
144+
145+
develocity {
146+
server = 'https://ge.example.com'
147+
}
148+
149+
buildCache {
150+
remote(develocity.buildCache) {
151+
enabled = true
152+
}
153+
}
154+
""",
155+
""
156+
)
157+
);
158+
}
159+
160+
@Test
161+
void removeHttpBuildCache() {
162+
rewriteRun(
163+
settingsGradle(
164+
"""
165+
plugins {
166+
id 'com.gradle.develocity' version '3.17'
167+
}
168+
169+
develocity {
170+
server = 'https://ge.example.com'
171+
}
172+
173+
buildCache {
174+
remote(HttpBuildCache) {
175+
url = 'https://other-cache.com/cache/'
176+
}
177+
remote(develocity.buildCache) {
178+
enabled = true
179+
}
180+
}
181+
""",
182+
""
183+
)
184+
);
185+
}
186+
187+
@Test
188+
void removeAllRemoteConfigurations() {
189+
rewriteRun(
190+
settingsGradle(
191+
"""
192+
plugins {
193+
id 'com.gradle.develocity' version '3.17'
194+
}
195+
196+
develocity {
197+
server = 'https://ge.example.com'
198+
buildScan {
199+
termsOfUseUrl = 'https://gradle.com/terms-of-service'
200+
termsOfUseAgree = 'yes'
201+
}
202+
}
203+
204+
buildCache {
205+
local {
206+
enabled = true
207+
}
208+
remote(HttpBuildCache) {
209+
url = 'https://cache.com'
210+
}
211+
remote(develocity.buildCache) {
212+
push = true
213+
}
214+
}
215+
""",
216+
"""
217+
218+
219+
buildCache {
220+
local {
221+
enabled = true
222+
}
223+
}
224+
""",
225+
SourceSpec::noTrim
226+
)
227+
);
228+
}
229+
230+
@Test
231+
void removeGradleEnterpriseConfiguration() {
232+
rewriteRun(
233+
settingsGradle(
234+
"""
235+
plugins {
236+
id 'com.gradle.enterprise' version '3.16.2'
237+
}
238+
239+
gradleEnterprise {
240+
server = 'https://ge.example.com'
241+
buildScan {
242+
publishAlways()
243+
}
244+
}
245+
246+
buildCache {
247+
local {
248+
enabled = true
249+
}
250+
remote(gradleEnterprise.buildCache) {
251+
enabled = true
252+
push = true
253+
}
254+
}
255+
""",
256+
"""
257+
258+
259+
buildCache {
260+
local {
261+
enabled = true
262+
}
263+
}
264+
""",
265+
SourceSpec::noTrim
266+
)
267+
);
268+
}
269+
270+
@Test
271+
void noChangesWhenNoRemoteCache() {
272+
rewriteRun(
273+
settingsGradle(
274+
"""
275+
buildCache {
276+
local {
277+
enabled = true
278+
}
279+
}
280+
"""
281+
)
282+
);
283+
}
284+
285+
@Test
286+
void removeEntireBuildCacheWithOnlyHttpBuildCache() {
287+
rewriteRun(
288+
settingsGradle(
289+
"""
290+
plugins {
291+
id 'com.gradle.develocity' version '3.17'
292+
}
293+
294+
develocity {
295+
server = 'https://ge.example.com'
296+
}
297+
298+
buildCache {
299+
remote(HttpBuildCache) {
300+
url = 'https://cache.example.com'
301+
}
302+
}
303+
""",
304+
""
305+
)
306+
);
307+
}
308+
309+
@Test
310+
void removeOnlyExtensionsWhenNoBuildCacheBlock() {
311+
rewriteRun(
312+
settingsGradle(
313+
"""
314+
plugins {
315+
id 'com.gradle.develocity' version '3.17'
316+
}
317+
318+
develocity {
319+
server = 'https://ge.example.com'
320+
buildScan {
321+
termsOfUseUrl = 'https://gradle.com/terms-of-service'
322+
termsOfUseAgree = 'yes'
323+
}
324+
}
325+
""",
326+
""
327+
)
328+
);
329+
}
330+
331+
@Test
332+
void preserveOtherExtensionsAndPlugins() {
333+
rewriteRun(
334+
settingsGradle(
335+
"""
336+
plugins {
337+
id 'com.gradle.develocity' version '3.17'
338+
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0'
339+
}
340+
341+
rootProject.name = 'my-project'
342+
343+
develocity {
344+
server = 'https://ge.example.com'
345+
}
346+
347+
buildCache {
348+
local {
349+
enabled = true
350+
}
351+
remote(develocity.buildCache) {
352+
enabled = true
353+
}
354+
}
355+
""",
356+
"""
357+
plugins {
358+
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0'
359+
}
360+
361+
rootProject.name = 'my-project'
362+
363+
buildCache {
364+
local {
365+
enabled = true
366+
}
367+
}
368+
"""
369+
)
370+
);
371+
}
95372
}

0 commit comments

Comments
 (0)