From bdbaddb507a49a7b0ff314aa49fc83c897fa0608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Kr=C3=BCger?= Date: Thu, 23 Nov 2023 10:15:06 +0100 Subject: [PATCH] Fix --- .../BootUpgradeIterativeApplication.java | 15 ++++ .../IterativeBoot3UpgradeExample.java | 88 ++++++++++--------- 2 files changed, 62 insertions(+), 41 deletions(-) diff --git a/boot-upgrade-iterative/src/main/java/com/example/bootupgrade/BootUpgradeIterativeApplication.java b/boot-upgrade-iterative/src/main/java/com/example/bootupgrade/BootUpgradeIterativeApplication.java index 620d8daf5..f8b8de39c 100644 --- a/boot-upgrade-iterative/src/main/java/com/example/bootupgrade/BootUpgradeIterativeApplication.java +++ b/boot-upgrade-iterative/src/main/java/com/example/bootupgrade/BootUpgradeIterativeApplication.java @@ -1,3 +1,18 @@ +/* + * Copyright 2021 - 2023 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.example.bootupgrade; import org.springframework.boot.SpringApplication; diff --git a/boot-upgrade-iterative/src/main/java/com/example/bootupgrade/IterativeBoot3UpgradeExample.java b/boot-upgrade-iterative/src/main/java/com/example/bootupgrade/IterativeBoot3UpgradeExample.java index 81e351ad6..08d7eec18 100644 --- a/boot-upgrade-iterative/src/main/java/com/example/bootupgrade/IterativeBoot3UpgradeExample.java +++ b/boot-upgrade-iterative/src/main/java/com/example/bootupgrade/IterativeBoot3UpgradeExample.java @@ -78,6 +78,7 @@ public static void main(String[] args) { } private static Map> versionToRecipeMap = new HashMap<>(); + static { versionToRecipeMap = Map.of( "2.3.5.RELEASE", @@ -150,7 +151,7 @@ public void run(ApplicationArguments args) throws Exception { @Scheduled(fixedDelay = 10_000) void loop() throws IOException, GitAPIException { log.info("Scheduling..."); - if(repoUrl == null || ghToken == null) { + if (repoUrl == null || ghToken == null) { return; } // clone @@ -159,35 +160,40 @@ void loop() throws IOException, GitAPIException { String pomXml = Files.readString(baseDir.resolve("pom.xml")); String currentBootVersion = extractSpringBootVersion(pomXml); - if(versionToRecipeMap.containsKey(currentBootVersion)) { - String newBootVersion = calculateNextBootVersion(currentBootVersion); - log.info("Found migration path to Spring Boot %s".formatted(newBootVersion)); - String branchName = calculateBranchName(newBootVersion); - Git git = Git.open(baseDir.resolve(".git").toFile()); - if(branchExists(git, branchName)) { - log.info("There might be an open PR? Otherwise please delete this remote branch: " + branchName); - return; + if (versionToRecipeMap.containsKey(currentBootVersion)) { + Optional newBootVersionOpt = calculateNextBootVersion(currentBootVersion); + if (newBootVersionOpt.isPresent()) { + String newBootVersion = newBootVersionOpt.get(); + log.info("Found migration path to Spring Boot %s".formatted(newBootVersion)); + String branchName = calculateBranchName(newBootVersion); + Git git = Git.open(baseDir.resolve(".git").toFile()); + if (branchExists(git, branchName)) { + log.info("There might be an open PR? Otherwise please delete this remote branch: " + branchName); + return; + } + // find recipe name + List recipeNames = versionToRecipeMap.get(currentBootVersion); + // parse + ProjectResourceSet projectResourceSet = parseProject(baseDir); + // discover + List recipes = discoverRecipes(recipeNames); + // apply + applyRecipe(projectResourceSet, recipes); + // create branch + Ref branchRef = createBranch(git, branchName); + checkout(git, branchName); + // commit + String message = "Upgrade Spring Boot from %s to %s".formatted(currentBootVersion, newBootVersion); + RevCommit commit = commit(git, message); + log.info("commit: " + commit); + push(git, branchRef, ghToken); + + // Create PR + String mainBranch = "main"; + GHPullRequest pr = createPullRequest(ghToken, message, branchName, mainBranch, message); + } else { + log.info("No migration path found."); } - // find recipe name - List recipeNames = versionToRecipeMap.get(currentBootVersion); - // parse - ProjectResourceSet projectResourceSet = parseProject(baseDir); - // discover - List recipes = discoverRecipes(recipeNames); - // apply - applyRecipe(projectResourceSet, recipes); - // create branch - Ref branchRef = createBranch(git, branchName); - checkout(git, branchName); - // commit - String message = "Upgrade Spring Boot from %s to %s".formatted(currentBootVersion, newBootVersion); - RevCommit commit = commit(git, message); - System.out.println("commit: " + commit); - push(git, branchRef, ghToken); - - // Create PR - String mainBranch = "main"; - GHPullRequest pr = createPullRequest(ghToken, message, branchName, mainBranch, message); } else { log.info("No migration found for Spring Boot version: %s".formatted(currentBootVersion)); } @@ -211,13 +217,13 @@ private String calculateBranchName(String newBootVersion) { return "app-analyzer/boot-upgrade-%s".formatted(newBootVersion.replace(".", "")); } - private String calculateNextBootVersion(String currentBootVersion) { + private Optional calculateNextBootVersion(String currentBootVersion) { List sortedVersions = versionToRecipeMap.keySet().stream().sorted(String::compareTo).toList(); int i = sortedVersions.indexOf(currentBootVersion); - if(i>0) { - return sortedVersions.get(i+1); + if (i < sortedVersions.size() -1) { + return Optional.of(sortedVersions.get(i + 1)); } - throw new IllegalArgumentException("No higher version found than %s.".formatted(currentBootVersion)); + return Optional.empty(); } private String extractSpringBootVersion(String pomXml) { @@ -235,7 +241,7 @@ private static void checkout(Git git, String branchName) throws GitAPIException } private String getGitHubToken(ApplicationArguments args) { - if(args.getNonOptionArgs().size() < 2) { + if (args.getNonOptionArgs().size() < 2) { throw new IllegalArgumentException("The GitHub token must be provided as second parameter."); } return args.getNonOptionArgs().get(1); @@ -252,10 +258,10 @@ private static RevCommit commit(Git git, String message) throws GitAPIException private static Ref createBranch(Git git, String branchName) throws GitAPIException { Ref branchRef = git.branchCreate() - .setName(branchName) - .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM) - .setForce(true) - .call(); + .setName(branchName) + .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM) + .setForce(true) + .call(); return branchRef; } @@ -272,7 +278,7 @@ private ProjectResourceSet parseProject(Path baseDir) { @NotNull private Path cloneFreshRepo(String repoUrl) throws IOException { Path targetDir = Path.of(System.getProperty("java.io.tmpdir")).resolve(GH_REPO_NAME); //Files.createTempDirectory(GH_REPO_NAME).toAbsolutePath(); - if(targetDir.toFile().exists()) { + if (targetDir.toFile().exists()) { FileSystemUtils.deleteRecursively(targetDir); } String commitHash = null;// GIT_COMMIT_HASH; // https://github.com/spring-projects/spring-petclinic/commit/a3294f2 @@ -282,7 +288,7 @@ private Path cloneFreshRepo(String repoUrl) throws IOException { } private static String getRepoUrl(ApplicationArguments args) { - if(args.getNonOptionArgs().isEmpty()) { + if (args.getNonOptionArgs().isEmpty()) { throw new IllegalArgumentException("A repository URL must be provided."); } String repoUrl = args.getNonOptionArgs().get(0); @@ -325,7 +331,7 @@ private GHPullRequest createPullRequest(String ghToken, String name, String head private void applyRecipe(ProjectResourceSet projectResourceSet, List recipes) { // FIXME: If recipes is a list they need to be provided through Recipe.getRecipeList() to not require a new parse log.info("Applying recipes %s".formatted(recipes.stream().map(Recipe::getDisplayName).toList())); - if(recipes.size() > 1) throw new UnsupportedOperationException("Can only handle single recipes."); + if (recipes.size() > 1) throw new UnsupportedOperationException("Can only handle single recipes."); recipes.forEach(r -> this.applyRecipe(projectResourceSet, r)); }