Skip to content

Commit 31549d0

Browse files
committed
Allow the notify bot to watch multiple branches
Reviewed-by: ehelin
1 parent 9ca8782 commit 31549d0

File tree

6 files changed

+108
-30
lines changed

6 files changed

+108
-30
lines changed

bots/notify/src/main/java/org/openjdk/skara/bots/notify/JNotifyBot.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ class JNotifyBot implements Bot, WorkItem {
4040
private final Logger log = Logger.getLogger("org.openjdk.skara.bots");;
4141
private final HostedRepository repository;
4242
private final Path storagePath;
43-
private final Branch branch;
43+
private final List<Branch> branches;
4444
private final StorageBuilder<Tag> tagStorageBuilder;
4545
private final StorageBuilder<ResolvedBranch> branchStorageBuilder;
4646
private final List<UpdateConsumer> updaters;
4747

48-
JNotifyBot(HostedRepository repository, Path storagePath, String branch, StorageBuilder<Tag> tagStorageBuilder, StorageBuilder<ResolvedBranch> branchStorageBuilder, List<UpdateConsumer> updaters) {
48+
JNotifyBot(HostedRepository repository, Path storagePath, List<String> branches, StorageBuilder<Tag> tagStorageBuilder, StorageBuilder<ResolvedBranch> branchStorageBuilder, List<UpdateConsumer> updaters) {
4949
this.repository = repository;
5050
this.storagePath = storagePath;
51-
this.branch = new Branch(branch);
51+
this.branches = branches.stream()
52+
.map(Branch::new)
53+
.collect(Collectors.toList());
5254
this.tagStorageBuilder = tagStorageBuilder;
5355
this.branchStorageBuilder = branchStorageBuilder;
5456
this.updaters = updaters;
@@ -66,9 +68,7 @@ public boolean concurrentWith(WorkItem other) {
6668
return false;
6769
}
6870

69-
private void handleBranch(Repository localRepo, UpdateHistory history, Branch branch) throws IOException {
70-
var curHead = localRepo.resolve("FETCH_HEAD").orElseThrow(IOException::new);
71-
71+
private void handleBranch(Repository localRepo, UpdateHistory history, Branch branch, Hash curHead) throws IOException {
7272
var lastRef = history.branchHash(branch);
7373
if (lastRef.isEmpty()) {
7474
log.warning("No previous history found for branch '" + branch + "' - resetting mark");
@@ -87,7 +87,7 @@ private void handleBranch(Repository localRepo, UpdateHistory history, Branch br
8787

8888
Collections.reverse(newCommits);
8989
for (var updater : updaters) {
90-
updater.handleCommits(repository, newCommits);
90+
updater.handleCommits(repository, newCommits, branch);
9191
}
9292
}
9393

@@ -141,10 +141,14 @@ public void run(Path scratchPath) {
141141
var historyPath = scratchPath.resolve("notify").resolve("history");
142142

143143
try {
144-
var localRepo = Repository.materialize(path, repository.getUrl(), branch.name(), false);
144+
var localRepo = Repository.materialize(path, repository.getUrl(), "master", false);
145145
var history = UpdateHistory.create(tagStorageBuilder, historyPath.resolve("tags"), branchStorageBuilder, historyPath.resolve("branches"));
146-
handleBranch(localRepo, history, branch);
147146
handleTags(localRepo, history);
147+
148+
for (var branch : branches) {
149+
var hash = localRepo.fetch(repository.getUrl(), branch.name());
150+
handleBranch(localRepo, history, branch, hash);
151+
}
148152
} catch (IOException e) {
149153
throw new UncheckedIOException(e);
150154
}

bots/notify/src/main/java/org/openjdk/skara/bots/notify/JNotifyBotFactory.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424

2525
import org.openjdk.skara.bot.*;
2626
import org.openjdk.skara.email.EmailAddress;
27+
import org.openjdk.skara.json.JSONValue;
2728
import org.openjdk.skara.storage.StorageBuilder;
28-
import org.openjdk.skara.vcs.Tag;
29+
import org.openjdk.skara.vcs.*;
2930

3031
import java.nio.file.Path;
3132
import java.util.*;
3233
import java.util.logging.Logger;
34+
import java.util.stream.Collectors;
3335

3436
public class JNotifyBotFactory implements BotFactory {
3537
private final Logger log = Logger.getLogger("org.openjdk.skara.bots");;
@@ -52,7 +54,9 @@ public List<Bot> create(BotConfiguration configuration) {
5254

5355
for (var repo : specific.get("repositories").fields()) {
5456
var repoName = repo.name();
55-
var branch = repo.value().get("branch").asString();
57+
var branches = repo.value().get("branches").stream()
58+
.map(JSONValue::asString)
59+
.collect(Collectors.toList());
5660
var build = repo.value().get("build").asString();
5761
var version = repo.value().get("version").asString();
5862

@@ -65,7 +69,7 @@ public List<Bot> create(BotConfiguration configuration) {
6569
var senderName = mailcfg.get("name").asString();
6670
var senderMail = mailcfg.get("email").asString();
6771
var sender = EmailAddress.from(senderName, senderMail);
68-
updaters.add(new MailingListUpdater(mailcfg.get("smtp").asString(), EmailAddress.parse(mailcfg.get("recipient").asString()), sender));
72+
updaters.add(new MailingListUpdater(mailcfg.get("smtp").asString(), EmailAddress.parse(mailcfg.get("recipient").asString()), sender, branches.size() > 1));
6973
}
7074

7175
if (updaters.isEmpty()) {
@@ -77,7 +81,7 @@ public List<Bot> create(BotConfiguration configuration) {
7781
.remoteRepository(databaseRepo, databaseRef, databaseName, databaseEmail, "Added tag for " + repoName);
7882
var branchStorageBuilder = new StorageBuilder<ResolvedBranch>(repoName + ".branches.txt")
7983
.remoteRepository(databaseRepo, databaseRef, databaseName, databaseEmail, "Added branch hash for " + repoName);
80-
var bot = new JNotifyBot(configuration.repository(repoName), configuration.storageFolder(), branch, tagStorageBuilder, branchStorageBuilder, updaters);
84+
var bot = new JNotifyBot(configuration.repository(repoName), configuration.storageFolder(), branches, tagStorageBuilder, branchStorageBuilder, updaters);
8185
ret.add(bot);
8286
}
8387

bots/notify/src/main/java/org/openjdk/skara/bots/notify/JsonUpdater.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import org.openjdk.skara.host.HostedRepository;
2626
import org.openjdk.skara.json.*;
27-
import org.openjdk.skara.vcs.Commit;
27+
import org.openjdk.skara.vcs.*;
2828
import org.openjdk.skara.vcs.openjdk.*;
2929

3030
import java.nio.file.Path;
@@ -76,7 +76,7 @@ private JSONObject issuesToChanges(HostedRepository repository, List<Issue> issu
7676
}
7777

7878
@Override
79-
public void handleCommits(HostedRepository repository, List<Commit> commits) {
79+
public void handleCommits(HostedRepository repository, List<Commit> commits, Branch branch) {
8080
try (var writer = new JsonUpdateWriter(path, repository.getName())) {
8181
for (var commit : commits) {
8282
var json = commitToChanges(repository, commit, defaultBuild);

bots/notify/src/main/java/org/openjdk/skara/bots/notify/MailingListUpdater.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ public class MailingListUpdater implements UpdateConsumer {
3535
private final String host;
3636
private final EmailAddress recipient;
3737
private final EmailAddress sender;
38+
private final boolean includeBranch;
3839

39-
MailingListUpdater(String host, EmailAddress recipient, EmailAddress sender) {
40+
MailingListUpdater(String host, EmailAddress recipient, EmailAddress sender, boolean includeBranch) {
4041
this.host = host;
4142
this.recipient = recipient;
4243
this.sender = sender;
44+
this.includeBranch = includeBranch;
4345
}
4446

4547
private String patchToText(Patch patch) {
@@ -78,12 +80,16 @@ private String commitToText(HostedRepository repository, Commit commit) {
7880
return writer.toString();
7981
}
8082

81-
private String commitsToSubject(HostedRepository repository, List<Commit> commits) {
83+
private String commitsToSubject(HostedRepository repository, List<Commit> commits, Branch branch) {
8284
var subject = new StringBuilder();
8385
subject.append(repository.getRepositoryType().shortName());
8486
subject.append(": ");
8587
subject.append(repository.getName());
8688
subject.append(": ");
89+
if (includeBranch) {
90+
subject.append(branch.name());
91+
subject.append(": ");
92+
}
8793
if (commits.size() > 1) {
8894
subject.append(commits.size());
8995
subject.append(" new changesets");
@@ -94,11 +100,11 @@ private String commitsToSubject(HostedRepository repository, List<Commit> commit
94100
}
95101

96102
@Override
97-
public void handleCommits(HostedRepository repository, List<Commit> commits) {
103+
public void handleCommits(HostedRepository repository, List<Commit> commits, Branch branch) {
98104
var writer = new StringWriter();
99105
var printer = new PrintWriter(writer);
100106

101-
var subject = commitsToSubject(repository, commits);
107+
var subject = commitsToSubject(repository, commits, branch);
102108

103109
for (var commit : commits) {
104110
printer.println(commitToText(repository, commit));

bots/notify/src/main/java/org/openjdk/skara/bots/notify/UpdateConsumer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
package org.openjdk.skara.bots.notify;
2424

2525
import org.openjdk.skara.host.HostedRepository;
26-
import org.openjdk.skara.vcs.Commit;
27-
import org.openjdk.skara.vcs.openjdk.*;
26+
import org.openjdk.skara.vcs.*;
27+
import org.openjdk.skara.vcs.openjdk.OpenJDKTag;
2828

2929
import java.util.List;
3030

3131
public interface UpdateConsumer {
32-
void handleCommits(HostedRepository repository, List<Commit> commits);
32+
void handleCommits(HostedRepository repository, List<Commit> commits, Branch branch);
3333
void handleTagCommits(HostedRepository repository, List<Commit> commits, OpenJDKTag tag);
3434
}

bots/notify/src/test/java/org/openjdk/skara/bots/notify/UpdaterTests.java

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void testJsonUpdaterBranch(TestInfo testInfo) throws IOException {
7575
var storageFolder = tempFolder.path().resolve("storage");
7676

7777
var updater = new JsonUpdater(jsonFolder, "12", "team");
78-
var notifyBot = new JNotifyBot(repo, storageFolder, "master", tagStorage, branchStorage, List.of(updater));
78+
var notifyBot = new JNotifyBot(repo, storageFolder, List.of("master"), tagStorage, branchStorage, List.of(updater));
7979

8080
TestBotRunner.runPeriodicItems(notifyBot);
8181
assertEquals(List.of(), findJsonFiles(jsonFolder, ""));
@@ -114,7 +114,7 @@ void testJsonUpdaterTag(TestInfo testInfo) throws IOException {
114114
var storageFolder =tempFolder.path().resolve("storage");
115115

116116
var updater = new JsonUpdater(jsonFolder, "12", "team");
117-
var notifyBot = new JNotifyBot(repo, storageFolder, "master", tagStorage, branchStorage, List.of(updater));
117+
var notifyBot = new JNotifyBot(repo, storageFolder, List.of("master"), tagStorage, branchStorage, List.of(updater));
118118

119119
TestBotRunner.runPeriodicItems(notifyBot);
120120
assertEquals(List.of(), findJsonFiles(jsonFolder, ""));
@@ -164,8 +164,8 @@ void testMailingList(TestInfo testInfo) throws IOException {
164164

165165
var sender = EmailAddress.from("duke", "duke@duke.duke");
166166
var recipient = EmailAddress.from("list", "list@list.list");
167-
var updater = new MailingListUpdater(smtpServer.address(), recipient, sender);
168-
var notifyBot = new JNotifyBot(repo, storageFolder, "master", tagStorage, branchStorage, List.of(updater));
167+
var updater = new MailingListUpdater(smtpServer.address(), recipient, sender, false);
168+
var notifyBot = new JNotifyBot(repo, storageFolder, List.of("master"), tagStorage, branchStorage, List.of(updater));
169169

170170
// No mail should be sent on the first run as there is no history
171171
TestBotRunner.runPeriodicItems(notifyBot);
@@ -177,6 +177,8 @@ void testMailingList(TestInfo testInfo) throws IOException {
177177
var email = smtpServer.receive(Duration.ofSeconds(10));
178178
assertEquals(email.sender(), sender);
179179
assertEquals(email.recipients(), List.of(recipient));
180+
assertTrue(email.subject().contains(": 23456789: More fixes"));
181+
assertFalse(email.subject().contains("master"));
180182
assertTrue(email.body().contains("Changeset: " + editHash.abbreviate()));
181183
assertTrue(email.body().contains("23456789: More fixes"));
182184
assertFalse(email.body().contains("Committer"));
@@ -202,8 +204,8 @@ void testMailingListMultiple(TestInfo testInfo) throws IOException {
202204

203205
var sender = EmailAddress.from("duke", "duke@duke.duke");
204206
var recipient = EmailAddress.from("list", "list@list.list");
205-
var updater = new MailingListUpdater(smtpServer.address(), recipient, sender);
206-
var notifyBot = new JNotifyBot(repo, storageFolder, "master", tagStorage, branchStorage, List.of(updater));
207+
var updater = new MailingListUpdater(smtpServer.address(), recipient, sender, false);
208+
var notifyBot = new JNotifyBot(repo, storageFolder, List.of("master"), tagStorage, branchStorage, List.of(updater));
207209

208210
// No mail should be sent on the first run as there is no history
209211
TestBotRunner.runPeriodicItems(notifyBot);
@@ -218,6 +220,8 @@ void testMailingListMultiple(TestInfo testInfo) throws IOException {
218220
var email = smtpServer.receive(Duration.ofSeconds(10));
219221
assertEquals(email.sender(), sender);
220222
assertEquals(email.recipients(), List.of(recipient));
223+
assertTrue(email.subject().contains(": 2 new changesets"));
224+
assertFalse(email.subject().contains("master"));
221225
assertTrue(email.body().contains("Changeset: " + editHash1.abbreviate()));
222226
assertTrue(email.body().contains("23456789: More fixes"));
223227
assertTrue(email.body().contains("Changeset: " + editHash2.abbreviate()));
@@ -244,8 +248,8 @@ void testMailingListSponsored(TestInfo testInfo) throws IOException {
244248

245249
var sender = EmailAddress.from("duke", "duke@duke.duke");
246250
var recipient = EmailAddress.from("list", "list@list.list");
247-
var updater = new MailingListUpdater(smtpServer.address(), recipient, sender);
248-
var notifyBot = new JNotifyBot(repo, storageFolder, "master", tagStorage, branchStorage, List.of(updater));
251+
var updater = new MailingListUpdater(smtpServer.address(), recipient, sender, false);
252+
var notifyBot = new JNotifyBot(repo, storageFolder, List.of("master"), tagStorage, branchStorage, List.of(updater));
249253

250254
// No mail should be sent on the first run as there is no history
251255
TestBotRunner.runPeriodicItems(notifyBot);
@@ -267,4 +271,64 @@ void testMailingListSponsored(TestInfo testInfo) throws IOException {
267271
}
268272
}
269273

274+
@Test
275+
void testMailingListMultipleBranches(TestInfo testInfo) throws IOException {
276+
try (var smtpServer = new SMTPServer();
277+
var credentials = new HostCredentials(testInfo);
278+
var tempFolder = new TemporaryDirectory()) {
279+
var repo = credentials.getHostedRepository();
280+
var repoFolder = tempFolder.path().resolve("repo");
281+
var localRepo = CheckableRepository.init(repoFolder, repo.getRepositoryType());
282+
var masterHash = localRepo.resolve("master").orElseThrow();
283+
credentials.commitLock(localRepo);
284+
var branch = localRepo.branch(masterHash, "another");
285+
localRepo.pushAll(repo.getUrl());
286+
287+
var tagStorage = createTagStorage(repo);
288+
var branchStorage = createBranchStorage(repo);
289+
var storageFolder = tempFolder.path().resolve("storage");
290+
291+
var sender = EmailAddress.from("duke", "duke@duke.duke");
292+
var recipient = EmailAddress.from("list", "list@list.list");
293+
var updater = new MailingListUpdater(smtpServer.address(), recipient, sender, true);
294+
var notifyBot = new JNotifyBot(repo, storageFolder, List.of("master", "another"), tagStorage, branchStorage, List.of(updater));
295+
296+
// No mail should be sent on the first run as there is no history
297+
TestBotRunner.runPeriodicItems(notifyBot);
298+
assertThrows(RuntimeException.class, () -> smtpServer.receive(Duration.ofMillis(1)));
299+
300+
var editHash1 = CheckableRepository.appendAndCommit(localRepo, "Another line", "23456789: More fixes");
301+
localRepo.push(editHash1, repo.getUrl(), "master");
302+
var editHash2 = CheckableRepository.appendAndCommit(localRepo, "Yet another line", "3456789A: Even more fixes");
303+
localRepo.push(editHash2, repo.getUrl(), "master");
304+
305+
TestBotRunner.runPeriodicItems(notifyBot);
306+
var email = smtpServer.receive(Duration.ofSeconds(10));
307+
assertEquals(email.sender(), sender);
308+
assertEquals(email.recipients(), List.of(recipient));
309+
assertFalse(email.subject().contains("another"));
310+
assertTrue(email.subject().contains(": master: 2 new changesets"));
311+
assertTrue(email.body().contains("Changeset: " + editHash1.abbreviate()));
312+
assertTrue(email.body().contains("23456789: More fixes"));
313+
assertTrue(email.body().contains("Changeset: " + editHash2.abbreviate()));
314+
assertTrue(email.body().contains("3456789A: Even more fixes"));
315+
assertFalse(email.body().contains(masterHash.abbreviate()));
316+
assertFalse(email.body().contains("456789AB: Yet more fixes"));
317+
318+
localRepo.checkout(branch, true);
319+
var editHash3 = CheckableRepository.appendAndCommit(localRepo, "Another branch", "456789AB: Yet more fixes");
320+
localRepo.push(editHash3, repo.getUrl(), "another");
321+
322+
TestBotRunner.runPeriodicItems(notifyBot);
323+
email = smtpServer.receive(Duration.ofSeconds(10));
324+
assertEquals(email.sender(), sender);
325+
assertEquals(email.recipients(), List.of(recipient));
326+
assertTrue(email.subject().contains(": another: 456789AB: Yet more fixes"));
327+
assertFalse(email.subject().contains("master"));
328+
assertTrue(email.body().contains("Changeset: " + editHash3.abbreviate()));
329+
assertTrue(email.body().contains("456789AB: Yet more fixes"));
330+
assertFalse(email.body().contains("Changeset: " + editHash2.abbreviate()));
331+
assertFalse(email.body().contains("3456789A: Even more fixes"));
332+
}
333+
}
270334
}

0 commit comments

Comments
 (0)