如何通过rest api创建Azure DevOps任务

问题描述 投票:0回答:3

我编写了一些 PowerShell 函数来帮助我更快地创建用户故事,这一切都很好,但现在我陷入了如何为用户故事/工作项创建任务的困境,并且显然将它们分配给一个特定的工作项目。

我也找不到任何描述这一点的文档。 我几乎想象我需要使用 uri "https://dev.azure.com/$($Organization)/$Project/_apis/wit/workitems/`$Task?api-version=5.1" 但我不能了解如何将其与工作项关联起来作为此过程的一部分或之后。

任何人都可以帮助我或给我指出一些实际的文档吗?

编辑:在寻找其他东西时,我偶然发现了this,但遗憾的是这对我来说是错误的,所以它现在可能已被弃用......

编辑;感谢大家的帮助。这现在对我有用 这是我的代码,以防将来有一天它对某人有用:

#96116 is the parent work item, 96113 the child task
$ContentType = "application/json-patch+json"
$Token = System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PersonalAccessToken)"))
$Header = @{Authorization = 'Basic ' + $Token;accept=$ContentType}
$uri = "https://dev.azure.com/$Organisation/$Project/_apis/wit/workitems/96113?api-version=6.1-preview.3"
$body= @'
[
  {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "System.LinkTypes.Hierarchy-Reverse",
      "url": "https://dev.azure.com/$Organisation/$Project/_apis/wit/workitems/96113",
      "attributes": {
          "isLocked": false,
          "name": "Parent"
      }
    }
  }
]
'@
Invoke-RestMethod -Uri $uri -Method PATCH -Headers $Header -Body $Body -ContentType $contentType
powershell azure-devops-rest-api
3个回答
4
投票

您可以按照以下步骤创建一个新的

Task
,并链接指定的
User Story
作为这个新Task
Parent

  1. 使用端点“工作项 - 创建”创建新的

    Task

  2. 使用端点“工作项 - 更新”将指定的

    User Story
    链接为Parent

    • 请求URI
      PATCH https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?api-version=7.1-preview.3
      
    • 必填标题
      Content-Type: application/json-patch+json
      
    • 请求正文
      [
        {
          "op": "add",
          "path": "/relations/-",
          "value": {
            "rel": "System.LinkTypes.Hierarchy-Reverse",
            // This is the URL of the linked parent work item.
            "url": "https://dev.azure.com/{organization}/{project}/_apis/wit/workItems/{parent work item ID}",
            "attributes": {
                "isLocked": false,
                "name": "Parent"
            }
          }
        }
      ]
      

这个方法我已经测试过,可以正常工作,符合预期。


2
投票

如上所述,可以在创建后使用单独的 PATCH 请求添加关系,但您也可以在单个调用中组合多个工作项跟踪请求。您需要 POST 到

batch
端点并发送 JsonPatchDocuments 数组:

PATCH https://dev.azure.com/{organization}/{project}/_apis/wit/$batch?api-version=2.2
"Content-Type": "application/json"
"Accept": "application/json"
"Authorization": "Basic {PAT}"

[
    {
         "method": "PATCH",
         // Replace $Task below with the WIT you want to create
         "uri": "/{Project}/_apis/wit/workitems/$Task?api-version=2.2",
         "headers": { "Content-Type": "application/json-patch+json" },
         "body": [
              { "op": "add", "path": "/fields/System.Title", "value": "Customer can sign in using their Microsoft Account" },

              // Indicates new work item instead of an update. Each new work item uses a unique negative number in the batch.
              { "op": "add", "path": "/id", "value": "-1" }, 

              {
                  "op": "add",
                  "path": "/relations/-",
                  "value": {
                      "rel": "System.LinkTypes.Hierarchy-Reverse",
                      "url": "https://dev.azure.com/{organization}/{project}_apis/wit/workitems/{work item id to link to}"
                  }
              }
          ]
     }
]

使用此 API,您还可以在一次调用中创建工作项树。您使用负 ID 将工作项链接在一起,并且在执行批处理时将它们转换为实际工作项 ID。

