如何以编程方式将 AzureDevOps PullRequest 设置为自动完成? [英] How to programmatically set an AzureDevOps PullRequest to complete Automatically?

查看:29
本文介绍了如何以编程方式将 AzureDevOps PullRequest 设置为自动完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When creating a pull request as my code below shows, I also would like to set it to complete automatically. Meaning when all the completion conditions on it are met, the pull request completes automatically, so that the author does not have to manually complete it through the vsts interface.

有什么建议可以执行吗?似乎在创建拉取请求时我没有任何可能性.换句话说,用于创建拉取请求的界面没有显示任何自动完成选项.

Any suggestions how this can be performed? It seems at the time of pull request creation I do not have any possibility for this. In other words the interface for creating a pull request does not show any options for auto complete.

这是我的示例代码:

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

推荐答案

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

This is a 2-step process. First you need to create a pull request, and second update it by setting its autocompleter’s identity, and optionally some other parameters as the code below illustrates.

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

这篇关于如何以编程方式将 AzureDevOps PullRequest 设置为自动完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