Skip to content

Commit

Permalink
Split SMTP creation and deletion notifications (#2120)
Browse files Browse the repository at this point in the history
  • Loading branch information
raikbitters authored Dec 9, 2024
1 parent 70f4b98 commit ab76b4f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/java-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,4 @@ jobs:
call-java-checks:
name: Call Java Checks
uses: reportportal/.github/.github/workflows/java-checks.yaml@main
with:
runs-on: 'rp-beta-runner-set'
drill-agent-enabled: 'true'
secrets: inherit
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import com.epam.ta.reportportal.dao.IntegrationRepository;
import com.epam.ta.reportportal.entity.EmailSettingsEnum;
import com.epam.ta.reportportal.entity.integration.Integration;
import com.epam.ta.reportportal.util.email.EmailService;
import com.epam.ta.reportportal.entity.integration.IntegrationType;
import com.epam.ta.reportportal.model.integration.IntegrationRQ;
import com.epam.ta.reportportal.util.email.MailServiceFactory;
import com.google.common.collect.Maps;
import com.mchange.lang.IntegerUtils;
Expand Down Expand Up @@ -60,9 +61,9 @@ public class EmailServerIntegrationService extends BasicIntegrationServiceImpl {
* Constructs an EmailServerIntegrationService with the specified dependencies.
*
* @param integrationRepository the repository for integration entities
* @param pluginBox the plugin box for managing plugins
* @param basicTextEncryptor the text encryptor for encrypting sensitive data
* @param emailServiceFactory the factory for creating email services
* @param pluginBox the plugin box for managing plugins
* @param basicTextEncryptor the text encryptor for encrypting sensitive data
* @param emailServiceFactory the factory for creating email services
*/
public EmailServerIntegrationService(
IntegrationRepository integrationRepository,
Expand Down Expand Up @@ -160,31 +161,56 @@ public Map<String, Object> retrieveUpdatedParams(
}

@Override
public boolean checkConnection(Integration integration) {
Optional<EmailService> emailService = emailServiceFactory.getEmailService(integration);
final boolean isIntegrationCreated = integration.getId() == null;
public Integration createIntegration(IntegrationRQ integrationRq,
IntegrationType integrationType) {
Integration integration = super.createIntegration(integrationRq, integrationType);
sendConnectionTestEmail(integration, true);
return integration;
}

@Override
public Integration updateIntegration(Integration integration, IntegrationRQ integrationRq) {
Integration updatedIntegration = super.updateIntegration(integration, integrationRq);
sendConnectionTestEmail(updatedIntegration, false);
return updatedIntegration;
}

if (emailService.isPresent()) {
@Override
public boolean checkConnection(Integration integration) {
return emailServiceFactory.getEmailService(integration).map(emailService -> {
try {
emailService.get().testConnection();
if (BooleanUtils.toBoolean(
EmailSettingsEnum.AUTH_ENABLED
.getAttribute(integration.getParams().getParams())
.orElse("false"))) {
emailService.get().sendConnectionTestEmail(isIntegrationCreated);
}
emailService.testConnection();
} catch (MessagingException ex) {
LOGGER.error("Cannot send email to user", ex);
LOGGER.error("Connection to email server failed", ex);
fail()
.withError(
EMAIL_CONFIGURATION_IS_INCORRECT,
"Email configuration is incorrect. Please, check your configuration. "
+ ex.getMessage());
return false;
}
} else {
return false;
}
return true;
}).orElse(false);
}

return true;
private void sendConnectionTestEmail(Integration integration, boolean isNewIntegration) {
boolean isAuthEnabled = BooleanUtils.toBoolean(
EmailSettingsEnum.AUTH_ENABLED
.getAttribute(integration.getParams().getParams())
.orElse("false"));
emailServiceFactory.getEmailService(integration).ifPresent(emailService -> {
if (isAuthEnabled) {
try {
emailService.sendConnectionTestEmail(isNewIntegration);
} catch (MessagingException ex) {
LOGGER.error("Cannot send email to user", ex);
fail()
.withError(
EMAIL_CONFIGURATION_IS_INCORRECT,
"Email configuration is incorrect. Please, check your configuration. "
+ ex.getMessage());
}
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,15 @@ public void sendCreateUserConfirmationEmail(CreateUserRQFull req, String basicUr
* Send email to user with connection test result. If email address is not valid, exception will
* be thrown.
*
* @param isCreated - flag that indicates if integration was created or updated. Used to determine
* email subject.
* @param isNewIntegration - flag that indicates if integration was created or updated.
* @throws AddressException - if email address is not valid or not exist.
*/
public void sendConnectionTestEmail(boolean isCreated)
public void sendConnectionTestEmail(boolean isNewIntegration)
throws AddressException {
InternetAddress sender =
getFrom().orElseThrow(() -> new AddressException("Sender email address is not exist"));
String subject =
isCreated ? "Email server integration creation" : "Email server integration updated";
isNewIntegration ? "Email server integration creation" : "Email server integration updated";
MimeMessagePreparator preparator =
mimeMessage -> {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "utf-8");
Expand Down

0 comments on commit ab76b4f

Please sign in to comment.