From 1ee676a1cf78b4b73e6ec76957bc8d4add86e88f Mon Sep 17 00:00:00 2001 From: The-Huginn Date: Wed, 28 Feb 2024 09:13:16 +0100 Subject: [PATCH] Change email body to html, update formatting of the sent out email --- README.md | 2 +- .../jboss/set/JiraIssueLotteryCommand.java | 2 +- src/main/java/org/jboss/set/draw/Lottery.java | 24 +++--- .../set/processing/CollectorProducer.java | 4 +- .../jboss/set/draw/AbstractLotteryTest.java | 4 +- .../jboss/set/draw/LotteryDrawingTest.java | 77 ++++++++++--------- .../set/draw/MaxIssuesGlobalOptionTest.java | 27 +++---- .../set/state/ConfigDeserializationTest.java | 2 +- 8 files changed, 73 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 078f4fa..5dc9d5a 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Fork this repository to your GitHub account. ```yaml delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: Tadpole@thehuginn.com projects: - project: WFLY components: [Logging] diff --git a/src/main/java/org/jboss/set/JiraIssueLotteryCommand.java b/src/main/java/org/jboss/set/JiraIssueLotteryCommand.java index 5d14698..1d806f1 100644 --- a/src/main/java/org/jboss/set/JiraIssueLotteryCommand.java +++ b/src/main/java/org/jboss/set/JiraIssueLotteryCommand.java @@ -7,10 +7,10 @@ import org.apache.camel.component.jira.JiraComponent; import org.apache.camel.component.jira.JiraConfiguration; import org.apache.camel.component.jira.JiraEndpoint; -import org.jboss.set.config.JiraLotteryAppConfig; import org.jboss.set.processing.CollectorProducer; import org.jboss.set.processing.NewIssueCollector; import org.jboss.set.testing.JiraCommand; +import org.jboss.set.config.JiraLotteryAppConfig; import picocli.CommandLine.Command; @Command(name = "jira-issue-lottery", mixinStandardHelpOptions = true) diff --git a/src/main/java/org/jboss/set/draw/Lottery.java b/src/main/java/org/jboss/set/draw/Lottery.java index 65cb16a..454280a 100644 --- a/src/main/java/org/jboss/set/draw/Lottery.java +++ b/src/main/java/org/jboss/set/draw/Lottery.java @@ -7,11 +7,12 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.eclipse.microprofile.config.ConfigProvider; -import org.jboss.set.config.LotteryConfig; -import org.jboss.set.config.LotteryConfigProducer; +import org.jboss.set.config.GitHubRawUrl; import org.jboss.set.draw.entities.Issue; import org.jboss.set.draw.entities.Participant; import org.jboss.set.processing.state.EveryIssueState; +import org.jboss.set.config.LotteryConfig; +import org.jboss.set.config.LotteryConfigProducer; import java.util.Collections; import java.util.List; @@ -23,7 +24,6 @@ import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.joining; -import static org.jboss.set.config.GitHubRawUrl.GitHubRepoToRawUrlConverter.RELATIVE_PATH; @Unremovable @ApplicationScoped @@ -44,10 +44,11 @@ public class Lottery { %s - If you would like to unsubscribe, please remove yourself from the following repository's configuration file %s + If you would like to unsubscribe, please remove yourself from the following repository's configuration file %3$s - Have a nice day, - your Jira issue lottery bot"""; + Jira issue lottery bot"""; + private static final String PROJECT_LINE = "► %s\n"; + private static final String LINK_LINE = " • %1$s\n"; public void run(EveryIssueState collectedIssues) { LotteryConfig lotteryConfig = lotteryConfigProducer.getLotteryConfig(); @@ -87,7 +88,7 @@ public void run(EveryIssueState collectedIssues) { .collect(Collectors.groupingBy(Issue::getAssignee)); aggregatedByAssignee.forEach((participant, assignedIssues) -> mailer.send(new Mail() .setSubject(EMAIL_SUBJECT.formatted(getUsername(participant.getEmail()))) - .setText(createEmailText(participant.getEmail(), assignedIssues)) + .setHtml(createEmailText(participant.getEmail(), assignedIssues)) .setTo(List.of(participant.getEmail())))); } @@ -101,9 +102,10 @@ public static String createEmailText(String email, List issues) { // 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"))) + .map(projectLinksEntry -> PROJECT_LINE.formatted(projectLinksEntry.getKey()) + + projectLinksEntry.getValue().stream() + .map(issue -> LINK_LINE.formatted(issue.getBrowseUri().toString())) + .collect(Collectors.joining())) .collect(joining("\n")), configFileUrl()); } @@ -111,6 +113,6 @@ public static String createEmailText(String email, List issues) { private static String configFileUrl() { String repo = ConfigProvider.getConfig().getConfigValue("jira-issue-lottery.config-file-repo").getRawValue(); repo = repo.endsWith("/") ? repo.substring(0, repo.length() - 1) : repo; - return repo + "/blob/" + RELATIVE_PATH; + return repo + "/blob/" + GitHubRawUrl.GitHubRepoToRawUrlConverter.RELATIVE_PATH; } } diff --git a/src/main/java/org/jboss/set/processing/CollectorProducer.java b/src/main/java/org/jboss/set/processing/CollectorProducer.java index 05df655..c98fd5c 100644 --- a/src/main/java/org/jboss/set/processing/CollectorProducer.java +++ b/src/main/java/org/jboss/set/processing/CollectorProducer.java @@ -3,11 +3,11 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.apache.camel.component.jira.JiraEndpoint; +import org.jboss.set.query.IssueStatus; +import org.jboss.set.query.SearchQuery; import org.jboss.set.config.LotteryConfig; import org.jboss.set.config.LotteryConfigProducer; import org.jboss.set.jql.JqlBuilder; -import org.jboss.set.query.IssueStatus; -import org.jboss.set.query.SearchQuery; import java.time.LocalDate; import java.util.Collection; diff --git a/src/test/java/org/jboss/set/draw/AbstractLotteryTest.java b/src/test/java/org/jboss/set/draw/AbstractLotteryTest.java index 2ff472d..5264a21 100644 --- a/src/test/java/org/jboss/set/draw/AbstractLotteryTest.java +++ b/src/test/java/org/jboss/set/draw/AbstractLotteryTest.java @@ -4,7 +4,7 @@ * tests. * Furthermore, then you can access List of {@code org.jboss.draw.entities.Issue}, with * the same issues, in variable {@code ourIssues}. This is useful for methods such as - * {@link org.jboss.draw.Lottery#createEmailText(java.lang.String, java.util.List)}. + * {@link org.jboss.set.draw.Lottery#createEmailText(java.lang.String, java.util.List)}. */ package org.jboss.set.draw; @@ -26,10 +26,10 @@ import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; import org.apache.camel.spi.Registry; import org.jboss.set.JiraEndpointProducer; +import org.jboss.set.testing.JiraCommand; import org.jboss.set.JiraIssueLotteryCommand; import org.jboss.set.config.LotteryConfigProducer; import org.jboss.set.helper.MockedSearchRestClientProducer; -import org.jboss.set.testing.JiraCommand; import org.junit.jupiter.api.BeforeEach; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; diff --git a/src/test/java/org/jboss/set/draw/LotteryDrawingTest.java b/src/test/java/org/jboss/set/draw/LotteryDrawingTest.java index dcc9a79..5cacfa1 100644 --- a/src/test/java/org/jboss/set/draw/LotteryDrawingTest.java +++ b/src/test/java/org/jboss/set/draw/LotteryDrawingTest.java @@ -2,10 +2,11 @@ import io.quarkus.mailer.Mail; import io.quarkus.test.junit.QuarkusTest; -import org.jboss.set.config.LotteryConfig; - +import org.jboss.set.draw.entities.Issue; import org.jboss.set.query.IssueStatus; import org.jboss.set.wrappers.IssueWrapper; +import org.jboss.set.config.LotteryConfig; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import picocli.CommandLine; @@ -38,16 +39,16 @@ protected List setupIssues() { @Test public void testAssigningOneIssue() throws Exception { - String email = "The-Huginn@thehuginn.com"; + String email = "Tadpole@thehuginn.com"; String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: %s maxIssues: 5 projects: - project: WFLY components: [Logging, Documentation] - maxIssues: 5"""; + maxIssues: 5""".formatted(email); LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class); when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig); @@ -61,21 +62,21 @@ public void testAssigningOneIssue() throws Exception { List sent = mailbox.getMailsSentTo(email); assertEquals(1, sent.size()); - assertEquals(Lottery.EMAIL_SUBJECT.formatted("The-Huginn"), sent.get(0).getSubject()); - assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0))), sent.get(0).getText()); + Assertions.assertEquals(Lottery.EMAIL_SUBJECT.formatted("Tadpole"), sent.get(0).getSubject()); + assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0))), sent.get(0).getHtml()); } @Test public void testAssigningOneIssueOnlyProjectDefined() throws Exception { - String email = "The-Huginn@thehuginn.com"; + String email = "Tadpole@thehuginn.com"; String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: %s maxIssues: 5 projects: - project: WFLY - maxIssues: 5"""; + maxIssues: 5""".formatted(email); LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class); when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig); @@ -89,16 +90,16 @@ public void testAssigningOneIssueOnlyProjectDefined() throws Exception { List sent = mailbox.getMailsSentTo(email); assertEquals(1, sent.size()); - assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0))), sent.get(0).getText()); + assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0))), sent.get(0).getHtml()); } @Test public void testAssigningAllIssues() throws Exception { - String email = "The-Huginn@thehuginn.com"; + String email = "Tadpole@thehuginn.com"; String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: %s maxIssues: 10 projects: - project: WFLY @@ -106,7 +107,7 @@ public void testAssigningAllIssues() throws Exception { maxIssues: 5 - project: RESTEASY components: [Logging] - maxIssues: 5"""; + maxIssues: 5""".formatted(email); LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class); when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig); @@ -120,21 +121,21 @@ public void testAssigningAllIssues() throws Exception { List sent = mailbox.getMailsSentTo(email); assertEquals(1, sent.size()); - assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0), ourIssues.get(1))), sent.get(0).getText()); + assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0), ourIssues.get(1))), sent.get(0).getHtml()); } @Test public void testAppendingRepositoryForUnsubcription() throws Exception { - String email = "The-Huginn@thehuginn.com"; + String email = "Tadpole@thehuginn.com"; String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: %s maxIssues: 5 projects: - project: WFLY components: [Documentation] - maxIssues: 5"""; + maxIssues: 5""".formatted(email); LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class); when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig); @@ -148,7 +149,7 @@ public void testAppendingRepositoryForUnsubcription() throws Exception { List sent = mailbox.getMailsSentTo(email); assertEquals(1, sent.size()); - String emailText = sent.get(0).getText(); + String emailText = sent.get(0).getHtml(); //Checks only if email message is correctly formatted. The URL is only for test purpose and may be invalid. String expectedUrl = "https://github.com/jboss-set/jira-issue-lottery/blob/main/.github/jira-issue-lottery.yml"; @@ -157,16 +158,16 @@ public void testAppendingRepositoryForUnsubcription() throws Exception { @Test public void testExtractingUsername() throws Exception { - String email = "The-Huginn@thehuginn.com"; + String email = "Tadpole@thehuginn.com"; String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: %s maxIssues: 5 projects: - project: WFLY components: [Documentation] - maxIssues: 5"""; + maxIssues: 5""".formatted(email); LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class); when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig); @@ -181,17 +182,17 @@ public void testExtractingUsername() throws Exception { List sent = mailbox.getMailsSentTo(email); 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")); + assertTrue(sent.get(0).getHtml().contains("Hi Tadpole,")); + assertTrue(sent.get(0).getSubject().contains("Tadpole")); } @Test public void testAssigningNoIssues() throws Exception { - String email = "The-Huginn@thehuginn.com"; + String email = "Tadpole@thehuginn.com"; String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: %s maxIssues: 10 projects: - project: WFLY @@ -199,7 +200,7 @@ public void testAssigningNoIssues() throws Exception { maxIssues: 5 - project: RESTEASY components: [Documentation] - maxIssues: 5"""; + maxIssues: 5""".formatted(email); LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class); when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig); @@ -220,7 +221,7 @@ public void testAssigningAllIssuesToTwoParticipants() throws Exception { String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: Tadpole@thehuginn.com maxIssues: 5 projects: - project: WFLY @@ -243,29 +244,29 @@ public void testAssigningAllIssuesToTwoParticipants() throws Exception { int exitCode = cmd.execute(); assertEquals(0, exitCode); - for (Map.Entry userIssue : List.of( - Map.entry("The-Huginn@thehuginn.com", ourIssues.get(0)), + for (Map.Entry userIssue : List.of( + Map.entry("Tadpole@thehuginn.com", ourIssues.get(0)), Map.entry("xstefank@redhat.com", ourIssues.get(1)))) { List sent = mailbox.getMailsSentTo(userIssue.getKey()); assertEquals(1, sent.size()); 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()); + sent.get(0).getHtml()); } } @Test public void testAssigningMaximumIssues() throws Exception { - String email = "The-Huginn@thehuginn.com"; + String email = "Tadpole@thehuginn.com"; String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: %s maxIssues: 5 projects: - project: WELD components: [Logging] - maxIssues: 1"""; + maxIssues: 1""".formatted(email); LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class); when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig); @@ -279,7 +280,7 @@ public void testAssigningMaximumIssues() throws Exception { List sent = mailbox.getMailsSentTo(email); assertEquals(1, sent.size()); - assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(2))), sent.get(0).getText()); + assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(2))), sent.get(0).getHtml()); } @Test @@ -307,7 +308,7 @@ public void testAssigningPartialComponentsHitFromConfig() throws Exception { List sent = mailbox.getMailsSentTo(email); assertEquals(1, sent.size()); - assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(2), ourIssues.get(3))), sent.get(0).getText()); + assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(2), ourIssues.get(3))), sent.get(0).getHtml()); } @Test @@ -335,7 +336,7 @@ public void testAssigningPartialComponentsHitFromIssue() throws Exception { List sent = mailbox.getMailsSentTo(email); assertEquals(1, sent.size()); - assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(2), ourIssues.get(3))), sent.get(0).getText()); + assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(2), ourIssues.get(3))), sent.get(0).getHtml()); } @Test @@ -363,6 +364,6 @@ public void testAssigningWithNoPartialComponentsHit() throws Exception { List sent = mailbox.getMailsSentTo(email); assertEquals(1, sent.size()); - assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(3))), sent.get(0).getText()); + assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(3))), sent.get(0).getHtml()); } } diff --git a/src/test/java/org/jboss/set/draw/MaxIssuesGlobalOptionTest.java b/src/test/java/org/jboss/set/draw/MaxIssuesGlobalOptionTest.java index 6d54a3d..6c2d9e9 100644 --- a/src/test/java/org/jboss/set/draw/MaxIssuesGlobalOptionTest.java +++ b/src/test/java/org/jboss/set/draw/MaxIssuesGlobalOptionTest.java @@ -3,9 +3,10 @@ import com.atlassian.jira.rest.client.api.domain.Issue; import io.quarkus.mailer.Mail; import io.quarkus.test.junit.QuarkusTest; -import org.jboss.set.config.LotteryConfig; import org.jboss.set.query.IssueStatus; import org.jboss.set.wrappers.IssueWrapper; +import org.jboss.set.config.LotteryConfig; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import picocli.CommandLine; @@ -41,15 +42,15 @@ protected List setupIssues() { @Test public void testAssigningOneIssue() throws Exception { - String email = "The-Huginn@thehuginn.com"; + String email = "Tadpole@thehuginn.com"; String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: %s maxIssues: 1 projects: - project: WFLY - maxIssues: 2"""; + maxIssues: 2""".formatted(email); LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class); when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig); @@ -63,22 +64,22 @@ public void testAssigningOneIssue() throws Exception { List sent = mailbox.getMailsSentTo(email); assertEquals(1, sent.size()); - assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0))), sent.get(0).getText()); + Assertions.assertEquals(Lottery.createEmailText(email, List.of(ourIssues.get(0))), sent.get(0).getHtml()); } @Test public void testRespectingGlobalMaxIssuesWithTwoProjects() throws Exception { - String email = "The-Huginn@thehuginn.com"; + String email = "Tadpole@thehuginn.com"; String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: %s maxIssues: 5 projects: - project: WFLY maxIssues: 5 - project: RESTEASY - maxIssues: 5"""; + maxIssues: 5""".formatted(email); LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class); when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig); @@ -92,16 +93,16 @@ public void testRespectingGlobalMaxIssuesWithTwoProjects() throws Exception { List sent = mailbox.getMailsSentTo(email); assertEquals(1, sent.size()); - assertEquals(Lottery.createEmailText(email, ourIssues.subList(0, 5)), sent.get(0).getText()); + assertEquals(Lottery.createEmailText(email, ourIssues.subList(0, 5)), sent.get(0).getHtml()); } @Test public void testRespectingGlobalMaxIssuesWithThreeProjects() throws Exception { - String email = "The-Huginn@thehuginn.com"; + String email = "Tadpole@thehuginn.com"; String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: %s maxIssues: 5 projects: - project: WFLY @@ -109,7 +110,7 @@ public void testRespectingGlobalMaxIssuesWithThreeProjects() throws Exception { - project: RESTEASY maxIssues: 2 - project: WELD - maxIssues: 2"""; + maxIssues: 2""".formatted(email); LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class); when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig); @@ -126,6 +127,6 @@ public void testRespectingGlobalMaxIssuesWithThreeProjects() throws Exception { assertEquals(Lottery.createEmailText(email, List.of( ourIssues.get(0), ourIssues.get(1), ourIssues.get(3), ourIssues.get(4), - ourIssues.get(6))), sent.get(0).getText()); + ourIssues.get(6))), sent.get(0).getHtml()); } } diff --git a/src/test/java/org/jboss/set/state/ConfigDeserializationTest.java b/src/test/java/org/jboss/set/state/ConfigDeserializationTest.java index ad720a1..28b0d4b 100644 --- a/src/test/java/org/jboss/set/state/ConfigDeserializationTest.java +++ b/src/test/java/org/jboss/set/state/ConfigDeserializationTest.java @@ -22,7 +22,7 @@ void testDeserializingSimpleConfigFile() { String configFile = """ delay: P14D participants: - - email: The-Huginn@thehuginn.com + - email: Tadpole@thehuginn.com maxIssues: 5 projects: - project: WFLY