Пример #1
0
        public async Task <string> ExecuteAsync(IRule rule, WorkItemEventContext eventContext, CancellationToken cancellationToken)
        {
            logger.WriteVerbose($"Connecting to Azure DevOps using {configuration.DevOpsTokenType}...");
            var clientCredentials = default(VssCredentials);

            if (configuration.DevOpsTokenType == DevOpsTokenType.PAT)
            {
                clientCredentials = new VssBasicCredential(configuration.DevOpsTokenType.ToString(), configuration.DevOpsToken);
            }
            else
            {
                logger.WriteError($"Azure DevOps Token type {configuration.DevOpsTokenType} not supported!");
                throw new ArgumentOutOfRangeException(nameof(configuration.DevOpsTokenType));
            }

            cancellationToken.ThrowIfCancellationRequested();

            // TODO improve from https://github.com/Microsoft/vsts-work-item-migrator
            using (var devops = new VssConnection(eventContext.CollectionUri, clientCredentials))
            {
                await devops.ConnectAsync(cancellationToken);

                logger.WriteInfo($"Connected to Azure DevOps");
                using (var clientsContext = new AzureDevOpsClientsContext(devops))
                {
                    var engine = new RuleEngine(logger, configuration.SaveMode, configuration.DryRun);

                    var ruleResult = await engine.RunAsync(rule, eventContext.ProjectId, eventContext.WorkItemPayload, clientsContext, cancellationToken);

                    logger.WriteInfo(ruleResult);
                    return(ruleResult);
                }
            }
        }
Пример #2
0
        private async Task <string> ExecAsyncImpl(IRule rule, WorkItemEventContext eventContext, VssCredentials clientCredentials, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            // TODO improve from https://github.com/Microsoft/vsts-work-item-migrator
            using var devops = new VssConnection(eventContext.CollectionUri, clientCredentials);
            try
            {
                await devops.ConnectAsync(cancellationToken);

                logger.WriteInfo($"Connected to Azure DevOps");
            }
            catch (System.Exception ex)
            {
                logger.WriteError(ex.Message);
                if (ex.InnerException != null)
                {
                    logger.WriteError(ex.InnerException.Message);
                }
                throw ex;
            }
            using var clientsContext = new AzureDevOpsClientsContext(devops);
            var engine = new RuleEngine(logger, configuration.SaveMode, configuration.DryRun);

            var ruleResult = await engine.RunAsync(rule, eventContext.ProjectId, eventContext.WorkItemPayload, eventContext.EventType, clientsContext, cancellationToken);

            logger.WriteInfo(ruleResult);
            return(ruleResult);
        }
        internal async Task <bool> InvokeLocalAsync(string projectName, string @event, int workItemId, string ruleFilePath, bool dryRun, SaveMode saveMode, CancellationToken cancellationToken)
        {
            if (!File.Exists(ruleFilePath))
            {
                _logger.WriteError($"Rule code not found at {ruleFilePath}");
                return(false);
            }

            var devopsLogonData = DevOpsLogon.Load().connection;

            _logger.WriteVerbose($"Connecting to Azure DevOps using {devopsLogonData.Mode}...");
            var clientCredentials = default(VssCredentials);

            if (devopsLogonData.Mode == DevOpsTokenType.PAT)
            {
                clientCredentials = new VssBasicCredential(devopsLogonData.Mode.ToString(), devopsLogonData.Token);
            }
            else
            {
                _logger.WriteError($"Azure DevOps Token type {devopsLogonData.Mode} not supported!");
                throw new ArgumentOutOfRangeException(nameof(devopsLogonData.Mode));
            }

            string collectionUrl = devopsLogonData.Url;

            using (var devops = new VssConnection(new Uri(collectionUrl), clientCredentials))
            {
                await devops.ConnectAsync(cancellationToken);

                _logger.WriteInfo($"Connected to Azure DevOps");

                Guid teamProjectId;
                using (var projectClient = devops.GetClient <ProjectHttpClient>())
                {
                    _logger.WriteVerbose($"Reading Azure DevOps project data...");
                    var project = await projectClient.GetProject(projectName);

                    _logger.WriteInfo($"Project {projectName} data read.");
                    teamProjectId = project.Id;
                }

                using (var clientsContext = new AzureDevOpsClientsContext(devops))
                {
                    _logger.WriteVerbose($"Rule code found at {ruleFilePath}");
                    var ruleCode = await File.ReadAllLinesAsync(ruleFilePath, cancellationToken);

                    var engineLogger = new EngineWrapperLogger(_logger);
                    var engine       = new Engine.RuleEngine(engineLogger, ruleCode, saveMode, dryRun: dryRun);

                    var workItem = await clientsContext.WitClient.GetWorkItemAsync(projectName, workItemId, expand : WorkItemExpand.All, cancellationToken : cancellationToken);

                    string result = await engine.ExecuteAsync(teamProjectId, workItem, clientsContext, cancellationToken);

                    _logger.WriteInfo($"Rule returned '{result}'");

                    return(true);
                }
            }
        }
