Azure Devops Get package Workitem state

217 views Asked by At

I'm creating a package work Item using Powershell script in Azure Devops workitem and updating the same on successful completion of deployment as Completed.

Question: How do i Query the package state? I tried using the below powershell, which queries the exisitng package but unable to find the logic to get system.state of the package

My Powershell:

function TestState{
Param(
    [Parameter(Mandatory)]
    [string[]]
    $package,
    [Parameter(Mandatory)]
    [string[]]
    $secret,
    [Parameter(Mandatory)]
    [string[]]
    $DevopsProject

)
$header = prepareHeader $secret
$query = "SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = '$DevopsProject' AND [System.WorkItemType] = 'Package' AND [System.Title] = '$PackageName' "
$json = @{ query = $query }
$jsonBody = ConvertTo-Json $json
$url  = "http://10.0.0.5/Test/$DevopsProject/_apis/wit/wiql?api-version=6.0"
$output = Invoke-RestMethod -Uri $url -Method Post -ContentType "application/json" -Headers $header -Body $jsonbody
$testurl = $output.workitems.url
$testurl = $testurl.Split('')
$test_url = $testurl[0]+'?api-version=6.0'
$output1 = Invoke-webrequest -uri "$test_url" -useBaseParsing -Method Get -ContentType "application/json" -Headers $header | ConvertFrom-json
return $output1
}    
                                                                                   

My Pipeline:

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
        $test = checkPackagestate "$PackageName" "$(secret)" $(DevopsProject)" 
2

There are 2 answers

0
Shamrai Aleksander On BEST ANSWER

You may update your script to this version

function TestState{
Param(
    [Parameter(Mandatory)]
    [string[]]
    $package,
    [Parameter(Mandatory)]
    [string[]]
    $secret,
    [Parameter(Mandatory)]
    [string[]]
    $DevopsProject

)
$header = prepareHeader $secret
$query = "SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = '$DevopsProject' AND [System.WorkItemType] = 'Package' AND [System.Title] = '$PackageName' "
$json = @{ query = $query }
$jsonBody = ConvertTo-Json $json
$url  = "http://10.0.0.5/Test/$DevopsProject/_apis/wit/wiql?api-version=6.0"
$output = Invoke-RestMethod -Uri $url -Method Post -ContentType "application/json" -Headers $header -Body $jsonbody

$outputres = ""

foreach ($wi in $output.workitems)
{
  $test_url = $wi.url +'?api-version=6.0'
  $output1 = Invoke-webrequest -uri "$test_url" -Method Get -ContentType "application/json" -Headers $header | ConvertFrom-json
  $outputres += "$($output1.id) : $($output1.fields.'System.Title') : $($output1.fields.'System.State')`n"
}    

return $outputres
}    
5
Shamrai Aleksander On

Because you are using just a query: Query By Wiql It returns the set of workitems ids:

{
  "queryType": "flat",
  "asOf": "2014-12-29T20:49:34.617Z",
  "columns": [
    {
      "referenceName": "System.Id",
      ....
    },
    ...
  ],
  "sortColumns": [
    {
      "field": {
        "referenceName": "Microsoft.VSTS.Common.Priority",
        ....
      },
      "descending": false
    },
    ...
  ],
  "workItems": [
    {
      "id": 300,
      "url": "https://dev.azure.com/fabrikam/_apis/wit/workItems/300"
    },
    {
      "id": 299,
      "url": "https://dev.azure.com/fabrikam/_apis/wit/workItems/299"
    },
    {
      "id": 298,
      "url": "https://dev.azure.com/fabrikam/_apis/wit/workItems/298"
    },
    .....
  ]
}

Then you have to use each id to get workitem details: Get Work Item

or use url under id