Search in sources :

Example 71 with LinkableItem

use of com.synopsys.integration.alert.common.message.model.LinkableItem in project hub-alert by blackducksoftware.

the class ProjectMessageToMessageContentGroupConversionUtils method toMessageContentGroup.

public static MessageContentGroup toMessageContentGroup(ProjectMessage projectMessage) {
    ProviderMessageContent.Builder providerMessageContentBuilder = new ProviderMessageContent.Builder();
    LinkableItem provider = projectMessage.getProvider();
    providerMessageContentBuilder.applyProvider(provider.getLabel(), -1L, provider.getValue(), provider.getUrl().orElse(null));
    LinkableItem project = projectMessage.getProject();
    providerMessageContentBuilder.applyTopic(project.getLabel(), project.getValue(), project.getUrl().orElse(null));
    Optional<LinkableItem> optionalProjectVersion = projectMessage.getProjectVersion();
    if (optionalProjectVersion.isPresent()) {
        LinkableItem projectVersion = optionalProjectVersion.get();
        providerMessageContentBuilder.applySubTopic(projectVersion.getLabel(), projectVersion.getValue(), projectVersion.getUrl().orElse(null));
    }
    projectMessage.getOperation().map(ProjectMessageToMessageContentGroupConversionUtils::convertToItemOperation).ifPresent(providerMessageContentBuilder::applyAction);
    MessageContentGroup messageContentGroup = new MessageContentGroup();
    List<ComponentItem> componentItems = new LinkedList<>();
    for (BomComponentDetails bomComponent : projectMessage.getBomComponents()) {
        List<ComponentItem> bomComponentItems = convertToComponentItems(bomComponent);
        componentItems.addAll(bomComponentItems);
    }
    providerMessageContentBuilder.applyAllComponentItems(componentItems);
    try {
        ProviderMessageContent providerMessageContent = providerMessageContentBuilder.build();
        messageContentGroup.add(providerMessageContent);
    } catch (AlertException e) {
    // Ignore for feature parity
    }
    return messageContentGroup;
}
Also used : ComponentItem(com.synopsys.integration.alert.channel.email.attachment.compatibility.ComponentItem) ProviderMessageContent(com.synopsys.integration.alert.channel.email.attachment.compatibility.ProviderMessageContent) LinkableItem(com.synopsys.integration.alert.common.message.model.LinkableItem) MessageContentGroup(com.synopsys.integration.alert.channel.email.attachment.compatibility.MessageContentGroup) LinkedList(java.util.LinkedList) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) BomComponentDetails(com.synopsys.integration.alert.processor.api.extract.model.project.BomComponentDetails)

Example 72 with LinkableItem

use of com.synopsys.integration.alert.common.message.model.LinkableItem in project hub-alert by blackducksoftware.

the class ProjectMessageToMessageContentGroupConversionUtils method convertToComponentItems.

private static List<ComponentItem> convertToComponentItems(BomComponentDetails bomComponent) {
    List<ComponentItem> componentItems = new LinkedList<>();
    for (ComponentConcern componentConcern : bomComponent.getComponentConcerns()) {
        ComponentItem.Builder componentItemBuilder = new ComponentItem.Builder();
        componentItemBuilder.applyOperation(componentConcern.getOperation());
        ComponentConcernType type = componentConcern.getType();
        String category = StringUtils.capitalize(StringUtils.lowerCase(type.name()));
        componentItemBuilder.applyCategory(category);
        componentItemBuilder.applyComponentData(bomComponent.getComponent());
        bomComponent.getComponentVersion().ifPresent(componentItemBuilder::applySubComponent);
        String categoryItemLabel = convertToCategoryLabel(type);
        LinkableItem categoryItem = new LinkableItem(categoryItemLabel, componentConcern.getName(), componentConcern.getUrl().orElse(null));
        componentItemBuilder.applyCategoryItem(categoryItem);
        String severity = componentConcern.getSeverity().name();
        componentItemBuilder.applyCategoryGroupingAttribute("Severity", severity);
        boolean collapseOnCategory = ComponentConcernType.VULNERABILITY.equals(type);
        componentItemBuilder.applyCollapseOnCategory(collapseOnCategory);
        List<LinkableItem> componentAttributes = new LinkedList<>();
        componentAttributes.add(bomComponent.getLicense());
        LinkableItem usageItem = new LinkableItem("Usage", bomComponent.getUsage());
        componentAttributes.add(usageItem);
        ComponentUpgradeGuidance upgradeGuidance = bomComponent.getComponentUpgradeGuidance();
        upgradeGuidance.getLongTermUpgradeGuidance().ifPresent(componentAttributes::add);
        upgradeGuidance.getShortTermUpgradeGuidance().ifPresent(componentAttributes::add);
        componentAttributes.addAll(bomComponent.getAdditionalAttributes());
        componentItemBuilder.applyAllComponentAttributes(componentAttributes);
        try {
            componentItems.add(componentItemBuilder.build());
        } catch (AlertException e) {
        // Ignored for feature parity
        }
    }
    return componentItems;
}
Also used : ComponentItem(com.synopsys.integration.alert.channel.email.attachment.compatibility.ComponentItem) ComponentConcernType(com.synopsys.integration.alert.processor.api.extract.model.project.ComponentConcernType) LinkableItem(com.synopsys.integration.alert.common.message.model.LinkableItem) ComponentConcern(com.synopsys.integration.alert.processor.api.extract.model.project.ComponentConcern) LinkedList(java.util.LinkedList) ComponentUpgradeGuidance(com.synopsys.integration.alert.processor.api.extract.model.project.ComponentUpgradeGuidance) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 73 with LinkableItem

