diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePlugin.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePlugin.java index dd6acd299609..96e304352c99 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePlugin.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePlugin.java @@ -144,7 +144,7 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon return cu; } // Don't modify an existing gradle enterprise DSL, only add one which is not already present - if (containsGradleEnterpriseDsl(cu)) { + if (containsGradleDevelocityDsl(cu)) { return cu; } @@ -156,7 +156,24 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon return cu; } GradleSettings gradleSettings = maybeGradleSettings.get(); - cu = withPlugin(cu, "com.gradle.enterprise", versionComparator, null, gradleSettings, ctx); + + try { + String newVersion = findNewerVersion(new DependencyVersionSelector(null, null, gradleSettings), ctx); + if (newVersion == null) { + return cu; + } + + String pluginId; + if (versionComparator.compare(null, newVersion, "3.17") >= 0) { + pluginId = "com.gradle.develocity"; + } else { + pluginId = "com.gradle.enterprise"; + } + + cu = withPlugin(cu, pluginId, newVersion, versionComparator, ctx); + } catch (MavenDownloadingException e) { + return e.warn(cu); + } } else if (!gradleSixOrLater && cu.getSourcePath().toString().equals("build.gradle")) { // Older than 6.0 goes in root build.gradle only, not in build.gradle of subprojects Optional maybeGradleProject = cu.getMarkers().findFirst(GradleProject.class); @@ -165,38 +182,47 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon } GradleProject gradleProject = maybeGradleProject.get(); - cu = withPlugin(cu, "com.gradle.build-scan", versionComparator, gradleProject, null, ctx); + try { + String newVersion = findNewerVersion(new DependencyVersionSelector(null, gradleProject, null), ctx); + if (newVersion == null) { + return cu; + } + + cu = withPlugin(cu, "com.gradle.build-scan", newVersion, versionComparator, ctx); + } catch (MavenDownloadingException e) { + return e.warn(cu); + } } return cu; } - }); - } - private G.CompilationUnit withPlugin(G.CompilationUnit cu, String pluginId, VersionComparator versionComparator, @Nullable GradleProject gradleProject, @Nullable GradleSettings gradleSettings, ExecutionContext ctx) { - try { - String newVersion = new DependencyVersionSelector(null, gradleProject, gradleSettings) - .select(new GroupArtifact("com.gradle.enterprise", "com.gradle.enterprise.gradle.plugin"), "classpath", version, null, ctx); - if (newVersion == null) { - return cu; + private @Nullable String findNewerVersion(DependencyVersionSelector versionSelector, ExecutionContext ctx) throws MavenDownloadingException { + String newVersion = versionSelector + .select(new GroupArtifact("com.gradle.develocity", "com.gradle.develocity.gradle.plugin"), "classpath", version, null, ctx); + if (newVersion == null) { + newVersion = versionSelector + .select(new GroupArtifact("com.gradle.enterprise", "com.gradle.enterprise.gradle.plugin"), "classpath", version, null, ctx); + } + return newVersion; } + }); + } - cu = (G.CompilationUnit) new AddPluginVisitor(pluginId, newVersion, null, null) - .visitNonNull(cu, ctx); - cu = (G.CompilationUnit) new UpgradePluginVersion(pluginId, newVersion, null).getVisitor() - .visitNonNull(cu, ctx); - J.MethodInvocation gradleEnterpriseInvocation = gradleEnterpriseDsl( - newVersion, - versionComparator, - getIndent(cu), - ctx); - return cu.withStatements(ListUtils.concat(cu.getStatements(), gradleEnterpriseInvocation)); - } catch (MavenDownloadingException e) { - return e.warn(cu); - } + private G.CompilationUnit withPlugin(G.CompilationUnit cu, String pluginId, String newVersion, VersionComparator versionComparator, ExecutionContext ctx) { + cu = (G.CompilationUnit) new AddPluginVisitor(pluginId, newVersion, null, null) + .visitNonNull(cu, ctx); + cu = (G.CompilationUnit) new UpgradePluginVersion(pluginId, newVersion, null).getVisitor() + .visitNonNull(cu, ctx); + J.MethodInvocation gradleEnterpriseInvocation = gradleEnterpriseDsl( + newVersion, + versionComparator, + getIndent(cu), + ctx); + return cu.withStatements(ListUtils.concat(cu.getStatements(), gradleEnterpriseInvocation)); } - private static boolean containsGradleEnterpriseDsl(JavaSourceFile cu) { + private static boolean containsGradleDevelocityDsl(JavaSourceFile cu) { AtomicBoolean found = new AtomicBoolean(false); new GroovyIsoVisitor() { @Override @@ -209,7 +235,7 @@ private static boolean containsGradleEnterpriseDsl(JavaSourceFile cu) { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) { - if (method.getSimpleName().equals("gradleEnterprise")) { + if (method.getSimpleName().equals("gradleEnterprise") || method.getSimpleName().equals("develocity")) { atomicBoolean.set(true); } return super.visitMethodInvocation(method, atomicBoolean); @@ -226,7 +252,13 @@ private J.MethodInvocation gradleEnterpriseDsl(String newVersion, VersionCompara } boolean versionIsAtLeast3_2 = versionComparator.compare(null, newVersion, "3.2") >= 0; boolean versionIsAtLeast3_7 = versionComparator.compare(null, newVersion, "3.7") >= 0; - StringBuilder ge = new StringBuilder("\ngradleEnterprise {\n"); + boolean versionIsAtLeast3_17 = versionComparator.compare(null, newVersion, "3.17") >= 0; + StringBuilder ge; + if (versionIsAtLeast3_17) { + ge = new StringBuilder("\ndevelocity {\n"); + } else { + ge = new StringBuilder("\ngradleEnterprise {\n"); + } if (server != null && !server.isEmpty()) { ge.append(indent).append("server = '").append(server).append("'\n"); } @@ -237,9 +269,17 @@ private J.MethodInvocation gradleEnterpriseDsl(String newVersion, VersionCompara ge.append(indent).append("buildScan {\n"); if (publishCriteria != null) { if (publishCriteria == PublishCriteria.Always) { - ge.append(indent).append(indent).append("publishAlways()\n"); + if (versionIsAtLeast3_17) { + ge.append(indent).append(indent).append("publishing.onlyIf { true }\n"); + } else { + ge.append(indent).append(indent).append("publishAlways()\n"); + } } else { - ge.append(indent).append(indent).append("publishOnFailure()\n"); + if (versionIsAtLeast3_17) { + ge.append(indent).append(indent).append("publishing.onlyIf { !it.buildResult.failures.empty }\n"); + } else { + ge.append(indent).append(indent).append("publishOnFailure()\n"); + } } } if (allowUntrustedServer != null && !versionIsAtLeast3_2) { @@ -251,7 +291,11 @@ private J.MethodInvocation gradleEnterpriseDsl(String newVersion, VersionCompara if (captureTaskInputFiles != null) { if (versionIsAtLeast3_7) { ge.append(indent).append(indent).append("capture {\n"); - ge.append(indent).append(indent).append(indent).append("taskInputFiles = ").append(captureTaskInputFiles).append("\n"); + if (versionIsAtLeast3_17) { + ge.append(indent).append(indent).append(indent).append("fileFingerprints = ").append(captureTaskInputFiles).append("\n"); + } else { + ge.append(indent).append(indent).append(indent).append("taskInputFiles = ").append(captureTaskInputFiles).append("\n"); + } ge.append(indent).append(indent).append("}\n"); } else { ge.append(indent).append(indent).append("captureTaskInputFiles = ").append(captureTaskInputFiles).append("\n"); diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePluginTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePluginTest.java index ff9bc6e20cf2..1add0c40ef8b 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePluginTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePluginTest.java @@ -138,7 +138,7 @@ void addNewSettingsPluginsBlock() { """, interpolateResolvedVersion(""" plugins { - id 'com.gradle.enterprise' version '%s' + id 'com.gradle.develocity' version '%s' } rootProject.name = 'my-project' @@ -164,7 +164,7 @@ void addExistingSettingsPluginsBlock() { """, interpolateResolvedVersion(""" plugins { - id 'com.gradle.enterprise' version '%s' + id 'com.gradle.develocity' version '%s' } rootProject.name = 'my-project' @@ -176,10 +176,10 @@ void addExistingSettingsPluginsBlock() { @Issue("https://github.com/openrewrite/rewrite/issues/2697") @Test - void withConfigurationInSettings() { + void withGradleEnterpriseConfigurationInSettings() { rewriteRun( spec -> spec.allSources(s -> s.markers(new BuildTool(randomId(), BuildTool.Type.Gradle, "7.6.1"))) - .recipe(new AddDevelocityGradlePlugin("3.x", "https://ge.sam.com/", true, true, true, AddDevelocityGradlePlugin.PublishCriteria.Always)), + .recipe(new AddDevelocityGradlePlugin("3.16.x", "https://ge.sam.com/", true, true, true, AddDevelocityGradlePlugin.PublishCriteria.Always)), buildGradle( "" ), @@ -206,6 +206,37 @@ void withConfigurationInSettings() { ); } + @Test + void withDevelocityConfigurationInSettings() { + rewriteRun( + spec -> spec.allSources(s -> s.markers(new BuildTool(randomId(), BuildTool.Type.Gradle, "7.6.1"))) + .recipe(new AddDevelocityGradlePlugin("3.x", "https://ge.sam.com/", true, true, true, AddDevelocityGradlePlugin.PublishCriteria.Always)), + buildGradle( + "" + ), + settingsGradle( + "", + interpolateResolvedVersion(""" + plugins { + id 'com.gradle.develocity' version '%s' + } + develocity { + server = 'https://ge.sam.com/' + allowUntrustedServer = true + buildScan { + publishing.onlyIf { true } + uploadInBackground = true + capture { + fileFingerprints = true + } + } + } + """ + ) + ) + ); + } + @Issue("https://github.com/openrewrite/rewrite/issues/2697") @Test void withConfigurationOldInputCapture() { @@ -242,7 +273,7 @@ void defaultsToLatestRelease() { settingsGradle( "", spec -> spec.after(after -> { - Matcher versionMatcher = Pattern.compile("id 'com\\.gradle\\.enterprise' version '(.*?)'").matcher(after); + Matcher versionMatcher = Pattern.compile("id 'com\\.gradle\\.develocity' version '(.*?)'").matcher(after); assertThat(versionMatcher.find()).isTrue(); String version = versionMatcher.group(1); VersionComparator versionComparator = requireNonNull(Semver.validate("[3.14,)", null).getValue()); @@ -250,7 +281,7 @@ void defaultsToLatestRelease() { return """ plugins { - id 'com.gradle.enterprise' version '%s' + id 'com.gradle.develocity' version '%s' } """.formatted(version); }) @@ -279,7 +310,7 @@ void addNewSettingsPluginsBlockWithLicenseHeader() { */ plugins { - id 'com.gradle.enterprise' version '%s' + id 'com.gradle.develocity' version '%s' } rootProject.name = 'my-project'