Skip to content

Commit

Permalink
[Build-trigger-54] Sending a confirmation email to the person that tr…
Browse files Browse the repository at this point in the history
…iggered the build
  • Loading branch information
khermano committed Oct 24, 2024
1 parent 8003f6c commit b0e440f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mailer</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/org/jboss/set/BuildTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jboss.logging.Logger;
import org.jboss.set.model.json.BuildJMSTriggerPayload;
import jakarta.jms.JMSRuntimeException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.JMSContext;
import jakarta.jms.Session;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import org.jboss.set.exception.BuildTriggerException;
import org.jboss.set.model.json.BuildJMSTriggerPayload;

@ApplicationScoped
public class BuildTrigger {

Logger logger = Logger.getLogger(BuildTrigger.class);

@Inject
EmailService emailService;

@ConfigProperty(name = "build-trigger.topic")
String triggerTopic;

Expand All @@ -27,10 +32,21 @@ public class BuildTrigger {
public void triggerBuild(BuildJMSTriggerPayload payloadMessage) {
try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE)) {
context.createProducer().send(context.createTopic(triggerTopic), objectMapper.writeValueAsString(payloadMessage));
logger.infof("Build message sent to UMB. Payload: %s", payloadMessage);
if (payloadMessage.email != null && !payloadMessage.email.isBlank()
&& !payloadMessage.email.equals("Email not provided in the token")) {
emailService.sendMail(payloadMessage);
} else {
logger.warnf("Confirmation email not sent: missing or invalid email");
}
} catch (JsonProcessingException e) {
logger.errorf("Failed to serialize BuildJMSTriggerPayload to JSON: "
+ e.getMessage() + ". Payload: " + payloadMessage);
throw new RuntimeException("Failed to serialize BuildJMSTriggerPayload to JSON: " + e.getMessage(), e);
throw new BuildTriggerException("Failed to serialize BuildJMSTriggerPayload to JSON: " + e.getMessage(), e);
} catch (JMSRuntimeException e) {
logger.errorf("Build message failed to be sent to UMB. Payload: %s. Exception: %s",
payloadMessage, e.getMessage());
throw new BuildTriggerException("Build message failed to be sent to UMB due to: " + e.getMessage(), e);
}
}
}
46 changes: 46 additions & 0 deletions src/main/java/org/jboss/set/EmailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.jboss.set;

import io.quarkus.mailer.Mail;
import io.quarkus.mailer.Mailer;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.set.model.json.BuildJMSTriggerPayload;

import java.util.List;

@ApplicationScoped
public class EmailService {

@Inject
Mailer mailer;

public void sendMail(BuildJMSTriggerPayload payload) {
String project = payload.gitRepo.substring(payload.gitRepo.lastIndexOf('/') + 1);
String subject = "[Build Trigger] Build of the " + capitalizeFirstLetter(project) + " (version " + payload.projectVersion + ") requested";
String htmlText = "<p>Request to build <b>" + payload.gitRepo + "</b> in version <b>" + payload.projectVersion + "</b> "
+ "was successfully sent to the productization team using the following parameters:</p>"
+ "<ul>"
+ "<li><b>Tag:</b> " + payload.tag + "</li>"
+ "<li><b>Repository:</b> " + payload.gitRepo + "</li>"
+ "<li><b>Version:</b> " + payload.projectVersion + "</li>"
+ "<li><b>Commit:</b> " + payload.commitSha + "</li>"
+ "<li><b>Streams:</b> " + payload.streams + "</li>"
+ "</ul>"
+ "<p>Report any issues or share suggestions using our "
+ "<a href=\"https://github.com/jboss-set/build-trigger/issues\">GitHub Repository</a>.</p>"
+ "<p>This is an automated Build Trigger message, please do not respond!</p>";


mailer.send(new Mail()
.setSubject(subject)
.setHtml(htmlText)
.setTo(List.of(payload.email)));
}

private String capitalizeFirstLetter(String input) {
if (input == null || input.isEmpty()) {
return input;
}
return Character.toUpperCase(input.charAt(0)) + input.substring(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jboss.set.exception;

public class BuildTriggerException extends RuntimeException {
public BuildTriggerException(String message, Throwable cause) {
super(message, cause);
}
}
19 changes: 19 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ quarkus.openshift.env.mapping.GITHUB_KEY.with-key=github-key
quarkus.openshift.env.mapping.GITLAB_KEY.from-secret=build-trigger-secrets
quarkus.openshift.env.mapping.GITLAB_KEY.with-key=gitlab-key

quarkus.openshift.env.mapping.MAILER_FROM.from-secret=build-trigger-secrets
quarkus.openshift.env.mapping.MAILER_FROM.with-key=mailer-from

quarkus.openshift.env.mapping.MAILER_HOST.from-secret=build-trigger-secrets
quarkus.openshift.env.mapping.MAILER_HOST.with-key=mailer-host

quarkus.openshift.env.mapping.MAILER_PORT.from-secret=build-trigger-secrets
quarkus.openshift.env.mapping.MAILER_PORT.with-key=mailer-port

# oidc
# specify auth URL only in prod mode so in dev mode keycloak is started as dev service
%prod.quarkus.oidc.auth-server-url=${auth.server.url}
Expand All @@ -61,3 +70,13 @@ quarkus.tls.trust-all=true
%test.build-trigger.topic=not-required
%test.github-key=not-required
%test.gitlab-key=not-required

# Sending emails
quarkus.mailer.auth-methods=DIGEST-MD5 CRAM-SHA256 CRAM-SHA1 CRAM-MD5 PLAIN LOGIN
%prod.quarkus.mailer.from=${MAILER_FROM}
%prod.quarkus.mailer.host=${MAILER_HOST}
%prod.quarkus.mailer.port=${MAILER_PORT}
quarkus.mailer.tls=false
# Enabling STARTTLS (this is required to upgrade the connection to secure)
quarkus.mailer.starttls=true

0 comments on commit b0e440f

Please sign in to comment.