diff --git a/pom.xml b/pom.xml index fe281cb..45e0387 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,10 @@ io.quarkus quarkus-hibernate-validator + + io.quarkus + quarkus-mailer + io.quarkus quarkus-junit5 diff --git a/src/main/java/org/jboss/set/BuildTrigger.java b/src/main/java/org/jboss/set/BuildTrigger.java index 331a47d..994cd23 100644 --- a/src/main/java/org/jboss/set/BuildTrigger.java +++ b/src/main/java/org/jboss/set/BuildTrigger.java @@ -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; @@ -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); } } } diff --git a/src/main/java/org/jboss/set/EmailService.java b/src/main/java/org/jboss/set/EmailService.java new file mode 100644 index 0000000..3a00c4c --- /dev/null +++ b/src/main/java/org/jboss/set/EmailService.java @@ -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 = "

Request to build " + payload.gitRepo + " in version " + payload.projectVersion + " " + + "was successfully sent to the productization team using the following parameters:

" + + "" + + "

Report any issues or share suggestions using our " + + "GitHub Repository.

" + + "

This is an automated Build Trigger message, please do not respond!

"; + + + 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); + } +} \ No newline at end of file diff --git a/src/main/java/org/jboss/set/exception/BuildTriggerException.java b/src/main/java/org/jboss/set/exception/BuildTriggerException.java new file mode 100644 index 0000000..42226ed --- /dev/null +++ b/src/main/java/org/jboss/set/exception/BuildTriggerException.java @@ -0,0 +1,7 @@ +package org.jboss.set.exception; + +public class BuildTriggerException extends RuntimeException { + public BuildTriggerException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index bcf0b38..61a6ae7 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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} @@ -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 +