-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaNotificationHandlers.cs
More file actions
492 lines (462 loc) · 22.2 KB
/
Copy pathLambdaNotificationHandlers.cs
File metadata and controls
492 lines (462 loc) · 22.2 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// -----------------------------------------------------------------------------
// Copyright (c) Balsamic Solutions, LLC. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER
// EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR
// -----------------------------------------------------------------------------
using Amazon.CodeCommit;
using Amazon.CodeCommit.Model;
using Amazon.Lambda.Core;
using Amazon.Lambda.SNSEvents;
using Amazon.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace BalsamicSolutions.CodeCommit2Teams
{
/// <summary>
/// handles notifications from CodeCommit triggers and from CodeCommit
/// notifications.
///
/// Teams configuration for this class relies on an enviornment variable
/// named TeamsChannelUrl that contains the url to the Teams Channel. The
/// Teams WebHook must be created as described here
/// https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook
/// enabled to post messages to your channel. We do not pass any images
/// with this app, so it needs it's own logo
///
/// Optionally you can set environment variables for TimeZone and for
///for MaxChangesWarningThreshold to customize the behaviour. The
///TimeZone variable is in Windows format (e.g. "Eastern Standard Time"
///or "UTC-11"). MaxChangesWarningThreshold is an integer value, set
///it to 0 to disable max change warnings
///
/// Triggers are configured on CodeCommit individually (because they do not
/// identify the trigger type (push, new branch, delete branch) with custom
/// data that defines each one, e.g all triggers point to this Lambda function, but
/// push with thier custom data.
///
/// Pull request notifications are handled by first creating a Pull request notifcation
/// on the console, then modifying the subsequent CloudWatch rule to send the
/// data to this Lambda instead of to SNS.
/// </summary>
public class LambdaNotificationHandlers
{
private readonly bool _Enabled = false;
private readonly int _MaxChangesWarningThreshold = 100;
private readonly TimeZoneInfo _TimeZone;
private List<Uri> _TeamsChannelUrls = new List<Uri>();
private AWSCredentials _AwsCredentials = null;
#region CTORs
/// <summary>
/// Default constructor. This constructor is used by Lambda to construct the instance. When invoked in a Lambda environment
/// the AWS credentials will come from the IAM role associated with the function and the AWS region will be set to the
/// region the Lambda function is executed in.
/// </summary>
public LambdaNotificationHandlers()
{
_AwsCredentials = FallbackCredentialsFactory.GetCredentials();
//first see if there is a time zone specified as regular timezones id's
_TimeZone = TimeZoneInfo.Utc;
string tempVal = Environment.GetEnvironmentVariable("TimeZone");
if (!string.IsNullOrWhiteSpace(tempVal))
{
tempVal = tempVal.Trim();
try
{
_TimeZone = tempVal.TimeZoneById();
}
catch (TimeZoneNotFoundException)
{
_TimeZone = TimeZoneInfo.Utc;
Console.WriteLine("TimeZone setting is invalid " + tempVal);
}
}
Console.WriteLine("TimeZone set to " + _TimeZone.ToString()); ;
//now see if we have a different Warning Threshold than the default
tempVal = Environment.GetEnvironmentVariable("MaxChangesWarningThreshold");
if (!tempVal.IsNullOrWhiteSpace() && int.TryParse(tempVal, out int maxChanges))
{
tempVal = tempVal.Trim();
_MaxChangesWarningThreshold = maxChanges;
Console.WriteLine("MaxChangesWarningThreshold set to " + _MaxChangesWarningThreshold.ToString());
}
//now collect our TeamsChannelUrl
tempVal = Environment.GetEnvironmentVariable("TeamsChannelUrl");
if (!tempVal.IsNullOrWhiteSpace())
{
tempVal = tempVal.Trim();
try
{
Uri teamsChannelUrl = new Uri(tempVal);
_TeamsChannelUrls.Add(teamsChannelUrl);
for (int i = 1; i < 10; i++)
{
string extraName = $"TeamsChannelUrl{i}";
tempVal = Environment.GetEnvironmentVariable(extraName);
if (!tempVal.IsNullOrWhiteSpace())
{
tempVal = tempVal.Trim();
try
{
teamsChannelUrl = new Uri(tempVal);
_TeamsChannelUrls.Add(teamsChannelUrl);
}
catch (UriFormatException) { }
}
}
foreach(Uri tempUri in _TeamsChannelUrls)
{
Console.WriteLine("TeamsChannelUrl set to " + tempUri.ToString());
}
if (!(_AwsCredentials is AnonymousAWSCredentials))
{
_Enabled = true;
Console.WriteLine("CodeCommit2Teams is configured and enabled");
}
else
{
Console.WriteLine("Invalid IAM role assigned to this Lambda");
}
}
catch (UriFormatException)
{
Console.WriteLine("Invalid url found in environment variable TeamsChannelUrl");
}
}
else
{
Console.WriteLine("Missing environment variable TeamsChannelUrl");
}
}
/// <summary>
/// Constructs an instance with a preconfigured client.
/// This can be used for testing the outside of the Lambda environment.
/// </summary>
/// <param name="ccClient"></param>
public LambdaNotificationHandlers(AWSCredentials awsCreds, string teamsChannelUrl)
{
//no checking on this one, as we assume this is a test client
//so if its broken its your fault
_AwsCredentials = awsCreds;
_TeamsChannelUrls.Add(new Uri(teamsChannelUrl));
_Enabled = true;
_TimeZone = TimeZoneInfo.Local;
}
#endregion CTORs
#region Lambda handlers
/// <summary>
/// handle a notification for a branch create,delete or push
///
///Use this name to register with Lambda
/// BalsamicSolutions.CodeCommit2Teams::BalsamicSolutions.CodeCommit2Teams.LambdaNotificationHandlers::HandleBranchEvent
/// </summary>
/// <param name="jsonPull"></param>
/// <param name="context"></param>
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public void HandleBranchEvent(BranchTypes.CodeCommitBranchEvent input, ILambdaContext context)
{
Console.WriteLine("Starting HandleBranchEvent");
if (_Enabled)
{
string commitId = "Unknown CommitID";
string refBranch = "Unknown Branch";
BranchTypes.Record ccRecord = input.Records[0];
BranchTypes.Codecommit commitRecord = ccRecord.Codecommit;
//I have only seen multiple references nodes in the test data
//so we only process record [0]
if (null != commitRecord && null != commitRecord.References && commitRecord.References.Length > 0)
{
bool isDelete = false;
bool isCreated = false;
BranchTypes.Reference ccRef = commitRecord.References[0];
commitId = ccRef.Commit;
refBranch = ccRef.Ref;
if (ccRef.Deleted.HasValue)
{
isDelete = ccRef.Deleted.Value;
}
else if (ccRef.Created.HasValue)
{
isCreated = true;
}
string[] branchParts = refBranch.Split('/');
string branchName = branchParts[branchParts.Length - 1];
string eventSourceARN = ccRecord.EventSourceArn;
string[] eventSourcARNParts = eventSourceARN.Split(':');
string repositoryName = eventSourcARNParts[5];
string regionName = eventSourcARNParts[3];
Amazon.RegionEndpoint regionEndPoint = Amazon.RegionEndpoint.GetBySystemName(regionName);
string userIdentityARN = ccRecord.UserIdentityArn;
string eventTimeAsText = ccRecord.EventTime;
DateTime eventTime = DateTime.Parse(eventTimeAsText).ToUniversalTime();
DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(eventTime, _TimeZone);
using (AmazonCodeCommitClient codeCommit = new AmazonCodeCommitClient(_AwsCredentials, regionEndPoint))
{
GetRepositoryResponse repositoryInfo = GetRepositoryInfo(codeCommit, repositoryName);
GetCommitResponse commitInfo = GetCommitInfo(codeCommit, repositoryName, commitId);
string authorName = userIdentityARN.LastSplitElement('/');
string notificationMessage = "Unknown Lambda Event";
if (isCreated)
{
notificationMessage = GenerateBranchCreateMessage(authorName, localTime, repositoryName, branchName);
}
else if (isDelete)
{
notificationMessage = GenerateBranchDeleteMessage(authorName, localTime, repositoryName, branchName);
}
else
{
string commitParentId = "HEAD";
if (commitInfo.Commit.Parents.Count > 0)
{
commitParentId = commitInfo.Commit.Parents[0];
}
List<Difference> commitDifferences = GetCommitDifferences(codeCommit, repositoryName, commitId, commitParentId, _MaxChangesWarningThreshold);
if (_MaxChangesWarningThreshold > 0 && commitDifferences.Count >= _MaxChangesWarningThreshold)
{
notificationMessage = GenerateBranchWarningMessage(authorName, commitId, localTime, commitInfo.Commit.Message, repositoryName, branchName);
}
else
{
notificationMessage = GenerateBranchCommitMessage(authorName, commitId, localTime, commitInfo.Commit.Message, repositoryName, branchName, commitDifferences);
}
}
ChannelClient teamsClient = new ChannelClient(_TeamsChannelUrls);
teamsClient.PostMessage(notificationMessage);
}
}
else
{
Console.WriteLine("No References node, nothing to process");
}
}
Console.WriteLine("Completed HandleBranchEvent");
}
/// <summary>
/// this is our callback for all of the AWS triggers and notifications. We do not
/// use the AWS LambdaSerializer because we are processing more than
/// one type of JSON data packages on the same endpoint.
///
///Use this name to register with Lambda
///BalsamicSolutions.CodeCommit2Teams::BalsamicSolutions.CodeCommit2Teams.LambdaNotificationHandlers::HandleEvent
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
public void HandleEvent(Stream input, ILambdaContext context)
{
string jsonAsText = string.Empty;
Console.WriteLine("Dispatching CodeCommitEvent");
if (null != input)
{
StreamReader streamReader = new StreamReader(input);
jsonAsText = streamReader.ReadToEnd();
}
Console.WriteLine($"CodeCommitEvent: received the following JSON: {jsonAsText}");
if (_Enabled)
{
//instead of desesralizing the object, look for the "Records" text which indicates we have a
//branch notification, its faster than parsing the entire jsonData twice for a typed object
int indexOfRecords = jsonAsText.IndexOf("\"Records\"", 0, StringComparison.OrdinalIgnoreCase);
if (-1 == indexOfRecords)
{
PullTypes.CodeCommitPullEvent codeCommitEvent = jsonAsText.FromJson<PullTypes.CodeCommitPullEvent>();
HandlePullEvent(codeCommitEvent, context);
}
else
{
BranchTypes.CodeCommitBranchEvent codeCommitEvent = jsonAsText.FromJson<BranchTypes.CodeCommitBranchEvent>();
HandleBranchEvent(codeCommitEvent, context);
}
}
else
{
Console.WriteLine("This Lambda handler requires additional configuration");
}
Console.WriteLine("Dispatch of CodeCommitEvent complete");
}
/// <summary>
/// handle a notification for a pull event
///
/// Use this name to register with Lambda
/// BalsamicSolutions.CodeCommit2Teams::BalsamicSolutions.CodeCommit2Teams.LambdaNotificationHandlers::HandlePullEvent
/// </summary>
/// <param name="codeCommitEvent"></param>
/// <param name="context"></param>
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public void HandlePullEvent(PullTypes.CodeCommitPullEvent input, ILambdaContext context)
{
Console.WriteLine("Starting HandlePullEvent");
if (_Enabled)
{
//for this one we are just going to re use the existing notification body
string notificationMessage = input.Detail.NotificationBody;
ChannelClient teamsClient = new ChannelClient(_TeamsChannelUrls);
teamsClient.PostMessage(notificationMessage);
}
Console.WriteLine("Completed HandlePullEvent");
}
#endregion Lambda handlers
#region CodeCommit calls
//= GetBranchInfo(codeCommit, repositoryName, refBranch);
/// <summary>
/// gets the commit information
/// </summary>
/// <param name="codeCommit"></param>
/// <param name="repositoryName"></param>
/// <param name="branchName"></param>
/// <returns></returns>
private GetBranchResponse GetBranchInfo(AmazonCodeCommitClient codeCommit, string repositoryName, string branchName)
{
GetBranchRequest branchRequest = new GetBranchRequest()
{
RepositoryName = repositoryName,
BranchName = branchName
};
GetBranchResponse returnValue = null;
try
{
returnValue = codeCommit.GetBranchAsync(branchRequest).GetAwaiter().GetResult();
}
catch (BranchDoesNotExistException)
{
returnValue = null;
}
return returnValue;
}
/// <summary>
/// get file differences in a particular commit
/// </summary>
/// <param name="codeCommit"></param>
/// <param name="repositoryName"></param>
/// <param name="afterCommitSpecifier"></param>
/// <returns></returns>
private List<Difference> GetCommitDifferences(IAmazonCodeCommit codeCommit, string repositoryName, string afterCommitSpecifier, string beforeCommitSpecifier, int maxResults)
{
List<Difference> returnValue = new List<Difference>();
GetDifferencesRequest diffRequest;
GetDifferencesResponse diffResponse;
string nextToken = String.Empty;
do
{
diffRequest = new GetDifferencesRequest()
{
RepositoryName = repositoryName,
AfterCommitSpecifier = afterCommitSpecifier,
BeforeCommitSpecifier = beforeCommitSpecifier,
MaxResults = maxResults
};
if (!nextToken.IsNullOrWhiteSpace())
{
diffRequest.NextToken = nextToken;
}
diffResponse = codeCommit.GetDifferencesAsync(diffRequest).GetAwaiter().GetResult();
returnValue.AddRange(diffResponse.Differences);
nextToken = diffResponse.NextToken;
} while (!String.IsNullOrEmpty(nextToken));
return returnValue;
}
/// <summary>
/// gets the commit information
/// </summary>
/// <param name="codeCommit"></param>
/// <param name="repositoryName"></param>
/// <param name="commitId"></param>
/// <returns></returns>
private GetCommitResponse GetCommitInfo(AmazonCodeCommitClient codeCommit, string repositoryName, string commitId)
{
GetCommitRequest commitRequest = new GetCommitRequest()
{
RepositoryName = repositoryName,
CommitId = commitId
};
GetCommitResponse returnValue = codeCommit.GetCommitAsync(commitRequest).GetAwaiter().GetResult();
return returnValue;
}
/// <summary>
/// gets the Repository information
/// </summary>
/// <param name="codeCommit"></param>
/// <param name="repositoryName"></param>
/// <param name="commitId"></param>
/// <returns></returns>
private GetRepositoryResponse GetRepositoryInfo(AmazonCodeCommitClient codeCommit, string repositoryName)
{
GetRepositoryRequest repoRequest = new GetRepositoryRequest()
{
RepositoryName = repositoryName
};
GetRepositoryResponse returnValue = codeCommit.GetRepositoryAsync(repoRequest).GetAwaiter().GetResult();
return returnValue;
}
#endregion CodeCommit calls
#region Message generation
/// <summary>
/// generates content for a branch commit notification
/// </summary>
/// <param name="authorName"></param>
/// <param name="commitId"></param>
/// <param name="commitParentId"></param>
/// <param name="repositoryInfo"></param>
/// <param name="commitInfo"></param>
/// <returns></returns>
private string GenerateBranchCommitMessage(string authorName, string commitId, DateTime commitTime, string commitMessage, string repoName, string branchName, List<Difference> commitDifferences)
{
string returnValue = authorName + " pushed changes to the " + branchName + " branch of " + repoName;
returnValue += " at " + commitTime.ToString() + " \r\n";
returnValue += "*" + commitId + "* : " + commitMessage + " \r\n";
foreach (Difference commitDiff in commitDifferences)
{
string changeType = commitDiff.ChangeType.ToString();
switch (changeType)
{
case "A":
returnValue += "*Added* " + commitDiff.AfterBlob.Path.ToString();
break;
case "D":
returnValue += "*Deleted* " + commitDiff.BeforeBlob.Path.ToString();
break;
case "M":
returnValue += "*Modified* " + commitDiff.AfterBlob.Path.ToString();
break;
}
returnValue += "\r\n";
}
return returnValue;
}
/// <summary>
/// generates content for a new branch notification
/// </summary>
/// <returns></returns>
private string GenerateBranchCreateMessage(string authorName, DateTime commitTime, string repoName, string branchName)
{
string returnValue = authorName + " *created* a new branch named " + branchName + " in " + repoName;
returnValue += " at " + commitTime.ToString("F") + " \r\n";
return returnValue;
}
/// <summary>
/// generates content for a new branch notification
/// </summary>
/// <returns></returns>
private string GenerateBranchDeleteMessage(string authorName, DateTime commitTime, string repoName, string branchName)
{
string returnValue = authorName + " *deleted* the branch " + branchName + " from " + repoName;
returnValue += " at " + commitTime.ToString("F") + " \r\n";
return returnValue;
}
/// <summary>
/// generates contentent for a warning message
/// </summary>
/// <returns></returns>
private string GenerateBranchWarningMessage(string authorName, string commitId, DateTime commitTime, string commitMessage, string repoName, string branchName)
{
string returnValue = "## Warning \r\n"; ;
returnValue += authorName + " pushed changes to the " + branchName + " branch of " + repoName;
returnValue += " at " + commitTime.ToString("F") + " \r\n";
returnValue += "*" + commitId + "* : " + commitMessage + " \r\n";
returnValue += "*This commit had more changes than expected and should be reviewed*";
return returnValue;
}
#endregion Message generation
}
}