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

Migrate add Develocity recipes to handle the new develocity configurations #4312

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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<GradleProject> maybeGradleProject = cu.getMarkers().findFirst(GradleProject.class);
Expand All @@ -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<AtomicBoolean>() {
@Override
Expand All @@ -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);
Expand All @@ -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");
}
Expand All @@ -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) {
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void addNewSettingsPluginsBlock() {
""",
interpolateResolvedVersion("""
plugins {
id 'com.gradle.enterprise' version '%s'
id 'com.gradle.develocity' version '%s'
}

rootProject.name = 'my-project'
Expand All @@ -164,7 +164,7 @@ void addExistingSettingsPluginsBlock() {
""",
interpolateResolvedVersion("""
plugins {
id 'com.gradle.enterprise' version '%s'
id 'com.gradle.develocity' version '%s'
}

rootProject.name = 'my-project'
Expand All @@ -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(
""
),
Expand All @@ -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() {
Expand Down Expand Up @@ -242,15 +273,15 @@ 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());
assertThat(versionComparator.compare(null, "3.14", version)).isLessThanOrEqualTo(0);

return """
plugins {
id 'com.gradle.enterprise' version '%s'
id 'com.gradle.develocity' version '%s'
}
""".formatted(version);
})
Expand Down Expand Up @@ -279,7 +310,7 @@ void addNewSettingsPluginsBlockWithLicenseHeader() {
*/

plugins {
id 'com.gradle.enterprise' version '%s'
id 'com.gradle.develocity' version '%s'
}

rootProject.name = 'my-project'
Expand Down
Loading
Loading