Skip to content

Commit

Permalink
Correct links used in upgrade issues
Browse files Browse the repository at this point in the history
Update BOMR so that the `Upgrade` type now contains a copy of the
library we're upgrading to complete with an updated version number.

This allows the correct links to be generated.

Closes gh-43545
  • Loading branch information
philwebb committed Dec 17, 2024
1 parent cbb8d12 commit aa374bf
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public Library(String name, String calendarName, LibraryVersion version, List<Gr
this.versionAlignment = versionAlignment;
this.alignsWithBom = alignsWithBom;
this.linkRootName = (linkRootName != null) ? linkRootName : generateLinkRootName(name);
this.links = Collections.unmodifiableMap(new TreeMap<>(links));
this.links = (links != null) ? Collections.unmodifiableMap(new TreeMap<>(links)) : Collections.emptyMap();
}

private static String generateLinkRootName(String name) {
Expand Down Expand Up @@ -167,6 +167,15 @@ public List<Link> getLinks(String name) {
return this.links.get(name);
}

public String getNameAndVersion() {
return getName() + " " + getVersion();
}

public Library withVersion(LibraryVersion version) {
return new Library(this.name, this.calendarName, version, this.groups, this.prohibitedVersions,
this.considerSnapshots, this.versionAlignment, this.alignsWithBom, this.linkRootName, this.links);
}

/**
* A version or range of versions that are prohibited from being used in a bom.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,42 @@ public List<Upgrade> resolveUpgrades(Collection<Library> librariesToUpgrade, Col
for (Library library : libraries) {
librariesByName.put(library.getName(), library);
}
List<LibraryWithVersionOptions> libraryUpdates = this.libraryUpdateResolver
.findLibraryUpdates(librariesToUpgrade, librariesByName);
try {
return libraryUpdates.stream().map(this::resolveUpgrade).filter(Objects::nonNull).toList();
return this.libraryUpdateResolver.findLibraryUpdates(librariesToUpgrade, librariesByName)
.stream()
.map(this::resolveUpgrade)
.filter(Objects::nonNull)
.toList();
}
catch (UpgradesInterruptedException ex) {
return Collections.emptyList();
}
}

private Upgrade resolveUpgrade(LibraryWithVersionOptions libraryWithVersionOptions) {
if (libraryWithVersionOptions.getVersionOptions().isEmpty()) {
Library library = libraryWithVersionOptions.getLibrary();
List<VersionOption> versionOptions = libraryWithVersionOptions.getVersionOptions();
if (versionOptions.isEmpty()) {
return null;
}
VersionOption defaultOption = new VersionOption(
libraryWithVersionOptions.getLibrary().getVersion().getVersion());
VersionOption defaultOption = new VersionOption(library.getVersion().getVersion());
VersionOption selected = selectOption(defaultOption, library, versionOptions);
return (selected.equals(defaultOption)) ? null : new Upgrade(library, selected.getVersion());
}

private VersionOption selectOption(VersionOption defaultOption, Library library,
List<VersionOption> versionOptions) {
VersionOption selected = this.userInputHandler.askUser((questions) -> {
String question = libraryWithVersionOptions.getLibrary().getName() + " "
+ libraryWithVersionOptions.getLibrary().getVersion().getVersion();
String question = library.getNameAndVersion();
List<VersionOption> options = new ArrayList<>();
options.add(defaultOption);
options.addAll(libraryWithVersionOptions.getVersionOptions());
options.addAll(versionOptions);
return questions.selectOption(question, options, defaultOption);
}).get();
if (this.userInputHandler.interrupted()) {
throw new UpgradesInterruptedException();
}
return (selected.equals(defaultOption)) ? null
: new Upgrade(libraryWithVersionOptions.getLibrary(), selected.getVersion());
return selected;
}

static class UpgradesInterruptedException extends RuntimeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.boot.build.bom.bomr;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
Expand All @@ -33,7 +32,6 @@
import org.springframework.boot.build.bom.BomExtension;
import org.springframework.boot.build.bom.Library;
import org.springframework.boot.build.bom.bomr.ReleaseSchedule.Release;
import org.springframework.boot.build.bom.bomr.github.Issue;
import org.springframework.boot.build.bom.bomr.github.Milestone;
import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
import org.springframework.boot.build.properties.BuildProperties;
Expand Down Expand Up @@ -67,38 +65,10 @@ void upgradeDependencies() {
super.upgradeDependencies();
}

@Override
protected String issueTitle(Upgrade upgrade) {
return "Upgrade to " + description(upgrade);
}

private String description(Upgrade upgrade) {
String snapshotVersion = upgrade.getVersion().toString();
String releaseVersion = snapshotVersion.substring(0, snapshotVersion.length() - "-SNAPSHOT".length());
return upgrade.getLibrary().getName() + " " + releaseVersion;
}

@Override
protected String issueBody(Upgrade upgrade, Issue existingUpgrade) {
Library library = upgrade.getLibrary();
String releaseNotesLink = library.getLinkUrl("releaseNotes");
List<String> lines = new ArrayList<>();
String description = description(upgrade);
if (releaseNotesLink != null) {
lines.add("Upgrade to [%s](%s).".formatted(description, releaseNotesLink));
}
else {
lines.add("Upgrade to %s.".formatted(description));
}
if (existingUpgrade != null) {
lines.add("Supersedes #" + existingUpgrade.getNumber());
}
return String.join("\n\n", lines);
}

@Override
protected String commitMessage(Upgrade upgrade, int issueNumber) {
return "Start building against " + description(upgrade) + " snapshots" + "\n\nSee gh-" + issueNumber;
return "Start building against " + upgrade.toRelease().getNameAndVersion() + " snapshots" + "\n\nSee gh-"
+ issueNumber;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,30 +17,33 @@
package org.springframework.boot.build.bom.bomr;

import org.springframework.boot.build.bom.Library;
import org.springframework.boot.build.bom.Library.LibraryVersion;
import org.springframework.boot.build.bom.bomr.version.DependencyVersion;

/**
* An upgrade to change a {@link Library} to use a new version.
*
* @author Andy Wilkinson
* @author Phillip Webb
* @param from the library we're upgrading from
* @param to the library we're upgrading to (may be a SNAPSHOT)
* @param toRelease the release version of the library we're ultimately upgrading to
*/
final class Upgrade {
record Upgrade(Library from, Library to, Library toRelease) {

private final Library library;

private final DependencyVersion version;

Upgrade(Library library, DependencyVersion version) {
this.library = library;
this.version = version;
Upgrade(Library from, DependencyVersion to) {
this(from, from.withVersion(new LibraryVersion(to)));
}

Library getLibrary() {
return this.library;
Upgrade(Library from, Library to) {
this(from, to, withReleaseVersion(to));
}

DependencyVersion getVersion() {
return this.version;
private static Library withReleaseVersion(Library to) {
String version = to.getVersion().toString();
version = version.replace(".BUILD-SNAPSHOT", "");
version = version.replace("-SNAPSHOT", "");
return to.withVersion(new LibraryVersion(DependencyVersion.parse(version)));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,16 +42,14 @@ class UpgradeApplicator {

Path apply(Upgrade upgrade) throws IOException {
String buildFileContents = Files.readString(this.buildFile);
Matcher matcher = Pattern.compile("library\\(\"" + upgrade.getLibrary().getName() + "\", \"(.+)\"\\)")
.matcher(buildFileContents);
String toName = upgrade.to().getName();
Matcher matcher = Pattern.compile("library\\(\"" + toName + "\", \"(.+)\"\\)").matcher(buildFileContents);
if (!matcher.find()) {
matcher = Pattern
.compile("library\\(\"" + upgrade.getLibrary().getName() + "\"\\) \\{\\s+version\\(\"(.+)\"\\)",
Pattern.MULTILINE)
matcher = Pattern.compile("library\\(\"" + toName + "\"\\) \\{\\s+version\\(\"(.+)\"\\)", Pattern.MULTILINE)
.matcher(buildFileContents);
if (!matcher.find()) {
throw new IllegalStateException("Failed to find definition for library '"
+ upgrade.getLibrary().getName() + "' in bom '" + this.buildFile + "'");
throw new IllegalStateException("Failed to find definition for library '" + upgrade.to().getName()
+ "' in bom '" + this.buildFile + "'");
}
}
String version = matcher.group(1);
Expand All @@ -68,14 +66,14 @@ Path apply(Upgrade upgrade) throws IOException {
private void updateGradleProperties(Upgrade upgrade, String version) throws IOException {
String property = version.substring(2, version.length() - 1);
String gradlePropertiesContents = Files.readString(this.gradleProperties);
String modified = gradlePropertiesContents.replace(
property + "=" + upgrade.getLibrary().getVersion().getVersion(), property + "=" + upgrade.getVersion());
String modified = gradlePropertiesContents.replace(property + "=" + upgrade.from().getVersion(),
property + "=" + upgrade.to().getVersion());
overwrite(this.gradleProperties, modified);
}

private void updateBuildFile(Upgrade upgrade, String buildFileContents, int versionStart, int versionEnd)
throws IOException {
String modified = buildFileContents.substring(0, versionStart) + upgrade.getVersion()
String modified = buildFileContents.substring(0, versionStart) + upgrade.to().getVersion()
+ buildFileContents.substring(versionEnd);
overwrite(this.buildFile, modified);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package org.springframework.boot.build.bom.bomr;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.gradle.api.Task;
Expand All @@ -27,8 +24,6 @@
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;

import org.springframework.boot.build.bom.BomExtension;
import org.springframework.boot.build.bom.Library.LibraryVersion;
import org.springframework.boot.build.bom.bomr.github.Issue;
import org.springframework.boot.build.properties.BuildProperties;

/**
Expand Down Expand Up @@ -63,32 +58,9 @@ private void addCommercialRepositories() {
"spring-commercial-release");
}

@Override
protected String issueTitle(Upgrade upgrade) {
return "Upgrade to " + upgrade.getLibrary().getName() + " " + upgrade.getVersion();
}

@Override
protected String commitMessage(Upgrade upgrade, int issueNumber) {
return issueTitle(upgrade) + "\n\nCloses gh-" + issueNumber;
}

@Override
protected String issueBody(Upgrade upgrade, Issue existingUpgrade) {
LibraryVersion upgradeVersion = new LibraryVersion(upgrade.getVersion());
String releaseNotesLink = upgrade.getLibrary().getLinkUrl("releaseNotes");
List<String> lines = new ArrayList<>();
String description = upgrade.getLibrary().getName() + " " + upgradeVersion;
if (releaseNotesLink != null) {
lines.add("Upgrade to [%s](%s).".formatted(description, releaseNotesLink));
}
else {
lines.add("Upgrade to %s.".formatted(description));
}
if (existingUpgrade != null) {
lines.add("Supersedes #" + existingUpgrade.getNumber());
}
return String.join("\n\n", lines);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void applyUpgrades(GitHubRepository repository, List<String> issueLabels
System.out.println("Applying upgrades...");
System.out.println("");
for (Upgrade upgrade : upgrades) {
System.out.println(upgrade.getLibrary().getName() + " " + upgrade.getVersion());
System.out.println(upgrade.to().getNameAndVersion());
Issue existingUpgradeIssue = findExistingUpgradeIssue(existingUpgradeIssues, upgrade);
try {
Path modified = this.upgradeApplicator.apply(upgrade);
Expand Down Expand Up @@ -207,7 +207,7 @@ private Milestone determineMilestone(GitHubRepository repository) {
}

private Issue findExistingUpgradeIssue(List<Issue> existingUpgradeIssues, Upgrade upgrade) {
String toMatch = "Upgrade to " + upgrade.getLibrary().getName();
String toMatch = "Upgrade to " + upgrade.toRelease().getName();
for (Issue existingUpgradeIssue : existingUpgradeIssues) {
String title = existingUpgradeIssue.getTitle();
int lastSpaceIndex = title.lastIndexOf(' ');
Expand Down Expand Up @@ -285,10 +285,21 @@ protected boolean eligible(Library library) {
return libraryPredicate.test(library.getName());
}

protected abstract String issueTitle(Upgrade upgrade);

protected abstract String commitMessage(Upgrade upgrade, int issueNumber);

protected abstract String issueBody(Upgrade upgrade, Issue existingUpgrade);
protected String issueTitle(Upgrade upgrade) {
return "Upgrade to " + upgrade.toRelease().getNameAndVersion();
}

protected String issueBody(Upgrade upgrade, Issue existingUpgrade) {
String description = upgrade.toRelease().getNameAndVersion();
String releaseNotesLink = upgrade.toRelease().getLinkUrl("releaseNotes");
String body = (releaseNotesLink != null) ? "Upgrade to [%s](%s).".formatted(description, releaseNotesLink)
: "Upgrade to %s.".formatted(description);
if (existingUpgrade != null) {
body += "\n\nSupersedes #" + existingUpgrade.getNumber();
}
return body;
}

}
Loading

0 comments on commit aa374bf

Please sign in to comment.