如何以编程方式将审阅者分配给AzureDevOps PullRequest?

编程入门 行业动态 更新时间:2024-10-14 06:23:55
本文介绍了如何以编程方式将审阅者分配给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?

本文发布于:2023-11-28 18:23:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643465.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   AzureDevOps   PullRequest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!