Skip to content

Commit

Permalink
Merge pull request #65 from The-Huginn/issue-60
Browse files Browse the repository at this point in the history
Group links by project, add username into subject of the email
  • Loading branch information
xstefank authored Feb 27, 2024
2 parents 5c10bcb + 7d4231a commit 95a2eca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
23 changes: 17 additions & 6 deletions src/main/java/org/jboss/draw/Lottery.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
import static org.jboss.config.GitHubRawUrl.GitHubRepoToRawUrlConverter.RELATIVE_PATH;

@Unremovable
Expand All @@ -34,7 +36,7 @@ public class Lottery {
private Mailer mailer;

private static final Pattern USERNAME_FROM_EMAIL = Pattern.compile("\\s*(\\b[a-zA-Z0-9._%+-]+)@");
public static final String EMAIL_SUBJECT = "This week's lottery issues picked for You";
public static final String EMAIL_SUBJECT = "This week's lottery issues picked for %s";
private static final String EMAIL_BODY = """
Hi %s,
Expand Down Expand Up @@ -84,16 +86,25 @@ public void run(EveryIssueState collectedIssues) {
Map<Participant, List<Issue>> aggregatedByAssignee = issues.stream().filter(issue -> issue.getAssignee() != null)
.collect(Collectors.groupingBy(Issue::getAssignee));
aggregatedByAssignee.forEach((participant, assignedIssues) -> mailer.send(new Mail()
.setSubject(EMAIL_SUBJECT)
.setSubject(EMAIL_SUBJECT.formatted(getUsername(participant.getEmail())))
.setText(createEmailText(participant.getEmail(), assignedIssues))
.setTo(List.of(participant.getEmail()))));
}

public static String createEmailText(String email, List<Issue> issues) {
public static String getUsername(String email) {
Matcher matcher = USERNAME_FROM_EMAIL.matcher(email);
String username = matcher.find() ? matcher.group(1) : "Somebody";
return EMAIL_BODY.formatted(username,
issues.stream().map(issue -> issue.getBrowseUri().toString()).collect(Collectors.joining("\n")),
return matcher.find() ? matcher.group(1) : "Somebody";
}

public static String createEmailText(String email, List<Issue> issues) {
return EMAIL_BODY.formatted(getUsername(email),
// group by Project name
issues.stream().collect(groupingBy(Issue::getProject)).entrySet().stream()
// write project and then collect all links with bullet point prepended
.map(projectLinksEntry -> projectLinksEntry.getKey() + "\n" +
projectLinksEntry.getValue().stream().map(issue -> "\t-" + issue.getBrowseUri().toString())
.collect(Collectors.joining("\n\n")))
.collect(joining("\n")),
configFileUrl());
}

Expand Down
8 changes: 3 additions & 5 deletions src/test/java/org/jboss/set/draw/LotteryDrawingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testAssigningOneIssue() throws Exception {

List<Mail> sent = mailbox.getMailsSentTo(email);
assertEquals(1, sent.size());
assertEquals(Lottery.EMAIL_SUBJECT, sent.get(0).getSubject());
assertEquals(Lottery.EMAIL_SUBJECT.formatted("The-Huginn"), sent.get(0).getSubject());
assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0))), sent.get(0).getText());
}

Expand Down Expand Up @@ -89,7 +89,6 @@ public void testAssigningOneIssueOnlyProjectDefined() throws Exception {

List<Mail> sent = mailbox.getMailsSentTo(email);
assertEquals(1, sent.size());
assertEquals(Lottery.EMAIL_SUBJECT, sent.get(0).getSubject());
assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0))), sent.get(0).getText());
}

Expand Down Expand Up @@ -121,7 +120,6 @@ public void testAssigningAllIssues() throws Exception {

List<Mail> sent = mailbox.getMailsSentTo(email);
assertEquals(1, sent.size());
assertEquals(Lottery.EMAIL_SUBJECT, sent.get(0).getSubject());
assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0), ourIssues.get(1))), sent.get(0).getText());
}

Expand Down Expand Up @@ -182,6 +180,7 @@ public void testExtractingUsername() throws Exception {
assertEquals(1, sent.size());
// Check for correct parsing of username from email
assertTrue(sent.get(0).getText().contains("Hi The-Huginn,"));
assertTrue(sent.get(0).getSubject().contains("The-Huginn"));
}

@Test
Expand Down Expand Up @@ -247,7 +246,7 @@ public void testAssigningAllIssuesToTwoParticipants() throws Exception {
Map.entry("[email protected]", ourIssues.get(1)))) {
List<Mail> sent = mailbox.getMailsSentTo(userIssue.getKey());
assertEquals(1, sent.size());
assertEquals(Lottery.EMAIL_SUBJECT, sent.get(0).getSubject());
assertEquals(Lottery.EMAIL_SUBJECT.formatted(Lottery.getUsername(userIssue.getKey())), sent.get(0).getSubject());
assertEquals(Lottery.createEmailText(userIssue.getKey(), List.of(userIssue.getValue())),
sent.get(0).getText());
}
Expand Down Expand Up @@ -278,7 +277,6 @@ public void testAssigningMaximumIssues() throws Exception {

List<Mail> sent = mailbox.getMailsSentTo(email);
assertEquals(1, sent.size());
assertEquals(Lottery.EMAIL_SUBJECT, sent.get(0).getSubject());
assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(2))), sent.get(0).getText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public void testAssigningOneIssue() throws Exception {

List<Mail> sent = mailbox.getMailsSentTo(email);
assertEquals(1, sent.size());
assertEquals(Lottery.EMAIL_SUBJECT, sent.get(0).getSubject());
assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0))), sent.get(0).getText());
}

Expand Down Expand Up @@ -94,7 +93,6 @@ public void testRespectingGlobalMaxIssuesWithTwoProjects() throws Exception {

List<Mail> sent = mailbox.getMailsSentTo(email);
assertEquals(1, sent.size());
assertEquals(Lottery.EMAIL_SUBJECT, sent.get(0).getSubject());
assertEquals(Lottery.createEmailText(email, ourIssues.subList(0, 5)), sent.get(0).getText());
}

Expand Down Expand Up @@ -126,7 +124,6 @@ public void testRespectingGlobalMaxIssuesWithThreeProjects() throws Exception {

List<Mail> sent = mailbox.getMailsSentTo(email);
assertEquals(1, sent.size());
assertEquals(Lottery.EMAIL_SUBJECT, sent.get(0).getSubject());
assertEquals(Lottery.createEmailText(email, List.of(
ourIssues.get(0), ourIssues.get(1),
ourIssues.get(3), ourIssues.get(4),
Expand Down

0 comments on commit 95a2eca

Please sign in to comment.