【问题标题】:How to programmatically set an AzureDevOps PullRequest to complete Automatically?如何以编程方式将 Azure DevOps 拉取请求设置为自动完成?
【发布时间】:2018-08-26 10:39:17
【问题描述】:

如下面的代码所示,创建拉取请求时,我还想将其设置为自动完成。意思是当它上面的所有完成条件都满足时,拉取请求会自动完成,这样作者就不必通过vsts接口手动完成了。

任何建议如何执行?在创建拉取请求时,我似乎没有任何可能。换句话说,创建拉取请求的界面没有显示任何自动完成选项。

这是我的示例代码:

public static void CreatePullRequestAndSetAutoComplete(GitHttpClient gitHttpClient, string repositoryId, GitPullRequest pullRequest, string mergeCommitMessage)
        {
            pullRequest = gitHttpClient.CreatePullRequestAsync(
                pullRequest, 
                repositoryId, 
                cancellationToken: CancellationToken.None).Result;
}

【问题讨论】:

标签: c# autocomplete azure-devops pull-request


【解决方案1】:

这是一个两步的过程。首先,您需要创建一个拉取请求,然后通过设置其自动完成器的身份以及可选的一些其他参数来更新它,如下面的代码所示。

using System.Threading;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.WebApi;

namespace CreateVstsPullRequestAndSetAutoComplete
{
    public class PullRequestAutoCompleter
    {
        /// <summary>
        /// Creates a pull request, and then sets it to auto complete. 
        /// </summary>
        /// <param name="gitHttpClient">GitHttpClient that is created for accessing vsts repo, and codebase.</param>
        /// <param name="repositoryId">The unique identifier of the repository</param>
        /// <param name="pullRequest">The pull request to be created, and then set autocomplete.</param>
        /// <param name="mergeCommitMessage">Provides text to post, when the pull request is completed and merged.</param>
        public static GitPullRequest CreatePullRequestAndSetAutoComplete(GitHttpClient gitHttpClient, string repositoryId, GitPullRequest pullRequest, string mergeCommitMessage)
        {
            // 1- Create the pull request.
            pullRequest = gitHttpClient.CreatePullRequestAsync(
                pullRequest, 
                repositoryId, 
                cancellationToken: CancellationToken.None).Result;

            //2- Set autocomplete.
            pullRequest = EnableAutoCompleteOnAnExistingPullRequest(gitHttpClient, pullRequest, mergeCommitMessage);

            return pullRequest;
        }

        /// <summary>
        /// Sets an existing (meaning created earlier) pullrequest to complete automatically, 
        /// once all of its completion conditions are resolved.
        /// (i.e., a(many) reviewer(s) has(have) approved the pull request, the author has resolved all the commits, and etc)
        /// </summary>
        /// <param name="gitHttpClient">GitHttpClient that is created for accessing vsts repo, and codebase.</param>
        /// <param name="pullRequest">Is an existing pull request, meaning it was created before.</param>
        /// <param name="mergeCommitMessage">Provides text to post, when the pull request is completed and merged.</param>
        /// <returns>An updated pull request, where the update is maninly about setting the autocomplete on it. </returns>
        public static GitPullRequest EnableAutoCompleteOnAnExistingPullRequest(GitHttpClient gitHttpClient, GitPullRequest pullRequest, string mergeCommitMessage)
        {
            var pullRequestWithAutoCompleteEnabled = new GitPullRequest
            {
                AutoCompleteSetBy = new IdentityRef { Id = pullRequest.CreatedBy.Id },
                CompletionOptions = new GitPullRequestCompletionOptions
                {
                    SquashMerge = true,
                    DeleteSourceBranch = true, // false if prefered otherwise
                    MergeCommitMessage = mergeCommitMessage
                }
            };

            GitPullRequest updatedPullrequest = gitHttpClient.UpdatePullRequestAsync(
                pullRequestWithAutoCompleteEnabled, 
                pullRequest.Repository.Id, 
                pullRequest.PullRequestId).Result;

            return updatedPullrequest;
        }
    }
}

【讨论】:

  • 为什么不在第一次创建拉取请求时设置自动完成标识?这必须是一个两步过程吗?
  • 据我记忆,这在当时是不可行的。如果今天可以做到,那为什么不呢!
  • 哦,是的,我并不是说我知道它可以完成,我只是想知道您是否还记得为什么它需要一个两步过程的细节:)
  • 有助于看到更新对象是GitPullRequest 的一个新实例 - 不要只是重用CreatePullRequestAsync 返回的那个
猜你喜欢
  • 1970-01-01
  • 2020-02-23
  • 2018-04-30
  • 1970-01-01
  • 2020-06-17
  • 1970-01-01
  • 2020-05-27
  • 2020-01-25
相关资源
最近更新 更多