如何以编程方式将审阅者分配给 Azure DevOps Pull 请求?

2024-04-23

我知道如何使用 gitHttpClient 在 VSTS 中创建拉取请求,如下所示gitHttpClient.CreatePullRequestAsync(gitPullRequest, repositoryId).Result,但我不知道如何添加审稿人。有什么建议么?

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

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

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

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

    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);
            }
   }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何以编程方式将审阅者分配给 Azure DevOps Pull 请求? 的相关文章

  • C# 用数组封送结构体

    假设我有一个类似于 public struct MyStruct public float a 我想用一些自定义数组大小实例化一个这样的结构 在本例中假设为 2 然后我将其封送到字节数组中 MyStruct s new MyStruct s
  • HttpClient 像浏览器一样请求

    当我通过 HttpClient 类调用网站 www livescore com 时 我总是收到错误 500 可能服务器阻止了来自 HttpClient 的请求 1 还有其他方法可以从网页获取html吗 2 如何设置标题来获取html内容 当
  • 当 Cortex-M3 出现硬故障时如何保留堆栈跟踪?

    使用以下设置 基于 Cortex M3 的 C gcc arm 交叉工具链 https launchpad net gcc arm embedded 使用 C 和 C FreeRtos 7 5 3 日食月神 Segger Jlink 与 J
  • 按字典顺序对整数数组进行排序 C++

    我想按字典顺序对一个大整数数组 例如 100 万个元素 进行排序 Example input 100 21 22 99 1 927 sorted 1 100 21 22 927 99 我用最简单的方法做到了 将所有数字转换为字符串 非常昂贵
  • .Net Core / 控制台应用程序 / 配置 / XML

    我第一次尝试使用新的 ConfigurationBuilder 和选项模式进入 Net Core 库 这里有很多很好的例子 https docs asp net en latest fundamentals configuration ht
  • 使用 LINQ 查找列表中特定类型的第一个元素

    使用 LINQ 和 C 在元素列表中查找特定类型的第一个项目的最短表示法是什么 var first yourCollection OfType
  • 像“1$”这样的位置参数如何与 printf() 一起使用?

    By man I find printf d width num and printf 2 1 d width num 是等价的 但在我看来 第二种风格应该与以下相同 printf d num width 然而通过测试似乎man是对的 为什
  • AccessViolationException 未处理

    我正在尝试使用史蒂夫 桑德森的博客文章 http blog stevensanderson com 2010 01 28 editing a variable length list aspnet mvc 2 style 为了在我的 ASP
  • 什么是 C 语言的高效工作流程? - Makefile + bash脚本

    我正在开发我的第一个项目 该项目将跨越多个 C 文件 对于我的前几个练习程序 我只是在中编写了我的代码main c并使用编译gcc main c o main 当我学习时 这对我有用 现在 我正在独自开展一个更大的项目 我想继续自己进行编译
  • 将日期参数传递给对 MVC 操作的 ajax 调用的安全方法

    我有一个 MVC 操作 它的参数之一是DateTime如果我通过 17 07 2012 它会抛出一个异常 指出参数为空但不能有空值 但如果我通过01 07 2012它被解析为Jan 07 2012 我将日期传递给 ajax 调用DD MM
  • 方法参数内的变量赋值

    我刚刚发现 通过发现错误 你可以这样做 string s 3 int i int TryParse s hello out i returns false 使用赋值的返回值是否合法 Obviously i is but is this th
  • 为什么 strtok 会导致分段错误?

    为什么下面的代码给出了Seg 最后一行有问题吗 char m ReadName printf nRead String s n m Writes OK char token token strtok m 如前所述 读取字符串打印没有问题 但
  • 将输入数据发送到node.js中的子进程

    我正在编写代码以在 Node js 环境中创建在线 C 编译器 使用spawn函数我创建了一个子进程 它将编译代码并执行它并将输出发送回用户 但我需要将输入发送到正在运行的 C 程序 我用了child stdin write data 用于
  • 将新行添加到表后如何更新 datagridview 的行列表

    我有一个 datagridview 在表单的加载事件上填充了表集合 我还有一个由用户填写的表单 并将新行添加到表 onclick 事件 我想在向该表添加新行后更新 datagridview表 我使用绑定到绑定数据源的 sqladapter
  • 使用 libcurl 获取 https

    我正在尝试连接到 google api 这在我的终端中运行良好 我正在这样做 curl https www googleapis com tasks v1 users me lists header Authorization Bearer
  • IIS 上托管的 WCF 服务无法运行

    我想构建一个公开 basicHTTP 端点和 webHTTP 端点的服务 如果我在运行模式下使用 VS2010 测试以下项目 一切都很好 但我想在 IIS 中托管服务 本地或远程 并通过测试 服务 svc 我将我的网站托管到本地 IIS 中
  • 将非模板基类向下转型为模板化派生类:可能吗?

    我正在为游戏实现一个事件系统 它使用事件队列和数据结构来保存给定事件类型的所有注册事件处理程序 到目前为止 注册处理程序工作得很好 但是当涉及到取消注册它们时 例如 当游戏对象被销毁时会发生这种情况 我在模板和转换方面遇到了一些麻烦 我将
  • 如何将小时数大于24的字符串解析为TimeSpan?

    如何在 C 中将 30 15 这样的字符串解析为 TimeSpan 30 15 表示 30 小时 15 分钟 string span 30 15 TimeSpan ts TimeSpan FromHours Convert ToDouble
  • 需要在c#中的字符串中的“单词”之后获取一个字符串

    我在 C 中有一个字符串 我必须在字符串中找到特定的单词 code 并且必须获取单词 code 之后的剩余字符串 该字符串是 错误描述 code 1 所以我必须找到这个词code在上面的字符串中 我必须获取错误代码 我见过正则表达式 但现在
  • 如何命名泛型类的 C# 源文件

    我试图坚持通用命名约定 例如中描述的那些开发类库的设计指南 http msdn microsoft com en us library ms229042 aspx 我将每种类型放入其自己的源文件中 并且部分类将拆分为多个文件 如问题中所述部

