Skip to content

Commit

Permalink
Add support for teams
Browse files Browse the repository at this point in the history
  • Loading branch information
KeesV committed Sep 4, 2017
1 parent f9a0eca commit bb42e3c
Show file tree
Hide file tree
Showing 3 changed files with 442 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Team.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
'src\queues.psm1',
'src\releaseDefinitions.psm1',
'src\releases.psm1',
'src\serviceendpoints.psm1')
'src\serviceendpoints.psm1',
'.\src\teams.psm1')

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @('Add-AzureRMServiceEndpoint',
Expand All @@ -90,6 +91,7 @@
'Add-ReleaseEnvironment',
'Add-ReleaseDefinition',
'Add-TeamAccount',
'Add-Team',
'Clear-DefaultProject',
'Get-Approval',
'Get-Build',
Expand All @@ -102,17 +104,21 @@
'Get-ReleaseDefinition',
'Get-ServiceEndpoint',
'Get-TeamInfo',
'Get-Team',
'Get-TeamMembers',
'Remove-Build',
'Remove-BuildDefinition',
'Remove-Project',
'Remove-Release',
'Remove-ReleaseDefinition',
'Remove-ServiceEndpoint',
'Remove-TeamAccount',
'Remove-Team',
'Set-Approval',
'Set-DefaultProject',
'Set-ReleaseStatus',
'Update-Project',
'Update-Team',
'Get-GitRepository',
'Add-GitRepository',
'Remove-GitRepository',
Expand Down
309 changes: 309 additions & 0 deletions src/teams.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
Set-StrictMode -Version Latest

# Load common code
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\common.ps1"

function _buildURL {
param(
[parameter(Mandatory = $true)]
[string] $ProjectName,
[string] $TeamId,
[switch] $GetMembers
)

if(-not $env:TEAM_ACCT) {
throw 'You must call Add-TeamAccount before calling any other functions in this module.'
}

$version = '1.0'
$resource = "/projects/$ProjectName/teams"
$instance = $env:TEAM_ACCT

if ($TeamId) {
$resource += "/$TeamId"
}

if ($GetMembers.IsPresent) {
if(-not $TeamID) {
throw 'You must provide $TeamId when getting team members.'
}
$resource += "/members"
}

# Build the url to list the projects
return $instance + '/_apis' + $resource + '?api-version=' + $version
}

# 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.Project')

# Only returned for a single item
if ($item.PSObject.Properties.Match('defaultTeam').count -gt 0 -and $null -ne $item.defaultTeam) {
$item.defaultTeam.PSObject.TypeNames.Insert(0, 'Team.Team')
}

if ($item.PSObject.Properties.Match('_links').count -gt 0 -and $null -ne $item._links) {
$item._links.PSObject.TypeNames.Insert(0, 'Team.Links')
}
}

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

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

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

DynamicParam {
_buildProjectNameDynamicParam
}

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

if($TeamId) {
foreach ($item in $TeamId) {
# Build the url to return the single build
$listurl = _buildURL -projectName $ProjectName -teamId $item

# Call the REST API
if (_useWindowsAuthenticationOnPremise) {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Uri $listurl -UseDefaultCredentials
}
else {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Uri $listurl -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}

# HOW DOES THIS WORK?!?
# _applyTypes -item $resp

Write-Output $resp
}
} else {
# Build the url to list the builds
$listurl = _buildURL -projectName $ProjectName

$listurl += _appendQueryString -name "`$top" -value $top
$listurl += _appendQueryString -name "`$skip" -value $skip

# Call the REST API
if (_useWindowsAuthenticationOnPremise) {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Uri $listurl -UseDefaultCredentials
}
else {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Uri $listurl -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}

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

Write-Output $resp.value
}
}
}

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

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

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

DynamicParam {
_buildProjectNameDynamicParam
}

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


# Build the url to list the builds
$listurl = _buildURL -projectName $ProjectName -teamId $TeamId -GetMembers

$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
}
else {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Uri $listurl -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}

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

Write-Output $resp.value
}
}

function Add-Team {
param(
[Parameter(Mandatory = $true)]
[string]$TeamName,
[string]$Description = ""
)
DynamicParam {
_buildProjectNameDynamicParam
}

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

$listurl = _buildURL -ProjectName $ProjectName
$body = '{ "name": "' + $TeamName + '", "description": "' + $Description + '" }'

# Call the REST API
if (_useWindowsAuthenticationOnPremise) {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Post -ContentType "application/json" -Body $body -Uri $listurl -UseDefaultCredentials
}
else {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Post -ContentType "application/json" -Body $body -Uri $listurl -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}

# HOW DOES THIS WORK?!?
#_applyTypes -item $resp

return $resp
}
}

function Add-Team {
param(
[Parameter(Mandatory = $true)]
[string]$TeamName,
[string]$Description = ""
)
DynamicParam {
_buildProjectNameDynamicParam
}

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

$listurl = _buildURL -ProjectName $ProjectName
$body = '{ "name": "' + $TeamName + '", "description": "' + $Description + '" }'

# Call the REST API
if (_useWindowsAuthenticationOnPremise) {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Post -ContentType "application/json" -Body $body -Uri $listurl -UseDefaultCredentials
}
else {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Post -ContentType "application/json" -Body $body -Uri $listurl -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}

# HOW DOES THIS WORK?!?
#_applyTypes -item $resp

return $resp
}
}

function Update-Team {
param(
[Parameter(Mandatory = $True)]
[string]$TeamToUpdate,
[string]$NewTeamName,
[string]$Description
)
DynamicParam {
_buildProjectNameDynamicParam
}

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

$listurl = _buildURL -ProjectName $ProjectName -TeamId $TeamToUpdate
if(-not $NewTeamName -and -not $Description) {
throw 'You must provide a new team name or description, or both.'
}

if(-not $NewTeamName)
{
$body = '{"description": "' + $Description + '" }'
}
if(-not $Description)
{
$body = '{ "name": "' + $NewTeamName + '" }'
}
if($NewTeamName -and $Description)
{
$body = '{ "name": "' + $NewTeamName + '", "description": "' + $Description + '" }'
}

# Call the REST API
if (_useWindowsAuthenticationOnPremise) {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Patch -ContentType "application/json" -Body $body -Uri $listurl -UseDefaultCredentials
}
else {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Patch -ContentType "application/json" -Body $body -Uri $listurl -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}

# HOW DOES THIS WORK?!?
#_applyTypes -item $resp

return $resp
}
}

function Remove-Team {
param(
[Parameter(Mandatory = $True)]
[string]$TeamId
)
DynamicParam {
_buildProjectNameDynamicParam
}

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

$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"}
}

# HOW DOES THIS WORK?!?
#_applyTypes -item $resp

return $resp
}
}

Export-ModuleMember -Alias * -Function Get-Team, Get-TeamMembers, Add-Team, Update-Team, Remove-Team
Loading

1 comment on commit bb42e3c

@DarqueWarrior
Copy link
Collaborator

@DarqueWarrior DarqueWarrior commented on bb42e3c Sep 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this might be an exception. If that api can return a single Team Member as well as a list we need to drop the s. But from reviewing the api I don't think it can. I think it only returns a list.
Take the S off Get-TeamMembers so it is just Get-TeamMember.

Please sign in to comment.