use of com.synopsys.integration.alert.common.message.model.LinkableItem in project hub-alert by blackducksoftware.

the class EmailChannelMessageConverter method createSubjectLine.

private String createSubjectLine(EmailJobDetailsModel distributionDetails, ProjectMessage projectMessage) {
    LinkableItem project = projectMessage.getProject();
    String subjectLinePrefix = createSubjectLinePrefix(distributionDetails);
    String subjectLine = String.format("%s%s | %s", subjectLinePrefix, projectMessage.getMessageReason().name(), project.getValue());
    Optional<String> projectVersionName = projectMessage.getProjectVersion().map(LinkableItem::getValue);
    if (projectVersionName.isPresent()) {
        subjectLine += String.format("[%s]", projectVersionName.get());
    }
    return StringUtils.abbreviate(subjectLine, SUBJECT_LINE_MAX_LENGTH);
}
Also used : LinkableItem(com.synopsys.integration.alert.common.message.model.LinkableItem)

Example 74 with LinkableItem

use of com.synopsys.integration.alert.common.message.model.LinkableItem in project hub-alert by blackducksoftware.

the class EmailChannelMessageConverter method convertSimpleMessageToChannelMessages.

@Override
protected List<EmailChannelMessageModel> convertSimpleMessageToChannelMessages(EmailJobDetailsModel distributionDetails, SimpleMessage simpleMessage, List<String> messageChunks) {
    String subjectLinePrefix = createSubjectLinePrefix(distributionDetails);
    String subjectLine = String.format("%s%s", subjectLinePrefix, simpleMessage.getSummary());
    subjectLine = StringUtils.abbreviate(subjectLine, SUBJECT_LINE_MAX_LENGTH);
    String messageContent = StringUtils.join(messageChunks, "");
    LinkableItem provider = simpleMessage.getProvider();
    String providerName = provider.getValue();
    String providerUrl = provider.getUrl().orElse("#");
    Optional<ProjectMessage> optionalSource = simpleMessage.getSource();
    EmailChannelMessageModel model;
    if (optionalSource.isPresent()) {
        model = EmailChannelMessageModel.simpleProject(subjectLine, messageContent, providerName, providerUrl, optionalSource.get());
    } else {
        model = EmailChannelMessageModel.simple(subjectLine, messageContent, providerName, providerUrl);
    }
    return List.of(model);
}
Also used : LinkableItem(com.synopsys.integration.alert.common.message.model.LinkableItem) ProjectMessage(com.synopsys.integration.alert.processor.api.extract.model.project.ProjectMessage)

Example 75 with LinkableItem

use of com.synopsys.integration.alert.common.message.model.LinkableItem in project hub-alert by blackducksoftware.

the class EmailChannelMessageSender method sendMessages.

