备份 VS Team Services 上的工作项
Backup workitems on VS Team Services
是否可以通过某种方式 backup/export/download 所有工作项?我查看了 REST API,但您似乎无法执行 f.e。通过此查询 API...
您可以使用 Excel 的 Team Foundation 加载项将工作项查询导出到 Excel,如 here
所述
好的,找到了完成工作的方法。我再次查看了 API 文档。 https://www.visualstudio.com/en-us/docs/integrate/api/wit/overview 页面帮助了我。你首先需要做一个
GET https://{account}.visualstudio.com/defaultcollection/{project}/_apis/wit/queries/{folderpath}?api-version={version}&$expand=wiql
从生成的 JSON 中,您需要获取 wiql 部分,这是实际的查询。在此之后你需要做一个
POST https://{account}.visualstudio.com/defaultcollection/{project}/_apis/wit/wiql?api-version={version}
正文是 JSON 和 { "query" = "YOURQUERY" }
因此您将收到包含所有工作项 urls/ids 的 JSON。您需要浏览它们并通过
查询每个工作项
GET URL?$expand=all
注意:仅当您也需要关系和附件时才添加 ?$expand=all。
我为 PowerShell 整合了一些东西。注意:我决定对查询进行硬编码并删除错误处理以使其更短。
function loadJsonFile($fileName)
{
return ConvertFrom-Json "$(Get-Content $fileName)"
}
function getLastItemFromURL($url)
{
$absPath = ([System.Uri]$url).AbsolutePath
$lastSlash = $absPath.LastIndexOf("/")
$absPath.Substring($lastSlash+1)
}
function getWorkItemId($url)
{
getLastItemFromURL($url)
}
# make sure you enabled alternative credentials and access for them
# you can get the value for YOURCODE i.e. via Fiddler
$headers = @{Authorization="Basic YOURCODE"}
# before this you would need to find the WIQL of the query; left to you
$body = @{
"query" = "THEQUERYFROMTHEJSON"
}
$bodyJson = $body | ConvertTo-Json
Invoke-RestMethod -method Post -ContentType application/json -Uri "https://{account}.visualstudio.com/defaultcollection/{project}/_apis/wit/wiql?api-version=1.0" -Headers $headers -Body $bodyJson -OutFile workitems.json
$workItemsJson = $(loadJsonFile workitems.json)
$workItems = $(foreach ($relation in $workItemsJson.workItemRelations)
{
$relation.target.url
$relation.source.url
}) | select -Unique | sort
echo "Going to download the following ids from $(getWorkItemId $workItems[0])-$(getWorkItemId $workItems[-1])"
# download the workitems
foreach($workItemUrl in $workItems)
{
$workItemId = getWorkItemId $workItemUrl
echo "Download ID: $workItemId"
$workItemUrl = "$workItemUrl`?`$expand=all"
$fileName = "workitem_$workItemId.json"
Invoke-RestMethod -ContentType application/json -Uri "$workItemUrl" -Headers $headers -OutFile "$fileName"
# download attachments
$workItemJson = $(loadJsonFile "$fileName")
foreach($relation in $workItemJson.relations)
{
if($relation.rel -eq "AttachedFile")
{
$fileUrl = $relation.url
Invoke-WebRequest $fileUrl -Headers $headers -OutFile $(getLastItemFromURL $fileUrl)
}
}
}
是否可以通过某种方式 backup/export/download 所有工作项?我查看了 REST API,但您似乎无法执行 f.e。通过此查询 API...
您可以使用 Excel 的 Team Foundation 加载项将工作项查询导出到 Excel,如 here
所述好的,找到了完成工作的方法。我再次查看了 API 文档。 https://www.visualstudio.com/en-us/docs/integrate/api/wit/overview 页面帮助了我。你首先需要做一个
GET https://{account}.visualstudio.com/defaultcollection/{project}/_apis/wit/queries/{folderpath}?api-version={version}&$expand=wiql
从生成的 JSON 中,您需要获取 wiql 部分,这是实际的查询。在此之后你需要做一个
POST https://{account}.visualstudio.com/defaultcollection/{project}/_apis/wit/wiql?api-version={version}
正文是 JSON 和 { "query" = "YOURQUERY" }
因此您将收到包含所有工作项 urls/ids 的 JSON。您需要浏览它们并通过
查询每个工作项GET URL?$expand=all
注意:仅当您也需要关系和附件时才添加 ?$expand=all。 我为 PowerShell 整合了一些东西。注意:我决定对查询进行硬编码并删除错误处理以使其更短。
function loadJsonFile($fileName)
{
return ConvertFrom-Json "$(Get-Content $fileName)"
}
function getLastItemFromURL($url)
{
$absPath = ([System.Uri]$url).AbsolutePath
$lastSlash = $absPath.LastIndexOf("/")
$absPath.Substring($lastSlash+1)
}
function getWorkItemId($url)
{
getLastItemFromURL($url)
}
# make sure you enabled alternative credentials and access for them
# you can get the value for YOURCODE i.e. via Fiddler
$headers = @{Authorization="Basic YOURCODE"}
# before this you would need to find the WIQL of the query; left to you
$body = @{
"query" = "THEQUERYFROMTHEJSON"
}
$bodyJson = $body | ConvertTo-Json
Invoke-RestMethod -method Post -ContentType application/json -Uri "https://{account}.visualstudio.com/defaultcollection/{project}/_apis/wit/wiql?api-version=1.0" -Headers $headers -Body $bodyJson -OutFile workitems.json
$workItemsJson = $(loadJsonFile workitems.json)
$workItems = $(foreach ($relation in $workItemsJson.workItemRelations)
{
$relation.target.url
$relation.source.url
}) | select -Unique | sort
echo "Going to download the following ids from $(getWorkItemId $workItems[0])-$(getWorkItemId $workItems[-1])"
# download the workitems
foreach($workItemUrl in $workItems)
{
$workItemId = getWorkItemId $workItemUrl
echo "Download ID: $workItemId"
$workItemUrl = "$workItemUrl`?`$expand=all"
$fileName = "workitem_$workItemId.json"
Invoke-RestMethod -ContentType application/json -Uri "$workItemUrl" -Headers $headers -OutFile "$fileName"
# download attachments
$workItemJson = $(loadJsonFile "$fileName")
foreach($relation in $workItemJson.relations)
{
if($relation.rel -eq "AttachedFile")
{
$fileUrl = $relation.url
Invoke-WebRequest $fileUrl -Headers $headers -OutFile $(getLastItemFromURL $fileUrl)
}
}
}