Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Github -> Amazon SNS -> Lambda on .NET Core -> Slack
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization;
using Newtonsoft.Json;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializerAttribute(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace GithubWebhook
{
public class Function
{
private readonly string webhookUrl = Environment.GetEnvironmentVariable("SlackWebhookUrl");
/// <summary>
/// Github Webhook -> SNS -> Lambda Parse (HERE!!) -> Slack Executor
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public async Task<Response> FunctionHandlerAsync(SnsRequest input, ILambdaContext context)
{
var snsMessage = input.Records.FirstOrDefault()?.Sns.Message;
if (snsMessage == null)
{
throw new NullReferenceException(nameof(snsMessage));
}
var githubEvent = input.Records.FirstOrDefault()?.Sns.MessageAttributes.XGithubEvent?.Value;
if (!githubEvent.HasValue)
{
throw new NullReferenceException(nameof(githubEvent));
}
dynamic githubWebhook = JsonConvert.DeserializeObject(input.Records.First().Sns.Message);
context.Logger.LogLine($"GitHub WebHook triggered");
var message = "";
switch (githubEvent.Value)
{
case GithubEventKind.issue_comment:
{
// Issue Comment
message = $@"New GitHub issue comment posted by {githubWebhook.comment.user.login} at {githubWebhook.repository.name},
Url : {githubWebhook.comment.url}
Tite : {githubWebhook.issue.title}
-----
{githubWebhook.comment.body}";
break;
}
case GithubEventKind.issues:
break;
case GithubEventKind.pull_request_review_comment:
break;
case GithubEventKind.pull_request_review:
break;
case GithubEventKind.pull_request:
break;
default:
throw new ArgumentOutOfRangeException();
}
var payload = new
{
channel = "#github", username = "AWS Lambda Bot", text = message, icon_url = "https://d0.awsstatic.com/events/new-reinvent/reinvent_launch-page_illustration_lambda.png",
};
var jsonString = JsonConvert.SerializeObject(payload);
using (var client = new HttpClient())
{
var res = await client.PostAsync(webhookUrl, new StringContent(jsonString, Encoding.UTF8, "application/json"));
return new Response(res.StatusCode, $"Github webhook invoked. Message : {message}");
}
}
}
public class Response
{
public HttpStatusCode StatusCode { get; set; }
public string Body { get; set; }
public Response(HttpStatusCode code, string body)
{
StatusCode = code;
Body = body;
}
}
}
namespace GithubWebhook
{
public enum GithubEventKind
{
commit_comment,
create,
delete,
deployment,
deployment_status,
fork,
gollum,
issue_comment,
issues,
label,
member,
membership,
milestone,
organization,
page_build,
@public,
pull_request_review_comment,
pull_request_review,
pull_request,
push,
repository,
release,
status,
team,
team_add,
watch,
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace GithubWebhook
{
public class SnsRequest
{
public Record[] Records { get; set; }
}
public class Record
{
public string EventVersion { get; set; }
public string EventSubscriptionArn { get; set; }
public string EventSource { get; set; }
public Sns Sns { get; set; }
}
public class Sns
{
public string SignatureVersion { get; set; }
public DateTime Timestamp { get; set; }
public string Signature { get; set; }
public string SigningCertUrl { get; set; }
public string MessageId { get; set; }
public string Message { get; set; }
public Messageattributes MessageAttributes { get; set; }
public string Type { get; set; }
public string UnsubscribeUrl { get; set; }
public string TopicArn { get; set; }
public string Subject { get; set; }
}
public class Messageattributes
{
[JsonProperty("X-Github-Event")]
public XGithubEvent XGithubEvent { get; set; }
}
public class XGithubEvent
{
public string Type { get; set; }
public GithubEventKind Value { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment