diff --git a/bots/notify/src/main/java/org/openjdk/skara/bots/notify/prbranch/PullRequestBranchNotifier.java b/bots/notify/src/main/java/org/openjdk/skara/bots/notify/prbranch/PullRequestBranchNotifier.java index aa2b28a2e..975101477 100644 --- a/bots/notify/src/main/java/org/openjdk/skara/bots/notify/prbranch/PullRequestBranchNotifier.java +++ b/bots/notify/src/main/java/org/openjdk/skara/bots/notify/prbranch/PullRequestBranchNotifier.java @@ -29,9 +29,11 @@ import java.io.*; import java.nio.file.Path; +import java.util.logging.Logger; public class PullRequestBranchNotifier implements Notifier, PullRequestListener { private final Path seedFolder; + private final Logger log = Logger.getLogger("org.openjdk.skara.bots.notify"); public PullRequestBranchNotifier(Path seedFolder) { this.seedFolder = seedFolder; @@ -53,6 +55,14 @@ private void pushBranch(PullRequest pr) { } private void deleteBranch(PullRequest pr) { + var branchExists = pr.repository().branches().stream() + .map(HostedBranch::name) + .anyMatch(name -> name.equals(PreIntegrations.preIntegrateBranch(pr))); + if (!branchExists) { + log.info("Pull request pre-integration branch " + PreIntegrations.preIntegrateBranch(pr) + " doesn't exist on remote - ignoring"); + return; + } + var hostedRepositoryPool = new HostedRepositoryPool(seedFolder); try { var seedRepo = hostedRepositoryPool.seedRepository(pr.repository(), false); diff --git a/bots/notify/src/test/java/org/openjdk/skara/bots/notify/prbranch/PullRequestBranchNotifierTests.java b/bots/notify/src/test/java/org/openjdk/skara/bots/notify/prbranch/PullRequestBranchNotifierTests.java index d76f2c854..2ceeb6f9c 100644 --- a/bots/notify/src/test/java/org/openjdk/skara/bots/notify/prbranch/PullRequestBranchNotifierTests.java +++ b/bots/notify/src/test/java/org/openjdk/skara/bots/notify/prbranch/PullRequestBranchNotifierTests.java @@ -24,9 +24,10 @@ import org.junit.jupiter.api.*; import org.openjdk.skara.forge.*; -import org.openjdk.skara.issuetracker.*; -import org.openjdk.skara.json.*; +import org.openjdk.skara.issuetracker.Issue; +import org.openjdk.skara.json.JSON; import org.openjdk.skara.test.*; +import org.openjdk.skara.vcs.Branch; import java.io.IOException; import java.nio.file.Path; @@ -137,4 +138,42 @@ void updated(TestInfo testInfo) throws IOException { assertEquals(updatedHash, hash); } } + + @Test + void branchMissing(TestInfo testInfo) throws IOException { + try (var credentials = new HostCredentials(testInfo); + var tempFolder = new TemporaryDirectory()) { + + var repo = credentials.getHostedRepository(); + var repoFolder = tempFolder.path().resolve("repo"); + var localRepo = CheckableRepository.init(repoFolder, repo.repositoryType()); + credentials.commitLock(localRepo); + localRepo.pushAll(repo.url()); + + var storageFolder = tempFolder.path().resolve("storage"); + var notifyBot = testBotBuilder(repo, storageFolder).create("notify", JSON.object()); + + // Initialize history + TestBotRunner.runPeriodicItems(notifyBot); + + // Create a PR + var editHash = CheckableRepository.appendAndCommit(localRepo, "Another line"); + localRepo.push(editHash, repo.url(), "source", true); + var pr = credentials.createPullRequest(repo, "master", "source", "This is a PR", false); + pr.addLabel("rfr"); + TestBotRunner.runPeriodicItems(notifyBot); + + // The target repo should now contain the new branch + var hash = localRepo.fetch(repo.url(), PreIntegrations.preIntegrateBranch(pr)); + assertEquals(editHash, hash); + try { + localRepo.prune(new Branch(PreIntegrations.preIntegrateBranch(pr)), repo.url().toString()); + } catch (IOException ignored) { + } + + // Now close it - no exception should be raised + pr.setState(Issue.State.CLOSED); + TestBotRunner.runPeriodicItems(notifyBot); + } + } }