Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active December 31, 2016 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/05dc97af22d0ef06e25e262a90f0b08b to your computer and use it in GitHub Desktop.
Save guitarrapc/05dc97af22d0ef06e25e262a90f0b08b to your computer and use it in GitHub Desktop.
AWS Lambda (.NET Core) sample to parse Unity Cloud Build Webhook and send message to Chatwork
{
"Information": [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
"dotnet lambda help",
"All the command line options for the Lambda command can be specified in this file."
],
"profile": "default",
"region": "ap-northeast-1",
"configuration": "Release",
"framework": "netcoreapp1.0",
"function-runtime": "dotnetcore1.0",
"function-memory-size": 128,
"function-timeout": 30,
"function-handler": "UnityCloudBuildNotificationProxy::UnityCloudBuildNotificationProxy.Function::FunctionHandler",
"function-name": "UnityCloudBuildNotificationProxy",
"function-role": "arn:aws:iam::158352693096:role/lambda_collaborate_role"
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace UnityCloudBuildNotificationProxy
{
public class ChatworkNotification
{
[JsonProperty("channel")]
public int Channel { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda;
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 UnityCloudBuildNotificationProxy
{
public class Function
{
private const string ChatworkFunctionName = "SendToChatwork";
private static readonly string channel = Environment.GetEnvironmentVariable("ChatworkChannel");
private static string Region => Environment.GetEnvironmentVariable("AWS_DEFAULT_REGION");
#if DEBUG
private static string AccessKey => Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID");
private static string SecretAccessKey => Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY");
private static AmazonLambdaClient LambdaClient => new AmazonLambdaClient(AccessKey, SecretAccessKey, Amazon.RegionEndpoint.GetBySystemName(Region));
#elif RELEASE
private static AmazonLambdaClient LambdaClient => new AmazonLambdaClient(Amazon.RegionEndpoint.GetBySystemName(Region));
#endif
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public string FunctionHandler(dynamic input, ILambdaContext context)
{
// Check json input string.
context.Logger.LogLine("UnityCloudBuildNotificationProxy webhook started!");
context.Logger.LogLine(Region);
var rawJson = input.ToString();
context.Logger.LogLine(rawJson);
if (input.body == null)
{
context.Logger.LogLine("Input json detected not contain body key. return immediately.");
throw new NullReferenceException(nameof(input.body));
}
if (input.body.hookId != null)
{
var m = "Input json detected as ping. return immediately.";
context.Logger.LogLine(m);
return m;
}
if (input.body.projectName == null)
{
var m = "Input json not detected to contain body.projectName Key. return immediately.";
context.Logger.LogLine(m);
return m;
}
var channelId = 0;
if (!int.TryParse(channel, out channelId))
{
var m = "Missing environment variable ChatworkRoomId";
context.Logger.LogLine(m);
throw new NullReferenceException(m);
}
var data = JsonConvert.DeserializeObject<UnityCloudBuildWebhook>(rawJson);
var message = $@"[info][title]Unity Cloud Build #{data.body.buildNumber}: Build {data.body.buildStatus}[/title]Platform : {data.body.platform}
{data.body.buildTargetName}
[hr]https://developer.cloud.unity3d.com{data.body.links.dashboard_summary.href}
Started by : {data.body.startedBy}[/info]";
var notification = new ChatworkNotification
{
Channel = channelId,
Text = message,
};
// Pass to SendToChatwork Lambda function when Chatwork recover from Maintenance.
var result = LambdaClient.InvokeAsync(new Amazon.Lambda.Model.InvokeRequest
{
FunctionName = ChatworkFunctionName,
Payload = JsonConvert.SerializeObject(notification),
}).Result;
context.Logger.LogLine(result.HttpStatusCode.ToString());
return message;
}
}
}
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"Amazon.Lambda.Core": "1.0.0*",
"Amazon.Lambda.Serialization.Json": "1.0.1",
"Amazon.Lambda.Tools": {
"type": "build",
"version": "1.0.0-preview1"
},
"Newtonsoft.Json": "9.0.1",
"AWSSDK.Lambda": "3.3.2.4",
"LambdaShared": "1.0.0-*"
},
"tools": {
"Amazon.Lambda.Tools": "1.0.0-preview1"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace UnityCloudBuildNotificationProxy
{
public class UnityCloudBuildWebhook
{
public Body body { get; set; }
}
public class Body
{
public string projectName { get; set; }
public string buildTargetName { get; set; }
public string projectGuid { get; set; }
public string orgForeignKey { get; set; }
public int buildNumber { get; set; }
public string buildStatus { get; set; }
public string lastBuiltRevision { get; set; }
public string startedBy { get; set; }
public string platform { get; set; }
public string scmType { get; set; }
public Links links { get; set; }
}
public class Links
{
public Api_Self api_self { get; set; }
public Dashboard_Url dashboard_url { get; set; }
public Dashboard_Project dashboard_project { get; set; }
public Dashboard_Summary dashboard_summary { get; set; }
public Dashboard_Log dashboard_log { get; set; }
}
public class Api_Self
{
public string method { get; set; }
public string href { get; set; }
}
public class Dashboard_Url
{
public string method { get; set; }
public string href { get; set; }
}
public class Dashboard_Project
{
public string method { get; set; }
public string href { get; set; }
}
public class Dashboard_Summary
{
public string method { get; set; }
public string href { get; set; }
}
public class Dashboard_Log
{
public string method { get; set; }
public string href { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment