Ejemplo n.º 1
0
        public async Task RunAsync(IAzureDevOpsClient azDoclient, GitPushEventPayload payload)
        {
            _logger.LogInformation(new EventId(1, "RunningPolicies"), "Running policies for {RepositoryId} with {UpdateCount}", payload.Resource.Repository.Id, payload.Resource.RefUpdates.Count);

            //run policies serially to avoid race conditions in AzDO like duplicate pull request creation.
            foreach (var policy in _policies)
            {
                _logger.LogDebug(new EventId(2, "RunningPolicy"), "Running {Policy}", policy.Name);
                foreach (var update in payload.Resource.RefUpdates)
                {
                    if (IsNewBranch(update.OldObjectId))
                    {
                        _logger.LogInformation(new EventId(2, "SkippingNewBranch"), "Skipping new branch push");
                        continue;
                    }

                    var context = new MergePolicyContext(azDoclient, update, payload);
                    try
                    {
                        await policy.HandleAsync(context);
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(new EventId(7, "PolicyFailure"), e, "Policy {@Policy} for {RepositoryId} failed: {Message}", policy, payload.Resource.Repository.Id, e.Message);
                    }
                }
            }
        }
        public override async Task HandleAsync(MergePolicyContext context)
        {
            var update       = context.Update;
            var payload      = context.Payload;
            var azDoClient   = context.AzDoClient;
            var sourceBranch = _sourceBranch !;
            var targetBranch = _targetBranch !;

            if (!string.IsNullOrEmpty(_repositoryId) && !string.Equals(_repositoryId, payload.Resource.Repository.Id))
            {
                _logger.LogDebug(new EventId(5, "SkippingRepoMismatch"), "Skipping {Repository}, does not match {PolicyRepositoryId}", payload.Resource.Repository.Id, _repositoryId);
                return;
            }

            if (!GitBranch.IsEqual(update.Name, sourceBranch !))
            {
                _logger.LogDebug(new EventId(1, "SkippingNonSourceBranch"), "Skipping {BranchName} as it is not does not match {SourceBranch}", update.Name, sourceBranch);
                return;
            }

            var openPullRequests = await azDoClient.GetOpenPullRequestsAsync(payload.Resource.Repository.NormalizedUrl, sourceBranch, targetBranch);

            if (openPullRequests.Count > 0)
            {
                _logger.LogDebug(new EventId(3, "PullRequestAlreadyOpen"), "Pull request already open for {RepositoryId} from {SourceBranch} to {TargetBranch}", payload.Resource.Repository.Id, sourceBranch, targetBranch);
                return;
            }

            _logger.LogInformation(new EventId(2, "CreatingPullRequest"), "Creating pull request for {RepositoryId} from {SourceBranch} to {TargetBranch}", payload.Resource.Repository.Id, sourceBranch, targetBranch);
            var pullRequest = await azDoClient.CreatePullRequestAsync(payload.Resource.Repository.NormalizedUrl, sourceBranch, targetBranch);

            _logger.LogInformation(new EventId(4, "CreatedPullRequest"), "Created pull request {PullRequestId} for {RepositoryId} from {SourceBranch} to {TargetBranch}", pullRequest.PullRequestId, payload.Resource.Repository.Id, sourceBranch, targetBranch);

            _pullRequestGateCompletionMonitor.Monitor(new PullRequestMonitorItem(azDoClient, pullRequest));
        }
Ejemplo n.º 3
0
        public override async Task HandleAsync(MergePolicyContext context)
        {
            var update     = context.Update;
            var azDoClient = context.AzDoClient;
            var payload    = context.Payload;

            if (!string.IsNullOrEmpty(_repositoryId) && !string.Equals(_repositoryId, payload.Resource.Repository.Id))
            {
                _logger.LogDebug(new EventId(5, "SkippingRepoMismatch"), "Skipping {Repository}, does not match {PolicyRepositoryId}", payload.Resource.Repository.Id, _repositoryId);
                return;
            }

            if (!GitBranch.IsRelease(update.Name))
            {
                _logger.LogDebug(new EventId(1, "SkippingNonReleaseBranch"), "Skipping {BranchName} as it is not a release branch", update.Name);
                return;
            }

            var refs = await azDoClient.GetRefsAsync(payload.Resource.Repository.NormalizedUrl);

            var branches = refs.Value
                           .Where(n => GitBranch.IsReleaseOrDefault(n.Name, _defaultBranch !))
                           .Select(n => new GitBranch(n))
                           .OrderBy(n => n, _branchComparer)
                           .ToList();

            var sourceBranchIndex = branches.FindIndex(n => n.Name == update.Name);

            if (branches.Count <= sourceBranchIndex + 1)
            {
                _logger.LogDebug(new EventId(2, "TargetBranchNotFound"), "Target branch was not found for {RepositoryId} and {BranchName}", payload.Resource.Repository.Id, update.Name);
                return;
            }

            var sourceBranch = branches[sourceBranchIndex];
            var targetBranch = branches[sourceBranchIndex + 1];

            var openPullRequests = await azDoClient.GetOpenPullRequestsAsync(payload.Resource.Repository.NormalizedUrl, sourceBranch.Name, targetBranch.Name);

            if (openPullRequests.Count > 0)
            {
                _logger.LogDebug(new EventId(3, "PullRequestAlreadyOpen"), "Pull request already open for {RepositoryId} from {SourceBranch} to {TargetBranch}", payload.Resource.Repository.Id, sourceBranch.Name, targetBranch.Name);
                return;
            }

            _logger.LogInformation(new EventId(1, "CreatingPullRequest"), "Creating pull request for {RepositoryId} from {SourceBranch} to {TargetBranch}", payload.Resource.Repository.Id, sourceBranch.Name, targetBranch.Name);
            var pullRequest = await azDoClient.CreatePullRequestAsync(payload.Resource.Repository.NormalizedUrl, sourceBranch.Ref.Name, targetBranch.Ref.Name);

            _logger.LogInformation(new EventId(1, "CreatedPullRequest"), "Created pull request {PullRequestId} for {RepositoryId} from {SourceBranch} to {TargetBranch}", pullRequest.PullRequestId, payload.Resource.Repository.Id, sourceBranch.Name, targetBranch.Name);

            _pullRequestGateCompletionMonitor.Monitor(new PullRequestMonitorItem(azDoClient, pullRequest));
        }
 public abstract Task HandleAsync(MergePolicyContext context);