Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CMDLET to get work items via WIQL #205

Merged
merged 17 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions .docs/Get-VSTeamWiql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!-- #include "./common/header.md" -->

# Get-VSTeamWiqlItem

## SYNOPSIS

<!-- #include "./synopsis/Get-VSTeamWiql.md" -->

## SYNTAX

## DESCRIPTION

<!-- #include "./synopsis/Get-VSTeamWiql.md" -->

## EXAMPLES

### -------------------------- EXAMPLE 1 --------------------------

```PowerShell
PS C:\> Get-VSTeamWiql -Query "Select [System.Id], [System.Title], [System.State] From WorkItems" -Team "MyProject Team" -Project "MyProject" -Expand
```

This command gets work items via a WIQL query and expands the return work items with only the selected fields System.Id, System.Title and System.State.

### -------------------------- EXAMPLE 2 --------------------------

```PowerShell
PS C:\> Get-VSTeamWiql -Query "Select [System.Id], [System.Title], [System.State] From WorkItems" -Team "MyProject Team" -Project "MyProject"
```

This command gets work items via a WIQL query and returns the WIQL query result with only work item IDs.

## PARAMETERS

### -Id

The id query to return work items for. This is the ID of any saved query within a team in a project

```yaml
Type: Int32
Parameter Sets: ByID
Required: True
```

### -Query

The WIQL query. For the syntax check [the official documentation](https://docs.microsoft.com/en-us/azure/devops/boards/queries/wiql-syntax?view=azure-devops).

```yaml
Type: String
Parameter Sets: ByQuery
Required: True
```

### -Top

The max number of results to return.

```yaml
Type: String
Required: False
Default value: 100
```

### -TimePrecision

Whether or not to use time precision.

```yaml
Type: Switch
```

### -Expand

The expand the work items with the selected attributes in the WIQL query.

```yaml
Type: Switch
```

## INPUTS

### System.String

ProjectName

## OUTPUTS

## NOTES

If you do not set the default project by called Set-VSTeamDefaultProject before calling Get-VSTeamWiql you will have to type in the names.

## RELATED LINKS
1 change: 1 addition & 0 deletions .docs/synopsis/Get-VSTeamWiql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Returns work items from the given WIQL query or a saved query by ID from your projects team.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 6.4.0

Added Get-VSTeamWiql to get work items via [WIQL](https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/wiql?view=azure-devops-rest-5.1) and also to expand the returned work items with all fields selected.

## 6.3.6

Merged [Pull Request](https://github.com/DarqueWarrior/vsteam/pull/200) from [Chris Gardner](https://github.com/ChrisLGardner) which included the following:
Expand Down
7 changes: 7 additions & 0 deletions Source/Private/applyTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ function _applyTypesToWorkItem {
}
}

function _applyTypesToWiql {
param($item)
if ($item) {
$item.PSObject.TypeNames.Insert(0, 'Team.Wiql')
}
}

function _applyTypesToUser {
param(
[Parameter(Mandatory = $true)]
Expand Down
10 changes: 8 additions & 2 deletions Source/Private/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function _hasAccount {
function _buildRequestURI {
[CmdletBinding()]
param(
[string]$team,
[string]$resource,
[string]$area,
[string]$id,
Expand All @@ -90,6 +91,10 @@ function _buildRequestURI {
$sb.Append("/$projectName") | Out-Null
}

if ($team) {
$sb.Append("/$team") | Out-Null
}

$sb.Append("/_apis/") | Out-Null

if ($area) {
Expand Down Expand Up @@ -588,8 +593,9 @@ function _callAPI {
[object]$body,
[string]$InFile,
[string]$OutFile,
[string]$ContentType,
[string]$ContentType,
[string]$ProjectName,
[string]$Team,
[string]$Url,
[object]$QueryString
)
Expand Down Expand Up @@ -628,7 +634,7 @@ function _callAPI {
}

# We have to remove any extra parameters not used by Invoke-RestMethod
$extra = 'Area', 'Resource', 'SubDomain', 'Id', 'Version', 'JSON', 'ProjectName', 'Url', 'QueryString'
$extra = 'Area', 'Resource', 'SubDomain', 'Id', 'Version', 'JSON', 'ProjectName', 'Team', 'Url', 'QueryString'
foreach ($e in $extra) { $params.Remove($e) | Out-Null }

try {
Expand Down
81 changes: 81 additions & 0 deletions Source/Public/Get-VSTeamWiql.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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
}

if ($Expand) {

[array]$Ids = $resp.workItems.id
$Fields = $resp.columns.referenceName

$resp.workItems = @()
#splitting id array by 200, since a maximum of 200 ids are allowed per call
$countIds = $Ids.Count
$resp.workItems = 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)]
}

(Get-VSTeamWorkItem -Fields $Fields -Ids $idArray).value
}

}

_applyTypesToWiql -item $resp

return $resp
}
}
2 changes: 1 addition & 1 deletion Source/VSTeam.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'VSTeam.psm1'

# Version number of this module.
ModuleVersion = '6.3.6'
ModuleVersion = '6.4.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
28 changes: 28 additions & 0 deletions Source/formats/Team.Wiql.ListView.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>Team.Wiql.ListView</Name>
<ViewSelectedBy>
<TypeName>Team.Wiql</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<PropertyName>queryType</PropertyName>
</ListItem>
<ListItem>
<PropertyName>WorkItemIDs</PropertyName>
</ListItem>
<ListItem>
<PropertyName>workItems</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>
39 changes: 39 additions & 0 deletions Source/formats/Team.Wiql.TableView.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>Team.Wiql.TableView</Name>
<ViewSelectedBy>
<TypeName>Team.Wiql</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>QueryType</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>WorkItemIDs</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>WorkItems</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>queryType</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>WorkItemIDs</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>workItems</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
2 changes: 2 additions & 0 deletions Source/formats/_formats.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
"Team.PSDrive.Leaf.Default.TableView.ps1xml",
"Team.WorkItemType.TableView.ps1xml",
"Team.WorkItemType.ListView.ps1xml",
"Team.Wiql.TableView.ps1xml",
"Team.Wiql.ListView.ps1xml",
"Team.WorkItem.TableView.ps1xml",
"Team.WorkItem.ListView.ps1xml",
"Team.JobRequest.TableView.ps1xml"
Expand Down
49 changes: 49 additions & 0 deletions Source/types/Team.Wiql.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8" ?>
<Types>
<Type>
<Name>Team.Wiql</Name>
<Members>
<ScriptProperty>
<Name>QueryType</Name>
<GetScriptBlock>$this.PSObject.Properties['queryType'].Value</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>QueryResultType</Name>
<GetScriptBlock>$this.PSObject.Properties['queryResultType'].Value</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>AsOf</Name>
<GetScriptBlock>$this.PSObject.Properties['asOf'].Value</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>Columns</Name>
<GetScriptBlock>$this.PSObject.Properties['columns'].Value</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>SortColumns</Name>
<GetScriptBlock>$this.PSObject.Properties['sortColumns'].Value</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>WorkItemIDs</Name>
<GetScriptBlock>[int[]]$this.PSObject.Properties['workItems'].Value.id</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>WorkItems</Name>
<GetScriptBlock>$this.PSObject.Properties['workItems'].Value</GetScriptBlock>
</ScriptProperty>
<MemberSet>
<Name>PSStandardMembers</Name>
<Members>
<PropertySet>
<Name>DefaultDisplayPropertySet</Name>
<ReferencedProperties>
<Name>queryType</Name>
<Name>WorkItemIDs</Name>
<Name>workItems</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</MemberSet>
</Members>
</Type>
</Types>
Loading