A TDS Post Deploy Step for sending Slack notifications
using HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.Contracts; | |
using System; | |
using System.Collections.Specialized; | |
using System.ComponentModel; | |
using System.IO; | |
using System.Net; | |
using System.Web; | |
using System.Xml.Linq; | |
namespace MyProject.PostDeployActions | |
{ | |
[Serializable] | |
[Description("Sends a Slack notification message after a successful deployment.\nSpecify a 'webhookUrl' and 'payload' parameter in query string notation below.")] | |
public class SendSlackMessagePostDeployAction : IPostDeployAction | |
{ | |
public void RunPostDeployAction(XDocument deployedItems, IPostDeployActionHost host, string parameter) | |
{ | |
NameValueCollection parameters = HttpUtility.ParseQueryString(parameter ?? string.Empty); | |
Uri webhookUrl; | |
if (!Uri.TryCreate(parameters["webhookUrl"], UriKind.Absolute, out webhookUrl)) | |
{ | |
host.LogMessage("[SendSlackMessagePostDeployAction] 'webHookUrl' parameter is required."); | |
return; | |
} | |
string payload = parameters["payload"]; | |
if (string.IsNullOrWhiteSpace(payload)) | |
{ | |
host.LogMessage("[SendSlackMessagePostDeployAction] 'payload' parameter is required."); | |
return; | |
} | |
InvokeWebhookForUrl(webhookUrl, payload); | |
host.LogMessage("[SendSlackMessagePostDeployAction] Post deploy action completed successfully."); | |
} | |
private static void InvokeWebhookForUrl(Uri webhookUrl, string payload) | |
{ | |
var request = (HttpWebRequest)WebRequest.Create(webhookUrl); | |
request.ContentType = "application/json"; | |
request.Method = "POST"; | |
using (var streamWriter = new StreamWriter(request.GetRequestStream())) | |
{ | |
streamWriter.Write(payload); | |
} | |
var response = (HttpWebResponse)request.GetResponse(); | |
var stream = response.GetResponseStream(); | |
if (stream != null) | |
{ | |
using (var streamReader = new StreamReader(stream)) | |
{ | |
streamReader.ReadToEnd(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment