Skip to content

Commit

Permalink
246: Avoid bridging too large emails
Browse files Browse the repository at this point in the history
Reviewed-by: ehelin
  • Loading branch information
rwestberg committed Jan 24, 2020
1 parent 1b57804 commit d62e49e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ private void postNewMessage(Email email) {
"](mailto:" + email.author().address() + ") on [" + email.sender().localPart() +
"](mailto:" + email.sender().address() + "):*\n\n" +
TextToMarkdown.escapeFormatting(email.body());
if (body.length() > 64000) {
body = body.substring(0, 64000) + "...\n\n" + "" +
"This message was too large to bridge in full, and has been truncated. " +
"Please check the mailing list archive to see the full text.";
}
pr.addComment(body);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@
import static org.junit.jupiter.api.Assertions.*;

class MailingListArchiveReaderBotTests {
private void addReply(Conversation conversation, MailingList mailingList, PullRequest pr) {
private void addReply(Conversation conversation, MailingList mailingList, PullRequest pr, String reply) {
var first = conversation.first();

var reply = "Looks good";
var references = first.id().toString();
var email = Email.create(EmailAddress.from("Commenter", "c@test.test"), "Re: RFR: " + pr.title(), reply)
.recipient(first.author())
Expand All @@ -52,6 +50,10 @@ private void addReply(Conversation conversation, MailingList mailingList, PullRe
mailingList.post(email);
}

private void addReply(Conversation conversation, MailingList mailingList, PullRequest pr) {
addReply(conversation, mailingList, pr, "Looks good");
}

@Test
void simpleArchive(TestInfo testInfo) throws IOException {
try (var credentials = new HostCredentials(testInfo);
Expand Down Expand Up @@ -194,4 +196,78 @@ void rememberBridged(TestInfo testInfo) throws IOException {
assertEquals(2, notUpdated.size());
}
}

@Test
void largeEmail(TestInfo testInfo) throws IOException {
try (var credentials = new HostCredentials(testInfo);
var tempFolder = new TemporaryDirectory();
var listServer = new TestMailmanServer();
var webrevServer = new TestWebrevServer()) {
var author = credentials.getHostedRepository();
var archive = credentials.getHostedRepository();
var ignored = credentials.getHostedRepository();
var listAddress = EmailAddress.parse(listServer.createList("test"));
var censusBuilder = credentials.getCensusBuilder()
.addAuthor(author.forge().currentUser().id());
var from = EmailAddress.from("test", "test@test.mail");
var mlBot = new MailingListBridgeBot(from, author, archive, "master",
censusBuilder.build(), "master",
listAddress,
Set.of(ignored.forge().currentUser().userName()),
Set.of(),
listServer.getArchive(), listServer.getSMTP(),
archive, "webrev", Path.of("test"),
webrevServer.uri(),
Set.of(), Map.of(),
URIBuilder.base("http://issues.test/browse/").build(),
Map.of(), Duration.ZERO);

// The mailing list as well
var mailmanServer = MailingListServerFactory.createMailmanServer(listServer.getArchive(), listServer.getSMTP(),
Duration.ZERO);
var mailmanList = mailmanServer.getList(listAddress.address());
var readerBot = new MailingListArchiveReaderBot(from, Set.of(mailmanList), Set.of(archive));

// Populate the projects repository
var localRepo = CheckableRepository.init(tempFolder.path(), author.repositoryType());
var masterHash = localRepo.resolve("master").orElseThrow();
localRepo.push(masterHash, author.url(), "master", true);
localRepo.push(masterHash, archive.url(), "webrev", true);

// Make a change with a corresponding PR
var editHash = CheckableRepository.appendAndCommit(localRepo, "A simple change",
"Change msg\n\nWith several lines");
localRepo.push(editHash, author.url(), "edit", true);
var pr = credentials.createPullRequest(archive, "master", "edit", "This is a pull request");
pr.setBody("This should now be ready");

// Run an archive pass
TestBotRunner.runPeriodicItems(mlBot);
listServer.processIncoming();

// Run an archive pass
TestBotRunner.runPeriodicItems(readerBot);
TestBotRunner.runPeriodicItems(readerBot);

// Post a large reply directly to the list
var conversations = mailmanList.conversations(Duration.ofDays(1));
assertEquals(1, conversations.size());

var replyBody = "This line is about 30 bytes long\n".repeat(1000 * 10);
addReply(conversations.get(0), mailmanList, pr, replyBody);
listServer.processIncoming();

// Another archive reader pass - has to be done twice
TestBotRunner.runPeriodicItems(readerBot);
TestBotRunner.runPeriodicItems(readerBot);

// The bridge should now have processed the reply
var updated = pr.comments();
assertEquals(2, updated.size());
assertTrue(updated.get(1).body().contains("Mailing list message from"));
assertTrue(updated.get(1).body().contains("[Commenter](mailto:c@test.test)"));
assertTrue(updated.get(1).body().contains("[test](mailto:test@" + listAddress.domain() + ")"));
assertTrue(updated.get(1).body().contains("This message was too large"));
}
}
}

0 comments on commit d62e49e

Please sign in to comment.