-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnotifications-msteams.cake
95 lines (79 loc) · 3.74 KB
/
notifications-msteams.cake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#addin "nuget:?package=Cake.MicrosoftTeams&version=2.0.0"
//-------------------------------------------------------------
public class MsTeamsNotifier : INotifier
{
public MsTeamsNotifier(BuildContext buildContext)
{
BuildContext = buildContext;
WebhookUrl = buildContext.BuildServer.GetVariable("MsTeamsWebhookUrl", showValue: false);
WebhookUrlForErrors = buildContext.BuildServer.GetVariable("MsTeamsWebhookUrlForErrors", WebhookUrl, showValue: false);
}
public BuildContext BuildContext { get; private set; }
public string WebhookUrl { get; private set; }
public string WebhookUrlForErrors { get; private set; }
public string GetMsTeamsWebhookUrl(string project, TargetType targetType)
{
// Allow per target overrides via "MsTeamsWebhookUrlFor[TargetType]"
var targetTypeUrl = GetTargetSpecificConfigurationValue(BuildContext, targetType, "MsTeamsWebhookUrlFor", string.Empty);
if (!string.IsNullOrEmpty(targetTypeUrl))
{
return targetTypeUrl;
}
// Allow per project overrides via "MsTeamsWebhookUrlFor[ProjectName]"
var projectTypeUrl = GetProjectSpecificConfigurationValue(BuildContext, project, "MsTeamsWebhookUrlFor", string.Empty);
if (!string.IsNullOrEmpty(projectTypeUrl))
{
return projectTypeUrl;
}
// Return default fallback
return WebhookUrl;
}
//-------------------------------------------------------------
private string GetMsTeamsTarget(string project, TargetType targetType, NotificationType notificationType)
{
if (notificationType == NotificationType.Error)
{
return WebhookUrlForErrors;
}
return GetMsTeamsWebhookUrl(project, targetType);
}
//-------------------------------------------------------------
public async Task NotifyAsync(string project, string message, TargetType targetType, NotificationType notificationType)
{
var targetWebhookUrl = GetMsTeamsTarget(project, targetType, notificationType);
if (string.IsNullOrWhiteSpace(targetWebhookUrl))
{
return;
}
var messageCard = new MicrosoftTeamsMessageCard
{
title = project,
summary = notificationType.ToString(),
sections = new []
{
new MicrosoftTeamsMessageSection
{
activityTitle = notificationType.ToString(),
activitySubtitle = message,
activityText = " ",
activityImage = "https://raw.githubusercontent.com/cake-build/graphics/master/png/cake-small.png",
facts = new []
{
new MicrosoftTeamsMessageFacts { name ="Project", value = project },
new MicrosoftTeamsMessageFacts { name ="Version", value = BuildContext.General.Version.FullSemVer },
new MicrosoftTeamsMessageFacts { name ="CakeVersion", value = BuildContext.CakeContext.Environment.Runtime.CakeVersion.ToString() },
//new MicrosoftTeamsMessageFacts { name ="TargetFramework", value = Context.Environment.Runtime .TargetFramework.ToString() },
},
}
}
};
var result = BuildContext.CakeContext.MicrosoftTeamsPostMessage(messageCard, new MicrosoftTeamsSettings
{
IncomingWebhookUrl = targetWebhookUrl
});
if (result != System.Net.HttpStatusCode.OK)
{
BuildContext.CakeContext.Warning(string.Format("MsTeams result: {0}", result));
}
}
}