Пример #4
0
        private async Task <string> ExecAsyncImpl(IRule rule, WorkItemEventContext eventContext, VssCredentials clientCredentials, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var devops = await ConnectToAzureDevOpsAsync(eventContext, clientCredentials, cancellationToken);

            using var clientsContext = new AzureDevOpsClientsContext(devops);
            var engine = new RuleEngine(logger, configuration.SaveMode, configuration.DryRun);

            var ruleResult = await engine.RunAsync(rule, eventContext.ProjectId, eventContext.WorkItemPayload, eventContext.EventType, clientsContext, cancellationToken);

            logger.WriteInfo(ruleResult);
            return(ruleResult);
        }
Пример #5
0
#pragma warning disable S107 // Methods should not have too many parameters
        private async Task <bool> InvokeLocalAsyncImpl(string projectName, string @event, int workItemId, string ruleFilePath, bool dryRun, SaveMode saveMode, bool impersonateExecution, DevOpsLogon devopsLogonData, VssCredentials clientCredentials, CancellationToken cancellationToken)
#pragma warning restore S107 // Methods should not have too many parameters
        {
            string collectionUrl = devopsLogonData.Url;

            using (var devops = new VssConnection(new Uri(collectionUrl), clientCredentials))
            {
                await devops.ConnectAsync(cancellationToken);

                _logger.WriteInfo($"Connected to Azure DevOps");

                Guid teamProjectId;
                using (var projectClient = devops.GetClient <ProjectHttpClient>())
                {
                    _logger.WriteVerbose($"Reading Azure DevOps project data...");
                    var project = await projectClient.GetProject(projectName);

                    _logger.WriteInfo($"Project {projectName} data read.");
                    teamProjectId = project.Id;
                }

                using (var clientsContext = new AzureDevOpsClientsContext(devops))
                {
                    _logger.WriteVerbose($"Rule code found at {ruleFilePath}");
                    var(preprocessedRule, _) = await RuleFileParser.ReadFile(ruleFilePath, cancellationToken);

                    var rule = new Engine.ScriptedRuleWrapper(Path.GetFileNameWithoutExtension(ruleFilePath), preprocessedRule)
                    {
                        ImpersonateExecution = impersonateExecution
                    };

                    var engineLogger = new EngineWrapperLogger(_logger);
                    var engine       = new Engine.RuleEngine(engineLogger, saveMode, dryRun: dryRun);

                    var workItem = await clientsContext.WitClient.GetWorkItemAsync(projectName, workItemId, expand : WorkItemExpand.All, cancellationToken : cancellationToken);

                    string result = await engine.RunAsync(rule, teamProjectId, workItem, @event, clientsContext, cancellationToken);

                    _logger.WriteInfo($"Rule returned '{result}'");

                    return(true);
                }
            }
        }