diff --git a/fappdeploy/httptrigger/GHubEventDispatch.cs b/fappdeploy/httptrigger/GHubEventDispatch.cs index 1dfe2b2..d17adae 100644 --- a/fappdeploy/httptrigger/GHubEventDispatch.cs +++ b/fappdeploy/httptrigger/GHubEventDispatch.cs @@ -28,31 +28,80 @@ 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")] public static async Task 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")); @@ -60,64 +109,14 @@ public static async Task Run([HttpTrigger(AuthorizationLevel.Func 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(); @@ -125,6 +124,6 @@ public static async Task Run([HttpTrigger(AuthorizationLevel.Func } } - return (ActionResult)new OkObjectResult(current_event); + return (ActionResult)new OkObjectResult(current_event); } }