从 Postman 在 Azure Devops 中创建工作项

Create workitem in Azure Devops from Postman

我们希望将所有项目从 Jira 迁移到 Azure Devops,其中之一就是我们的帮助台功能。

我的任务是移植我们当前关于工单的解决方案:我们有一个 Laravel 项目 运行 检查某些电子邮件地址是否有新的帮助请求。当一封新电子邮件到达时,它会向我们的 Slack 发送一条通知,其中一名团队成员会在其中确认并选择在 Jira 中的何处创建工单。然后它会根据 Slack 中的选择在 Jira 中创建一个带有 api-call 的新工单。

我发现并试用了 Azure Devops API,可以列出项目和获取工作项等。但是我不能通过调用

POST 一个新的工作项
http://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$task?api-version=6.1-preview.3&bypassRules=true

与 JSON body 像这样:

[
    {
        "op": "add",
        "path": "/fields/System.Title",
        "from": null,
        "value": "Test of REST functionality"
    },
    {
        "op": "add",
        "path": "/fields/System.State",
        "from": null,
        "value": "New"
    }
]

我收到了这样的 JSON 回复

{
    "fields": {
        "System.WorkItemType": "Task",
        "System.AreaPath": "xxxxx",
        "System.TeamProject": "xxxx",
        "System.IterationPath": "xxxx",
        "System.State": "New",
        "System.Reason": "New",
        "Microsoft.VSTS.Common.StateChangeDate": "1753-01-01T00:00:00Z",
        "System.ChangedBy": {
            "displayName": "xxxx",
            "url": "xxxx",
            "_links": {
                "avatar": {
                    "href": "xxxx"
                }
            },
            "id": "xxxx",
            "uniqueName": "xxxx",
            "imageUrl": "xxxx",
            "descriptor": "xxxx"
        },
        "System.CreatedBy": {
            "displayName": "xxxx",
            "url": "xxxx",
            "_links": {
                "avatar": {
                    "href": "xxxx"
                }
            },
            "id": "xxxx",
            "uniqueName": "xxxx",
            "imageUrl": "xxxx",
            "descriptor": "xxxx"
        },
        "Microsoft.VSTS.Common.Priority": 2
    },
    "_links": {
        "workItemType": {
            "href": "https://dev.azure.com/{organization}/{project}/_apis/wit/workItemTypes/Task"
        },
        "fields": {
            "href": "https://dev.azure.com/{organization}/{project}/_apis/wit/fields"
        }
    },
    "url": "https://dev.azure.com/{organization}/{project}/_apis/wit/workItems"
}

最后一个 url 将我引向一个 JSON object 包含此消息的页面:

No HTTP resource was found that matches the request URI 'https://dev.azure.com/{project}/_apis/wit/workItems'

并且没有创建新任务。

我从查阅文档开始。

我已经尝试使用关于工作项的几种不同权限组合来编辑和重新生成我的个人访问令牌。我已尝试授予 PAT 完全访问权限。

我试过调用不同版本的API。

我尝试添加除标题和状态之外的字段,这些字段是按要求列出的,最后将 bypassRules 设置为 true。

Google 和 /r/azuredevops subreddit 没有返回有效的结果。

任何人都可以指出我应该去的方向,或者我错过了什么吗? 提前致谢:-)

编辑:我正在 POSTing。

编辑解决方案:

我觉得自己好傻。在 Postman 中 POSTing 时,我使用了 http:// 而不是 https:// 使用 https:// 解决了问题。

您正在尝试创建多个任务,根据 documentation
此端点不支持该任务 .

我成功地创建了一个具有以下 body 的任务:

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

Headers包括:

  • Content-Type : application/json-patch+json
  • 身份验证:基本 encoded-PAT

documentation 中的 Body 请求部分提到了内容类型 header。

对于PAT Token,你只需要vso.work_write,在documentation.

中也有提到

端点 Url 是:https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$Task?api-version=6.0

响应 body 看起来像这样

{
    "id": 8921,
    "rev": 1,
    "fields": {
        "System.AreaPath": "Project",
        "System.TeamProject": "Project",
        "System.IterationPath": "Project",
        "System.WorkItemType": "Task",
        "System.State": "New",
        "System.Reason": "New",
        "System.CreatedDate": "2021-03-17T10:36:25.513Z",
        "System.CreatedBy": ....,
        "System.ChangedDate": "2021-03-17T10:36:25.513Z",
        "System.ChangedBy": ....,
        "System.CommentCount": 0,
        "System.Title": "Sample task",
        "Microsoft.VSTS.Common.StateChangeDate": "2021-03-17T10:36:25.513Z",
        "Microsoft.VSTS.Common.Priority": 2
    },
    "_links": { some links
    },
    "url": "https://dev.azure.com/Organization/14360bf9-7578-4sfs-b43f-8dfda139fb17/_apis/wit/workItems/8921"
}

最后的属性 url 包括新创建的 WorkItem 的正确 link。

请看图,根据要求body,看来你用的是RESTAPIget work item type definition instead of Create Work Item

如果方法是 GET,它将获取工作项类型。

请求Body:

GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitemtypes/{type}?api-version=6.0

JSON 响应如下:

    {
            "fields": {
                  .....
                },
            "_links": {
                  .....                    
                },
            "url": "https://dev.azure.com/{organization}/{project}/_apis/wit/workItems"
        }

邮递员结果:

如果我们需要创建工作项,我们需要将方法更改为POST

请求URL:

POST https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${type}?api-version=6.0

请求Body:

[
    {
        "op": "add",
        "path": "/fields/System.Title",
        "from": null,
        "value": "Test of REST functionality"
    },
    {
        "op": "add",
        "path": "/fields/System.State",
        "from": null,
        "value": "New"
    }
]

JSON 响应如下:

{
    "id": {Work Item ID},
    "rev": 1,
    "fields": {
      ...
    },
    "_links": {
      ...
    },
    "url": "https://dev.azure.com/{organization}/{project}/_apis/wit/workItems/{Work Item ID}"
}

邮递员结果:

和 Azure DevOps 服务结果:

更新1

but I'd like to know what you have in your url

我的URL是

https://dev.azure.com/{Org name}/{Project name}/_apis/wit/workitems/$task?api-version=6.0

You have 12 headers and I have 9, and my url seems longer than yours

  1. 在postman中新建一个请求页面,默认Header为7,输入请求URL,会添加header Cookie,现在Header 是 8.

  1. 将方法从 GET 更改为 POST,它将添加 header Content-Length,现在 Header 为 9。

  1. 单击 Body 选项卡->select raw 并将内容类型更改为 JSON 添加请求 Body,它会添加 header Content-Type 现在 Header 是 10

  1. 单击授权选项卡->select基本授权并输入 PAT 令牌作为密码,它将添加header Authorization 现在 Header 是 11.

  1. 现在,当我们点击发送按钮时,我们会收到错误信息 The request indicated a Content-Type of \"application/json\" for method type \"POST\" which is not supported. Valid content types for this method are: application/json-patch+json.

  1. 点击Header选项卡,Content-Type的默认值为application/json,我们需要禁用它并添加新的header Content-Type并设置application/json-patch+json 的值,现在 Header 是 12

  1. 点击发送按钮,响应代码为 200