Skip to content

Instantly share code, notes, and snippets.

@dthunziker
Last active August 9, 2016 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dthunziker/1b6e855873325c5c6e79ba4bcf78f354 to your computer and use it in GitHub Desktop.
Save dthunziker/1b6e855873325c5c6e79ba4bcf78f354 to your computer and use it in GitHub Desktop.
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