Skip to content

Commit

Permalink
Make teams and teammember related cmdlets properly support the pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
KeesV committed Sep 7, 2017
1 parent 010feef commit cb5a844
Show file tree
Hide file tree
Showing 5 changed files with 460 additions and 389 deletions.
25 changes: 16 additions & 9 deletions src/teammembers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,29 @@ function _buildURL {
# Apply types to the returned objects so format and type files can
# identify the object and act on it.
function _applyTypes {
param($item)
$item.PSObject.TypeNames.Insert(0, 'Team.TeamMember')
param(
[Parameter(Mandatory = $true)]
$item,
[Parameter(Mandatory = $true)]
$team
)

# Add the team name as a NoteProperty so we can use it further down the pipeline (it's not returned from the REST call)
$item | Add-Member -MemberType NoteProperty -Name Team -Value $team
$item.PSObject.TypeNames.Insert(0, 'Team.TeamMember')
}

function Get-TeamMember {
[CmdletBinding(DefaultParameterSetName = 'List')]
[CmdletBinding()]
param (
[Parameter(ParameterSetName = 'List')]
[Parameter()]
[int] $Top,

[Parameter(ParameterSetName = 'List')]
[Parameter()]
[int] $Skip,

[Parameter(Mandatory = $true, ParameterSetName = 'List', ValueFromPipeline = $true)]
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName=$true)]
[Alias('name')]
[string] $TeamId
)

Expand All @@ -59,8 +68,6 @@ function Get-TeamMember {
$listurl += _appendQueryString -name "`$top" -value $top
$listurl += _appendQueryString -name "`$skip" -value $skip

Write-Output $listurl

# Call the REST API
if (_useWindowsAuthenticationOnPremise) {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Uri $listurl -UseDefaultCredentials
Expand All @@ -71,7 +78,7 @@ function Get-TeamMember {

# Apply a Type Name so we can use custom format view and custom type extensions
foreach ($item in $resp.value) {
_applyTypes -item $item
_applyTypes -item $item -team $TeamId
}

Write-Output $resp.value
Expand Down
53 changes: 35 additions & 18 deletions src/teams.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ function _buildURL {
# Apply types to the returned objects so format and type files can
# identify the object and act on it.
function _applyTypes {
param($item)
$item.PSObject.TypeNames.Insert(0, 'Team.Team')
param(
[Parameter(Mandatory = $true)]
$item,
[Parameter(Mandatory = $true)]
$ProjectName
)

# Add the ProjectName as a NoteProperty so we can use it further down the pipeline (it's not returned from the REST call)
$item | Add-Member -MemberType NoteProperty -Name ProjectName -Value $ProjectName
$item.PSObject.TypeNames.Insert(0, 'Team.Team')
}

function Get-Team {
Expand All @@ -43,7 +51,7 @@ function Get-Team {
[Parameter(ParameterSetName = 'List')]
[int] $Skip,

[Parameter(ParameterSetName = 'ByID', ValueFromPipeline = $true)]
[Parameter(ParameterSetName = 'ByID')]
[string[]] $TeamId
)

Expand All @@ -68,7 +76,7 @@ function Get-Team {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Uri $listurl -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}

_applyTypes -item $resp
_applyTypes -item $resp -ProjectName $ProjectName

Write-Output $resp
}
Expand All @@ -89,7 +97,7 @@ function Get-Team {

# Apply a Type Name so we can use custom format view and custom type extensions
foreach ($item in $resp.value) {
_applyTypes -item $item
_applyTypes -item $item -ProjectName $ProjectName
}

Write-Output $resp.value
Expand All @@ -98,6 +106,7 @@ function Get-Team {
}

function Add-Team {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$TeamName,
Expand All @@ -122,15 +131,17 @@ function Add-Team {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Post -ContentType "application/json" -Body $body -Uri $listurl -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}

_applyTypes -item $resp
_applyTypes -item $resp -ProjectName $ProjectName

return $resp
}
}

function Update-Team {
[CmdletBinding()]
param(
[Parameter(Mandatory = $True)]
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $true)]
[Alias('name')]
[string]$TeamToUpdate,
[string]$NewTeamName,
[string]$Description
Expand Down Expand Up @@ -169,16 +180,20 @@ function Update-Team {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Patch -ContentType "application/json" -Body $body -Uri $listurl -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}

_applyTypes -item $resp
_applyTypes -item $resp -ProjectName $ProjectName

return $resp
}
}

function Remove-Team {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
param(
[Parameter(Mandatory = $True)]
[string]$TeamId
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $true)]
[Alias('name')]
[string]$TeamId,

[switch]$Force
)
DynamicParam {
_buildProjectNameDynamicParam
Expand All @@ -190,15 +205,17 @@ function Remove-Team {

$listurl = _buildURL -ProjectName $ProjectName -TeamId $TeamId

# Call the REST API
if (_useWindowsAuthenticationOnPremise) {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Delete -Uri $listurl -UseDefaultCredentials
}
else {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Delete -Uri $listurl -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}
if ($Force -or $PSCmdlet.ShouldProcess($TeamId, "Delete team")) {
# Call the REST API
if (_useWindowsAuthenticationOnPremise) {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Delete -Uri $listurl -UseDefaultCredentials
}
else {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Delete -Uri $listurl -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}

return $resp
Write-Output "Deleted team $TeamId"
}
}
}

Expand Down
Loading

0 comments on commit cb5a844

Please sign in to comment.