Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
681 views
in Technique[技术] by (71.8m points)

tfs - Azure DevOps Export Import query to different project

Tried searching if there's any out of the box method to export import queries in new projects. Seems like there isn't any.

Can it be achieved by code? If yes, would like to know how.

Appreciate any tips.

Regards,

question from:https://stackoverflow.com/questions/66058397/azure-devops-export-import-query-to-different-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the rest api to read and create queries: Queries

Powershell example to export queries into local folder:

$user = ""
$token = "<pat>" #https://docs.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 example to import queries from local folder:

$user = ""
$token = "<pat>" #https://docs.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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...