-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSetupJavaUpgradeJavaVersion.java
More file actions
90 lines (75 loc) · 3.37 KB
/
Copy pathSetupJavaUpgradeJavaVersion.java
File metadata and controls
90 lines (75 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.github;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.yaml.JsonPathMatcher;
import org.openrewrite.yaml.YamlVisitor;
import org.openrewrite.yaml.tree.Yaml;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@EqualsAndHashCode(callSuper = false)
@Value
public class SetupJavaUpgradeJavaVersion extends Recipe {
@Option(displayName = "Minimum major Java version (defaults to 21)",
example = "21",
required = false)
@Nullable
Integer minimumJavaMajorVersion;
String displayName = "Upgrade `actions/setup-java` `java-version`";
String description = "Update the Java version used by `actions/setup-java` if it is below the expected version number.";
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new IsGitHubActionsFile(), new UpgradeJavaVersionVisitor(
minimumJavaMajorVersion == null ? 21 : minimumJavaMajorVersion
));
}
@AllArgsConstructor
private static class UpgradeJavaVersionVisitor extends YamlVisitor<ExecutionContext> {
private static final JsonPathMatcher javaVersion = new JsonPathMatcher("..steps[?(@.uses =~ 'actions/setup-java@v*.*')].with.java-version");
private static final Pattern javaVersionPattern = Pattern.compile("([0-9]+)(\\.[0-9]+)*([-+].*)?");
private final int minimumJavaMajorVersion;
@Override
public Yaml visitMappingEntry(Yaml.Mapping.Entry entry, ExecutionContext ctx) {
if (!"java-version".equals(entry.getKey().getValue()) ||
!javaVersion.matches(getCursor())) {
return super.visitMappingEntry(entry, ctx);
}
Yaml.Scalar currentValue = (Yaml.Scalar) entry.getValue();
// specific versions are allowed by `actions/setup-java`
Matcher matcher = javaVersionPattern.matcher(currentValue.getValue());
if (!matcher.matches()) {
return super.visitMappingEntry(entry, ctx);
}
int currentMajorVersion;
try {
currentMajorVersion = Integer.parseInt(matcher.group(1));
} catch (NumberFormatException ex) {
return super.visitMappingEntry(entry, ctx);
}
if (currentMajorVersion >= minimumJavaMajorVersion) {
return super.visitMappingEntry(entry, ctx);
}
return super.visitMappingEntry(
entry.withValue(currentValue.withValue(String.valueOf(minimumJavaMajorVersion))),
ctx
);
}
}
}