Skip to content

Commit f1fb66d

Browse files
authored
Collapse UpgradeDockerImageVersion cartesian via ChangeFrom capture (#1060)
`getRecipeList()` previously emitted one `ChangeFrom` per (image, oldVersion, suffix) triple because `ChangeFrom.newTag` was a literal string with no template support. ~35 hard-coded suffixes × 10 image families × (version - 8) version steps produced 3,150 children per invocation at version=17 and 5,950 at version=25. `ChangeFrom` now supports glob `*` capture and `$N` backref substitution in its new* fields. Replace the suffix enumeration with a single `oldTag = "<oldVersion>*"` + `newTag = "<version>$1"` recipe per (image, oldVersion) pair. `$1` carries whatever follows the old version digits (may be empty, `-jdk-alpine`, `-jre-noble`, or any arbitrary suffix), so the rewrite preserves the full OS/jdk/jre variant without enumerating it in advance. Per-invocation child count drops from ~3,150 -> ~90 at version=17 (35x), and from ~5,950 -> ~170 at version=25. Add two parameterized test cases exercising an exotic suffix that previously only survived the now-removed wildcard fallback (and lost its suffix).
1 parent 38caf16 commit f1fb66d

2 files changed

Lines changed: 20 additions & 54 deletions

File tree

src/main/java/org/openrewrite/java/migrate/UpgradeDockerImageVersion.java

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -33,71 +33,34 @@ public class UpgradeDockerImageVersion extends Recipe {
3333
example = "11")
3434
Integer version;
3535

36+
private static final String[] DEPRECATED_IMAGES = {"openjdk", "adoptopenjdk"};
37+
private static final String[] CURRENT_IMAGES = {
38+
"eclipse-temurin", "amazoncorretto", "azul/zulu-openjdk",
39+
"bellsoft/liberica-openjdk-debian", "bellsoft/liberica-openjdk-alpine",
40+
"bellsoft/liberica-openjdk-centos", "ibm-semeru-runtimes", "sapmachine"
41+
};
42+
3643
String displayName = "Upgrade Docker image Java version";
3744
String description = "Upgrade Docker image tags to use the specified Java version. " +
3845
"Updates common Java Docker images including eclipse-temurin, amazoncorretto, azul/zulu-openjdk, " +
39-
"and others. Also migrates deprecated images (openjdk, adoptopenjdk) to eclipse-temurin.";
46+
"and others. Also migrates deprecated images (openjdk, adoptopenjdk) to eclipse-temurin. " +
47+
"Uses a single `ChangeFrom` glob capture per (image, oldVersion) to preserve any tag suffix.";
4048

4149
@Override
4250
public List<Recipe> getRecipeList() {
4351
List<Recipe> recipes = new ArrayList<>();
44-
if (version == null) { // for uninitialized version
52+
if (version == null) {
4553
return recipes;
4654
}
47-
// Deprecated images -> migrate to eclipse-temurin
48-
String[] deprecatedImages = {"openjdk", "adoptopenjdk"};
49-
String[] currentImages = {
50-
"eclipse-temurin", "amazoncorretto", "azul/zulu-openjdk",
51-
"bellsoft/liberica-openjdk-debian", "bellsoft/liberica-openjdk-alpine",
52-
"bellsoft/liberica-openjdk-centos", "ibm-semeru-runtimes", "sapmachine"
53-
};
54-
// Common tag suffixes to preserve when upgrading current images
55-
// Longer suffixes must come before shorter ones to match correctly
56-
String[] commonSuffixes = {
57-
// Alpine
58-
"-jdk-alpine", "-jre-alpine",
59-
// Ubuntu LTS
60-
"-jdk-noble", "-jre-noble",
61-
"-jdk-jammy", "-jre-jammy",
62-
"-jdk-focal", "-jre-focal",
63-
"-jdk-bionic", "-jre-bionic",
64-
// Debian
65-
"-jdk-slim-bookworm", "-jre-slim-bookworm",
66-
"-jdk-slim-bullseye", "-jre-slim-bullseye",
67-
"-jdk-slim-buster", "-jre-slim-buster",
68-
"-jdk-bookworm", "-jre-bookworm",
69-
"-jdk-bullseye", "-jre-bullseye",
70-
"-jdk-buster", "-jre-buster",
71-
// Other Linux
72-
"-jdk-centos7", "-jre-centos7",
73-
"-jdk-ubi9-minimal", "-jre-ubi9-minimal",
74-
// Windows
75-
"-jdk-nanoserver", "-jre-nanoserver",
76-
"-jdk-windowsservercore", "-jre-windowsservercore",
77-
// Generic suffixes (must come last)
78-
"-alpine", "-slim",
79-
"-jdk", "-jre"
80-
};
55+
String newVer = version.toString();
8156
for (int oldVersion = 8; oldVersion < version; oldVersion++) {
82-
// Deprecated images: match specific suffixes first to preserve them
83-
for (String image : deprecatedImages) {
84-
for (String suffix : commonSuffixes) {
85-
recipes.add(new ChangeFrom(image, oldVersion + suffix, null, null, "eclipse-temurin", version + suffix, null, null));
86-
}
87-
}
88-
// Deprecated images: fall back to wildcard for remaining patterns
89-
for (String image : deprecatedImages) {
90-
recipes.add(new ChangeFrom(image, oldVersion + "*", null, null, "eclipse-temurin", version.toString(), null, null));
91-
}
92-
// Current images: match specific suffixes first to preserve them
93-
for (String image : currentImages) {
94-
for (String suffix : commonSuffixes) {
95-
recipes.add(new ChangeFrom(image, oldVersion + suffix, null, null, null, version + suffix, null, null));
96-
}
57+
String oldTag = oldVersion + "*";
58+
String newTag = newVer + "$1";
59+
for (String image : DEPRECATED_IMAGES) {
60+
recipes.add(new ChangeFrom(image, oldTag, null, null, "eclipse-temurin", newTag, null, null));
9761
}
98-
// Current images: fall back to wildcard for remaining patterns
99-
for (String image : currentImages) {
100-
recipes.add(new ChangeFrom(image, oldVersion + "*", null, null, null, version.toString(), null, null));
62+
for (String image : CURRENT_IMAGES) {
63+
recipes.add(new ChangeFrom(image, oldTag, null, null, null, newTag, null, null));
10164
}
10265
}
10366
return recipes;

src/test/java/org/openrewrite/java/migrate/UpgradeDockerImageVersionTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class UpgradeDockerImageVersionTest implements RewriteTest {
4747
"eclipse-temurin, 11-jdk-focal, eclipse-temurin, 17-jdk-focal, 17",
4848
"amazoncorretto, 11-alpine, amazoncorretto, 17-alpine, 17",
4949
"azul/zulu-openjdk, 11-jdk, azul/zulu-openjdk, 17-jdk, 17",
50+
// Arbitrary suffix preserved via $1 capture (previously lost through wildcard fallback)
51+
"openjdk, 11-jdk-custom-suffix, eclipse-temurin, 17-jdk-custom-suffix, 17",
52+
"eclipse-temurin, 11-jdk-custom-suffix, eclipse-temurin, 17-jdk-custom-suffix, 17",
5053
})
5154
@ParameterizedTest
5255
void upgradeDockerImage(String fromImage, String fromTag, String toImage, String toTag, int targetVersion) {

0 commit comments

Comments
 (0)