Skip to content

Commit

Permalink
added new cmdlet for querying workitems by WIQL
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianSchuetze committed Oct 3, 2019
1 parent 1d18f39 commit 64bc1dd
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Source/Public/Get-VSTeamWiql.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
function Get-VSTeamWiql {
[CmdletBinding(DefaultParameterSetName = 'ByID')]
param(
[Parameter(ParameterSetName = 'ByID', Mandatory = $true, Position = 0)]
[string] $Id,

[Parameter(ParameterSetName = 'ByQuery', Mandatory = $true, Position = 0)]
[string] $Query,

[Parameter(Mandatory = $true, Position = 1)]
[string] $Team,

[int] $Top = 100,

[Switch] $TimePrecision,

[Switch] $Expand
)
DynamicParam {
$arrSet = Get-VSTeamProject | Select-Object -ExpandProperty Name
_buildProjectNameDynamicParam -mandatory $true -arrSet $arrSet
}

Process {

# Bind the parameter to a friendly variable
$ProjectName = $PSBoundParameters["ProjectName"]

$QueryString = @{
'$top' = $Top
timePrecision = $TimePrecision
}

# Call the REST API
if ($Query) {

$body = (@{
query = $Query
}) | ConvertTo-Json

$resp = _callAPI -ProjectName $ProjectName -Team $Team -Area 'wit' -Resource 'wiql' `
-method "POST" -ContentType "application/json" `
-Version $([VSTeamVersions]::Core) `
-Querystring $QueryString `
-Body $body
}
else {
$resp = _callAPI -ProjectName $ProjectName -Team $Team -Area 'wit' -Resource 'wiql' `
-Version $([VSTeamVersions]::Core) -id "$Id" `
-Querystring $QueryString
}

$workItems = @()
if ($Expand) {

$Ids = $resp.workItems | Select-Object -ExpandProperty id
$Fields = $resp.columns | Select-Object -ExpandProperty referenceName

$resp.workItems = @()
#splitting id array by 200, since a maximum of 200 ids are allowed per call
$countIds = $Ids.Count
for ($beginRange = 0; $beginRange -lt $countIds; $beginRange += 200) {

$endRange = ($beginRange + 199)

if ($endRange -gt $countIds) {
$idArray = $Ids[$beginRange..($countIds - 1)]
}
else {
$idArray = $Ids[$beginRange..($endRange)]
}

$resp.workItems += (Get-VSTeamWorkItem -Fields $Fields -Ids $idArray).value

This comment has been minimized.

Copy link
@ferwe

ferwe Oct 18, 2019

If I may give a hint to this code

I would avoid '+=' because of performance.
Like this:

Resp.workItems = for ($begin...
...
(Get-VSTeamWorkItem -Fields $Fields -Id's $idArray).value
}

This comment has been minimized.

Copy link
@SebastianSchuetze

SebastianSchuetze Oct 19, 2019

Author Collaborator

I updated it according to your suggestion. Although I think it will not have a big impact. As stated in the blog about the performance of this approach it only takes effect with more than 1000 iterations. Mine does merely an average of 5 if you query 1000 work items at once.

But it's still good to program a bit more on performance if it is not an overengineered complicated improvement.

}

}

_applyTypesToWiql -item $resp

return $resp
}
}

0 comments on commit 64bc1dd

Please sign in to comment.