Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 62 additions & 63 deletions fappdeploy/httptrigger/GHubEventDispatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,103 +28,102 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using System.Collections.Generic;

public static class GridEventHandler{
[FunctionName("PrettyPoisons")]

private static string ParseEventGridValidationCode(dynamic requestObject)
{
var webhook_res = string.Empty;
if (requestObject != null && requestObject[0]["data"] != null){
var validationCode = requestObject[0].data.validationCode;
if(validationCode != null){
webhook_res = Newtonsoft.Json.JsonConvert.SerializeObject(new Newtonsoft.Json.Linq.JObject {["validationResponse"]= validationCode});
}
}
return webhook_res;
}

private static dynamic ParseMachineLearningEvent(dynamic requestObject)
{
return requestObject[0]["data"];
}

private static dynamic ParseBlobStorageEvent(dynamic requestObject)
{
return requestObject[0]["data"];
}

private static dynamic ParseContainerRegistryEvent(dynamic requestObject)
{
return requestObject[0]["data"];
}

[FunctionName("generic_triggers")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change will also require a change in the ARM templates... we might want to think about that

public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequestMessage req, ILogger log, ExecutionContext context)
{
log.LogInformation("C# HTTP trigger function processed a request.");

string requestBody = await req.Content.ReadAsStringAsync();
dynamic requestObject = JsonConvert.DeserializeObject(requestBody);
var webhook_res = string.Empty;
var current_event = requestObject[0]["eventType"].ToString();

if (current_event == "Microsoft.EventGrid.SubscriptionValidationEvent" ){
if (requestObject != null && requestObject[0]["data"] != null){
var validationCode = requestObject[0].data.validationCode;
if(validationCode != null){
webhook_res= Newtonsoft.Json.JsonConvert.SerializeObject(new Newtonsoft.Json.Linq.JObject {["validationResponse"]= validationCode});
string webhook_res = ParseEventGridValidationCode(requestObject);
if(!string.IsNullOrEmpty(webhook_res)){
return (ActionResult)new OkObjectResult($"{webhook_res}");
}
}
}

string[] event_data = current_event.Split(".");
string event_source = string.Empty;
string event_type = string.Empty;

if(event_data.Length>1){
event_source = event_data[1];
}

if (current_event.Contains("Microsoft.MachineLearningServices"))
var queryParams = System.Web.HttpUtility.ParseQueryString(req.RequestUri.Query);
string repo_name = queryParams.Get("repoName");

if(event_data.Length>2 && !string.IsNullOrEmpty(repo_name))
{
log.LogInformation("fetching repo name from query parameters."+repo_name);
event_type = event_data[2].ToLower();
var req_data = requestObject[0]["data"];

if(event_source == "MachineLearningServices"){
req_data = ParseMachineLearningEvent(requestObject);
}
if(event_source == "Storage"){
req_data = ParseBlobStorageEvent(requestObject);
}
if(event_source == "ContainerRegistry"){
req_data = ParseContainerRegistryEvent(requestObject);
}

using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add("User-Agent", "Awesome-Octocat-App");
httpClient.DefaultRequestHeaders.Accept.Clear();

var PATTOKEN = Environment.GetEnvironmentVariable("PAT_TOKEN", EnvironmentVariableTarget.Process);
// var repo_name = Environment.GetEnvironmentVariable("REPO_NAME", EnvironmentVariableTarget.Process);
var repo_name = "";

if(requestObject[0]["data"]["runTags"]==null || requestObject[0]["data"]["runTags"]["githuB_REPOSITORY"]==null)
{
repo_name = Environment.GetEnvironmentVariable("REPO_NAME", EnvironmentVariableTarget.Process);
log.LogInformation("Fetching repo name from Environment variables.");
}
else
{
repo_name = requestObject[0]["data"]["runTags"]["githuB_REPOSITORY"].ToString();
log.LogInformation("Fetching repo name from runTags");
}

httpClient.DefaultRequestHeaders.Add("Authorization", "token "+PATTOKEN);

var client_payload = new Newtonsoft.Json.Linq.JObject { ["unit "] = false, ["integration"] = true, ["data"] = requestObject[0]["data"]};
var event_types = "unknown";

if(current_event == "Microsoft.MachineLearningServices.RunCompleted")
{
event_types = "run-completed";
}
else if(current_event == "Microsoft.MachineLearningServices.RunStatusChanged")
{
event_types = "run-status-changed";
}
else if(current_event == "Microsoft.MachineLearningServices.ModelRegistered")
{
// tags are different in case of model registered, modelTags
if(requestObject[0]["data"]["modelTags"]==null || requestObject[0]["data"]["modelTags"]["githuB_REPOSITORY"]==null)
{
repo_name = Environment.GetEnvironmentVariable("REPO_NAME", EnvironmentVariableTarget.Process);
log.LogInformation("Fetching repo name from Environment variables.");
}
else
{
repo_name = requestObject[0]["data"]["modelTags"]["githuB_REPOSITORY"].ToString();
log.LogInformation("Fetching repo name from modelTags");
}

event_types = "model-registered";
}
else if(current_event == "Microsoft.MachineLearningServices.ModelDeployed")
{
event_types = "model-deployed";
}
else if(current_event == "Microsoft.MachineLearningServices.DatasetDriftDetected")
{
event_types = "data-drift-detected";
}
else
{
event_types = "unknown";
}

var payload = Newtonsoft.Json.JsonConvert.SerializeObject(new Newtonsoft.Json.Linq.JObject { ["event_type"] = event_types, ["client_payload"] = client_payload });
var client_payload = new Newtonsoft.Json.Linq.JObject { ["unit "] = false, ["integration"] = true,
["data"] = req_data, ["event_source"] = event_source};

var payload = Newtonsoft.Json.JsonConvert.SerializeObject(new Newtonsoft.Json.Linq.JObject { ["event_type"] = event_type, ["client_payload"] = client_payload });

var content = new StringContent(payload, Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.PostAsync("https://api.github.com/repos/"+repo_name+"/dispatches", content);
var resultString = await response.Content.ReadAsStringAsync();
return (ActionResult)new OkObjectResult("dispatch event sent");
}
}

return (ActionResult)new OkObjectResult(current_event);
return (ActionResult)new OkObjectResult(current_event);
}
}