Skip to content

Commit

Permalink
Improve release note updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Donnerbart committed Nov 11, 2024
1 parent 0b9de61 commit b680f4e
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 48 deletions.
2 changes: 1 addition & 1 deletion github-release-note-updater/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ java {
}

application {
mainClass = "com.hivemq.Main"
mainClass = "com.hivemq.release.GitHubReleaseNotesUpdater"
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hivemq;
package com.hivemq.release;

import com.beust.jcommander.Parameter;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hivemq;
package com.hivemq.release;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hivemq;
package com.hivemq.release;

import com.beust.jcommander.JCommander;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -14,8 +14,10 @@
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 {
public class GitHubReleaseNotesUpdater {

private static final @NotNull String PLATFORM_RELEASE_NOTE_TEMPLATE = """
%s
Expand All @@ -28,6 +30,18 @@ public class Main {
[Updated to HiveMQ Platform Operator %s](%s)
""";

private static final @NotNull String PLATFORM_MAINTENANCE_RELEASE_URL =
"https://www.hivemq.com/changelog/hivemq-%s-%s-%s-released/";
private static final @NotNull String PLATFORM_FEATURE_RELEASE_URL =
"https://www.hivemq.com/changelog/whats-new-in-hivemq-%s-%s/";
private 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 All @@ -40,11 +54,11 @@ public static void main(final @NotNull String @NotNull [] args) throws Exception
final var chartsPath = Path.of(arguments.path, "charts.json");
final var releasesPath = Path.of(arguments.path, "releases.json");
if (Files.notExists(chartsPath)) {
System.err.println("Charts path does not exist.");
System.err.printf("Charts path '%s' does not exist%n", chartsPath);
System.exit(1);
}
if (Files.notExists(releasesPath)) {
System.err.println("Releases path does not exist.");
System.err.printf("Releases path '%s' does not exist%n", releasesPath);
System.exit(1);
}

Expand All @@ -64,20 +78,46 @@ 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,
GitHubReleaseNotesUpdater::getPlatformReleaseUrl,
platformOperatorCharts,
OPERATOR_RELEASE_NOTE_TEMPLATE,
GitHubReleaseNotesUpdater::getOperatorReleaseUrl);
setReleaseNotes(releaseNotes,
releases,
OPERATOR_RELEASE_PATTERN,
legacyOperatorCharts,
PLATFORM_RELEASE_NOTE_TEMPLATE,
GitHubReleaseNotesUpdater::getPlatformReleaseUrl,
platformOperatorCharts,
OPERATOR_RELEASE_NOTE_TEMPLATE,
GitHubReleaseNotesUpdater::getOperatorReleaseUrl);
setReleaseNotes(releaseNotes,
releases,
OPERATOR_RELEASE_PATTERN,
swarmCharts,
PLATFORM_RELEASE_NOTE_TEMPLATE,
GitHubReleaseNotesUpdater::getPlatformReleaseUrl,
platformOperatorCharts,
OPERATOR_RELEASE_NOTE_TEMPLATE,
GitHubReleaseNotesUpdater::getOperatorReleaseUrl);
setReleaseNotes(releaseNotes,
releases,
PLATFORM_RELEASE_PATTERN,
platformOperatorCharts,
OPERATOR_RELEASE_NOTE_TEMPLATE,
GitHubReleaseNotesUpdater::getOperatorReleaseUrl,
platformCharts,
PLATFORM_RELEASE_NOTE_TEMPLATE,
GitHubReleaseNotesUpdater::getPlatformReleaseUrl);

// update the GitHub release notes
try (var executorService = Executors.newSingleThreadExecutor()) {
var success = true;
for (final var release : releases) {
final var releaseTag = release.tagName();
final var releaseNote = releaseNotes.get(releaseTag);
Expand All @@ -94,73 +134,85 @@ public static void main(final @NotNull String @NotNull [] args) throws Exception
"hivemq/helm-charts",
"--notes",
releaseNote);
System.out.println(releaseTag + " -> " + (exitCode == 0 ? "SUCCESS" : "FAILURE"));
if (exitCode != 0) {
success = false;
}
}
if (!success) {
System.err.println("Not all release notes were updated successfully!");
System.exit(1);
}
}
}

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.appVersion()));
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hivemq;
package com.hivemq.release;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hivemq;
package com.hivemq.release;

import org.jetbrains.annotations.NotNull;

Expand Down

0 comments on commit b680f4e

Please sign in to comment.