internal async Task <string> Execute(string aggregatorVersion, dynamic data) { if (string.IsNullOrEmpty(aggregatorVersion)) { aggregatorVersion = "0.1"; } string collectionUrl = data.resourceContainers.collection.baseUrl; int workItemId = data.resource.id; logger.WriteVerbose($"Connecting to VSTS using {configuration.VstsTokenType}..."); var clientCredentials = default(VssCredentials); if (configuration.VstsTokenType == VstsTokenType.PAT) { clientCredentials = new VssBasicCredential(configuration.VstsTokenType.ToString(), configuration.VstsToken); } else { logger.WriteError($"VSTS Token type {configuration.VstsTokenType} not supported!"); throw new ArgumentOutOfRangeException(nameof(configuration.VstsTokenType)); } var vsts = new VssConnection(new Uri(collectionUrl), clientCredentials); await vsts.ConnectAsync(); logger.WriteInfo($"Connected to VSTS"); var witClient = vsts.GetClient <WorkItemTrackingHttpClient>(); var context = new Engine.EngineContext(witClient); var store = new Engine.WorkItemStore(context); var self = store.GetWorkItem(workItemId); logger.WriteInfo($"Initial WorkItem retrieved"); string ruleFilePath = Path.Combine(functionDirectory, $"{ruleName}.rule"); if (!File.Exists(ruleFilePath)) { logger.WriteError($"Rule code not found at {ruleFilePath}"); return("Rule file not found!"); } logger.WriteVerbose($"Rule code found at {ruleFilePath}"); string ruleCode = File.ReadAllText(ruleFilePath); logger.WriteInfo($"Executing Rule..."); var globals = new Engine.Globals { self = self, store = store }; var types = new List <Type>() { typeof(object), typeof(System.Linq.Enumerable), typeof(System.Collections.Generic.CollectionExtensions) }; var references = types.ConvertAll(t => t.Assembly); var scriptOptions = ScriptOptions.Default .WithEmitDebugInformation(true) .WithReferences(references) // Add namespaces .WithImports("System") .WithImports("System.Linq") .WithImports("System.Collections.Generic"); var result = await CSharpScript.EvaluateAsync <string>( code : ruleCode, options : scriptOptions, globals : globals, globalsType : typeof(Engine.Globals)); logger.WriteInfo($"Rule returned {result}"); logger.WriteVerbose($"Post-execution, save all changes..."); context.SaveChanges(); logger.WriteInfo($"Changes saved to VSTS"); return(result); }