Azure DevOps导出导入查询到不同项目

3

我尝试搜索是否有现成的方法可以在新项目中导出和导入查询。似乎没有。

能否通过代码实现?如果可以,想知道如何实现。

感谢任何提示。

祝好,

3个回答

4

您可以使用REST API读取和创建查询:查询

将查询导出到本地文件夹的PowerShell示例:

$user = ""
$token = "<pat>" #https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page
$teamProject = "team_project_name"
$orgUrl = "https://dev.azure.com/<org>"
$sourceQueryFolder = "Shared Queries/Change Management"
$targetLocalFolder = "c:/temp/Change Management Queries"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$queriesUrl = "$orgUrl/$teamProject/_apis/wit/queries/$sourceQueryFolder"+"?`$depth=1&`$expand=all&api-version=5.0"

function InvokeGetRequest ($GetUrl)
{    
    return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}    
}

$resQuries = InvokeGetRequest $queriesUrl

if (![System.IO.Directory]::Exists($targetLocalFolder))
{
    New-Item -Path $targetLocalFolder -ItemType "directory"
}

if ($resQuries.isFolder -and $resQuries.hasChildren)
{
    foreach($item in $resQuries.children)
    {
        if (!$item.isFolder)
        {            
            $queryJson = "{`"name`":`"{queryname}`", `"wiql`":`"{querywiql}`"}"

            $queryJson = $queryJson -replace "{queryname}", $item.name
            $queryJson = $queryJson -replace "{querywiql}", $item.wiql

            $filepath = "$targetLocalFolder/" + $item.name

            Set-Content -Path $filepath -Value $queryJson
        }
    }
}

导入本地文件夹中查询的Powershell示例:

$user = ""
$token = "<pat>" #https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page
$teamProject = "team_project_name"
$orgUrl = "https://dev.azure.com/<org>"
$sourceLocalFolder = "c:/temp/Change Management Queries"
$targetQueryFolder = "Shared Queries/Change Management Imported" #should exist

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$queriesUrl = "$orgUrl/$teamProject/_apis/wit/queries/$targetQueryFolder"+"?&api-version=5.0"

function InvokePostRequest ($PostUrl, $body)
{   
    return Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
}

$files = Get-ChildItem -File -Path $sourceLocalFolder

foreach($wiqlfile in $files)
{
    $wiqlBody = Get-Content $wiqlfile

    InvokePostRequest $queriesUrl $wiqlBody
}

1

通过rest API将一个团队项目中的查询文件夹内容复制到同一组织中的另一个团队项目的示例:

$user = ""
$token = "<pat>" #https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page
$teamProjectSource = "source_team_project_name"
$teamProjectTarget = "target_team_project_name"
$orgUrl = "https://dev.azure.com/<org>"
$sourceQueryFolder = "Shared Queries/Change Management"
$targetQueryFolder = "Shared Queries/Change Management" #should exist

$queryObject = [PSCustomObject]@{
    name = $null
    wiql = $null
    columns = $null
    sortColumns = $null
}

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$queriesPostUrl = "$orgUrl/$teamProjectTarget/_apis/wit/queries/$targetQueryFolder"+"?api-version=5.0"
$queriesGettUrl = "$orgUrl/$teamProjectSource/_apis/wit/queries/$sourceQueryFolder"+"?`$depth=1&`$expand=all&api-version=5.0"

function InvokeGetRequest ($GetUrl)
{    
    return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}    
}

function InvokePostRequest ($PostUrl, $body)
{   
    return Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
}

$resQuries = InvokeGetRequest $queriesGettUrl

if ($resQuries.isFolder -and $resQuries.hasChildren)
{
    foreach($item in $resQuries.children)
    {
        if (!$item.isFolder)
        {   
            $queryObject.name = $item.name 
            $queryObject.wiql = $item.wiql
            $queryObject.columns = $item.columns
            $queryObject.sortcolumns = $item.sortcolumns
            
            $wiqlbody = ConvertTo-Json $queryObject -Depth 10

            InvokePostRequest $queriesPostUrl $wiqlBody
        }
    }
}

1
你可以使用开箱即用的功能来导出和导入查询。Visual Studio 和 Team Explorer 包含这些功能。
1. 转到查询选项卡:

enter image description here

编辑查询。

enter image description here

  1. 保存为

enter image description here

  1. 选择另一个项目

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接