-
Notifications
You must be signed in to change notification settings - Fork 0
/
Processor.cs
170 lines (147 loc) · 6.5 KB
/
Processor.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Web;
using Amazon.Lambda.Core;
using Amazon.Lambda.APIGatewayEvents;
using System.Net.Http;
using System;
using System.Text;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace WebhookProcessing
{
public class Processor
{
private readonly HttpClient httpClient;
private readonly string teamsWebhook;
public Processor()
{
httpClient = new HttpClient();
teamsWebhook = Environment.GetEnvironmentVariable("TEAMS_WEBHOOK");
}
public async Task<APIGatewayProxyResponse> Invoke(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
{
context.Logger.LogLine(JsonConvert.SerializeObject(apigProxyEvent));
// documentation for ZenHub webhooks: https://github.com/ZenHubIO/API#custom-webhooks
var nvc = HttpUtility.ParseQueryString(apigProxyEvent.Body);
var parameters = nvc.AllKeys.ToDictionary(k => k, k => nvc[k]);
context.Logger.LogLine(JsonConvert.SerializeObject(parameters));
object teamsWebhookObject = null;
if (parameters.ContainsKey("type"))
{
switch (parameters["type"])
{
case "issue_transfer":
teamsWebhookObject = new TeamsPayload
{
Summary = "Issue moved",
Sections = new List<Section>{
new Section
{
ActivityTitle= $"{parameters["user_name"]} moved issue #{parameters["issue_number"]} {parameters["issue_title"]}",
Facts = new List<Fact>{
new Fact
{
Name= "From Pipeline:",
Value= parameters["from_pipeline_name"]
},
new Fact
{
Name= "To Pipeline:",
Value= parameters["to_pipeline_name"]
},
new Fact
{
Name= "Repository:",
Value= $"{parameters["organization"] }/{parameters["repo"]}"
}
}
}
},
PotentialAction = new List<PotentialAction>{
new PotentialAction
{
Type = "OpenUri",
Name = "Open in GitHub",
Targets = new List<Target>{
new Target{
Os = "default",
Uri = parameters["github_url"]
}
}
},
new PotentialAction
{
Type = "OpenUri",
Name = "View ZenHub Board",
Targets = new List<Target>{
new Target{
Os = "default",
Uri = $"https://github.com/{parameters["organization"] }/{parameters["repo"]}/#workspaces/{parameters["workspace_id"]}/boards"
}
}
}
}
};
break;
}
}
if (teamsWebhookObject != null)
{
var payload = (JsonConvert.SerializeObject(teamsWebhookObject));
context.Logger.LogLine($"Sending the following payload to teams webhook {teamsWebhook}");
context.Logger.LogLine(payload);
var httpContent = new StringContent(payload, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(teamsWebhook, httpContent);
}
return new APIGatewayProxyResponse
{
Body = apigProxyEvent.Body,
StatusCode = 200,
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};
}
}
// for the actual schema see https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference
public partial class TeamsPayload
{
[JsonProperty("summary")]
public string Summary { get; set; }
[JsonProperty("sections")]
public List<Section> Sections { get; set; }
[JsonProperty("potentialAction")]
public List<PotentialAction> PotentialAction { get; set; }
}
public partial class PotentialAction
{
[JsonProperty("@type")]
public string Type { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("targets")]
public List<Target> Targets { get; set; }
}
public partial class Target
{
[JsonProperty("os")]
public string Os { get; set; }
[JsonProperty("uri")]
public string Uri { get; set; }
}
public partial class Section
{
[JsonProperty("activityTitle")]
public string ActivityTitle { get; set; }
[JsonProperty("facts")]
public List<Fact> Facts { get; set; }
}
public partial class Fact
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
}