Skip to content

Commit 934b45e

Browse files
Laurens Westerlakengithub-actions[bot]
andauthored
Expand UpgradePluginVersion to support the kotlin plugin (#6570)
* Allow kotlin plugin to be upgraded Fix bug where `visitVariable` allowed the version to be downgraded --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 44698c8 commit 934b45e

2 files changed

Lines changed: 85 additions & 6 deletions

File tree

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
153153
if (!(pluginArgs.get(0) instanceof J.Literal)) {
154154
return m;
155155
}
156-
String pluginId = literalValue(pluginArgs.get(0));
156+
String pluginId;
157+
if ("kotlin".equals(((J.MethodInvocation) m.getSelect()).getSimpleName())) {
158+
pluginId = "kotlin";
159+
} else {
160+
pluginId = literalValue(pluginArgs.get(0));
161+
}
157162
if (pluginId == null || !StringUtils.matchesGlob(pluginId, pluginIdPattern)) {
158163
return m;
159164
}
@@ -162,8 +167,15 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
162167
try {
163168
String currentVersion = literalValue(versionArgs.get(0));
164169
if (currentVersion != null) {
165-
String resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, gradleSettings)
166-
.select(new GroupArtifactVersion(pluginId, pluginId + ".gradle.plugin", currentVersion), "classpath", newVersion, versionPattern, ctx);
170+
String resolvedVersion;
171+
if ("kotlin".equals(pluginId)) {
172+
String fullPluginId = String.format("org.jetbrains.%s.%s", pluginId, literalValue(pluginArgs.get(0)));
173+
resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, gradleSettings)
174+
.select(new GroupArtifactVersion(fullPluginId, fullPluginId + ".gradle.plugin", currentVersion), "classpath", newVersion, versionPattern, ctx);
175+
} else {
176+
resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, gradleSettings)
177+
.select(new GroupArtifactVersion(pluginId, pluginId + ".gradle.plugin", currentVersion), "classpath", newVersion, versionPattern, ctx);
178+
}
167179
acc.pluginIdToNewVersion.put(pluginId, resolvedVersion);
168180
} else if (versionArgs.get(0) instanceof G.GString) {
169181
G.GString gString = (G.GString) versionArgs.get(0);
@@ -236,7 +248,12 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
236248
}
237249
assert m.getSelect() != null;
238250
List<Expression> pluginArgs = ((J.MethodInvocation) m.getSelect()).getArguments();
239-
String pluginId = literalValue(pluginArgs.get(0));
251+
String pluginId;
252+
if ("kotlin".equals(((J.MethodInvocation) m.getSelect()).getSimpleName())) {
253+
pluginId = "kotlin";
254+
} else {
255+
pluginId = literalValue(pluginArgs.get(0));
256+
}
240257
if (pluginId == null || !StringUtils.matchesGlob(pluginId, pluginIdPattern)) {
241258
return m;
242259
}
@@ -264,8 +281,15 @@ public J visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionC
264281
String oldVersion = literalValue(initializer);
265282
String newVersion = acc.pluginIdToNewVersion.get(acc.versionPropNameToPluginId.get(visited.getSimpleName()));
266283
if (newVersion != null && !newVersion.equals(oldVersion)) {
267-
String valueSource = initializer.getValueSource() == null || oldVersion == null ? initializer.getValueSource() : initializer.getValueSource().replace(oldVersion, newVersion);
268-
return visited.withInitializer(initializer.withValueSource(valueSource).withValue(newVersion));
284+
VersionComparator versionComparator = Semver.validate(newVersion, versionPattern).getValue();
285+
if (versionComparator == null) {
286+
return visited;
287+
}
288+
Optional<String> finalVersion = versionComparator.upgrade(oldVersion != null ? oldVersion : "", singletonList(newVersion));
289+
if (finalVersion.isPresent()) {
290+
String valueSource = initializer.getValueSource() == null || oldVersion == null ? initializer.getValueSource() : initializer.getValueSource().replace(oldVersion, newVersion);
291+
return visited.withInitializer(initializer.withValueSource(valueSource).withValue(finalVersion.get()));
292+
}
269293
}
270294
}
271295
return visited;

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,44 @@ void upgradePlugin() {
5959
);
6060
}
6161

62+
@Test
63+
void upgradeKotlinPluginLiteralVersion() {
64+
rewriteRun(
65+
spec -> spec.recipe(new UpgradePluginVersion("kotlin", "2.3.0", null)),
66+
buildGradleKts(
67+
"""
68+
plugins {
69+
kotlin("jvm") version "2.0.0"
70+
}
71+
""",
72+
"""
73+
plugins {
74+
kotlin("jvm") version "2.3.0"
75+
}
76+
"""
77+
)
78+
);
79+
}
80+
81+
@Test
82+
void upgradeKotlinPlugin() {
83+
rewriteRun(
84+
spec -> spec.recipe(new UpgradePluginVersion("kotlin", "latest.minor", null)),
85+
buildGradleKts(
86+
"""
87+
plugins {
88+
kotlin("jvm") version "2.0.0"
89+
}
90+
""",
91+
"""
92+
plugins {
93+
kotlin("jvm") version "2.3.0"
94+
}
95+
"""
96+
)
97+
);
98+
}
99+
62100
@Test
63101
void upgradeGradleSettingsPlugin() {
64102
rewriteRun(
@@ -127,6 +165,23 @@ void change() {
127165
);
128166
}
129167

168+
@Test
169+
void dontDowngradeWhenExactVersionVariable() {
170+
rewriteRun(
171+
spec -> spec.recipe(new UpgradePluginVersion("org.openrewrite.rewrite", "5.39.9", null)),
172+
settingsGradle(
173+
"""
174+
pluginManagement {
175+
plugins {
176+
String v = '5.40.0'
177+
id 'org.openrewrite.rewrite' version v
178+
}
179+
}
180+
"""
181+
)
182+
);
183+
}
184+
130185
@Test
131186
void pluginManagementPlugin() {
132187
rewriteRun(

0 commit comments

Comments
 (0)