From 532285b47e9adb9edf495832967a2f5cba9b6b0c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 28 Jul 2022 16:29:33 +0100 Subject: [PATCH] Add support for upgrading specific libraries with Bomr Closes gh-31909 --- .../bom/bomr/InteractiveUpgradeResolver.java | 4 +-- .../boot/build/bom/bomr/UpgradeBom.java | 32 ++++++++++++++++++- .../boot/build/bom/bomr/UpgradeResolver.java | 7 ++-- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/InteractiveUpgradeResolver.java b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/InteractiveUpgradeResolver.java index c9e95a3f05..064e0905f3 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/InteractiveUpgradeResolver.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/InteractiveUpgradeResolver.java @@ -65,12 +65,12 @@ public final class InteractiveUpgradeResolver implements UpgradeResolver { } @Override - public List resolveUpgrades(Collection libraries) { + public List resolveUpgrades(Collection librariesToUpgrade, Collection libraries) { Map librariesByName = new HashMap<>(); for (Library library : libraries) { librariesByName.put(library.getName(), library); } - return libraries.stream().filter((library) -> !library.getName().equals("Spring Boot")) + return librariesToUpgrade.stream().filter((library) -> !library.getName().equals("Spring Boot")) .map((library) -> resolveUpgrade(library, librariesByName)).filter(Objects::nonNull) .collect(Collectors.toList()); } diff --git a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeBom.java b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeBom.java index 4c56ae74a7..8a38fce9be 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeBom.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeBom.java @@ -28,6 +28,9 @@ import java.util.List; import java.util.Optional; import java.util.Properties; import java.util.Set; +import java.util.function.Predicate; +import java.util.regex.Pattern; +import java.util.stream.Collectors; import javax.inject.Inject; @@ -42,6 +45,7 @@ import org.gradle.api.tasks.TaskExecutionException; import org.gradle.api.tasks.options.Option; import org.springframework.boot.build.bom.BomExtension; +import org.springframework.boot.build.bom.Library; import org.springframework.boot.build.bom.bomr.github.GitHub; import org.springframework.boot.build.bom.bomr.github.GitHubRepository; import org.springframework.boot.build.bom.bomr.github.Issue; @@ -61,6 +65,8 @@ public class UpgradeBom extends DefaultTask { private String milestone; + private String libraries; + @Inject public UpgradeBom(BomExtension bom) { this.bom = bom; @@ -83,6 +89,17 @@ public class UpgradeBom extends DefaultTask { return this.milestone; } + @Option(option = "libraries", description = "Regular expression that identifies the libraries to upgrade") + public void setLibraries(String libraries) { + this.libraries = libraries; + } + + @Input + @org.gradle.api.tasks.Optional + public String getLibraries() { + return this.libraries; + } + @TaskAction @SuppressWarnings("deprecation") void upgradeDependencies() { @@ -100,7 +117,7 @@ public class UpgradeBom extends DefaultTask { List existingUpgradeIssues = repository.findIssues(issueLabels, milestone); List upgrades = new InteractiveUpgradeResolver(new MavenMetadataVersionResolver(this.repositoryUrls), this.bom.getUpgrade().getPolicy(), getServices().get(UserInputHandler.class)) - .resolveUpgrades(this.bom.getLibraries()); + .resolveUpgrades(matchingLibraries(this.libraries), this.bom.getLibraries()); Path buildFile = getProject().getBuildFile().toPath(); Path gradleProperties = new File(getProject().getRootProject().getProjectDir(), "gradle.properties").toPath(); UpgradeApplicator upgradeApplicator = new UpgradeApplicator(buildFile, gradleProperties); @@ -140,6 +157,19 @@ public class UpgradeBom extends DefaultTask { } } + private List matchingLibraries(String pattern) { + if (pattern == null) { + return this.bom.getLibraries(); + } + Predicate libraryPredicate = Pattern.compile(pattern).asPredicate(); + List matchingLibraries = this.bom.getLibraries().stream() + .filter((library) -> libraryPredicate.test(library.getName())).collect(Collectors.toList()); + if (matchingLibraries.isEmpty()) { + throw new InvalidUserDataException("No libraries matched '" + pattern + "'"); + } + return matchingLibraries; + } + private Issue findExistingUpgradeIssue(List existingUpgradeIssues, Upgrade upgrade) { String toMatch = "Upgrade to " + upgrade.getLibrary().getName(); for (Issue existingUpgradeIssue : existingUpgradeIssues) { diff --git a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeResolver.java b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeResolver.java index ed1805eca0..ae26ded8d6 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeResolver.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 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. @@ -30,9 +30,10 @@ interface UpgradeResolver { /** * Resolves the upgrades to be applied to the given {@code libraries}. - * @param libraries the libraries + * @param librariesToUpgrade the libraries to upgrade + * @param libraries all libraries * @return the upgrades */ - List resolveUpgrades(Collection libraries); + List resolveUpgrades(Collection librariesToUpgrade, Collection libraries); }