Ejemplo n.º 1
0
        private async Task <IPreprocessedRule> LoadAndValidateRule(string ruleName, string filePath, CancellationToken cancellationToken)
        {
            var engineLogger = new EngineWrapperLogger(_logger);

            var(preprocessedRule, _) = await RuleFileParser.ReadFile(filePath, engineLogger, cancellationToken);

            try
            {
                var rule = new Engine.ScriptedRuleWrapper(ruleName, preprocessedRule, engineLogger);
                var(success, diagnostics) = rule.Verify();
                if (!success)
                {
                    var messages = string.Join('\n', diagnostics.Select(d => d.ToString()));
                    if (!string.IsNullOrEmpty(messages))
                    {
                        _logger.WriteError($"Errors in the rule file {filePath}:\n{messages}");
                    }

                    return(null);
                }
            }
            catch
            {
                return(null);
            }

            // Rule file is valid
            return(preprocessedRule);
        }
Ejemplo n.º 2
0
        internal async Task <bool> InvokeLocalAsync(string projectName, string @event, int workItemId, string ruleFilePath, bool dryRun, SaveMode saveMode)
        {
            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();

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

                Guid   teamProjectId;
                string teamProjectName;
                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;
                    teamProjectName = project.Name;
                }

                using (var witClient = devops.GetClient <WorkItemTrackingHttpClient>())
                {
                    logger.WriteVerbose($"Rule code found at {ruleFilePath}");
                    string[] ruleCode = File.ReadAllLines(ruleFilePath);

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

                    string result = await engine.ExecuteAsync(collectionUrl, teamProjectId, teamProjectName, devopsLogonData.Token, workItemId, witClient);

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

                    return(true);
                }
            }
        }
Ejemplo n.º 3
0
        internal async Task <bool> AddAsync(InstanceName instance, string ruleName, string filePath, CancellationToken cancellationToken)
        {
            _logger.WriteInfo($"Validate rule file {filePath}");

            var engineLogger = new EngineWrapperLogger(_logger);

            var(preprocessedRule, _) = await RuleFileParser.ReadFile(filePath, engineLogger, cancellationToken);

            try
            {
                var rule = new Engine.ScriptedRuleWrapper(ruleName, preprocessedRule, engineLogger);
                var(success, diagnostics) = rule.Verify();
                if (success)
                {
                    _logger.WriteInfo($"Rule file is valid");
                }
                else
                {
                    _logger.WriteInfo($"Rule file is invalid");
                    var messages = string.Join('\n', diagnostics.Select(d => d.ToString()));
                    if (!string.IsNullOrEmpty(messages))
                    {
                        _logger.WriteError($"Errors in the rule file {filePath}:\n{messages}");
                    }

                    return(false);
                }
            }
            catch
            {
                _logger.WriteInfo($"Rule file is invalid");
                return(false);
            }

            _logger.WriteVerbose($"Layout rule files");
            var inMemoryFiles = await PackagingFilesAsync(ruleName, preprocessedRule);

            _logger.WriteInfo($"Packaging rule {ruleName} complete.");

            _logger.WriteVerbose($"Uploading rule files to {instance.PlainName}");
            bool ok = await UploadRuleFilesAsync(instance, ruleName, inMemoryFiles, cancellationToken);

            if (ok)
            {
                _logger.WriteInfo($"All {ruleName} files successfully uploaded to {instance.PlainName}.");
            }

            if (preprocessedRule.Impersonate)
            {
                _logger.WriteInfo($"Configure {ruleName} to execute impersonated.");
                ok &= await ConfigureAsync(instance, ruleName, impersonate : true, cancellationToken : cancellationToken);

                if (ok)
                {
                    _logger.WriteInfo($"Updated {ruleName} configuration successfully.");
                }
            }

            return(ok);
        }
        internal async Task <bool> AddAsync(InstanceName instance, string name, string filePath, CancellationToken cancellationToken)
        {
            _logger.WriteInfo($"Validate rule file {filePath}");

            var ruleContent = await File.ReadAllLinesAsync(filePath);

            var engineLogger = new EngineWrapperLogger(_logger);

            try
            {
                var ruleEngine = new Engine.RuleEngine(engineLogger, ruleContent, SaveMode.Batch, true);
                (var success, var diagnostics) = ruleEngine.VerifyRule();
                if (success)
                {
                    _logger.WriteInfo($"Rule file is valid");
                }
                else
                {
                    _logger.WriteInfo($"Rule file is invalid");
                    var messages = string.Join('\n', diagnostics.Select(d => d.ToString()));
                    if (!string.IsNullOrEmpty(messages))
                    {
                        _logger.WriteError($"Errors in the rule file {filePath}:\n{messages}");
                    }

                    return(false);
                }
            }
            catch
            {
                _logger.WriteInfo($"Rule file is invalid");
                return(false);
            }

            _logger.WriteVerbose($"Layout rule files");
            var inMemoryFiles = await PackagingFilesAsync(name, filePath);

            _logger.WriteInfo($"Packaging {filePath} into rule {name} complete.");

            _logger.WriteVerbose($"Uploading rule files to {instance.PlainName}");
            bool ok = await UploadRuleFilesAsync(instance, name, inMemoryFiles, cancellationToken);

            if (ok)
            {
                _logger.WriteInfo($"All {name} files successfully uploaded to {instance.PlainName}.");
            }

            return(ok);
        }
Ejemplo n.º 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);
                }
            }
        }
Ejemplo n.º 6
0
        internal async Task <bool> InvokeLocalAsync(string projectName, string @event, int workItemId, string ruleFilePath, bool dryRun, SaveMode saveMode, bool impersonateExecution, 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(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, clientsContext, cancellationToken);

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

                    return(true);
                }
            }
        }