Skip to content

Commit

Permalink
Allow string or array of addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Feb 14, 2024
1 parent 231d933 commit c61136e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Action deserialize(JsonParser jp, DeserializationContext ctxt) throws IOE
labelsAction.labels = mapper.convertValue(root, LIST_STRING);
return labelsAction;
} else if (root.has("address")) {
return mapper.convertValue(root, EmailAction.class);
return new EmailAction(root.get("address"));
} else if (root.has("channel")) {
return mapper.convertValue(root, DiscordAction.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.kohsuke.github.GHPullRequest;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import io.quarkus.arc.Arc;
Expand All @@ -37,14 +38,28 @@ public static native MailTemplateInstance discussionEvent(String title, String h
}

@JsonProperty
String address;
String[] addresses;

public EmailAction() {
}

public EmailAction(JsonNode address) {
if (address.isArray()) {
this.addresses = new String[address.size()];
for (int i = 0; i < address.size(); i++) {
this.addresses[i] = address.get(i).asText();
}
} else {
this.addresses = new String[] { address.asText() };
}
}

@Override
public void apply(QueryContext queryContext) {
EventData eventData = queryContext.getEventData();
// Fire and forget: mail construction and sending happens in a separate thread
Arc.container().instance(ManagedExecutor.class).get().submit(() -> {
Log.debugf("EmailAction.apply: Preparing email to %s for %s.%s", address, eventData.getEventType(),
Log.debugf("EmailAction.apply: Preparing email to %s for %s.%s", addresses, eventData.getEventType(),
eventData.getAction());

final String subject;
Expand Down Expand Up @@ -78,19 +93,19 @@ public void apply(QueryContext queryContext) {
};
} catch (Exception e) {
mailTemplateInstance = null;
Log.errorf(e, "EmailAction.apply: Failed to prepare email to %s", address);
Log.errorf(e, "EmailAction.apply: Failed to prepare email to %s", (Object[]) addresses);
return;
}

if (mailTemplateInstance != null) {
Log.debugf("EmailAction.apply: Sending email to %s; %s", address, subject);
Log.debugf("EmailAction.apply: Sending email to %s; %s", addresses, subject);
mailTemplateInstance
.to(address)
.to(addresses)
.subject(subject)
.send()
.subscribe().with(
success -> Log.infof("EmailAction.apply: Email sent to %s; %s", address, subject),
failure -> Log.errorf(failure, "EmailAction.apply: Failed to send email to %s", address,
success -> Log.infof("EmailAction.apply: Email sent to %s; %s", addresses, subject),
failure -> Log.errorf(failure, "EmailAction.apply: Failed to send email to %s", addresses,
subject));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void discussionCreatedAnnouncements() throws Exception {
verifyNoMoreInteractions(mocks.ghObjects());
});

await().atMost(5, SECONDS).until(() -> mailbox.getTotalMessagesSent() == 1);
await().atMost(5, SECONDS).until(() -> mailbox.getTotalMessagesSent() != 0);
}

@Test
Expand All @@ -89,6 +89,6 @@ void testRelevantPr() throws Exception {
verifyNoMoreInteractions(mocks.installationGraphQLClient(installationId));
});

await().atMost(10, SECONDS).until(() -> mailbox.getTotalMessagesSent() == 1);
await().atMost(10, SECONDS).until(() -> mailbox.getTotalMessagesSent() != 0);
}
}
4 changes: 3 additions & 1 deletion src/test/resources/cf-email.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ notice:
then: [email_activity]
actions:
email_activity:
address: test@commonhaus.org
address:
- test@commonhaus.org
- automation@commonhaus.org

0 comments on commit c61136e

Please sign in to comment.