通过 REST 修改 Azure Devops 测试用例参数 API
Modify Azure Devops Test Case Parameters Through REST API
我正在尝试弄清楚如何使用 powershell 在 Azure Devops (ADO) 的测试用例中修改参数值。不幸的是,ADO REST API 文档没有详细介绍该主题,所以我大部分时间都是通过反复试验来完成的。
到目前为止,这是我的代码:
$PAT = "xxxxxxxxxxxxxxxxxxxxxxxx"
$Org = "MyOrganization"
$Project = "MyProject"
$ADOHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
$BaseURI = "https://dev.azure.com/$($Org)/$($Project)/"
$Endpoint = $BaseURI + "_apis/wit/workitems/224572?api-version=6.0"
$result = Invoke-RestMethod -Uri $Endpoint -Method get -Headers $ADOHeader
$fields = $result.fields
[XML]$table = $fields.'Microsoft.VSTS.TCM.LocalDataSource'
$table.NewDataSet.Table1[0].Parameter1 = "209623"
$update = @{
"op" = "add"
"path" = "/fields/Microsoft.VSTS.TCM.LocalDataSource"
"value" = $table.OuterXml
} | ConvertTo-Json -Depth 5
$patch = Invoke-RestMethod -Uri $Endpoint -Method patch -Headers $ADOHeader -Body $update -ContentType "application/json-patch+json"
当我执行获取请求以检索测试用例数据时,参数作为 XML 对象位于 Microsoft.VSTS.TCM.LocalDataSource 字段中。我认为既然它是以 XML 格式发送给我的,那么这就是它需要发回的格式,但情况似乎并非如此,因为我一直收到此错误:“你必须通过一个有效的请求正文中的补丁文件。”我似乎无法追踪补丁文件需要采用的格式。
如有任何帮助,我们将不胜感激。谢谢!
edit - 我添加了 c# 标签,以防有人有通过 c# 项目在 Azure Devops REST API 中处理测试用例的经验,并且知道补丁文档如何需要待格式化。
最终解决方案
这是我使用 Bright 运行-MSFT 的推荐
修改后的代码
$PAT = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$Org = "MyOrganization"
$Project = "MyProject"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $PAT)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
$headers.Add("Content-Type", "application/json-patch+json")
$BaseURI = "https://dev.azure.com/$($Org)/$($Project)/"
$Endpoint = $BaseURI + "_apis/wit/workitems/{id}?api-version=6.1-preview.3"
$result = Invoke-RestMethod -Uri $Endpoint -Method get -Headers $headers
$table = $result.fields.'Microsoft.VSTS.TCM.LocalDataSource'
$table.NewDataSet.Table1[0].Parameter1 = "123456"
$pBody = $table.OuterXml.Replace('"','''')
$body = "[
{
`"op`": `"replace`",
`"path`": `"/fields/Microsoft.VSTS.TCM.LocalDataSource`",
`"value`": `"$pBody`"
}
]"
$put = Invoke-RestMethod -Uri $Endpoint -Method patch -Headers $headers -Body $body
你可以这样试:
执行端点“Work Items - Get Work Item”,从响应body可以看到参数信息以XML格式字符串返回属性 '/fields/Microsoft.VSTS.TCM.LocalDataSource
'。复制 XML 格式字符串。
GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?$expand=all&api-version=6.1-preview.3
使用端点“Work Items - Update”更改参数值。
请求 URI
PATCH https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?api-version=6.1-preview.3
必需header
Content-Type: application/json-patch+json
请求Body
[
{
"op": "replace",
"path": "/fields/Microsoft.VSTS.TCM.LocalDataSource",
"value": "Paste the XML format string here and change parameter values to the new"
}
]
[更新]
根据我的进一步调查,我认为原因可能是在XML类型和字符串类型之间转换值时需要考虑一些转义字符。
为避免XML类型与字符串类型之间的转换可能导致的问题,您可以尝试直接更新的响应body中的参数GET
请求,没有将值转换为 XML 类型。
这里有一个示例 PowerShell 脚本作为参考。
$pat = "personal access token"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
$headers.Add("Content-Type", "application/json-patch+json")
$uri_GET = "https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?`$expand=all&api-version=6.1-preview.3"
$response = Invoke-RestMethod -Uri $uri_GET -Headers $headers -Method GET
# Replace the values of the parameters 'param01' and 'param02' from the XML format string
$str_table = [Regex]::Replace($response.fields.'Microsoft.VSTS.TCM.LocalDataSource', "<param01>.*</param01>", "<param01>112233</param01>")
$str_table = [Regex]::Replace($str_table, "<param02>.*</param02>", "<param02>445566</param02>")
$uri_PATCH = "https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?api-version=6.1-preview.3"
$body = "[
{
`"op`": `"replace`",
`"path`": `"/fields/Microsoft.VSTS.TCM.LocalDataSource`",
`"value`": `"$str_table`"
}
]"
Invoke-RestMethod -Uri $uri_PATCH -Headers $headers -Body $body -Method PATCH
我正在尝试弄清楚如何使用 powershell 在 Azure Devops (ADO) 的测试用例中修改参数值。不幸的是,ADO REST API 文档没有详细介绍该主题,所以我大部分时间都是通过反复试验来完成的。
到目前为止,这是我的代码:
$PAT = "xxxxxxxxxxxxxxxxxxxxxxxx"
$Org = "MyOrganization"
$Project = "MyProject"
$ADOHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
$BaseURI = "https://dev.azure.com/$($Org)/$($Project)/"
$Endpoint = $BaseURI + "_apis/wit/workitems/224572?api-version=6.0"
$result = Invoke-RestMethod -Uri $Endpoint -Method get -Headers $ADOHeader
$fields = $result.fields
[XML]$table = $fields.'Microsoft.VSTS.TCM.LocalDataSource'
$table.NewDataSet.Table1[0].Parameter1 = "209623"
$update = @{
"op" = "add"
"path" = "/fields/Microsoft.VSTS.TCM.LocalDataSource"
"value" = $table.OuterXml
} | ConvertTo-Json -Depth 5
$patch = Invoke-RestMethod -Uri $Endpoint -Method patch -Headers $ADOHeader -Body $update -ContentType "application/json-patch+json"
当我执行获取请求以检索测试用例数据时,参数作为 XML 对象位于 Microsoft.VSTS.TCM.LocalDataSource 字段中。我认为既然它是以 XML 格式发送给我的,那么这就是它需要发回的格式,但情况似乎并非如此,因为我一直收到此错误:“你必须通过一个有效的请求正文中的补丁文件。”我似乎无法追踪补丁文件需要采用的格式。
如有任何帮助,我们将不胜感激。谢谢!
edit - 我添加了 c# 标签,以防有人有通过 c# 项目在 Azure Devops REST API 中处理测试用例的经验,并且知道补丁文档如何需要待格式化。
最终解决方案
这是我使用 Bright 运行-MSFT 的推荐
修改后的代码$PAT = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$Org = "MyOrganization"
$Project = "MyProject"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $PAT)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
$headers.Add("Content-Type", "application/json-patch+json")
$BaseURI = "https://dev.azure.com/$($Org)/$($Project)/"
$Endpoint = $BaseURI + "_apis/wit/workitems/{id}?api-version=6.1-preview.3"
$result = Invoke-RestMethod -Uri $Endpoint -Method get -Headers $headers
$table = $result.fields.'Microsoft.VSTS.TCM.LocalDataSource'
$table.NewDataSet.Table1[0].Parameter1 = "123456"
$pBody = $table.OuterXml.Replace('"','''')
$body = "[
{
`"op`": `"replace`",
`"path`": `"/fields/Microsoft.VSTS.TCM.LocalDataSource`",
`"value`": `"$pBody`"
}
]"
$put = Invoke-RestMethod -Uri $Endpoint -Method patch -Headers $headers -Body $body
你可以这样试:
执行端点“Work Items - Get Work Item”,从响应body可以看到参数信息以XML格式字符串返回属性 '
/fields/Microsoft.VSTS.TCM.LocalDataSource
'。复制 XML 格式字符串。GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?$expand=all&api-version=6.1-preview.3
使用端点“Work Items - Update”更改参数值。
请求 URI
PATCH https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?api-version=6.1-preview.3
必需header
Content-Type: application/json-patch+json
请求Body
[ { "op": "replace", "path": "/fields/Microsoft.VSTS.TCM.LocalDataSource", "value": "Paste the XML format string here and change parameter values to the new" } ]
[更新]
根据我的进一步调查,我认为原因可能是在XML类型和字符串类型之间转换值时需要考虑一些转义字符。
为避免XML类型与字符串类型之间的转换可能导致的问题,您可以尝试直接更新的响应body中的参数GET
请求,没有将值转换为 XML 类型。
这里有一个示例 PowerShell 脚本作为参考。
$pat = "personal access token"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
$headers.Add("Content-Type", "application/json-patch+json")
$uri_GET = "https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?`$expand=all&api-version=6.1-preview.3"
$response = Invoke-RestMethod -Uri $uri_GET -Headers $headers -Method GET
# Replace the values of the parameters 'param01' and 'param02' from the XML format string
$str_table = [Regex]::Replace($response.fields.'Microsoft.VSTS.TCM.LocalDataSource', "<param01>.*</param01>", "<param01>112233</param01>")
$str_table = [Regex]::Replace($str_table, "<param02>.*</param02>", "<param02>445566</param02>")
$uri_PATCH = "https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?api-version=6.1-preview.3"
$body = "[
{
`"op`": `"replace`",
`"path`": `"/fields/Microsoft.VSTS.TCM.LocalDataSource`",
`"value`": `"$str_table`"
}
]"
Invoke-RestMethod -Uri $uri_PATCH -Headers $headers -Body $body -Method PATCH