如何检索链接到特定提交的工作项 - Azure Devops REST API

How to retrieve Work Item linked to specific commit - Azure Devops REST API

我需要能够检索任何给定特定提交的链接工作项。我目前正在使用以下 api 调用

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}?api-version=5.0

响应如下

{
  "parents": [],
  "treeId": "7fa1a3523ffef51c525ea476bffff7d648b8cb3d",
  "push": {
    "pushedBy": {
      "id": "8c8c7d32-6b1b-47f4-b2e9-30b477b5ab3d",
      "displayName": "Chuck Reinhart",
      "uniqueName": "fabrikamfiber3@hotmail.com",
      "url": "https://vssps.dev.azure.com/fabrikam/_apis/Identities/8c8c7d32-6b1b-47f4-b2e9-30b477b5ab3d",
      "imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=8c8c7d32-6b1b-47f4-b2e9-30b477b5ab3d"
    },
    "pushId": 1,
    "date": "2014-01-29T23:33:15.2434002Z"
  },
  "commitId": "be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4",
  "author": {
    "name": "Chuck Reinhart",
    "email": "fabrikamfiber3@hotmail.com",
    "date": "2014-01-29T23:32:09Z"
  },
  "committer": {
    "name": "Chuck Reinhart",
    "email": "fabrikamfiber3@hotmail.com",
    "date": "2014-01-29T23:32:09Z"
  },
  "comment": "First cut\n",
  "url": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4",
  "remoteUrl": "https://dev.azure.com/fabrikam/_git/Fabrikam-Fiber-Git/commit/be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4",
  "_links": {
    "self": {
      "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4"
    },
    "repository": {
      "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249"
    },
    "changes": {
      "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4/changes"
    },
    "web": {
      "href": "https://dev.azure.com/fabrikam/_git/Fabrikam-Fiber-Git/commit/be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4"
    },
    "tree": {
      "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/trees/7fa1a3523ffef51c525ea476bffff7d648b8cb3d"
    }
  }
}

来自 https://docs.microsoft.com/en-us/rest/api/azure/devops/git/commits/get?view=azure-devops-rest-5.0 并且我缺少一种方法来查看其链接到的工作项或是否完全链接。有谁知道获取此信息的方法?谢谢

您可以使用 Get Commits API、docs here。基本请求如下所示:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits?api-version=5.0

然后您可以添加以下参数:

  • fromCommitId - 字符串 - 如果提供,按字母顺序过滤提交的下限
  • toCommitId - 字符串 - 如果提供,按字母顺序过滤提交的上限
  • includeWorkItems - 布尔值 - 是否包含链接的工作项

因此您的最终查询看起来像这样,您的 toCommitId 和 fromCommitId 参数是您之后的提交 ID(文档没有具体说明这些是包含的还是排他的,因此您可能需要稍微调整一下):

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits?includeWorkItems=true&.toCommitId={searchCriteria.toCommitId}&fromCommitId={searchCriteria.fromCommitId}&api-version=5.0

结果应根据 this documentation.

在响应的每个提交对象中包含一个 workItems 属性

注:

Parameters that use the searchCriteria prefix in their name can be specified without it as query parameters, e.g. searchCriteria.$top -> $top


还有:

  • ids - 数组 - 如果提供,指定要获取的提交的确切提交 ID。不得与其他参数结合使用。

可以允许您放弃传入和传出提交 ID,但文档声明它不能与其他参数组合 - 即使示例请求 确实 将其与其他参数组合。我自己还没有尝试过,所以当你发现你是使用 from-to id 还是只使用 ids 时请发表评论。


OP 操作

OP 最终使用了以下请求,因为他们不介意返回所有提交:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits?includeWorkItems=true&api-version=5.0