如何以编程方式将审阅者分配给AzureDevOps PullRequest? [英] How to programmatically assign reviewers to an AzureDevOps PullRequest?

查看:53
本文介绍了如何以编程方式将审阅者分配给AzureDevOps PullRequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用gitHttpClient在VSTS中创建拉取请求,如在 gitHttpClient.CreatePullRequestAsync(gitPullRequest,repositoryId).Result 中一样,但是我不确定如何添加审阅者.有什么建议吗?

I know how to create a pull request in VSTS using gitHttpClient as in gitHttpClient.CreatePullRequestAsync(gitPullRequest, repositoryId).Result, but I am not sure how to add reviewers. Any suggestions?

以下是用于创建拉取请求的示例代码:

Here is a sample code for creating a pull request:

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

推荐答案

注意:要使以下代码正常工作,您需要首先在NuGet控制台中执行以下命令 Install-PackageMicrosoft.TeamFoundationServer.ExtendedClient ,以安装所需的库.

Note: To get the following code working, you need to first execute the following command in NuGet console Install-Package Microsoft.TeamFoundationServer.ExtendedClient, to install the required libraries.

在此解决方案中,先创建拉取请求,然后添加审阅者.以下代码显示了创建拉取请求然后添加审阅者所需的所有步骤.可以轻松地修改代码以添加多个审阅者.

In this solution, the pull request is created first, and then reviewers are added. The following code shows all the steps needed to create the pull request and then add the reviewer. The code can be easily modified to add multiple reviewers.

    using System;
    using System.Threading;
    using Microsoft.TeamFoundation.SourceControl.WebApi;
    using System.Threading.Tasks;
    using Microsoft.VisualStudio.Services.Identity;
    using Microsoft.VisualStudio.Services.Identity.Client;
    using Microsoft.VisualStudio.Services.Common;

namespace AddingReviewersToVstsPullRequestProgramatically
{
    public class PullRequestReviewerAdder
    {

            /// <summary>
            /// Creates a pull request, and then adds a reviewer to it.
            /// </summary>
            /// <param name="gitHttpClient"> GitHttpClient that is created for accessing vsts</param>
            /// <param name="gitPullRequest"> the pull request to be created</param>
            /// <param name="repositoryId"> the unique identifier of the repository</param>
            /// <param name="reviewerAlias"> reviewer's alias in vsts</param>
            /// <param name="vstsAccountUrl">vsts account's url</param>
            /// <param name="personalToken"> personal access token to access the vsts account. </param>
            public static async void CreatePullRequestAndAddReviewer(
                GitHttpClient gitHttpClient,
                GitPullRequest gitPullRequest,
                string repositoryId,
                string reviewerAlias,
                Uri vstsAccountUrl,
                string personalToken)
            {
                // 1- Create the pull request.
                GitPullRequest pullRequest = gitHttpClient.CreatePullRequestAsync(gitPullRequest, repositoryId, cancellationToken: CancellationToken.None).Result;

                // 2- Create an Identity Client to get the reviewer's vsts id
                IdentityHttpClient identityHttpClient = CreateIdentityClient(vstsAccountUrl, personalToken);

                // 3- Find the reviewer's vsts identity.
                Identity identity = SearchForReviewerVstsIdentity(identityHttpClient, reviewerAlias).Result;

                // 4- Create a IdentityRefWithVote for the reviewer
                IdentityRefWithVote identityRefWithVote = new IdentityRefWithVote
                {
                    Id = identity.Id.ToString(),
                    IsRequired = true // false otherwise.
                };

                // 5- Finally add the reviewer to the pull request.
                await AddReviewerToPullRequest(gitHttpClient, pullRequest, identityRefWithVote);
            }

            /// <summary>
            /// Creates an identity client. This is needed for fetching a reviewer's vsts identity.
            /// </summary>
            /// <param name="vstsAccountUrl">vsts account's url</param>
            /// <param name="personalToken"> personal access token to access the vsts account. </param>
            /// <returns>an IdentityHttpClient to use for retrieving identities from vsts. </returns>
            public static IdentityHttpClient CreateIdentityClient(Uri vstsAccountUrl, string personalToken)
            {
                var vstsCredential = new VssBasicCredential(string.Empty, personalToken);
                IdentityHttpClient identityHttpClient = new IdentityHttpClient(vstsAccountUrl, vstsCredential);
                return identityHttpClient;
            }

            /// <summary>
            /// Given an alias on vsts, searches for its vsts identity.
            /// </summary>
            /// <param name="identityHttpClient"> is the vsts identity client.</param>
            /// <param name="alias">is the alias for which the identity is being searched for.</param>
            public static async Task<Identity> SearchForReviewerVstsIdentity(IdentityHttpClient identityHttpClient, string alias)
            {
                try
                {
                    // Notice : you can also search based on factors other than alias.
                    IdentitiesCollection identitiesPerAlias = await identityHttpClient
                        .ReadIdentitiesAsync(IdentitySearchFilter.Alias, alias).ConfigureAwait(false);
                    if (identitiesPerAlias.Count == 1) // Found one identity-- the ideal case
                    {
                        return identitiesPerAlias[0];
                    }

                    Console.WriteLine($"Encountered a problem finding vsts identity foralias {alias}.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Exception when looking for vsts identity for alias {alias}. {ex}");
                }

                // Notice : watch out for null case...
                return null;
            }

            /// <summary>
            /// Adds a reviewer to a an already created pull request.
            /// </summary>
            /// <param name="gitHttpClient">GitHttpClient that is created for accessing vsts</param>
            /// <param name="pullRequest"> pull request that is already created.</param>
            /// <param name="identity">identity of the reviewer that we want to add to the pull request.</param>
            public static async Task AddReviewerToPullRequest(GitHttpClient gitHttpClient, GitPullRequest pullRequest, IdentityRefWithVote identity)
            {
                identity = await gitHttpClient.CreatePullRequestReviewerAsync(
                    identity,
                    pullRequest.Repository.Id,
                    pullRequest.PullRequestId,
                    identity.Id).ConfigureAwait(false);
            }
   }

这篇关于如何以编程方式将审阅者分配给AzureDevOps PullRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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