diff --git a/bots/notify/src/main/java/org/openjdk/skara/bots/notify/JNotifyBot.java b/bots/notify/src/main/java/org/openjdk/skara/bots/notify/JNotifyBot.java index c588650cd..8c84d3bf4 100644 --- a/bots/notify/src/main/java/org/openjdk/skara/bots/notify/JNotifyBot.java +++ b/bots/notify/src/main/java/org/openjdk/skara/bots/notify/JNotifyBot.java @@ -39,13 +39,15 @@ class JNotifyBot implements Bot, WorkItem { private final Logger log = Logger.getLogger("org.openjdk.skara.bots");; private final HostedRepository repository; + private final Path storagePath; private final Branch branch; private final StorageBuilder tagStorageBuilder; private final StorageBuilder branchStorageBuilder; private final List updaters; - JNotifyBot(HostedRepository repository, String branch, StorageBuilder tagStorageBuilder, StorageBuilder branchStorageBuilder, List updaters) { + JNotifyBot(HostedRepository repository, Path storagePath, String branch, StorageBuilder tagStorageBuilder, StorageBuilder branchStorageBuilder, List updaters) { this.repository = repository; + this.storagePath = storagePath; this.branch = new Branch(branch); this.tagStorageBuilder = tagStorageBuilder; this.branchStorageBuilder = branchStorageBuilder; @@ -65,10 +67,10 @@ public boolean concurrentWith(WorkItem other) { } private void handleBranch(Repository localRepo, UpdateHistory history, Branch branch) throws IOException { - var curHead = localRepo.resolve("HEAD").orElseThrow(IOException::new); + var curHead = localRepo.resolve("FETCH_HEAD").orElseThrow(IOException::new); var lastRef = history.branchHash(branch); - if (!lastRef.isPresent()) { + if (lastRef.isEmpty()) { log.warning("No previous history found for branch '" + branch + "' - resetting mark"); history.setBranchHash(branch, curHead); return; @@ -135,11 +137,11 @@ private void handleTags(Repository localRepo, UpdateHistory history) throws IOEx @Override public void run(Path scratchPath) { var sanitizedUrl = URLEncoder.encode(repository.getUrl().toString(), StandardCharsets.UTF_8); - var path = scratchPath.resolve("notify").resolve(sanitizedUrl); + var path = storagePath.resolve(sanitizedUrl); var historyPath = scratchPath.resolve("notify").resolve("history"); try { - var localRepo = Repository.materialize(path, repository.getUrl(), branch.name()); + var localRepo = Repository.materialize(path, repository.getUrl(), branch.name(), false); var history = UpdateHistory.create(tagStorageBuilder, historyPath.resolve("tags"), branchStorageBuilder, historyPath.resolve("branches")); handleBranch(localRepo, history, branch); handleTags(localRepo, history); diff --git a/bots/notify/src/main/java/org/openjdk/skara/bots/notify/JNotifyBotFactory.java b/bots/notify/src/main/java/org/openjdk/skara/bots/notify/JNotifyBotFactory.java index e22fce610..d80e1e50f 100644 --- a/bots/notify/src/main/java/org/openjdk/skara/bots/notify/JNotifyBotFactory.java +++ b/bots/notify/src/main/java/org/openjdk/skara/bots/notify/JNotifyBotFactory.java @@ -77,7 +77,7 @@ public List create(BotConfiguration configuration) { .remoteRepository(databaseRepo, databaseRef, databaseName, databaseEmail, "Added tag for " + repoName); var branchStorageBuilder = new StorageBuilder(repoName + ".branches.txt") .remoteRepository(databaseRepo, databaseRef, databaseName, databaseEmail, "Added branch hash for " + repoName); - var bot = new JNotifyBot(configuration.repository(repoName), branch, tagStorageBuilder, branchStorageBuilder, updaters); + var bot = new JNotifyBot(configuration.repository(repoName), configuration.storageFolder(), branch, tagStorageBuilder, branchStorageBuilder, updaters); ret.add(bot); } diff --git a/bots/notify/src/test/java/org/openjdk/skara/bots/notify/UpdaterTests.java b/bots/notify/src/test/java/org/openjdk/skara/bots/notify/UpdaterTests.java index 974aa6782..24e8997cd 100644 --- a/bots/notify/src/test/java/org/openjdk/skara/bots/notify/UpdaterTests.java +++ b/bots/notify/src/test/java/org/openjdk/skara/bots/notify/UpdaterTests.java @@ -61,26 +61,29 @@ private StorageBuilder createBranchStorage(HostedRepository repo @Test void testJsonUpdaterBranch(TestInfo testInfo) throws IOException { try (var credentials = new HostCredentials(testInfo); - var tempFolder = new TemporaryDirectory(); - var jsonFolder = new TemporaryDirectory()) { + var tempFolder = new TemporaryDirectory()) { var repo = credentials.getHostedRepository(); - var localRepo = CheckableRepository.init(tempFolder.path(), repo.getRepositoryType()); + var localRepoFolder = tempFolder.path().resolve("repo"); + var localRepo = CheckableRepository.init(localRepoFolder, repo.getRepositoryType()); credentials.commitLock(localRepo); localRepo.pushAll(repo.getUrl()); var tagStorage = createTagStorage(repo); var branchStorage = createBranchStorage(repo); + var jsonFolder = tempFolder.path().resolve("json"); + Files.createDirectory(jsonFolder); + var storageFolder = tempFolder.path().resolve("storage"); - var updater = new JsonUpdater(jsonFolder.path(), "12", "team"); - var notifyBot = new JNotifyBot(repo, "master", tagStorage, branchStorage, List.of(updater)); + var updater = new JsonUpdater(jsonFolder, "12", "team"); + var notifyBot = new JNotifyBot(repo, storageFolder, "master", tagStorage, branchStorage, List.of(updater)); TestBotRunner.runPeriodicItems(notifyBot); - assertEquals(List.of(), findJsonFiles(jsonFolder.path(), "")); + assertEquals(List.of(), findJsonFiles(jsonFolder, "")); var editHash = CheckableRepository.appendAndCommit(localRepo, "One more line", "12345678: Fixes"); localRepo.push(editHash, repo.getUrl(), "master"); TestBotRunner.runPeriodicItems(notifyBot); - var jsonFiles = findJsonFiles(jsonFolder.path(), ""); + var jsonFiles = findJsonFiles(jsonFolder, ""); assertEquals(1, jsonFiles.size()); var jsonData = Files.readString(jsonFiles.get(0), StandardCharsets.UTF_8); var json = JSON.parse(jsonData); @@ -95,10 +98,10 @@ void testJsonUpdaterBranch(TestInfo testInfo) throws IOException { @Test void testJsonUpdaterTag(TestInfo testInfo) throws IOException { try (var credentials = new HostCredentials(testInfo); - var tempFolder = new TemporaryDirectory(); - var jsonFolder = new TemporaryDirectory()) { + var tempFolder = new TemporaryDirectory()) { var repo = credentials.getHostedRepository(); - var localRepo = CheckableRepository.init(tempFolder.path(), repo.getRepositoryType()); + var localRepoFolder = tempFolder.path().resolve("repo"); + var localRepo = CheckableRepository.init(localRepoFolder, repo.getRepositoryType()); credentials.commitLock(localRepo); var masterHash = localRepo.resolve("master").orElseThrow(); localRepo.tag(masterHash, "jdk-12+1", "Added tag 1", "Duke", "duke@openjdk.java.net"); @@ -106,12 +109,15 @@ void testJsonUpdaterTag(TestInfo testInfo) throws IOException { var tagStorage = createTagStorage(repo); var branchStorage = createBranchStorage(repo); + var jsonFolder = tempFolder.path().resolve("json"); + Files.createDirectory(jsonFolder); + var storageFolder =tempFolder.path().resolve("storage"); - var updater = new JsonUpdater(jsonFolder.path(), "12", "team"); - var notifyBot = new JNotifyBot(repo, "master", tagStorage, branchStorage, List.of(updater)); + var updater = new JsonUpdater(jsonFolder, "12", "team"); + var notifyBot = new JNotifyBot(repo, storageFolder, "master", tagStorage, branchStorage, List.of(updater)); TestBotRunner.runPeriodicItems(notifyBot); - assertEquals(List.of(), findJsonFiles(jsonFolder.path(), "")); + assertEquals(List.of(), findJsonFiles(jsonFolder, "")); var editHash = CheckableRepository.appendAndCommit(localRepo, "Another line", "23456789: More fixes"); localRepo.fetch(repo.getUrl(), "history:history"); @@ -119,7 +125,7 @@ void testJsonUpdaterTag(TestInfo testInfo) throws IOException { localRepo.pushAll(repo.getUrl()); TestBotRunner.runPeriodicItems(notifyBot); - var jsonFiles = findJsonFiles(jsonFolder.path(), ""); + var jsonFiles = findJsonFiles(jsonFolder, ""); assertEquals(2, jsonFiles.size()); for (var file : jsonFiles) { @@ -154,11 +160,12 @@ void testMailingList(TestInfo testInfo) throws IOException { var tagStorage = createTagStorage(repo); var branchStorage = createBranchStorage(repo); + var storageFolder = tempFolder.path().resolve("storage"); var sender = EmailAddress.from("duke", "duke@duke.duke"); var recipient = EmailAddress.from("list", "list@list.list"); var updater = new MailingListUpdater(smtpServer.address(), recipient, sender); - var notifyBot = new JNotifyBot(repo, "master", tagStorage, branchStorage, List.of(updater)); + var notifyBot = new JNotifyBot(repo, storageFolder, "master", tagStorage, branchStorage, List.of(updater)); // No mail should be sent on the first run as there is no history TestBotRunner.runPeriodicItems(notifyBot); @@ -191,11 +198,12 @@ void testMailingListMultiple(TestInfo testInfo) throws IOException { var tagStorage = createTagStorage(repo); var branchStorage = createBranchStorage(repo); + var storageFolder = tempFolder.path().resolve("storage"); var sender = EmailAddress.from("duke", "duke@duke.duke"); var recipient = EmailAddress.from("list", "list@list.list"); var updater = new MailingListUpdater(smtpServer.address(), recipient, sender); - var notifyBot = new JNotifyBot(repo, "master", tagStorage, branchStorage, List.of(updater)); + var notifyBot = new JNotifyBot(repo, storageFolder, "master", tagStorage, branchStorage, List.of(updater)); // No mail should be sent on the first run as there is no history TestBotRunner.runPeriodicItems(notifyBot); @@ -232,11 +240,12 @@ void testMailingListSponsored(TestInfo testInfo) throws IOException { var tagStorage = createTagStorage(repo); var branchStorage = createBranchStorage(repo); + var storageFolder = tempFolder.path().resolve("storage"); var sender = EmailAddress.from("duke", "duke@duke.duke"); var recipient = EmailAddress.from("list", "list@list.list"); var updater = new MailingListUpdater(smtpServer.address(), recipient, sender); - var notifyBot = new JNotifyBot(repo, "master", tagStorage, branchStorage, List.of(updater)); + var notifyBot = new JNotifyBot(repo, storageFolder, "master", tagStorage, branchStorage, List.of(updater)); // No mail should be sent on the first run as there is no history TestBotRunner.runPeriodicItems(notifyBot); diff --git a/vcs/src/main/java/org/openjdk/skara/vcs/Repository.java b/vcs/src/main/java/org/openjdk/skara/vcs/Repository.java index 2ed6030ab..cc22ac940 100644 --- a/vcs/src/main/java/org/openjdk/skara/vcs/Repository.java +++ b/vcs/src/main/java/org/openjdk/skara/vcs/Repository.java @@ -143,6 +143,10 @@ static boolean exists(Path p) throws IOException { } static Repository materialize(Path p, URI remote, String ref) throws IOException { + return materialize(p, remote, ref, true); + } + + static Repository materialize(Path p, URI remote, String ref, boolean checkout) throws IOException { var localRepo = remote.getPath().endsWith(".git") ? Repository.init(p, VCS.GIT) : Repository.init(p, VCS.HG); if (!localRepo.exists()) { @@ -159,12 +163,14 @@ static Repository materialize(Path p, URI remote, String ref) throws IOException var baseHash = localRepo.fetch(remote, ref); - try { - localRepo.checkout(baseHash, true); - } catch (IOException e) { - localRepo.reinitialize(); - baseHash = localRepo.fetch(remote, ref); - localRepo.checkout(baseHash, true); + if (checkout) { + try { + localRepo.checkout(baseHash, true); + } catch (IOException e) { + localRepo.reinitialize(); + baseHash = localRepo.fetch(remote, ref); + localRepo.checkout(baseHash, true); + } } return localRepo;