0

During a build we use a powershell script to retrieve our current iteration path. This iteration path should be used to create a work item if the build fails.

try
{
    $Uri = "https://.../" + "_apis/work/TeamSettings/Iterations?`$timeframe=current&api-version=3.0"
    Write-Host $Uri
    $Response = Invoke-RestMethod -Method Get -UseDefaultCredentials -ContentType application/json -Uri $Uri
    $IterationPath = $Response.value.path
    Write-Host $IterationPath 
    Write-Host "##vso[task.setvariable variable=BUG_ITERATION]$IterationPath"
}
catch [Exception]
{
    $ErrorMessage = $_.Exception.Message
    Write-Warning "Cannot read iteration. $ErrorMessage"
    return "99"
}

Then we added a refeerence to the BUG_ITERATION variable here:

iterationpath set

But the build always fails, because of this:

##[warning]Failed to create work item for build failure: TF401346: Invalid Area/Iteration id given for work item -1, field 'System.IterationId'.

I played around for a bit now, and it apears, it does interprets the build variable on build initialization and it doesn't matter anymore, what I write into it during build. The reason why I think that is, because it does recognise it, if I hardcode it like this:

hardcoded iteration

Is there anything I can do to solve this?

Jannik
  • 2,310
  • 6
  • 32
  • 61

2 Answers2

0

In Azure DevOps, there is a difference between the path and the ID of an area or iteration. The error message you are getting is that Test\Release 13\Iteration 175 is not a valid ID for an iteration.

You will need to make an API call to get the ID of the iteration. Then you can use the ID in place of the path and it should work. The API call you will need to make to get the ID is Get Classification Node. This endpoint allows you pass in the path of the iteration and returns a JSON object with the ID of it.

tj-cappelletti
  • 1,774
  • 12
  • 19
  • As you can read in my initial question, it works with an hardcoded enviroment variable "Test\Release 13\Iteration 175", but not if I use a script to later set the value of the environment variable. – Jannik Feb 07 '23 at 16:09
  • Have you tried grabbing the ID of the iteration and setting it to the variable? – tj-cappelletti Feb 08 '23 at 11:36
0

Do you use System.IterationId for BUG_ITERATION? If yes, try to use System.IterationPath. If you want to use IterationId, the simplest way to get it - use a query:

enter image description here

But it is not available for the rest API.

Additionally, check the discussion here for area path (it is should be the same for iterations): https://developercommunity.visualstudio.com/t/create-work-item-to-area-using-rest-api/208734

Shamrai Aleksander
  • 13,096
  • 3
  • 24
  • 31