forked from cake-contrib/Cake.Recipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cake-contrib#972 from nils-a/feature/cake-contribG…
…H-510 (cake-contrib#510) post to mastodon on successful publishing
- Loading branch information
Showing
14 changed files
with
434 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,91 @@ | ||
using Cake.Email.Common; | ||
|
||
/////////////////////////////////////////////////////////////////////////////// | ||
// HELPER METHODS | ||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
public void SendEmail(string subject, string message, string recipient, string senderName, string senderAddress) | ||
public class EmailReporter : ISuccessReporter, IFailureReporter | ||
{ | ||
Information("Sending email..."); | ||
private EmailCredentials _credentials; | ||
|
||
public EmailReporter(EmailCredentials credentials) | ||
{ | ||
_credentials = credentials; | ||
} | ||
|
||
// The recipient parameter can contain a single email address or a comma/semi-colon separated list of email addresses | ||
var recipients = recipient | ||
.Split(new[] { ',', ';' }, StringSplitOptions.None) | ||
.Select(emailAddress => new MailAddress(emailAddress)) | ||
.ToArray(); | ||
public string Name { get; } = "EMail"; | ||
|
||
try | ||
public bool CanBeUsed | ||
{ | ||
var result = Email.SendEmail( | ||
senderName: senderName, | ||
senderAddress: senderAddress, | ||
recipients: recipients, | ||
subject: subject, | ||
htmlContent: message, | ||
textContent: null, | ||
attachments: null, | ||
settings: new EmailSettings | ||
{ | ||
SmtpHost = BuildParameters.Email.SmtpHost, | ||
Port = BuildParameters.Email.Port, | ||
EnableSsl = BuildParameters.Email.EnableSsl, | ||
Username = BuildParameters.Email.Username, | ||
Password = BuildParameters.Email.Password | ||
} | ||
); | ||
get => !string.IsNullOrEmpty(_credentials.SmtpHost) | ||
&& !string.IsNullOrEmpty(BuildParameters.EmailRecipient); | ||
} | ||
|
||
public bool ShouldBeUsed { get; set; } | ||
|
||
public void ReportSuccess(ICakeContext context, BuildVersion buildVersion) | ||
{ | ||
var subject = $"Continuous Integration Build of {BuildParameters.Title} completed successfully"; | ||
var messageArguments = BuildParameters.MessageArguments(buildVersion); | ||
var message = new StringBuilder(); | ||
message.AppendLine(string.Format(BuildParameters.StandardMessage, messageArguments) + "<br/>"); | ||
message.AppendLine("<br/>"); | ||
message.AppendLine($"<strong>Name</strong>: {BuildParameters.Title}<br/>"); | ||
message.AppendLine($"<strong>Version</strong>: {buildVersion.SemVersion}<br/>"); | ||
message.AppendLine($"<strong>Configuration</strong>: {BuildParameters.Configuration}<br/>"); | ||
message.AppendLine($"<strong>Target</strong>: {BuildParameters.Target}<br/>"); | ||
message.AppendLine($"<strong>Cake version</strong>: {buildVersion.CakeVersion}<br/>"); | ||
message.AppendLine($"<strong>Cake.Recipe version</strong>: {BuildMetaData.Version}<br/>"); | ||
|
||
SendEmail(context, subject, message.ToString(), BuildParameters.EmailRecipient, BuildParameters.EmailSenderName, BuildParameters.EmailSenderAddress); | ||
} | ||
|
||
public void ReportFailure(ICakeContext context, BuildVersion _, Exception thrownException) | ||
{ | ||
var subject = $"Continuous Integration Build of {BuildParameters.Title} failed"; | ||
var message = thrownException.ToString().Replace(System.Environment.NewLine, "<br/>"); | ||
|
||
SendEmail(context, subject, message, BuildParameters.EmailRecipient, BuildParameters.EmailSenderName, BuildParameters.EmailSenderAddress); | ||
} | ||
|
||
if (result.Ok) | ||
private void SendEmail(ICakeContext context, string subject, string message, string recipient, string senderName, string senderAddress) | ||
{ | ||
context.Information("Sending email..."); | ||
|
||
// The recipient parameter can contain a single email address or a comma/semi-colon separated list of email addresses | ||
var recipients = recipient | ||
.Split(new[] { ',', ';' }, StringSplitOptions.None) | ||
.Select(emailAddress => new MailAddress(emailAddress)) | ||
.ToArray(); | ||
|
||
try | ||
{ | ||
Information("Email successfully sent"); | ||
var result = context.Email().SendEmail( | ||
senderName: senderName, | ||
senderAddress: senderAddress, | ||
recipients: recipients, | ||
subject: subject, | ||
htmlContent: message, | ||
textContent: null, | ||
attachments: null, | ||
settings: new EmailSettings | ||
{ | ||
SmtpHost = _credentials.SmtpHost, | ||
Port = _credentials.Port, | ||
EnableSsl = _credentials.EnableSsl, | ||
Username = _credentials.Username, | ||
Password = _credentials.Password | ||
} | ||
); | ||
|
||
if (result.Ok) | ||
{ | ||
context.Information("Email successfully sent"); | ||
} | ||
else | ||
{ | ||
context.Error("Failed to send email: {0}", result.Error); | ||
} | ||
} | ||
else | ||
catch(Exception ex) | ||
{ | ||
Error("Failed to send email: {0}", result.Error); | ||
context.Error("{0}", ex); | ||
} | ||
} | ||
catch(Exception ex) | ||
{ | ||
Error("{0}", ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
public class MastodonReporter : SuccessReporter | ||
{ | ||
private MastodonCredentials _credentials; | ||
private string _messageTemplate; | ||
|
||
public MastodonReporter(MastodonCredentials credentials, string messageTemplate) | ||
{ | ||
_credentials = credentials; | ||
_messageTemplate = messageTemplate; | ||
} | ||
|
||
public override string Name { get; } = "Mastodon"; | ||
|
||
public override bool CanBeUsed | ||
{ | ||
get => !string.IsNullOrEmpty(_credentials.AccessToken) && | ||
!string.IsNullOrEmpty(_credentials.InstanceUrl); | ||
} | ||
|
||
|
||
public override void ReportSuccess(ICakeContext context, BuildVersion buildVersion) | ||
{ | ||
try | ||
{ | ||
context.Information("Sending message to Mastodon..."); | ||
|
||
var messageArguments = BuildParameters.MessageArguments(buildVersion); | ||
var message = string.Format(_messageTemplate, messageArguments); | ||
var idempotencyKey = Guid.NewGuid().ToString("d"); | ||
|
||
context.MastodonSendToot(_credentials.InstanceUrl, | ||
_credentials.AccessToken, | ||
message, | ||
idempotencyKey); | ||
|
||
context.Information("Message successfully sent."); | ||
} | ||
catch(Exception ex) | ||
{ | ||
context.Error("{0}", ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,39 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// HELPER METHODS | ||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
public void SendMessageToMicrosoftTeams(string message) | ||
public class MsTeamsReporter : SuccessReporter | ||
{ | ||
try | ||
private MicrosoftTeamsCredentials _credentials; | ||
private string _messageTemplate; | ||
|
||
public MsTeamsReporter(MicrosoftTeamsCredentials credentials, string messageTemplate) | ||
{ | ||
Information("Sending message to Microsoft Teams..."); | ||
_credentials = credentials; | ||
_messageTemplate = messageTemplate; | ||
} | ||
|
||
MicrosoftTeamsPostMessage(message, | ||
new MicrosoftTeamsSettings { | ||
IncomingWebhookUrl = BuildParameters.MicrosoftTeams.WebHookUrl | ||
}); | ||
public override string Name { get; } = "MicrosoftTeams"; | ||
|
||
public override bool CanBeUsed | ||
{ | ||
get => !string.IsNullOrEmpty(_credentials.WebHookUrl); | ||
} | ||
catch(Exception ex) | ||
|
||
public override void ReportSuccess(ICakeContext context, BuildVersion buildVersion) | ||
{ | ||
Error("{0}", ex); | ||
try | ||
{ | ||
context.Information("Sending message to Microsoft Teams..."); | ||
|
||
var messageArguments = BuildParameters.MessageArguments(buildVersion); | ||
var message = string.Format(_messageTemplate, messageArguments); | ||
|
||
context.MicrosoftTeamsPostMessage(message, | ||
new MicrosoftTeamsSettings { | ||
IncomingWebhookUrl = _credentials.WebHookUrl | ||
}); | ||
|
||
} | ||
catch(Exception ex) | ||
{ | ||
context.Error("{0}", ex); | ||
} | ||
} | ||
} |
Oops, something went wrong.