forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReleaseHighlightsGenerator.java
73 lines (62 loc) · 2.87 KB
/
ReleaseHighlightsGenerator.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
/*
* 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 java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* Generates the release highlights notes, for changelog files that contain the <code>highlight</code> field.
*/
public class ReleaseHighlightsGenerator {
static void update(File templateFile, File outputFile, List<ChangelogEntry> entries) throws IOException {
try (FileWriter output = new FileWriter(outputFile)) {
output.write(
generateFile(QualifiedVersion.of(VersionProperties.getElasticsearch()), Files.readString(templateFile.toPath()), entries)
);
}
}
@VisibleForTesting
static String generateFile(QualifiedVersion version, String template, List<ChangelogEntry> entries) throws IOException {
final List<String> priorVersions = new ArrayList<>();
if (version.minor() > 0) {
final int major = version.major();
for (int minor = version.minor() - 1; minor >= 0; minor--) {
String majorMinor = major + "." + minor;
priorVersions.add(
"{ref-bare}/"
+ majorMinor
+ "/release-highlights"
+ (minor <= 6 ? "-" + majorMinor + ".0" : "")
+ ".html["
+ majorMinor
+ "]"
);
}
}
final Map<Boolean, List<ChangelogEntry.Highlight>> groupedHighlights = entries.stream()
.map(ChangelogEntry::getHighlight)
.filter(Objects::nonNull)
.collect(Collectors.groupingBy(ChangelogEntry.Highlight::isNotable, Collectors.toList()));
final List<ChangelogEntry.Highlight> notableHighlights = groupedHighlights.getOrDefault(true, List.of());
final List<ChangelogEntry.Highlight> nonNotableHighlights = groupedHighlights.getOrDefault(false, List.of());
final Map<String, Object> bindings = new HashMap<>();
bindings.put("priorVersions", priorVersions);
bindings.put("notableHighlights", notableHighlights);
bindings.put("nonNotableHighlights", nonNotableHighlights);
return TemplateUtils.render(template, bindings);
}
}