示例#1
0
        /// <summary>
        /// Sets a status on the pull request.
        /// </summary>
        /// <param name="status">The description of the status which should be set.</param>
        /// <exception cref="AzureDevOpsPullRequestNotFoundException">If pull request could not be found and
        /// <see cref="AzureDevOpsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>true</c>.</exception>
        public void SetStatus(AzureDevOpsPullRequestStatus status)
        {
            status.NotNull(nameof(status));

            if (!this.ValidatePullRequest())
            {
                return;
            }

            using (var gitClient = this.gitClientFactory.CreateGitClient(this.CollectionUrl, this.credentials))
            {
                var request =
                    gitClient.CreatePullRequestStatusAsync(
                        new GitPullRequestStatus
                {
                    State       = status.State.ToGitStatusState(),
                    Description = status.Description,
                    TargetUrl   = status.TargetUrl?.ToString(),
                    Context     = new GitStatusContext()
                    {
                        Name  = status.Name,
                        Genre = status.Genre,
                    },
                },
                        this.pullRequest.Repository.Id,
                        this.pullRequest.PullRequestId);

                try
                {
                    var postedStatus = request.Result;

                    if (postedStatus == null)
                    {
                        throw new AzureDevOpsPullRequestNotFoundException(
                                  this.pullRequest.Repository.Id,
                                  this.pullRequest.PullRequestId);
                    }

                    this.log.Verbose(
                        "Set status '{0}' to {1}.",
                        postedStatus.Context?.Name,
                        postedStatus.State.ToString());
                }
                catch (Exception ex)
                {
                    this.log.Error("Error posting pull request status: " + ex.InnerException?.Message);
                    throw;
                }
            }
        }
        /// <summary>
        /// Sets a status on the pull request.
        /// </summary>
        /// <param name="status">The description of the status which should be set.</param>
        /// <exception cref="AzureDevOpsPullRequestNotFoundException">If pull request could not be found and
        /// <see cref="AzureDevOpsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>true</c>.</exception>
        public void SetStatus(AzureDevOpsPullRequestStatus status)
        {
            status.NotNull(nameof(status));

            if (!this.ValidatePullRequest())
            {
                return;
            }

            using (var gitClient = this.gitClientFactory.CreateGitClient(this.CollectionUrl, this.credentials))
            {
                var postedStatus =
                    gitClient
                    .CreatePullRequestStatusAsync(
                        new GitPullRequestStatus
                {
                    State       = status.State.ToGitStatusState(),
                    Description = status.Description,
                    TargetUrl   = status.TargetUrl?.ToString(),
                    Context     = new GitStatusContext()
                    {
                        Name  = status.Name,
                        Genre = status.Genre,
                    },
                },
                        this.pullRequest.Repository.Id,
                        this.pullRequest.PullRequestId)
                    .ConfigureAwait(false)
                    .GetAwaiter()
                    .GetResult();

                if (postedStatus == null)
                {
                    throw new AzureDevOpsPullRequestNotFoundException(
                              this.pullRequest.Repository.Id,
                              this.pullRequest.PullRequestId);
                }

                this.log.Verbose(
                    "Set status '{0}' to {1}.",
                    postedStatus.Context?.Name,
                    postedStatus.State.ToString());
            }
        }