如何在PowerShell中使用REST API在TFS中创建错误? [英] How do I create a bug in TFS using REST API in PowerShell?
问题描述
我试图使用下面的代码在PowerShell中使用REST API在TFS中创建错误,但是我无法弄清楚如何用那些参数和数据的名称填充$Bug
变量.
I am trying to create a bug in TFS using REST API in PowerShell with the code below, but I'm unable to figure out how to fill the $Bug
variable with names of those param's and data.
Param(
[string]$vstsAccount = "MyAccountName",
[string]$projectName = "ProjectName",
[string]$keepForever = "true",
[string]$user = "",
[string]$token = "Mytoken"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
#$uri = "https://$($vstsAccount).visualstudio.com/$($projectName)/_apis/wit/workitems/$Bug?api-version=2.2"
$result = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
我可以找到C#的示例 ,但不适用于PowerShell.任何帮助将不胜感激.
I could find a sample for C# here, but not for PowerShell. Any help would be appreciated.
欢呼
推荐答案
您需要创建 JSON正文,才能使用REST API在PowserShell中创建工作项,并且Content-Type
应该是application/json-patch+json
,也可以使用PATCH
方法.请参见创建作品项以获取详细信息.
You need to create a JSON body to use the REST API to create a work item in PowserShell, and the Content-Type
should be application/json-patch+json
, also use PATCH
method. See Create a work item for details.
您可以参考下面的示例PowerShell脚本来创建错误:
You can reference below sample PowerShell script to create a bug:
Param(
[string]$baseurl = "http://server:8080/tfs/DefaultCollection",
[string]$projectName = "ProjectName",
[string]$keepForever = "true",
[string]$user = "username",
[string]$token = "token"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function CreateJsonBody
{
$value = @"
[
{
"op": "add",
"path": "/fields/System.Title",
"value": "0925Bug"
},
{
"op": "add",
"path": "/fields/System.AreaPath",
"value": "LCScrum"
},
{
"op": "add",
"path": "/fields/System.IterationPath",
"value": "LCScrum\\Sprint 1"
},
{
"op": "add",
"path": "/fields/System.Tags",
"value": "Tag0921;Tag0926;Tag0927;Tag0928"
},
{
"op": "add",
"path": "/fields/Microsoft.VSTS.Common.Activity",
"value": "Development"
},
{
"op": "add",
"path": "/fields/Microsoft.VSTS.Scheduling.Effort",
"value": "8"
},
{
"op": "add",
"path": "/fields/Microsoft.VSTS.Common.ValueArea",
"value": "Business"
},
{
"op": "add",
"path": "/fields/Microsoft.VSTS.Common.Severity",
"value": "3 - Medium"
},
{
"op": "add",
"path": "/relations/-",
"value":
{
"rel": "System.LinkTypes.Dependency-Forward",
"url": "http://server:8080/tfs/DefaultCollection/_apis/wit/workItems/324",
"attributes":
{
"usage": "workItemLink",
"editable": false,
"enabled": true,
"acyclic": true,
"directional": true,
"singleTarget": true,
"topology": "dependency"
}
}
},
{
"op": "add",
"path": "/relations/-",
"value":
{
"rel": "System.LinkTypes.Hierarchy-Reverse",
"url": "http://server:8080/tfs/DefaultCollection/_apis/wit/workItems/58",
"attributes":
{
"usage": "workItemLink",
"editable": false,
"enabled": true,
"acyclic": true,
"directional": true,
"singleTarget": false,
"topology": "tree"
}
}
}
]
"@
return $value
}
$json = CreateJsonBody
$uri = "$baseurl/$($projectName)/_apis/wit/workitems/"+"$"+"bug?api-version=2.2"
Write-Host $uri
$result = Invoke-RestMethod -Uri $uri -Method Patch -Body $json -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
这篇关于如何在PowerShell中使用REST API在TFS中创建错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!