首页 > 解决方案 > 从 Postman 在 Azure Devops 中创建工作项

问题描述

我们希望将所有项目从 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 正文:

[
    {
        "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 对象的页面:

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

并且不会创建新任务。

我从查阅文档开始。

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

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

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

谷歌和 /r/azuredevops subreddit 没有返回任何有效的结果。

谁能指出我应该去哪里,或者我错过了什么?提前致谢 :-)

编辑:我在发帖。

邮政

编辑解决方案:

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

标签: azure-devopspostmanazure-devops-rest-apiazure-boards

解决方案


请检查图片,根据请求正文,您似乎正在使用 REST API获取工作项类型定义而不是创建工作项

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

请求正文:

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

请求网址:

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

请求正文:

[
    {
        "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

但我想知道你的网址中有什么

我的网址是

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

你有 12 个标题,我有 9 个,而且我的 url 似乎比你的长

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

在此处输入图像描述

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

在此处输入图像描述

  1. 单击Body选项卡- >选择raw并将内容类型更改为JSON以添加请求正文,它将添加标头Content-Type,现在标头为 10

在此处输入图像描述

  1. 单击授权选项卡->选择基本身份验证并输入 PAT 令牌作为密码,它将添加标题Authorization,现在标题为 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-Typeapplication/json我们需要禁用它并添加新的 headerContent-Type并将值设置为 application/json-patch+json,现在 Header 为 12

在此处输入图像描述

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

在此处输入图像描述


推荐阅读