internal override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var context = await Context
                          .WithAzureLogon()
                          .WithDevOpsLogon()
                          .BuildAsync(cancellationToken);

            var  rules = new AggregatorRules(context.Azure, context.Logger);
            bool ok    = DevOpsEvents.IsValidEvent(Event);

            if (!ok)
            {
                context.Logger.WriteError($"Invalid event type.");
                return(ExitCodes.InvalidArguments);
            }
            if (Local)
            {
                ok = await rules.InvokeLocalAsync(Project, Event, WorkItemId, Source, DryRun, SaveMode, ImpersonateExecution, cancellationToken);

                return(ok ? ExitCodes.Success : ExitCodes.Failure);
            }
            else
            {
                var instance = context.Naming.Instance(Instance, ResourceGroup);
                context.Logger.WriteWarning("Untested feature!");
                ok = await rules.InvokeRemoteAsync(Account, Project, Event, WorkItemId, instance, Name, DryRun, SaveMode, ImpersonateExecution, cancellationToken);

                return(ok ? ExitCodes.Success : ExitCodes.Failure);
            }
        }
예제 #2
0
        internal override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var context = await Context
                          .WithDevOpsLogon()
                          .BuildAsync(cancellationToken);

            var  mappings = new AggregatorMappings(context.Devops, context.Azure, context.Logger, context.Naming);
            bool ok       = DevOpsEvents.IsValidEvent(Event);

            if (!ok)
            {
                context.Logger.WriteError($"Invalid event type.");
                return(ExitCodes.InvalidArguments);
            }

            var filters = new EventFilters
            {
                AreaPath = FilterAreaPath,
                Type     = FilterType,
                Tag      = FilterTag,
                Fields   = FilterFields
            };

            var targetUrl = new Uri(TargetUrl);
            var id        = await mappings.AddFromUrlAsync(Project, Event, filters, targetUrl, Rule, ImpersonateExecution, cancellationToken);

            return(id.Equals(Guid.Empty) ? ExitCodes.Failure : ExitCodes.Success);
        }
예제 #3
0
        internal override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var context = await Context
                          .WithAzureLogon()
                          .WithDevOpsLogon()
                          .BuildAsync(cancellationToken);

            bool ok = DevOpsEvents.IsValidEvent(Event) || Event == "*";

            if (!ok)
            {
                context.Logger.WriteError($"Invalid event type.");
                return(ExitCodes.InvalidArguments);
            }
            var instance = context.Naming.Instance(Instance, ResourceGroup);
            var mappings = new AggregatorMappings(context.Devops, context.Azure, context.Logger, context.Naming);
            var outcome  = await mappings.RemoveRuleEventAsync(Event, instance, Project, Rule);

            switch (outcome)
            {
            case RemoveOutcome.Succeeded:
                return(ExitCodes.Success);

            case RemoveOutcome.NotFound:
                context.Logger.WriteWarning($"No mapping(s) found for rule(s) {instance.PlainName}/{Rule}");
                return(ExitCodes.NotFound);

            case RemoveOutcome.Failed:
                return(ExitCodes.Failure);

            default:
                return(ExitCodes.Unexpected);
            }
        }
예제 #4
0
        internal override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var context = await Context
                          .WithAzureLogon()
                          .WithDevOpsLogon()
                          .BuildAsync(cancellationToken);

            var  mappings = new AggregatorMappings(context.Devops, context.Azure, context.Logger);
            bool ok       = DevOpsEvents.IsValidEvent(Event);

            if (!ok)
            {
                context.Logger.WriteError($"Invalid event type.");
                return(2);
            }

            var filters = new AggregatorMappings.EventFilters
            {
                AreaPath = FilterAreaPath,
                Type     = FilterType,
                Tag      = FilterTag,
                Fields   = FilterFields
            };

            var instance = new InstanceName(Instance, ResourceGroup);
            var id       = await mappings.AddAsync(Project, Event, filters, instance, Rule, cancellationToken);

            return(id.Equals(Guid.Empty) ? 1 : 0);
        }
예제 #5
0
        internal override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var context = await Context
                          .WithAzureLogon()
                          .WithDevOpsLogon()
                          .BuildAsync(cancellationToken);

            context.ResourceGroupDeprecationCheck(this.ResourceGroup);
            var  mappings = new AggregatorMappings(context.Devops, context.Azure, context.Logger, context.Naming);
            bool ok       = DevOpsEvents.IsValidEvent(Event);

            if (!ok)
            {
                context.Logger.WriteError($"Invalid event type.");
                return(ExitCodes.InvalidArguments);
            }

            var filters = new EventFilters
            {
                AreaPath  = FilterAreaPath,
                Type      = FilterType,
                Tag       = FilterTag,
                Fields    = FilterFields,
                OnlyLinks = FilterOnlyLinks,
            };

            var instance = context.Naming.Instance(Instance, ResourceGroup);
            var id       = await mappings.AddAsync(Project, Event, filters, instance, Rule, ImpersonateExecution, cancellationToken);

            return(id.Equals(Guid.Empty) ? ExitCodes.Failure : ExitCodes.Success);
        }
