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); }
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); }
/// <inheritdoc /> public async Task <IRule> GetRule(string name) { var ruleFilePath = GetRuleFilePath(name); var(preprocessedRule, _) = await RuleFileParser.ReadFile(ruleFilePath); return(new ScriptedRuleWrapper(name, preprocessedRule)); }
#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); } } }
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); } } }