Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change email body to html, update formatting of the sent out email #70

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jboss/set/JiraIssueLotteryCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/org/jboss/set/draw/Lottery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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 <a href="%3$s">%3$s</a>

Have a nice day,
your Jira issue lottery bot""";
Jira issue lottery bot""";
private static final String PROJECT_LINE = "► <strong>%s</strong>\n";
private static final String LINK_LINE = " • <a href=\"%1$s\">%1$s</a>\n";

public void run(EveryIssueState collectedIssues) {
LotteryConfig lotteryConfig = lotteryConfigProducer.getLotteryConfig();
Expand Down Expand Up @@ -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()))));
}

Expand All @@ -101,16 +102,17 @@ public static String createEmailText(String email, List<Issue> 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());
}

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;
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/jboss/set/processing/CollectorProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/jboss/set/draw/AbstractLotteryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
77 changes: 39 additions & 38 deletions src/test/java/org/jboss/set/draw/LotteryDrawingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -38,16 +39,16 @@ protected List<com.atlassian.jira.rest.client.api.domain.Issue> setupIssues() {

@Test
public void testAssigningOneIssue() throws Exception {
String email = "The-Huginn@thehuginn.com";
String email = "Tadpole@thehuginn.com";
String configFile = """
delay: P14D
participants:
- email: [email protected]
- 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);
Expand All @@ -61,21 +62,21 @@ public void testAssigningOneIssue() throws Exception {

List<Mail> 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: [email protected]
- 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);
Expand All @@ -89,24 +90,24 @@ public void testAssigningOneIssueOnlyProjectDefined() throws Exception {

List<Mail> 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: [email protected]
- email: %s
maxIssues: 10
projects:
- project: WFLY
components: [Documentation]
maxIssues: 5
- project: RESTEASY
components: [Logging]
maxIssues: 5""";
maxIssues: 5""".formatted(email);

LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class);
when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig);
Expand All @@ -120,21 +121,21 @@ public void testAssigningAllIssues() throws Exception {

List<Mail> 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: [email protected]
- 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);
Expand All @@ -148,7 +149,7 @@ public void testAppendingRepositoryForUnsubcription() throws Exception {

List<Mail> 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";
Expand All @@ -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: [email protected]
- 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);
Expand All @@ -181,25 +182,25 @@ public void testExtractingUsername() throws Exception {
List<Mail> 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: [email protected]
- email: %s
maxIssues: 10
projects:
- project: WFLY
components: [Logging]
maxIssues: 5
- project: RESTEASY
components: [Documentation]
maxIssues: 5""";
maxIssues: 5""".formatted(email);

LotteryConfig testLotteryConfig = objectMapper.readValue(configFile, LotteryConfig.class);
when(lotteryConfigProducer.getLotteryConfig()).thenReturn(testLotteryConfig);
Expand All @@ -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
Expand All @@ -243,29 +244,29 @@ public void testAssigningAllIssuesToTwoParticipants() throws Exception {
int exitCode = cmd.execute();
assertEquals(0, exitCode);

for (Map.Entry<String, org.jboss.set.draw.entities.Issue> userIssue : List.of(
Map.entry("The-Huginn@thehuginn.com", ourIssues.get(0)),
for (Map.Entry<String, Issue> userIssue : List.of(
Map.entry("Tadpole@thehuginn.com", ourIssues.get(0)),
Map.entry("[email protected]", ourIssues.get(1)))) {
List<Mail> 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: [email protected]
- 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);
Expand All @@ -279,7 +280,7 @@ public void testAssigningMaximumIssues() throws Exception {

List<Mail> 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
Expand Down Expand Up @@ -307,7 +308,7 @@ public void testAssigningPartialComponentsHitFromConfig() throws Exception {

List<Mail> 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
Expand Down Expand Up @@ -335,7 +336,7 @@ public void testAssigningPartialComponentsHitFromIssue() throws Exception {

List<Mail> 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
Expand Down Expand Up @@ -363,6 +364,6 @@ public void testAssigningWithNoPartialComponentsHit() throws Exception {

List<Mail> 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());
}
}
Loading
Loading