예제 #6
0
        internal override async Task <int> RunAsync()
        {
            var context = await Context
                          .WithAzureLogon()
                          .WithDevOpsLogon()
                          .Build();

            var  mappings = new AggregatorMappings(context.Devops, context.Azure, context.Logger);
            bool ok       = DevOpsEvents.IsValidEvent(Event);

            if (!ok)
            {
                context.Logger.WriteError($"Invalid event type.");
                return(2);
            }
            var instance = new InstanceName(Instance, ResourceGroup);
            var id       = await mappings.Add(Project, Event, instance, Rule);

            return(id.Equals(Guid.Empty) ? 1 : 0);
        }
예제 #7
0
        internal override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var context = await Context
                          .WithAzureLogon()
                          .WithDevOpsLogon()
                          .BuildAsync(cancellationToken);

            bool ok = DevOpsEvents.IsValidEvent(Event);

            if (!ok)
            {
                context.Logger.WriteError($"Invalid event type.");
                return(2);
            }
            var instance = context.Naming.Instance(Instance, ResourceGroup);
            var mappings = new AggregatorMappings(context.Devops, context.Azure, context.Logger, context.Naming);

            ok = await mappings.RemoveRuleEventAsync(Event, instance, Project, Rule);

            return(ok ? 0 : 1);
        }
예제 #8
0
        public async Task <IActionResult> PostAsync(WebHookEvent eventData, string ruleName, CancellationToken cancellationToken)
        {
            var aggregatorVersion = RequestHelper.AggregatorVersion;

            _log.LogInformation($"Aggregator v{aggregatorVersion} executing rule '{ruleName}'");

            var webHookStartEvent = new EventTelemetry()
            {
                Name = "WebHookEvent Start"
            };

            webHookStartEvent.Properties["rule"]            = ruleName;
            webHookStartEvent.Properties["eventType"]       = eventData?.EventType;
            webHookStartEvent.Properties["resourceVersion"] = eventData?.ResourceVersion;
            Telemetry.TrackEvent(webHookStartEvent);

            if (eventData == null)
            {
                return(BadRequest("Request body is empty"));
            }

            // sanity check
            if (!DevOpsEvents.IsValidEvent(eventData.EventType) ||
                eventData.PublisherId != DevOpsEvents.PublisherId)
            {
                _log.LogDebug("return BadRequest");
                return(BadRequest(new { Error = "Not a good Azure DevOps post..." }));
            }

            var helper       = new RequestHelper(_log);
            var eventContext = helper.CreateContextFromEvent(eventData);

            if (eventContext.IsTestEvent())
            {
                Response.AddCustomHeaders(aggregatorVersion, ruleName);
                return(RespondToTestEventMessage(aggregatorVersion, ruleName));
            }

            var configContext = _configuration;
            var configuration = AggregatorConfiguration.ReadConfiguration(configContext)
                                .UpdateFromUrl(ruleName, GetRequestUri());

            var logger       = new ForwarderLogger(_log);
            var ruleProvider = new AspNetRuleProvider(logger, _configuration);
            var ruleExecutor = new RuleExecutor(logger, configuration);

            using (_log.BeginScope($"WorkItem #{eventContext.WorkItemPayload.WorkItem.Id}"))
            {
                try
                {
                    var rule = await ruleProvider.GetRule(ruleName);

                    var ruleStartEvent = new EventTelemetry()
                    {
                        Name = "Rule Exec Start"
                    };
                    ruleStartEvent.Properties["rule"] = rule.Name;
                    ruleStartEvent.Properties["ImpersonateExecution"] = rule.ImpersonateExecution.ToString();
                    ruleStartEvent.Properties["EnableRevisionCheck"]  = rule.Settings.EnableRevisionCheck.ToString();
                    ruleStartEvent.Properties["eventType"]            = eventContext.EventType;
                    Telemetry.TrackEvent(ruleStartEvent);
                    var ruleExecTimer = new Stopwatch();
                    ruleExecTimer.Start();

                    var execResult = await ruleExecutor.ExecuteAsync(rule, eventContext, cancellationToken);

                    ruleExecTimer.Stop();
                    var ruleEndEvent = new EventTelemetry()
                    {
                        Name = "Rule Exec End"
                    };
                    ruleEndEvent.Properties["rule"]  = ruleName;
                    ruleEndEvent.Metrics["duration"] = ruleExecTimer.ElapsedMilliseconds;
                    Telemetry.TrackEvent(ruleEndEvent);

                    if (string.IsNullOrEmpty(execResult))
                    {
                        return(Ok());
                    }
                    else
                    {
                        _log.LogInformation($"Returning '{execResult}' from '{rule.Name}'");
                        return(Ok(execResult));
                    }
                }
                catch (Exception ex)
                {
                    _log.LogWarning($"Rule '{ruleName}' failed: {ex.Message}");
                    Telemetry.TrackException(ex);
                    return(BadRequest(ex.Message));
                }
            }
        }