VSTS 拉取请求 API - 如何获取 IdentityRef

VSTS Pull request API - how to get the IdentityRef

我正在使用 VSTS Pullrequest 创建 API 方法来自动创建 PR,在请求中我需要为 createdby/autocompleteby/reviewers 属性提供各种 IdentityRef id 值。在我的例子中,我所拥有的只是用户详细信息(如全名、电子邮件地址 - user1@domain.com ),在这种情况下我如何检索 IdentityRef Guid 以便我可以将它传递给 PR Create API .

https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/create?view=azure-devops-rest-5.0

感谢任何帮助。

1) 使用 Graph - List Users,但还要检查 X-MS-ContinuationToken 响应 header 以确定是否还有其他分页数据需要被检索。如果是这样,重新发送带有延续令牌值的请求,直到返回所有值:

  • GET https://vssps.dev.azure.com/{organization}/_apis/graph/users?api-version=5.0-preview.1,接着是
  • GET https://vssps.dev.azure.com/{organization}/_apis/graph/users?continuationToken={continuationToken}&api-version=5.0-preview.1

(我想知道这是否就是为什么您在上面的评论中只能获得 500 个用户。不幸的是,文档没有列出此 API 的最大页面大小。如果您 使用延续令牌并且所有用户都没有返回,这对我来说听起来像是一个 API 错误。)

2) 使用Get User Entitlements。这提供了 topskip 参数。根据文档,top 参数有 10000 条记录的限制。

  • GET https://vsaex.dev.azure.com/{organization}/_apis/userentitlements?top=10000&api-version=5.0-preview.2

更简单的方法是使用 Identities - Read Identities API, which allows us to query for the IdentityRef of a user by email, by IdentityDescriptors, by ids, by name and by SubjectDescriptors。这避免了必须重复获取用户图来定位单个用户的昂贵方法,并且还有助于直接获取 IdentityRef 而不仅仅是用户描述符。