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);
        }
Beispiel #2
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);
                }
            }
        }
        public static bool IsTestEvent(this WorkItemEventContext eventContext)
        {
            const string TEST_EVENT_COLLECTION_URL = "http://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/";

            var workItem = eventContext.WorkItemPayload.WorkItem;

            return(workItem.Url.StartsWith(TEST_EVENT_COLLECTION_URL, StringComparison.OrdinalIgnoreCase));
        }
Beispiel #4
0
        public static bool IsTestEvent(this WorkItemEventContext eventContext)
        {
#pragma warning disable S5332 //Using http protocol is insecure
            const string TEST_EVENT_COLLECTION_URL = "http://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/";
#pragma warning restore S5332 //Using http protocol is insecure

            var workItem = eventContext.WorkItemPayload.WorkItem;
            return(workItem.Url.StartsWith(TEST_EVENT_COLLECTION_URL, StringComparison.OrdinalIgnoreCase));
        }
Beispiel #5
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);
        }
 public async Task <string> ExecuteAsync(IRule rule, WorkItemEventContext eventContext, CancellationToken cancellationToken)
 {
     logger.WriteVerbose($"Connecting to Azure DevOps using {configuration.DevOpsTokenType}...");
     if (configuration.DevOpsTokenType == DevOpsTokenType.PAT)
     {
         var clientCredentials = new VssBasicCredential(configuration.DevOpsTokenType.ToString(), configuration.DevOpsToken);
         // see https://rules.sonarsource.com/csharp/RSPEC-4457
         return(await ExecAsyncImpl(rule, eventContext, clientCredentials, cancellationToken));
     }
     else
     {
         logger.WriteError($"Azure DevOps Token type {configuration.DevOpsTokenType} not supported!");
         throw new ArgumentException($"Azure DevOps Token type {configuration.DevOpsTokenType} not supported.");
     }
 }
Beispiel #7
0
        private async Task <VssConnection> ConnectToAzureDevOpsAsync(WorkItemEventContext eventContext, VssCredentials clientCredentials, CancellationToken cancellationToken)
        {
            // see https://docs.microsoft.com/en-us/azure/devops/integrate/concepts/rate-limits#api-client-experience
            var policy = Policy
                         .Handle <HttpRequestException>()
                         // https://github.com/App-vNext/Polly/wiki/Retry#retryafter-when-the-response-specifies-how-long-to-wait
                         .OrResult <HttpResponseMessage>(r => r.StatusCode == (HttpStatusCode)429)
                         .WaitAndRetryAsync(
                retryCount: MaxRetries,
                sleepDurationProvider: (retryCount, response, context) => {
                return(response.Result?.Headers.RetryAfter.Delta.Value
                       ?? TimeSpan.FromSeconds(BaseRetryInterval * retryCount));
            },
#pragma warning disable CS1998
                onRetryAsync: async(response, timespan, retryCount, context) => {
                logger.WriteInfo($"{Environment.NewLine}Waiting {timespan} before retrying (attemp #{retryCount}/{MaxRetries})...");
            }
#pragma warning restore CS1998
                );
            var handler = new PolicyHttpMessageHandler(policy);

            var vssHandler = new VssHttpMessageHandler(clientCredentials, VssClientHttpRequestSettings.Default.Clone());

            var devops = new VssConnection(eventContext.CollectionUri, vssHandler, new DelegatingHandler[] { handler });

            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;
            }

            return(devops);
        }