-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprerequisites.java
executable file
·334 lines (281 loc) · 13.2 KB
/
prerequisites.java
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.quarkus.platform:quarkus-bom:3.17.7@pom
//DEPS io.quarkus:quarkus-picocli
//DEPS org.kohsuke:github-api:1.326
//DEPS org.apache.maven:maven-artifact:3.9.6
//JAVA 21
//JAVAC_OPTIONS -parameters
//JAVA_OPTIONS -Djava.util.logging.manager=org.jboss.logmanager.LogManager
//Q:CONFIG quarkus.log.level=SEVERE
//Q:CONFIG quarkus.banner.enabled=false
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.NavigableSet;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.kohsuke.github.GHFileNotFoundException;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHMilestone;
import org.kohsuke.github.GHRelease;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.apache.maven.artifact.versioning.ComparableVersion;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@Command(name = "prerequisites", mixinStandardHelpOptions = true)
public class prerequisites implements Runnable {
private static final Pattern VERSION_PATTERN = Pattern.compile("^[0-9]+\\.[0-9]+$");
private static final Pattern FINAL_VERSION_PATTERN = Pattern.compile("^[0-9]+\\.[0-9]+\\.[0-9]+(\\.[0-9]+)?$");
private static final Pattern DIGITS_PATTERN = Pattern.compile("\\d+");
@Option(names = "--branch", description = "The branch to build the release on", required = true)
String branch;
@Option(names = "--origin-branch", description = "The origin branch when creating the branch for CR1", defaultValue = "main")
String originBranch;
@Option(names = "--qualifier", description = "The qualifier to add to the version. Example: CR1.", defaultValue = "")
String qualifier;
@Deprecated(forRemoval = true) // this is automatically resolved now
@Option(names = "--micro", description = "Should we release a micro?", defaultValue = "false")
boolean micro;
@Option(names = "--major", description = "Should we release a major?", defaultValue = "false")
boolean major;
@Deprecated(forRemoval = true) // this is automatically resolved now
@Option(names = "--maintenance", description = "Is it a maintenance branch?", defaultValue = "false")
boolean maintenance;
@Option(names = "--emergency", description = "Should we release an emergency release?", defaultValue = "false")
boolean emergency;
@Option(names = "--lts", description = "Is it a LTS branch?", defaultValue = "false")
boolean lts;
@Override
public void run() {
if (new File("work").exists()) {
fail("Work directory exists, please execute cleanup.sh before releasing");
return;
}
if (major && micro) {
fail("A release may not be both a major release and a micro release");
return;
}
if (emergency && !qualifier.isBlank()) {
fail("May not release emergency release with qualifier defined");
return;
}
if (emergency && major) {
fail("A release may not be both a major release and an emergency release");
return;
}
if (emergency && micro) {
fail("A release may not be both a micro release and an emergency release");
return;
}
if (branch.isBlank()) {
fail("Branch should be defined with --branch <branch>");
return;
}
try {
GitHub github;
String releaseGitHubToken = System.getenv("RELEASE_GITHUB_TOKEN");
if (releaseGitHubToken != null && !releaseGitHubToken.isBlank()) {
github = new GitHubBuilder().withOAuthToken(releaseGitHubToken).build();
} else {
github = GitHubBuilder.fromPropertyFile().build();
}
GHRepository repository = getGithubProject(github);
if (major) {
System.out.println("Releasing a major release");
}
if (emergency) {
System.out.println("Releasing an emergency release");
}
micro = micro || (!isMain(branch) && !major);
boolean firstCrWithAutomatedRelease = releaseGitHubToken != null && isFirstCR(qualifier);
if (!VERSION_PATTERN.matcher(branch).matches() && !isMain(branch)) {
fail("Branch " + branch + " is not a valid version (X.y)");
return;
}
if (!firstCrWithAutomatedRelease) {
try {
repository.getBranch(branch);
} catch (GHFileNotFoundException e) {
fail("Branch " + branch + " does not exist in the repository");
return;
}
}
System.out.println("Working on branch: " + branch);
System.out.println("Listing tags of " + repository.getName());
NavigableSet<ComparableVersion> tags = new TreeSet<>();
tags.addAll(repository.listTags().toList().stream()
.map(t -> t.getName())
.map(n -> new ComparableVersion(n))
.collect(Collectors.toList()));
tags = tags.descendingSet();
if (tags.isEmpty()) {
fail("No tags in repository " + repository.getName());
return;
}
ComparableVersion tag = null;
if (isMain(branch)) {
// no branch, we take the last tag
tag = tags.iterator().next();
} else {
// we defined a branch, we determine the last tag with the branch prefix
for (ComparableVersion currentTag : tags) {
if (currentTag.toString().startsWith(branch + ".")) {
tag = currentTag;
break;
}
}
}
String newVersion;
if (tag != null) {
System.out.println("Last tag is: " + tag);
// Retrieve the associated release
GHRelease release = repository.getReleaseByTagName(tag.toString());
if (release == null) {
System.err.println("[WARNING] No release associated with tag " + tag);
}
// All good, compute new version.
newVersion = computeNewVersion(tag.toString(), micro, major, emergency, qualifier);
} else {
if (!qualifier.isBlank()) {
newVersion = branch + ".0." + qualifier;
} else {
newVersion = branch + ".0";
}
}
// Check there are no tag with this version
boolean tagAlreadyExists = tags.stream()
.anyMatch(t -> newVersion.equals(t.toString()));
if (tagAlreadyExists) {
fail("There is a tag with name " + newVersion + ", invalid increment");
}
// Check there is a milestone with the right name
if (!firstCrWithAutomatedRelease) {
checkIfMilestoneExists(repository, newVersion);
}
System.out.println("Listing releases of " + repository.getName());
NavigableSet<ComparableVersion> releases = new TreeSet<>();
releases.addAll(repository.listReleases().toList().stream()
.map(t -> t.getName())
.map(n -> new ComparableVersion(n))
.collect(Collectors.toList()));
releases = releases.descendingSet();
// Completion
new File("work/").mkdirs();
System.out.println("Writing " + newVersion + " into the 'work/newVersion' file");
Files.writeString(Path.of("work", "newVersion"), newVersion, StandardCharsets.UTF_8);
System.out.println("Writing " + branch + " into the 'work/branch' file");
Files.writeString(Path.of("work", "branch"), branch, StandardCharsets.UTF_8);
if (micro) {
System.out.println("Releasing a micro release");
new File("work/micro").createNewFile();
}
if (maintenance || isMaintenance(branch, releases)) {
System.out.println("Releasing a maintenance release");
new File("work/maintenance").createNewFile();
}
if (lts) {
System.out.println("Releasing a LTS release");
new File("work/lts").createNewFile();
}
if (!newVersion.endsWith(".Final") && !FINAL_VERSION_PATTERN.matcher(newVersion).matches()) {
new File("work/preview").createNewFile();
}
Files.writeString(Path.of("work", "originBranch"), originBranch);
} catch (IOException e) {
e.printStackTrace();
fail("IOException was thrown, please see above");
}
}
private static void checkIfMilestoneExists(GHRepository repository, String version) throws IOException {
Optional<GHMilestone> milestoneOptional = repository.listMilestones(GHIssueState.OPEN).toList().stream()
.filter(m -> version.equals(m.getTitle()))
.findFirst();
if (milestoneOptional.isEmpty()) {
fail("No milestone found with version " + version);
} else {
GHMilestone milestone = milestoneOptional.get();
System.out.println("Found milestone " + milestone.getTitle());
if (milestone.getOpenIssues() != 0) {
System.err.println("[WARNING] Milestone " + version + " found, but " + milestone.getOpenIssues() + " issue(s) is/are still opened, check " + milestone.getHtmlUrl());
}
}
}
private static GHRepository getGithubProject(GitHub github) throws IOException {
return github.getRepository("quarkusio/quarkus");
}
private static void fail(String message) {
System.err.println("[ERROR] " + message);
System.exit(2);
}
private static String computeNewVersion(String previousVersion, boolean micro, boolean major, boolean emergency, String qualifier) {
String[] segments = previousVersion.split("\\.");
if (segments.length < 3) {
fail("Invalid version " + previousVersion + ", number of segments must be at least 3, found: " + segments.length);
}
String newVersion;
if (emergency) {
if (segments.length >= 4) {
newVersion = segments[0] + "." + segments[1] + "." + segments[2] + "." + (Integer.parseInt(segments[3]) + 1);
} else {
newVersion = segments[0] + "." + segments[1] + "." + segments[2] + ".1";
}
} else if (micro) {
if (segments.length == 3) {
// previous version was a final, we increment
newVersion = segments[0] + "." + segments[1] + "." + (Integer.parseInt(segments[2]) + 1);
} else {
String previousQualifier = segments[3];
// we are post an emergency release
if (DIGITS_PATTERN.matcher(previousQualifier).matches()) {
newVersion = segments[0] + "." + segments[1] + "." + (Integer.parseInt(segments[2]) + 1);
} else if ("Final".equals(previousQualifier)) {
// previous version was a final, we increment
newVersion = segments[0] + "." + segments[1] + "." + (Integer.parseInt(segments[2]) + 1);
// previous version had a Final qualifier so we are releasing a micro of a version with Final qualifiers
qualifier = "Final";
} else {
// previous version was a preview, we don't increment
newVersion = segments[0] + "." + segments[1] + "." + segments[2];
}
}
} else if (major) {
newVersion = (Integer.parseInt(segments[0]) + 1) + ".0.0";
} else {
newVersion = segments[0] + "." + (Integer.parseInt(segments[1]) + 1) + ".0";
}
if (!qualifier.isBlank()) {
newVersion = newVersion + "." + qualifier;
}
return newVersion;
}
private static String getCurrentStableBranch(Set<ComparableVersion> tags) {
for (ComparableVersion candidate : tags) {
String[] segments = candidate.toString().split("\\.");
if (segments.length == 3) {
// this is the last stable version
return segments[0] + "." + segments[1];
}
}
fail("Unable to find the current stable branch");
return null;
}
private static boolean isMaintenance(String branch, Set<ComparableVersion> tags) {
if (isMain(branch)) {
return false;
}
return new ComparableVersion(getCurrentStableBranch(tags)).compareTo(new ComparableVersion(branch)) > 0;
}
private static boolean isMain(String branch) {
return "main".equals(branch);
}
private static boolean isFirstCR(String qualifier) {
return "CR1".equalsIgnoreCase(qualifier);
}
}