批量 API 的文档在这里:


0
投票

Microsoft 似乎有关于创建删除更新工作项的文档。

你的猜测很接近。这是提供的示例:

请求

POST https://dev.azure.com/fabrikam/{project}/_apis/wit/workitems/${type}?api-version=6.1-preview.3

身体

[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "from": null,
    "value": "Sample task"
  }
]

回复

{
  "id": 131489,
  "rev": 1,
  "fields": {
    "System.AreaPath": "CustomProcessPrj",
    "System.TeamProject": "CustomProcessPrj",
    "System.IterationPath": "CustomProcessPrj",
    "System.WorkItemType": "Task",
    "System.State": "New",
    "System.Reason": "New",
    "System.CreatedDate": "2017-10-06T01:04:51.57Z",
    "System.CreatedBy": {
      "displayName": "Jamal Hartnett",
      "url": "https://vssps.dev.azure.com/fabrikam/_apis/Identities/d291b0c4-a05c-4ea6-8df1-4b41d5f39eff",
      "_links": {
        "avatar": {
          "href": "https://dev.azure.com/mseng/_apis/GraphProfile/MemberAvatars/aad.YTkzODFkODYtNTYxYS03ZDdiLWJjM2QtZDUzMjllMjM5OTAz"
        }
      },
      "id": "d291b0c4-a05c-4ea6-8df1-4b41d5f39eff",
      "uniqueName": "[email protected]",
      "imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=d291b0c4-a05c-4ea6-8df1-4b41d5f39eff",
      "descriptor": "aad.YTkzODFkODYtNTYxYS03ZDdiLWJjM2QtZDUzMjllMjM5OTAz"
    },
    "System.ChangedDate": "2017-10-06T01:04:51.57Z",
    "System.ChangedBy": {
      "displayName": "Jamal Hartnett",
      "url": "https://vssps.dev.azure.com/fabrikam/_apis/Identities/d291b0c4-a05c-4ea6-8df1-4b41d5f39eff",
      "_links": {
        "avatar": {
          "href": "https://dev.azure.com/mseng/_apis/GraphProfile/MemberAvatars/aad.YTkzODFkODYtNTYxYS03ZDdiLWJjM2QtZDUzMjllMjM5OTAz"
        }
      },
      "id": "d291b0c4-a05c-4ea6-8df1-4b41d5f39eff",
      "uniqueName": "[email protected]",
      "imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=d291b0c4-a05c-4ea6-8df1-4b41d5f39eff",
      "descriptor": "aad.YTkzODFkODYtNTYxYS03ZDdiLWJjM2QtZDUzMjllMjM5OTAz"
    },
    "System.Title": "Sample task",
    "Microsoft.VSTS.Common.StateChangeDate": "2017-10-06T01:04:51.57Z",
    "Microsoft.VSTS.Common.Priority": 2
  },
  "_links": {
    "self": {
      "href": "https://dev.azure.com/fabrikam/_apis/wit/workItems/131489"
    },
    "workItemUpdates": {
      "href": "https://dev.azure.com/fabrikam/_apis/wit/workItems/131489/updates"
    },
    "workItemRevisions": {
      "href": "https://dev.azure.com/fabrikam/_apis/wit/workItems/131489/revisions"
    },
    "workItemHistory": {
      "href": "https://dev.azure.com/fabrikam/_apis/wit/workItems/131489/history"
    },
    "html": {
      "href": "https://dev.azure.com/fabrikam/web/wi.aspx?pcguid=20cda608-32f0-4e6e-9b7c-8def7b38d15a&id=131489"
    },
    "workItemType": {
      "href": "https://dev.azure.com/fabrikam/aaee31d9-14cf-48b9-a92b-3f1446c13f80/_apis/wit/workItemTypes/Task"
    },
    "fields": {
      "href": "https://dev.azure.com/fabrikam/_apis/wit/fields"
    }
  },
  "url": "https://dev.azure.com/fabrikam/_apis/wit/workItems/131489"
}
© www.soinside.com 2019 - 2024. All rights reserved.