forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateReleaseNotesTask.java
357 lines (295 loc) · 13.2 KB
/
GenerateReleaseNotesTask.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.gradle.internal.release;
import com.google.common.annotations.VisibleForTesting;
import org.elasticsearch.gradle.VersionProperties;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFile;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.gradle.process.ExecOperations;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.toSet;
/**
* Orchestrates the steps required to generate or update various release notes files.
*/
public class GenerateReleaseNotesTask extends DefaultTask {
private static final Logger LOGGER = Logging.getLogger(GenerateReleaseNotesTask.class);
private final ConfigurableFileCollection changelogs;
private final RegularFileProperty releaseNotesIndexTemplate;
private final RegularFileProperty releaseNotesTemplate;
private final RegularFileProperty releaseHighlightsTemplate;
private final RegularFileProperty breakingChangesTemplate;
private final RegularFileProperty releaseNotesIndexFile;
private final RegularFileProperty releaseNotesFile;
private final RegularFileProperty releaseHighlightsFile;
private final RegularFileProperty breakingChangesMigrationFile;
private final GitWrapper gitWrapper;
@Inject
public GenerateReleaseNotesTask(ObjectFactory objectFactory, ExecOperations execOperations) {
changelogs = objectFactory.fileCollection();
releaseNotesIndexTemplate = objectFactory.fileProperty();
releaseNotesTemplate = objectFactory.fileProperty();
releaseHighlightsTemplate = objectFactory.fileProperty();
breakingChangesTemplate = objectFactory.fileProperty();
releaseNotesIndexFile = objectFactory.fileProperty();
releaseNotesFile = objectFactory.fileProperty();
releaseHighlightsFile = objectFactory.fileProperty();
breakingChangesMigrationFile = objectFactory.fileProperty();
gitWrapper = new GitWrapper(execOperations);
}
@TaskAction
public void executeTask() throws IOException {
final String currentVersion = VersionProperties.getElasticsearch();
if (needsGitTags(currentVersion)) {
findAndUpdateUpstreamRemote(gitWrapper);
}
LOGGER.info("Finding changelog files...");
final Map<QualifiedVersion, Set<File>> filesByVersion = partitionFilesByVersion(
gitWrapper,
currentVersion,
this.changelogs.getFiles()
);
final List<ChangelogEntry> entries = new ArrayList<>();
final Map<QualifiedVersion, Set<ChangelogEntry>> changelogsByVersion = new HashMap<>();
filesByVersion.forEach((version, files) -> {
Set<ChangelogEntry> entriesForVersion = files.stream().map(ChangelogEntry::parse).collect(toSet());
entries.addAll(entriesForVersion);
changelogsByVersion.put(version, entriesForVersion);
});
final Set<QualifiedVersion> versions = getVersions(gitWrapper, currentVersion);
LOGGER.info("Updating release notes index...");
ReleaseNotesIndexGenerator.update(
versions,
this.releaseNotesIndexTemplate.get().getAsFile(),
this.releaseNotesIndexFile.get().getAsFile()
);
LOGGER.info("Generating release notes...");
final QualifiedVersion qualifiedVersion = QualifiedVersion.of(currentVersion);
ReleaseNotesGenerator.update(
this.releaseNotesTemplate.get().getAsFile(),
this.releaseNotesFile.get().getAsFile(),
qualifiedVersion,
changelogsByVersion.getOrDefault(qualifiedVersion, Set.of())
);
LOGGER.info("Generating release highlights...");
ReleaseHighlightsGenerator.update(
this.releaseHighlightsTemplate.get().getAsFile(),
this.releaseHighlightsFile.get().getAsFile(),
entries
);
LOGGER.info("Generating breaking changes / deprecations notes...");
BreakingChangesGenerator.update(
this.breakingChangesTemplate.get().getAsFile(),
this.breakingChangesMigrationFile.get().getAsFile(),
entries
);
}
/**
* Find all tags in the major series for the supplied version
* @param gitWrapper used to call `git`
* @param currentVersion the version to base the query upon
* @return all versions in the series
*/
@VisibleForTesting
static Set<QualifiedVersion> getVersions(GitWrapper gitWrapper, String currentVersion) {
QualifiedVersion v = QualifiedVersion.of(currentVersion);
final String pattern = "v" + v.major() + ".*";
Set<QualifiedVersion> versions = gitWrapper.listVersions(pattern).collect(toSet());
versions.add(v);
return versions;
}
/**
* Group a set of files by the version in which they first appeared, up until the supplied version. Any files not
* present in an earlier version are assumed to have been introduced in the specified version.
*
* <p>This method works by finding all git tags prior to {@param versionString} in the same minor series, and
* examining the git tree for that tag. By doing this over each tag, it is possible to see how the contents
* of the changelog directory changed over time.
*
* @param gitWrapper used to call `git`
* @param versionString the "current" version. Does not require a tag in git.
* @param allFilesInCheckout the files to partition
* @return a mapping from version to the files added in that version.
*/
@VisibleForTesting
static Map<QualifiedVersion, Set<File>> partitionFilesByVersion(
GitWrapper gitWrapper,
String versionString,
Set<File> allFilesInCheckout
) {
if (needsGitTags(versionString) == false) {
return Map.of(QualifiedVersion.of(versionString), allFilesInCheckout);
}
QualifiedVersion currentVersion = QualifiedVersion.of(versionString);
// Find all tags for this minor series, using a wildcard tag pattern.
String tagWildcard = String.format(Locale.ROOT, "v%d.%d*", currentVersion.major(), currentVersion.minor());
final List<QualifiedVersion> earlierVersions = gitWrapper.listVersions(tagWildcard)
// Only keep earlier versions, and if `currentVersion` is a prerelease, then only prereleases too.
.filter(
each -> each.isBefore(currentVersion)
&& (currentVersion.isSnapshot() || (currentVersion.hasQualifier() == each.hasQualifier()))
)
.sorted(naturalOrder())
.toList();
if (earlierVersions.isEmpty()) {
throw new GradleException("Failed to find git tags prior to [v" + currentVersion + "]");
}
Map<QualifiedVersion, Set<File>> partitionedFiles = new HashMap<>();
Set<File> mutableAllFilesInCheckout = new HashSet<>(allFilesInCheckout);
// 1. For each earlier version
earlierVersions.forEach(earlierVersion -> {
// 2. Find all the changelog files it contained
Set<String> filesInTreeForVersion = gitWrapper.listFiles("v" + earlierVersion, "docs/changelog")
.map(line -> Path.of(line).getFileName().toString())
.collect(toSet());
Set<File> filesForVersion = new HashSet<>();
partitionedFiles.put(earlierVersion, filesForVersion);
// 3. Find the `File` object for each one
final Iterator<File> filesIterator = mutableAllFilesInCheckout.iterator();
while (filesIterator.hasNext()) {
File nextFile = filesIterator.next();
if (filesInTreeForVersion.contains(nextFile.getName())) {
// 4. And remove it so that it is associated with the earlier version
filesForVersion.add(nextFile);
filesIterator.remove();
}
}
});
// 5. Associate whatever is left with the current version.
partitionedFiles.put(currentVersion, mutableAllFilesInCheckout);
return partitionedFiles;
}
/**
* Ensure the upstream git remote is up-to-date. The upstream is whatever git remote references `elastic/elasticsearch`.
* @param gitWrapper used to call `git`
*/
private static void findAndUpdateUpstreamRemote(GitWrapper gitWrapper) {
LOGGER.info("Finding upstream git remote");
// We need to ensure the tags are up-to-date. Find the correct remote to use
String upstream = gitWrapper.listRemotes()
.entrySet()
.stream()
.filter(entry -> entry.getValue().contains("elastic/elasticsearch"))
.findFirst()
.map(Map.Entry::getKey)
.orElseThrow(
() -> new GradleException(
"I need to ensure the git tags are up-to-date, but I couldn't find a git remote for [elastic/elasticsearch]"
)
);
LOGGER.info("Updating remote [{}]", upstream);
// Now update the remote, and make sure we update the tags too
gitWrapper.updateRemote(upstream);
LOGGER.info("Updating tags from [{}]", upstream);
gitWrapper.updateTags(upstream);
}
/**
* This methods checks the supplied version and answers {@code false} if the fetching of git
* tags can be skipped, or {@code true} otherwise.
* <p>
* The first version in a minor series will never have any preceding versions, so there's no
* need to fetch tags and examine the repository state in the past. This applies when the
* version is a release version, a snapshot, or the first alpha version. Subsequent alphas,
* betas and release candidates need to check the previous prelease tags.
*
* @param versionString the version string to check
* @return whether fetching git tags is required
*/
@VisibleForTesting
static boolean needsGitTags(String versionString) {
if (versionString.endsWith(".0") || versionString.endsWith(".0-SNAPSHOT") || versionString.endsWith(".0-alpha1")) {
return false;
}
return true;
}
@InputFiles
public FileCollection getChangelogs() {
return changelogs;
}
public void setChangelogs(FileCollection files) {
this.changelogs.setFrom(files);
}
@InputFile
public RegularFileProperty getReleaseNotesIndexTemplate() {
return releaseNotesIndexTemplate;
}
public void setReleaseNotesIndexTemplate(RegularFile file) {
this.releaseNotesIndexTemplate.set(file);
}
@InputFile
public RegularFileProperty getReleaseNotesTemplate() {
return releaseNotesTemplate;
}
public void setReleaseNotesTemplate(RegularFile file) {
this.releaseNotesTemplate.set(file);
}
@InputFile
public RegularFileProperty getReleaseHighlightsTemplate() {
return releaseHighlightsTemplate;
}
public void setReleaseHighlightsTemplate(RegularFile file) {
this.releaseHighlightsTemplate.set(file);
}
@InputFile
public RegularFileProperty getBreakingChangesTemplate() {
return breakingChangesTemplate;
}
public void setBreakingChangesTemplate(RegularFile file) {
this.breakingChangesTemplate.set(file);
}
@OutputFile
public RegularFileProperty getReleaseNotesIndexFile() {
return releaseNotesIndexFile;
}
public void setReleaseNotesIndexFile(RegularFile file) {
this.releaseNotesIndexFile.set(file);
}
@OutputFile
public RegularFileProperty getReleaseNotesFile() {
return releaseNotesFile;
}
public void setReleaseNotesFile(RegularFile file) {
this.releaseNotesFile.set(file);
}
@OutputFile
public RegularFileProperty getReleaseHighlightsFile() {
return releaseHighlightsFile;
}
public void setReleaseHighlightsFile(RegularFile file) {
this.releaseHighlightsFile.set(file);
}
@OutputFile
public RegularFileProperty getBreakingChangesMigrationFile() {
return breakingChangesMigrationFile;
}
public void setBreakingChangesMigrationFile(RegularFile file) {
this.breakingChangesMigrationFile.set(file);
}
}