Skip to content

Commit

Permalink
Generalize the release note generation to also determine operator rel…
Browse files Browse the repository at this point in the history
…ease notes with the same algorithm
  • Loading branch information
Donnerbart committed Nov 11, 2024
1 parent 018a355 commit e578145
Showing 1 changed file with 83 additions and 38 deletions.
121 changes: 83 additions & 38 deletions github-release-note-updater/src/main/java/com/hivemq/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.regex.Pattern;

public class Main {

Expand All @@ -28,6 +30,18 @@ public class Main {
[Updated to HiveMQ Platform Operator %s](%s)
""";

public static final @NotNull String PLATFORM_MAINTENANCE_RELEASE_URL =
"https://www.hivemq.com/changelog/hivemq-%s-%s-%s-released/";
public static final @NotNull String PLATFORM_FEATURE_RELEASE_URL =
"https://www.hivemq.com/changelog/whats-new-in-hivemq-%s-%s/";
public static final @NotNull String OPERATOR_RELEASE_URL =
"https://www.hivemq.com/changelog/hivemq-platform-operator-%s-%s-%s-release/";

private static final @NotNull Pattern PLATFORM_RELEASE_PATTERN =
Pattern.compile("^hivemq-platform-(\\d+\\.\\d+\\.\\d+)$");
private static final @NotNull Pattern OPERATOR_RELEASE_PATTERN =
Pattern.compile("^hivemq-platform-operator-(\\d+\\.\\d+\\.\\d+)$");

public static void main(final @NotNull String @NotNull [] args) throws Exception {
final var arguments = new Arguments();
final var jCommander = JCommander.newBuilder().addObject(arguments).build();
Expand Down Expand Up @@ -64,17 +78,42 @@ public static void main(final @NotNull String @NotNull [] args) throws Exception

// prepare the release notes
final var releaseNotes = new HashMap<String, String>();
setPlatformReleaseNotes(releaseNotes, releases, platformOperatorCharts, platformCharts);
setPlatformReleaseNotes(releaseNotes, releases, platformOperatorCharts, legacyOperatorCharts);
setPlatformReleaseNotes(releaseNotes, releases, platformOperatorCharts, swarmCharts);
platformOperatorCharts.forEach(chart -> {
final var releaseTag = String.format("%s-%s", chart.name(), chart.version());
final var releaseNote = String.format(OPERATOR_RELEASE_NOTE_TEMPLATE,
chart.description(),
chart.appVersion(),
setPlatformOperatorReleaseUrl(chart.appVersion()));
releaseNotes.put(releaseTag, releaseNote);
});
setReleaseNotes(releaseNotes,
releases,
OPERATOR_RELEASE_PATTERN,
platformCharts,
PLATFORM_RELEASE_NOTE_TEMPLATE,
Main::getPlatformReleaseUrl,
platformOperatorCharts,
OPERATOR_RELEASE_NOTE_TEMPLATE,
Main::getOperatorReleaseUrl);
setReleaseNotes(releaseNotes,
releases,
OPERATOR_RELEASE_PATTERN,
legacyOperatorCharts,
PLATFORM_RELEASE_NOTE_TEMPLATE,
Main::getPlatformReleaseUrl,
platformOperatorCharts,
OPERATOR_RELEASE_NOTE_TEMPLATE,
Main::getOperatorReleaseUrl);
setReleaseNotes(releaseNotes,
releases,
OPERATOR_RELEASE_PATTERN,
swarmCharts,
PLATFORM_RELEASE_NOTE_TEMPLATE,
Main::getPlatformReleaseUrl,
platformOperatorCharts,
OPERATOR_RELEASE_NOTE_TEMPLATE,
Main::getOperatorReleaseUrl);
setReleaseNotes(releaseNotes,
releases,
PLATFORM_RELEASE_PATTERN,
platformOperatorCharts,
OPERATOR_RELEASE_NOTE_TEMPLATE,
Main::getOperatorReleaseUrl,
platformCharts,
PLATFORM_RELEASE_NOTE_TEMPLATE,
Main::getPlatformReleaseUrl);

// update the GitHub release notes
try (var executorService = Executors.newSingleThreadExecutor()) {
Expand All @@ -99,68 +138,74 @@ public static void main(final @NotNull String @NotNull [] args) throws Exception
}
}

private static void setPlatformReleaseNotes(
private static void setReleaseNotes(
final @NotNull Map<String, String> releaseNotes,
final @NotNull List<Release> releases,
final @NotNull List<Chart> platformOperatorCharts,
final @NotNull List<Chart> charts) {
final @NotNull Pattern matchingReleasePattern,
final @NotNull List<Chart> charts,
final @NotNull String releaseNoteTemplate,
final @NotNull Function<Version, String> releaseUrlFunction,
final @NotNull List<Chart> otherCharts,
final @NotNull String otherReleaseNoteTemplate,
final @NotNull Function<Version, String> otherReleaseUrlFunction) {
for (int i = 0; i < charts.size(); i++) {
// we get the previous chart and check if the appVersion has changed in the current chart
// (if so we generate a release note for this chart, otherwise for the matching otherChart)
final var chart = charts.get(i);
final var previousChart = i == 0 ? null : charts.get(i - 1);
final var wasChartUpdated = previousChart == null || !previousChart.appVersion().equals(chart.appVersion());
final var releaseTag = String.format("%s-%s", chart.name(), chart.version());
final var operatorReleaseOptional = getMatchingOperatorRelease(releases, releaseTag);
if (wasChartUpdated || operatorReleaseOptional.isEmpty()) {
// this release was triggered by a HiveMQ Platform release
final var releaseNote = String.format(PLATFORM_RELEASE_NOTE_TEMPLATE,
final var otherReleaseOptional = getMatchingRelease(releases, releaseTag, matchingReleasePattern);
if (wasChartUpdated || otherReleaseOptional.isEmpty()) {
// this release was triggered by an update of this chart
final var releaseNote = String.format(releaseNoteTemplate,
chart.description(),
chart.appVersion(),
getPlatformReleaseUrl(chart.appVersion()));
releaseUrlFunction.apply(chart.version()));
releaseNotes.put(releaseTag, releaseNote);
} else {
// this release was triggered by a HiveMQ Platform Operator release
final var operatorRelease = operatorReleaseOptional.get();
final var operatorChartVersion = Version.parse(operatorRelease.tagName()
.substring(operatorRelease.tagName().lastIndexOf('-') + 1));
final var operatorChart = platformOperatorCharts.stream()
.filter(c -> c.version().equals(operatorChartVersion))
// this release was triggered by an update of the other chart
final var otherRelease = otherReleaseOptional.get();
final var otherChartVersion =
Version.parse(otherRelease.tagName().substring(otherRelease.tagName().lastIndexOf('-') + 1));
final var otherChart = otherCharts.stream()
.filter(c -> c.version().equals(otherChartVersion))
.findFirst()
.orElseThrow();
final var releaseNote = String.format(OPERATOR_RELEASE_NOTE_TEMPLATE,
final var releaseNote = String.format(otherReleaseNoteTemplate,
chart.description(),
operatorChart.appVersion(),
setPlatformOperatorReleaseUrl(operatorChart.appVersion()));
otherChart.appVersion(),
otherReleaseUrlFunction.apply(otherChart.appVersion()));
releaseNotes.put(releaseTag, releaseNote);
}
}
}

private static @NotNull Optional<Release> getMatchingOperatorRelease(
final @NotNull List<Release> releases, //
final @NotNull String releaseTag) {
private static @NotNull Optional<Release> getMatchingRelease(
final @NotNull List<Release> releases,
final @NotNull String releaseTag,
final @NotNull Pattern matchingReleasePattern) {
return releases.stream()
.filter(release -> release.tagName().equals(releaseTag))
.findFirst()
.flatMap(value -> releases.stream()
.filter(release -> release.tagName().startsWith("hivemq-platform-operator-"))
.filter(release -> matchingReleasePattern.matcher(release.tagName()).matches())
.filter(release -> release.publishedAt().equals(value.publishedAt()))
.findFirst());
}

private static @NotNull String getPlatformReleaseUrl(final @NotNull Version version) {
if (version.patchVersion() != 0) {
return String.format("https://www.hivemq.com/changelog/hivemq-%s-%s-%s-released/",
return String.format(PLATFORM_MAINTENANCE_RELEASE_URL,
version.majorVersion(),
version.minorVersion(),
version.patchVersion());
}
return String.format("https://www.hivemq.com/changelog/whats-new-in-hivemq-%s-%s/",
version.majorVersion(),
version.minorVersion());
return String.format(PLATFORM_FEATURE_RELEASE_URL, version.majorVersion(), version.minorVersion());
}

private static @NotNull String setPlatformOperatorReleaseUrl(final @NotNull Version version) {
return String.format("https://www.hivemq.com/changelog/hivemq-platform-operator-%s-%s-%s-release/",
private static @NotNull String getOperatorReleaseUrl(final @NotNull Version version) {
return String.format(OPERATOR_RELEASE_URL,
version.majorVersion(),
version.minorVersion(),
version.patchVersion());
Expand Down

0 comments on commit e578145

Please sign in to comment.