@Override
public MessageResult sendMessages(EmailJobDetailsModel emailJobDetails, List<EmailChannelMessageModel> emailMessages) throws AlertException {
    // Validation
    ValidatedEmailAddresses validatedAdditionalEmailAddresses;
    UUID jobId = emailJobDetails.getJobId();
    if (null != jobId) {
        validatedAdditionalEmailAddresses = emailAddressValidator.validate(jobId, emailJobDetails.getAdditionalEmailAddresses());
    } else {
        validatedAdditionalEmailAddresses = new ValidatedEmailAddresses(new HashSet<>(emailJobDetails.getAdditionalEmailAddresses()), Set.of());
    }
    Set<String> invalidEmailAddresses = validatedAdditionalEmailAddresses.getInvalidEmailAddresses();
    if (!invalidEmailAddresses.isEmpty()) {
        emailJobDetails = new EmailJobDetailsModel(emailJobDetails.getJobId(), emailJobDetails.getSubjectLine().orElse(null), emailJobDetails.isProjectOwnerOnly(), emailJobDetails.isAdditionalEmailAddressesOnly(), emailJobDetails.getAttachmentFileType(), new ArrayList<>(validatedAdditionalEmailAddresses.getValidEmailAddresses()));
    }
    // Distribution
    EmailGlobalConfigModel emailServerConfiguration = emailGlobalConfigAccessor.getConfiguration().orElseThrow(() -> new AlertConfigurationException("ERROR: Missing Email global config."));
    SmtpConfig smtpConfig = SmtpConfig.builder().setJavamailProperties(javamailPropertiesFactory.createJavaMailProperties(emailServerConfiguration)).setSmtpFrom(emailServerConfiguration.getSmtpFrom().orElse(null)).setSmtpHost(emailServerConfiguration.getSmtpHost().orElse(null)).setSmtpPort(emailServerConfiguration.getSmtpPort().orElse(-1)).setSmtpAuth(emailServerConfiguration.getSmtpAuth().orElse(false)).setSmtpUsername(emailServerConfiguration.getSmtpUsername().orElse(null)).setSmtpPassword(emailServerConfiguration.getSmtpPassword().orElse(null)).build();
    int totalEmailsSent = 0;
    for (EmailChannelMessageModel message : emailMessages) {
        Set<String> projectHrefs = message.getSource().map(ProjectMessage::getProject).flatMap(LinkableItem::getUrl).map(Set::of).orElse(Set.of());
        Set<String> gatheredEmailAddresses = emailAddressGatherer.gatherEmailAddresses(emailJobDetails, projectHrefs);
        if (gatheredEmailAddresses.isEmpty()) {
            if (invalidEmailAddresses.isEmpty()) {
                throw new AlertException("Could not determine what email addresses to send this content to");
            } else {
                String invalidEmailAddressesString = StringUtils.join(invalidEmailAddresses, ", ");
                throw new AlertException(String.format("No valid email addresses to send this content to. The following email addresses were invalid: %s", invalidEmailAddressesString));
            }
        }
        EmailTarget emailTarget = emailChannelMessagingService.createTarget(message, gatheredEmailAddresses);
        Optional<ProjectMessage> optionalProjectMessage = message.getSource();
        if (optionalProjectMessage.isPresent()) {
            EmailAttachmentFormat attachmentFormat = EmailAttachmentFormat.getValueSafely(emailJobDetails.getAttachmentFileType());
            emailChannelMessagingService.sendMessageWithAttachedProjectMessage(smtpConfig, emailTarget, optionalProjectMessage.get(), attachmentFormat);
        } else {
            emailChannelMessagingService.sendMessage(smtpConfig, emailTarget);
        }
        totalEmailsSent += emailTarget.getEmailAddresses().size();
    }
    // Reporting
    if (!invalidEmailAddresses.isEmpty()) {
        String invalidEmailAddressesString = StringUtils.join(invalidEmailAddresses, ", ");
        String errorMessage = String.format("No emails were sent to the following recipients because they were invalid: %s", invalidEmailAddressesString);
        AlertFieldStatus errorStatus = new AlertFieldStatus(EmailDescriptor.KEY_EMAIL_ADDITIONAL_ADDRESSES, FieldStatusSeverity.ERROR, errorMessage);
        return new MessageResult(errorMessage, List.of(errorStatus));
    }
    return new MessageResult(String.format("Successfully sent %d email(s)", totalEmailsSent));
}
Also used : EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) SmtpConfig(com.synopsys.integration.alert.service.email.SmtpConfig) LinkableItem(com.synopsys.integration.alert.common.message.model.LinkableItem) ArrayList(java.util.ArrayList) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) EmailAttachmentFormat(com.synopsys.integration.alert.channel.email.attachment.EmailAttachmentFormat) ProjectMessage(com.synopsys.integration.alert.processor.api.extract.model.project.ProjectMessage) EmailJobDetailsModel(com.synopsys.integration.alert.common.persistence.model.job.details.EmailJobDetailsModel) EmailTarget(com.synopsys.integration.alert.service.email.EmailTarget) UUID(java.util.UUID) ValidatedEmailAddresses(com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) HashSet(java.util.HashSet) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)

Aggregations

LinkableItem (com.synopsys.integration.alert.common.message.model.LinkableItem)94 Test (org.junit.jupiter.api.Test)39 ProviderDetails (com.synopsys.integration.alert.processor.api.extract.model.ProviderDetails)26 ProjectMessage (com.synopsys.integration.alert.processor.api.extract.model.project.ProjectMessage)22 IssueBomComponentDetails (com.synopsys.integration.alert.api.channel.issue.model.IssueBomComponentDetails)18 ProjectIssueModel (com.synopsys.integration.alert.api.channel.issue.model.ProjectIssueModel)15 BomComponentDetails (com.synopsys.integration.alert.processor.api.extract.model.project.BomComponentDetails)13 SimpleMessage (com.synopsys.integration.alert.processor.api.extract.model.SimpleMessage)11 ProjectOperation (com.synopsys.integration.alert.processor.api.extract.model.project.ProjectOperation)11 ArrayList (java.util.ArrayList)11 LinkedList (java.util.LinkedList)11 IssuePolicyDetails (com.synopsys.integration.alert.api.channel.issue.model.IssuePolicyDetails)9 ProviderMessageHolder (com.synopsys.integration.alert.processor.api.extract.model.ProviderMessageHolder)9 List (java.util.List)9 ComponentVulnerabilities (com.synopsys.integration.alert.processor.api.extract.model.project.ComponentVulnerabilities)8 Optional (java.util.Optional)7 IssueVulnerabilityDetails (com.synopsys.integration.alert.api.channel.issue.model.IssueVulnerabilityDetails)6 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)6 ComponentConcern (com.synopsys.integration.alert.processor.api.extract.model.project.ComponentConcern)6 ComponentConcernType (com.synopsys.integration.alert.processor.api.extract.model.project.ComponentConcernType)6