Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
/ NuGet.Jobs Public archive

Integrate NuGet.SupportRequests.Notifications with NuGet.Services.Messaging.Email #606

Merged
merged 5 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ namespace NuGet.SupportRequests.Notifications
public class InitializationConfiguration
{
/// <summary>
/// Obsolete: replace with IcM configuration
/// Connection string to email publisher job's service bus.
/// </summary>
public string PagerDutyAccountName { get; set; }
public string EmailPublisherConnectionString { get; set; }

/// <summary>
/// Obsolete: replace with IcM configuration
/// Name of the email publisher job's service bus topic.
/// </summary>
public string PagerDutyApiKey { get; set; }
public string EmailPublisherTopicName { get; set; }

/// <summary>
/// SMTP configuration.
/// Obsolete: replace with incident API configuration
/// </summary>
public string SmtpUri { get; set; }
public string PagerDutyAccountName { get; set; }

/// <summary>
/// Obsolete: replace with incident API configuration
/// </summary>
public string PagerDutyApiKey { get; set; }

/// <summary>
/// Email address to which the weekly report is sent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="PagerDutyClient.cs" />
<Compile Include="ScheduledTaskFactory.cs" />
<Compile Include="SqlQuery.cs" />
<Compile Include="SupportRequestNotificationEmailBuilder.cs" />
<Compile Include="Templates\NotificationTemplateProvider.cs" />
<Compile Include="Templates\HtmlSnippets.cs" />
<Compile Include="Templates\HtmlPlaceholders.cs" />
Expand Down Expand Up @@ -106,12 +107,12 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions">
<Version>1.0.0</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>9.0.1</Version>
</PackageReference>
<PackageReference Include="NuGet.Services.Messaging.Email">
<Version>2.32.0-master-2167353</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,26 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NuGet.Jobs;
using NuGet.Services.Messaging.Email;

namespace NuGet.SupportRequests.Notifications.Services
{
internal class MessagingService
{
private readonly MailAddress _fromAddress;
private readonly ILogger<MessagingService> _logger;
private readonly string _smtpUri;
private SmtpClient _smtpClient;
private readonly IMessageService _messageService;

private const string _noreplyAddress = "NuGet Gallery <[email protected]>";

public MessagingService(ILoggerFactory loggerFactory, string smtpUri)
public MessagingService(IMessageService messageService, ILogger<MessagingService> logger)
{
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}

if (smtpUri == null)
{
throw new ArgumentNullException(nameof(smtpUri));
}

_logger = loggerFactory.CreateLogger<MessagingService>();
_fromAddress = new MailAddress(_noreplyAddress);
_smtpUri = smtpUri;
_messageService = messageService ?? throw new ArgumentNullException(nameof(messageService));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

internal void SendNotification(
internal async Task SendNotification(
string subject,
string body,
string htmlBody,
DateTime referenceTime,
string targetEmailAddress)
{
Expand All @@ -46,67 +30,23 @@ internal void SendNotification(
throw new ArgumentException(nameof(subject));
}

if (string.IsNullOrEmpty(body))
if (string.IsNullOrEmpty(htmlBody))
{
throw new ArgumentException(nameof(body));
throw new ArgumentException(nameof(htmlBody));
}

if (string.IsNullOrEmpty(targetEmailAddress))
{
throw new ArgumentException(nameof(targetEmailAddress));
}

var targetAddress = new MailAddress(targetEmailAddress);
using (var mailMessage = new MailMessage())
{
mailMessage.Subject = subject;
mailMessage.From = _fromAddress;
mailMessage.ReplyToList.Add(_fromAddress);
mailMessage.Body = body;
mailMessage.To.Add(targetAddress);

SendMessage(mailMessage);
}
var builder = new SupportRequestNotificationEmailBuilder(subject, htmlBody, targetEmailAddress);
await _messageService.SendMessageAsync(builder);

_logger.LogInformation(
"Successfully sent notification '{NotificationType}' for reference time '{ReferenceTimeUtc}'",
subject,
referenceTime.ToShortDateString());
}

private void SendMessage(MailMessage mailMessage)
{
var smtpClient = GetOrCreateSmtpClient();

var alternateHtmlView = AlternateView.CreateAlternateViewFromString(mailMessage.Body, null, "text/html");
mailMessage.AlternateViews.Add(alternateHtmlView);

smtpClient.Send(mailMessage);
}

private SmtpClient GetOrCreateSmtpClient()
{
if (_smtpClient != null)
{
return _smtpClient;
}

var smtpUri = new SmtpUri(new Uri(_smtpUri));
_smtpClient = new SmtpClient();
_smtpClient.Host = smtpUri.Host;
_smtpClient.Port = smtpUri.Port;
_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
_smtpClient.EnableSsl = smtpUri.Secure;

if (!string.IsNullOrEmpty(smtpUri.UserName))
{
_smtpClient.UseDefaultCredentials = false;
_smtpClient.Credentials = new NetworkCredential(
smtpUri.UserName,
smtpUri.Password);
}

return _smtpClient;
}
}
}
3 changes: 2 additions & 1 deletion src/NuGet.SupportRequests.Notifications/Settings/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"Initialization": {
"PagerDutyAccountName": "nuget",
"PagerDutyApiKey": "$$Dev-PagerDuty-ApiKey$$",
"SmtpUri": "#{Jobs.supportrequests.notifications.SmtpUri}",
"EmailPublisherConnectionString": "Endpoint=sb://nugetdev.servicebus.windows.net/;SharedAccessKeyName=enqueuer;SharedAccessKey=$$Dev-ServiceBus-SharedAccessKey-EmailPublisher-Enqueuer$$",
"EmailPublisherTopicName": "email-publisher",
"TargetEmailAddress": "#{Jobs.supportrequests.notifications.weeklysummarynotification.TargetEmailAddress}"
},

Expand Down
3 changes: 2 additions & 1 deletion src/NuGet.SupportRequests.Notifications/Settings/int.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"Initialization": {
"PagerDutyAccountName": "nuget",
"PagerDutyApiKey": "$$Int-PagerDuty-ApiKey$$",
"SmtpUri": "#{Jobs.supportrequests.notifications.SmtpUri}",
"EmailPublisherConnectionString": "Endpoint=sb://nugetint.servicebus.windows.net/;SharedAccessKeyName=enqueuer;SharedAccessKey=$$Int-ServiceBus-SharedAccessKey-EmailPublisher-Enqueuer$$",
"EmailPublisherTopicName": "email-publisher",
"TargetEmailAddress": "#{Jobs.supportrequests.notifications.weeklysummarynotification.TargetEmailAddress}"
},

Expand Down
3 changes: 2 additions & 1 deletion src/NuGet.SupportRequests.Notifications/Settings/prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"Initialization": {
"PagerDutyAccountName": "nuget",
"PagerDutyApiKey": "$$Prod-PagerDuty-ApiKey$$",
"SmtpUri": "#{Jobs.supportrequests.notifications.SmtpUri}",
"EmailPublisherConnectionString": "Endpoint=sb://nugetprod.servicebus.windows.net/;SharedAccessKeyName=enqueuer;SharedAccessKey=$$Prod-ServiceBus-SharedAccessKey-EmailPublisher-Enqueuer$$",
"EmailPublisherTopicName": "email-publisher",
"TargetEmailAddress": "#{Jobs.supportrequests.notifications.weeklysummarynotification.TargetEmailAddress}"
},

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Net.Mail;
using NuGet.Services.Messaging.Email;

namespace NuGet.SupportRequests.Notifications
{
public class SupportRequestNotificationEmailBuilder : IEmailBuilder
{
public SupportRequestNotificationEmailBuilder(
string subject,
string htmlBody,
string targetEmailAddress)
{
_subject = subject;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null checks?

_htmlBody = htmlBody;
_targetAddress = new MailAddress(targetEmailAddress);
}

public static MailAddress NoReplyAddress = new MailAddress("NuGet Gallery <[email protected]>");
public MailAddress Sender => NoReplyAddress;

private MailAddress _targetAddress;
public IEmailRecipients GetRecipients()
{
return new EmailRecipients(to: new[] { _targetAddress });
}

private readonly string _subject;
public string GetSubject()
{
return _subject;
}

private readonly string _htmlBody;
public string GetBody(EmailFormat format)
{
// We only create an HTML version of the body for now.
// Ideally we would create a plaintext fallback as well, and return a different version of the body based on the format.
// For now, however, returning an HTML body as the plaintext fallback is better than having a completely empty plaintext fallback.
return _htmlBody;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected override async Task<OnCallDailyNotification> BuildNotification(
return new OnCallDailyNotification(referenceTime, unresolvedIssues, targetEmailAddress);
}

protected override string BuildNotificationBody(string template, OnCallDailyNotification notification)
protected override string BuildNotificationHtmlBody(string template, OnCallDailyNotification notification)
{
var result = template;
var referenceTimeLabel = notification.ReferenceTime.ToString("dd/MM/yy");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Data.SqlClient;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NuGet.Services.Messaging;
using NuGet.Services.Messaging.Email;
using NuGet.Services.ServiceBus;
using NuGet.SupportRequests.Notifications.Notifications;
using NuGet.SupportRequests.Notifications.Services;
using NuGet.SupportRequests.Notifications.Templates;
Expand Down Expand Up @@ -33,25 +36,29 @@ protected SupportRequestsNotificationScheduledTask(
throw new ArgumentNullException(nameof(loggerFactory));
}

_messagingService = new MessagingService(loggerFactory, configuration.SmtpUri);
var serializer = new ServiceBusMessageSerializer();
var topicClient = new TopicClientWrapper(configuration.EmailPublisherConnectionString, configuration.EmailPublisherTopicName);
var enqueuer = new EmailMessageEnqueuer(topicClient, serializer, loggerFactory.CreateLogger<EmailMessageEnqueuer>());
var messageService = new AsynchronousEmailMessageService(enqueuer);
_messagingService = new MessagingService(messageService, loggerFactory.CreateLogger<MessagingService>());

_supportRequestRepository = new SupportRequestRepository(loggerFactory, openSupportRequestSqlConnectionAsync);
}

protected abstract Task<TNotification> BuildNotification(SupportRequestRepository supportRequestRepository, DateTime referenceTime);

protected abstract string BuildNotificationBody(string template, TNotification notification);
protected abstract string BuildNotificationHtmlBody(string template, TNotification notification);

public async Task RunAsync()
{
var referenceTime = DateTime.UtcNow.Date;
var notification = await BuildNotification(_supportRequestRepository, referenceTime);
var template = NotificationTemplateProvider.Get(notification.TemplateName);
var body = BuildNotificationBody(template, notification);
var htmlBody = BuildNotificationHtmlBody(template, notification);

_messagingService.SendNotification(
await _messagingService.SendNotification(
notification.Subject,
body,
htmlBody,
referenceTime,
notification.TargetEmailAddress);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected override async Task<WeeklySummaryNotification> BuildNotification(
topSupportRequestReasonsLastWeek);
}

protected override string BuildNotificationBody(string template, WeeklySummaryNotification notification)
protected override string BuildNotificationHtmlBody(string template, WeeklySummaryNotification notification)
{

var newIssuesTrendPct = notification.GetNewIssuesTrendPct();
Expand Down