Adding support for Slack Markdown "light"

This commit is contained in:
Jeavon Leopold
2017-06-05 11:16:03 +02:00
parent 6e4ffcc068
commit 7047d3ebaa
2 changed files with 36 additions and 9 deletions

View File

@@ -1,20 +1,22 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarkdownSharp;
using Umbraco.Core.Logging;
namespace Umbraco.Web.HealthCheck
{
public class HealthCheckResults
internal class HealthCheckResults
{
private readonly Dictionary<string, IEnumerable<HealthCheckStatus>> _results;
public HealthCheckResults(IEnumerable<HealthCheck> checks)
internal HealthCheckResults(IEnumerable<HealthCheck> checks)
{
_results = checks.ToDictionary(t => t.Name, t => t.GetStatus());
}
public void LogResults()
internal void LogResults()
{
LogHelper.Info<HealthCheckResults>("Scheduled health check results:");
foreach (var result in _results)
@@ -38,9 +40,14 @@ namespace Umbraco.Web.HealthCheck
}
}
public string ResultsAsMarkDown()
internal string ResultsAsMarkDown(bool slackMarkDown = false)
{
var newItem = "- ";
if (slackMarkDown)
{
newItem = "• ";
}
var sb = new StringBuilder();
foreach (var result in _results)
@@ -50,19 +57,39 @@ namespace Umbraco.Web.HealthCheck
var checkIsSuccess = result.Value.All(x => x.ResultType == StatusResultType.Success);
if (checkIsSuccess)
{
sb.AppendFormat("{0}Checks for'{1}' all completed succesfully.", newItem, checkName);
sb.AppendFormat("{0}Checks for'{1}' all completed succesfully.{2}", newItem, checkName, Environment.NewLine);
}
else
{
sb.AppendFormat("{0}Checks for'{1}' completed with errors.", newItem, checkName);
sb.AppendFormat("{0}Checks for'{1}' completed with errors.{2}", newItem, checkName, Environment.NewLine);
}
foreach (var checkResult in checkResults)
{
sb.AppendFormat("{0}{1}Result:'{2}' , Message: '{3}'", newItem, newItem, checkResult.ResultType, checkResult.Message);
sb.AppendFormat("\t{0}Result:'{1}' , Message: '{2}'{3}", newItem, checkResult.ResultType, SimpleHtmlToMarkDown(checkResult.Message, slackMarkDown), Environment.NewLine);
}
}
return sb.ToString();
}
internal string ResultsAsHtml()
{
Markdown mark = new Markdown();
return mark.Transform(ResultsAsMarkDown());
}
private string SimpleHtmlToMarkDown(string html, bool slackMarkDown = false)
{
if (slackMarkDown)
{
return html.Replace("<strong>", "*")
.Replace("</strong>", "*")
.Replace("<em>", "_")
.Replace("</em>", "_");
}
return html.Replace("<strong>", "**")
.Replace("</strong>", "**")
.Replace("<em>", "*")
.Replace("</em>", "*");
}
}
}

View File

@@ -69,7 +69,7 @@ namespace Umbraco.Web.Scheduling
var slackMessage = new SlackMessage
{
Channel = "#test",
Text = results.ResultsAsMarkDown(),
Text = results.ResultsAsMarkDown(true),
IconEmoji = Emoji.Ghost,
Username = "Umbraco Health Check Notifier"
};