From d7bd711d9a983bbad78e5267fd314b517c925375 Mon Sep 17 00:00:00 2001 From: Jeavon Date: Mon, 26 Jun 2017 15:25:11 +0100 Subject: [PATCH] Switch Health Check Slack notification to use Slack Attachments --- .../SlackNotificationMethod.cs | 100 +++++++++++++++++- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Web/HealthCheck/NotificationMethods/SlackNotificationMethod.cs b/src/Umbraco.Web/HealthCheck/NotificationMethods/SlackNotificationMethod.cs index fc43ac37b8..6ca7ec1b0c 100644 --- a/src/Umbraco.Web/HealthCheck/NotificationMethods/SlackNotificationMethod.cs +++ b/src/Umbraco.Web/HealthCheck/NotificationMethods/SlackNotificationMethod.cs @@ -1,4 +1,7 @@ -using System.Threading.Tasks; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; using Slack.Webhooks; using Umbraco.Core.Configuration.HealthChecks; @@ -49,14 +52,107 @@ namespace Umbraco.Web.HealthCheck.NotificationMethods // todo construct Slack Message using Slack Attachments + var attachments = new List(); + + // adding strings here so that they can be localised + + var checkStringPural = "Checks"; + var checkString = "Check"; + + if (successResults.Any()) + { + var passedTitle = string.Format("{0} Health {1} Passed", successResults.Count, successResults.Count > 1 ? checkStringPural : checkString); + + var successAttachment = GenerateAttachment(successResults, "good", passedTitle); + attachments.Add(successAttachment); + } + + if (warnResults.Any()) + { + var warnTitle = string.Format("{0} Health {1} Passed with Warnings", warnResults.Count, warnResults.Count > 1 ? checkStringPural : checkString); + + var warnAttachment = GenerateAttachment(warnResults, "warning", warnTitle); + attachments.Add(warnAttachment); + } + + if (infoResults.Any()) + { + var infoTitle = string.Format("{0} Health {1} Passed with Info", infoResults.Count, infoResults.Count > 1 ? checkStringPural : checkString); + + var infoAttachment = GenerateAttachment(infoResults, "#439FE0", infoTitle); + attachments.Add(infoAttachment); + } + + if (errorResults.Any()) + { + var errorTitle = string.Format("{0} Health {1} Failed", errorResults.Count, errorResults.Count > 1 ? checkStringPural : checkString); + + var errorAttachment = GenerateAttachment(errorResults, "danger", errorTitle); + attachments.Add(errorAttachment); + } + var slackMessage = new SlackMessage { Channel = Channel, - Text = results.ResultsAsMarkDown(Verbosity, true), + Attachments = attachments, IconEmoji = icon, Username = Username }; await slackClient.PostAsync(slackMessage); + + } + + private SlackAttachment GenerateAttachment(Dictionary> successResults, string color, string title) + { + var slackFields = new List(); + foreach (var result in successResults) + { + var resultsText = string.Empty; + + // only show details if Verbosity is detailed + + var shortText = true; + + if (Verbosity == HealthCheckNotificationVerbosity.Detailed) + { + shortText = false; + + foreach (var check in result.Value) + { + // if more than one result use bullets + if (result.Value.Count() > 1) + { + resultsText += "• "; + } + + resultsText = resultsText + RemoveSimpleHtml(check.Message); + + // if not last result as a new line + if (check != result.Value.Last()) + { + resultsText = resultsText + Environment.NewLine; + } + } + } + slackFields.Add(new SlackField() { Title = result.Key, Value = resultsText, Short = shortText }); + } + + var slackAttachment = new SlackAttachment + { + Color = color, + Title = title, + Fields = slackFields + }; + + return slackAttachment; + } + + private static string RemoveSimpleHtml(string html) + { + return html.Replace("", "") + .Replace("", "") + .Replace("", "") + .Replace("", ""); } } }