随机推荐

  • css 中 & 的作用是什么

    我正在查看一些遗留代码 并在 css 文件中发现如下内容 user modal width auto height auto modal fade in margin top 0 margin left 0 top 83px 这里 的目的是
  • 向后滑动 - InteractivePopGestureRecognizer 不起作用

    我有像这张照片这样的屏幕 HomeViewController 将推送到 maintabbar 并且选项卡栏项目将推送到detailScreen 为什么向后滑动不起作用 我认为这是IOS默认的 请帮我 p s 如果我在 cocoapods
  • 奇怪的 bash 脚本行为 - 生成的命令在复制粘贴时有效,但在脚本中无效

    出于安全原因 我编写了一个简短的 bash 脚本来包装 ansible playbook 命令 这并不复杂 而且大部分脚本在这里都是无关紧要的 最后 我将脚本参数生成的变量中的 ansible 命令放在一起 如下所示 ansible pla
  • Spring:文件上传RESTFUL Web服务

    我正在使用 Spring 4 0 为 RESTFUL Web 服务创建 POC 如果我们只传递字符串或任何其他基本数据类型 它就可以正常工作 RequestMapping value upload file method RequestMa
  • 适用于 MYSQL 的 Logstash Jdbc 输入插件

    我在 Windows 中使用 Logstash 我无法安装输入 jdbc 插件 因此我手动下载了 zip 文件 并将插件中的logstash 文件夹放入我的logstash 1 5 2 文件夹中 文件夹结构 D elastic search
  • Matplotlibight_layout——删除多余的白色/空白区域

    我想尽量减少人物周围的空白 但不确定如何 a 为我的图像周围的 savefig 命令精确指定一个边界框 并且 b 为什么紧密布局命令在我的工作示例中不起作用 In my current example I set up an axis en
  • 使 ASP.NET MVC 应用程序为 Web Farm 做好准备

    使 ASP NET MVC 应用程序 Web 场做好准备的最有效方法是什么 最重要的是共享当前用户的信息 上下文 和 不太重要 缓存的对象 例如查找项目 州 街道类型 县等 我听说过 读过 MemCache 但还没有看到关于如何实现和测试它
  • PHP 将整数转换为 hh:mm:ss

    我有一个 hh mm ss 格式的字符串 我将其转换为表示总秒数的整数 例如 01 43 03 01 3600 43 60 03 1 上面的示例生成整数值 6183 使用该值执行一些逻辑后 我需要将整数转换回严格的 hh mm ss 格式
  • ActiveAdmin:按子对象计数过滤

    在严重依赖 ActiveAdmin 的 Ruby on Rails 应用程序中 我有一个赞助商模型 它与赞助商模型关联 一sponsor可以资助很多孩子 所以一个sponsor可以有很多sponsorships 我想做的是能够在赞助商索引页
  • 无法使用 RVM、Ruby 1.9.2 和 Rails 3 运行 RubyMine 调试器

    我已经设置了全新的 Ubuntu 安装并遵循本指南 http ryanbigg com 2010 12 ubuntu ruby rvm rails and you安装 RVM Ruby 1 9 2 和 Rails 3 然后我安装了RubyM
  • ADAL.js 和 MSAL.js 有什么区别?

    我正在尝试处理使用 Microsoft Graph 的应用程序的身份验证 这两个库有什么区别 JavaScript 的 Active Directory 身份验证库 ADAL js https github com AzureAD azur
  • java中如何读取xep文件数据

    有没有办法在 pdf 完全渲染之前获取总页数并输入我们提供 xml 文件 xslt 是页面布局的样式表 我们使用 RenderX 从 xslt 进行 pdf 转换 将其转换为 xsl fo 文件 从 xsl fo 转换为 xep 文件 从
  • Foreman/Puma 未使用开发环境中指定的端口

    我在 application yml 中将端口设置为 3000 figaro 管理环境变量 rails s使用端口 3000 但当我跑步时foreman start 根据 Heroku 的推荐 我得到以下输出 14 53 23 web 1
  • 错误:找不到函数“geom_sf”

    我目前在 Windows 上运行 R 版本 3 4 2 并拥有 ggplot2 通过 tidyverse 和 sf 包版本 3 4 2 我正在尝试使用 ggplot2 sf 套件来绘制空间数据 尝试运行 geom sf 时 我收到错误 co
  • 为什么我的解密方法抛出“要解密的数据长度无效”加密异常

    这是一个非常常见的异常 但显然我找到的解决方案都没有解决我的问题 我有一个加密和一个解密方法 我加密一个字符串并将其写入文件 然后从文件中读取该字符串并解密 理论上 事实上 我得到了一个 加密异常 要解密的数据长度无效 在该过程的解密方面
  • 为什么在 Rust 中无法在不引用其中之一的情况下连接两个字符串?

    这有效 let hello Hello to string let world world let hello world hello world 但这并没有 let hello Hello to string let world worl
  • 如何在windows上安装mongoDB?

    我正在尝试测试 mongoDB 看看它是否适合我 我下载了 32 位 Windows 版本 但不知道如何继续 我通常使用 WAMP 服务在本地计算机上进行开发 我可以在 Wamp 上运行 mongoDB 吗 但是 使它在 Windows 上
  • T-SQL 去除所有非字母和非数字字符

    有没有一种更聪明的方法来删除所有特殊字符 而不是使用一系列大约 15 个嵌套替换语句 以下代码有效 但仅处理三个字符 与号 空格和句点 select CustomerID CustomerName Replace Replace Repla
  • 在 bash 中使用 ssh 内的 Expect 执行 sudo

    我想创建一个脚本来自动在多个 Linux 主机上进行安装 我使用 ssh 密钥登录到主机 在登录中我想做一个 sudo 我正在尝试使用 Expect 我在站上有它 但是我的服务器上没有运行脚本 我该怎么做 这是我的尝试 但没有运气 bin
  • 如何以编程方式将审阅者分配给 Azure DevOps Pull 请求?

    我知道如何使用 gitHttpClient 在 VSTS 中创建拉取请求 如下所示gitHttpClient CreatePullRequestAsync gitPullRequest repositoryId Result 但我不知道如何