From 3770f85bd4e7662f00736d30525a077987368053 Mon Sep 17 00:00:00 2001 From: Shannon Pamperl Date: Fri, 8 Dec 2023 08:27:02 -0600 Subject: [PATCH] Skip updating Gradle when already updated (#3794) --- .../gradle/UpdateGradleWrapper.java | 14 +++- .../gradle/UpdateGradleWrapperTest.java | 64 +++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java index 18c1477854d..e47d02200c7 100755 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java @@ -287,9 +287,17 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { SourceFile sourceFile = (SourceFile) tree; if (acc.updatedMarker != null) { - sourceFile = sourceFile.getMarkers().findFirst(BuildTool.class) - .map(buildTool -> (SourceFile) tree.withMarkers(tree.getMarkers().computeByType(buildTool, (b, a) -> acc.updatedMarker))) - .orElse(sourceFile); + Optional maybeCurrentMarker = sourceFile.getMarkers().findFirst(BuildTool.class); + if (maybeCurrentMarker.isPresent()) { + BuildTool currentMarker = maybeCurrentMarker.get(); + VersionComparator versionComparator = requireNonNull(Semver.validate(isBlank(version) ? "latest.release" : version, null).getValue()); + int compare = versionComparator.compare(null, currentMarker.getVersion(), acc.updatedMarker.getVersion()); + if (compare < 0) { + sourceFile = sourceFile.withMarkers(sourceFile.getMarkers().computeByType(currentMarker, (b, a) -> acc.updatedMarker)); + } else { + return sourceFile; + } + } } if (sourceFile instanceof PlainText && PathUtils.matchesGlob(sourceFile.getSourcePath(), "**/" + WRAPPER_SCRIPT_LOCATION_RELATIVE_PATH)) { diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpdateGradleWrapperTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpdateGradleWrapperTest.java index e03b8af4060..5e527041178 100755 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpdateGradleWrapperTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpdateGradleWrapperTest.java @@ -442,6 +442,70 @@ void defaultsToLatestRelease() { ); } + @Test + void skipWorkIfUpdatedEarlier() { + rewriteRun( + spec -> spec.recipeFromYaml( + """ + type: specs.openrewrite.org/v1beta/recipe + name: org.openrewrite.gradle.MultipleWrapperUpdates + displayName: Multiple wrapper updates + description: Multiple wrapper updates. + recipeList: + - org.openrewrite.gradle.UpdateGradleWrapper: + version: 7.6.3 + addIfMissing: false + - org.openrewrite.gradle.UpdateGradleWrapper: + version: 6.9.4 + addIfMissing: false + """, + "org.openrewrite.gradle.MultipleWrapperUpdates") + .cycles(1) + .allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "5.6.4"))) + .afterRecipe(run -> { + var gradleSh = result(run, PlainText.class, "gradlew"); + assertThat(gradleSh.getSourcePath()).isEqualTo(WRAPPER_SCRIPT_LOCATION); + assertThat(gradleSh.getText()).isNotBlank(); + assertThat(gradleSh.getFileAttributes()).isNotNull(); + assertThat(gradleSh.getFileAttributes().isReadable()).isTrue(); + assertThat(gradleSh.getFileAttributes().isExecutable()).isTrue(); + + var gradleBat = result(run, PlainText.class, "gradlew.bat"); + assertThat(gradleBat.getSourcePath()).isEqualTo(WRAPPER_BATCH_LOCATION); + assertThat(gradleBat.getText()).isNotBlank(); + + var gradleWrapperJar = result(run, Remote.class, "gradle-wrapper.jar"); + assertThat(gradleWrapperJar.getSourcePath()).isEqualTo(WRAPPER_JAR_LOCATION); + //noinspection OptionalGetWithoutIsPresent + BuildTool buildTool = gradleWrapperJar.getMarkers().findFirst(BuildTool.class).get(); + assertThat(buildTool.getVersion()).isEqualTo("7.6.3"); + assertThat(gradleWrapperJar.getUri()).isEqualTo(URI.create("https://services.gradle.org/distributions/gradle-" + buildTool.getVersion() + "-bin.zip")); + assertThat(isValidWrapperJar(gradleWrapperJar)).as("Wrapper jar is not valid").isTrue(); + }), + properties( + """ + distributionBase=GRADLE_USER_HOME + distributionPath=wrapper/dists + distributionUrl=https\\://services.gradle.org/distributions/gradle-5.6.4-bin.zip + zipStoreBase=GRADLE_USER_HOME + zipStorePath=wrapper/dists + """, + """ + distributionBase=GRADLE_USER_HOME + distributionPath=wrapper/dists + distributionUrl=https\\://services.gradle.org/distributions/gradle-7.6.3-bin.zip + zipStoreBase=GRADLE_USER_HOME + zipStorePath=wrapper/dists + distributionSha256Sum=740c2e472ee4326c33bf75a5c9f5cd1e69ecf3f9b580f6e236c86d1f3d98cfac + """, + spec -> spec.path("gradle/wrapper/gradle-wrapper.properties") + ), + gradlew, + gradlewBat, + gradleWrapperJarQuark + ); + } + private S result(RecipeRun run, Class clazz, String endsWith) { return run.getChangeset().getAllResults().stream() .map(Result::getAfter)