/// <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"); }
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"); }