コード例 #1
0
        public async Task <HttpResponseMessage> Run(HttpRequestMessage req)
        {
            log.Verbose($"Context: {context.InvocationId} {context.FunctionName} {context.FunctionDirectory} {context.FunctionAppDirectory}");

            // TODO
            string aggregatorVersion = null;

            try
            {
                string rule = context.FunctionName;
                log.Info($"Welcome to {rule}");
            }
            catch (Exception ex)
            {
                log.Warning($"Failed parsing headers: {ex.Message}");
            }

            // Get request body
            string jsonContent = await req.Content.ReadAsStringAsync();

            dynamic data = JsonConvert.DeserializeObject(jsonContent);

            // sanity check
            if ((data.eventType != "workitem.created" &&
                 data.eventType != "workitem.updated") ||
                data.publisherId != "tfs")
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    error = "Not a good VSTS post..."
                }));
            }

            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            var logger = new TraceWriterLogger(log);

            /*
             * public string FunctionDirectory { get; set; }
             * public string FunctionAppDirectory { get; set; }
             */
            var    wrapper    = new RuleWrapper(config, logger, context.FunctionName, context.FunctionDirectory);
            string execResult = await wrapper.Execute(aggregatorVersion, data);

            log.Info($"Returning {execResult}");

            var resp = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(execResult)
            };

            return(resp);
        }
コード例 #2
0
        public async Task <HttpResponseMessage> Run(HttpRequestMessage req)
        {
            log.LogDebug($"Context: {context.InvocationId} {context.FunctionName} {context.FunctionDirectory} {context.FunctionAppDirectory}");

            var aggregatorVersion = GetCustomAttribute <System.Reflection.AssemblyInformationalVersionAttribute>()?.InformationalVersion;

            try
            {
                string rule = context.FunctionName;
                log.LogInformation($"Aggregator v{aggregatorVersion} executing rule '{rule}'");
            }
            catch (Exception ex)
            {
                log.LogWarning($"Failed parsing request headers: {ex.Message}");
            }

            // Get request body
            string jsonContent = await req.Content.ReadAsStringAsync();

            if (string.IsNullOrWhiteSpace(jsonContent))
            {
                log.LogWarning($"Failed parsing request body: empty");

                var resp = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("Request body is empty")
                };
                return(resp);
            }
            dynamic data      = JsonConvert.DeserializeObject(jsonContent);
            string  eventType = data.eventType;

            // sanity check
            if (!DevOpsEvents.IsValidEvent(eventType) ||
                data.publisherId != DevOpsEvents.PublisherId)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    error = "Not a good Azure DevOps post..."
                }));
            }

            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();
            var configuration = AggregatorConfiguration.Read(config);

            var logger  = new ForwarderLogger(log);
            var wrapper = new RuleWrapper(configuration, logger, context.FunctionName, context.FunctionDirectory);

            try
            {
                string execResult = await wrapper.Execute(data);

                if (string.IsNullOrEmpty(execResult))
                {
                    var resp = new HttpResponseMessage(HttpStatusCode.OK);
                    return(resp);
                }
                else
                {
                    log.LogInformation($"Returning '{execResult}' from '{context.FunctionName}'");

                    var resp = new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new StringContent(execResult)
                    };
                    return(resp);
                }
            }
            catch (Exception ex)
            {
                log.LogWarning($"Rule '{context.FunctionName}' failed: {ex.Message}");

                var resp = new HttpResponseMessage(HttpStatusCode.NotImplemented)
                {
                    Content = new StringContent(ex.Message)
                };
                return(resp);
            }
        }