Skip to content

Commit 532285b

Browse files
committed
Add support for upgrading specific libraries with Bomr
Closes gh-31909
1 parent 7d860df commit 532285b

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/InteractiveUpgradeResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ public final class InteractiveUpgradeResolver implements UpgradeResolver {
6565
}
6666

6767
@Override
68-
public List<Upgrade> resolveUpgrades(Collection<Library> libraries) {
68+
public List<Upgrade> resolveUpgrades(Collection<Library> librariesToUpgrade, Collection<Library> libraries) {
6969
Map<String, Library> librariesByName = new HashMap<>();
7070
for (Library library : libraries) {
7171
librariesByName.put(library.getName(), library);
7272
}
73-
return libraries.stream().filter((library) -> !library.getName().equals("Spring Boot"))
73+
return librariesToUpgrade.stream().filter((library) -> !library.getName().equals("Spring Boot"))
7474
.map((library) -> resolveUpgrade(library, librariesByName)).filter(Objects::nonNull)
7575
.collect(Collectors.toList());
7676
}

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeBom.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import java.util.Optional;
2929
import java.util.Properties;
3030
import java.util.Set;
31+
import java.util.function.Predicate;
32+
import java.util.regex.Pattern;
33+
import java.util.stream.Collectors;
3134

3235
import javax.inject.Inject;
3336

@@ -42,6 +45,7 @@
4245
import org.gradle.api.tasks.options.Option;
4346

4447
import org.springframework.boot.build.bom.BomExtension;
48+
import org.springframework.boot.build.bom.Library;
4549
import org.springframework.boot.build.bom.bomr.github.GitHub;
4650
import org.springframework.boot.build.bom.bomr.github.GitHubRepository;
4751
import org.springframework.boot.build.bom.bomr.github.Issue;
@@ -61,6 +65,8 @@ public class UpgradeBom extends DefaultTask {
6165

6266
private String milestone;
6367

68+
private String libraries;
69+
6470
@Inject
6571
public UpgradeBom(BomExtension bom) {
6672
this.bom = bom;
@@ -83,6 +89,17 @@ public String getMilestone() {
8389
return this.milestone;
8490
}
8591

92+
@Option(option = "libraries", description = "Regular expression that identifies the libraries to upgrade")
93+
public void setLibraries(String libraries) {
94+
this.libraries = libraries;
95+
}
96+
97+
@Input
98+
@org.gradle.api.tasks.Optional
99+
public String getLibraries() {
100+
return this.libraries;
101+
}
102+
86103
@TaskAction
87104
@SuppressWarnings("deprecation")
88105
void upgradeDependencies() {
@@ -100,7 +117,7 @@ void upgradeDependencies() {
100117
List<Issue> existingUpgradeIssues = repository.findIssues(issueLabels, milestone);
101118
List<Upgrade> upgrades = new InteractiveUpgradeResolver(new MavenMetadataVersionResolver(this.repositoryUrls),
102119
this.bom.getUpgrade().getPolicy(), getServices().get(UserInputHandler.class))
103-
.resolveUpgrades(this.bom.getLibraries());
120+
.resolveUpgrades(matchingLibraries(this.libraries), this.bom.getLibraries());
104121
Path buildFile = getProject().getBuildFile().toPath();
105122
Path gradleProperties = new File(getProject().getRootProject().getProjectDir(), "gradle.properties").toPath();
106123
UpgradeApplicator upgradeApplicator = new UpgradeApplicator(buildFile, gradleProperties);
@@ -140,6 +157,19 @@ void upgradeDependencies() {
140157
}
141158
}
142159

160+
private List<Library> matchingLibraries(String pattern) {
161+
if (pattern == null) {
162+
return this.bom.getLibraries();
163+
}
164+
Predicate<String> libraryPredicate = Pattern.compile(pattern).asPredicate();
165+
List<Library> matchingLibraries = this.bom.getLibraries().stream()
166+
.filter((library) -> libraryPredicate.test(library.getName())).collect(Collectors.toList());
167+
if (matchingLibraries.isEmpty()) {
168+
throw new InvalidUserDataException("No libraries matched '" + pattern + "'");
169+
}
170+
return matchingLibraries;
171+
}
172+
143173
private Issue findExistingUpgradeIssue(List<Issue> existingUpgradeIssues, Upgrade upgrade) {
144174
String toMatch = "Upgrade to " + upgrade.getLibrary().getName();
145175
for (Issue existingUpgradeIssue : existingUpgradeIssues) {

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeResolver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,9 +30,10 @@ interface UpgradeResolver {
3030

3131
/**
3232
* Resolves the upgrades to be applied to the given {@code libraries}.
33-
* @param libraries the libraries
33+
* @param librariesToUpgrade the libraries to upgrade
34+
* @param libraries all libraries
3435
* @return the upgrades
3536
*/
36-
List<Upgrade> resolveUpgrades(Collection<Library> libraries);
37+
List<Upgrade> resolveUpgrades(Collection<Library> librariesToUpgrade, Collection<Library> libraries);
3738

3839
}

0 commit comments

Comments
 (0)