Skip to content

Commit 009b943

Browse files
committed
Check if PR/branch exists
1 parent 9a397c6 commit 009b943

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

boot-upgrade-iterative/src/main/java/com/example/bootupgrade/IterativeBoot3UpgradeExample.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.openrewrite.SourceFile;
2929
import org.openrewrite.shaded.jgit.api.CreateBranchCommand;
3030
import org.openrewrite.shaded.jgit.api.Git;
31+
import org.openrewrite.shaded.jgit.api.ListBranchCommand;
3132
import org.openrewrite.shaded.jgit.api.ResetCommand;
3233
import org.openrewrite.shaded.jgit.api.errors.GitAPIException;
3334
import org.openrewrite.shaded.jgit.lib.Ref;
@@ -160,10 +161,11 @@ void loop() throws IOException, GitAPIException {
160161

161162
if(versionToRecipeMap.containsKey(currentBootVersion)) {
162163
String newBootVersion = calculateNextBootVersion(currentBootVersion);
164+
log.info("Found migration path to Spring Boot %s".formatted(newBootVersion));
163165
String branchName = calculateBranchName(newBootVersion);
164166
Git git = Git.open(baseDir.resolve(".git").toFile());
165167
if(branchExists(git, branchName)) {
166-
log.info("Found open branch: " + branchName);
168+
log.info("There might be an open PR? Otherwise please delete this remote branch: " + branchName);
167169
return;
168170
}
169171
// find recipe name
@@ -195,7 +197,11 @@ private boolean branchExists(Git git, String branchName) {
195197
try {
196198
git.fetch().call();
197199
// FIXME: This works only in this example. origin could be different
198-
return git.branchList().call().stream().anyMatch(b -> "remotes/origin/%s".formatted(branchName).equals(b.getName()));
200+
return git.branchList()
201+
.setListMode(ListBranchCommand.ListMode.REMOTE)
202+
.call()
203+
.stream()
204+
.anyMatch(b -> "refs/remotes/origin/%s".formatted(branchName).equals(b.getName()));
199205
} catch (GitAPIException e) {
200206
throw new RuntimeException(e);
201207
}
@@ -314,6 +320,7 @@ private GHPullRequest createPullRequest(String ghToken, String name, String head
314320

315321
private void applyRecipe(ProjectResourceSet projectResourceSet, List<Recipe> recipes) {
316322
// FIXME: If recipes is a list they need to be provided through Recipe.getRecipeList() to not require a new parse
323+
log.info("Applying recipes %s".formatted(recipes.stream().map(Recipe::getDisplayName).toList()));
317324
if(recipes.size() > 1) throw new UnsupportedOperationException("Can only handle single recipes.");
318325
recipes.forEach(r -> this.applyRecipe(projectResourceSet, r));
319326
}

0 commit comments

Comments
 (0)