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

Add support for teams #15

Merged
merged 8 commits into from
Sep 7, 2017
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
11 changes: 9 additions & 2 deletions Team.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
RootModule = ''

# Version number of this module.
ModuleVersion = '0.1.29'
ModuleVersion = '0.1.30'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -78,7 +78,9 @@
'src\queues.psm1',
'src\releaseDefinitions.psm1',
'src\releases.psm1',
'src\serviceendpoints.psm1')
'src\serviceendpoints.psm1',
'src\teams.psm1',
'src\teammembers.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 +92,7 @@
'Add-ReleaseEnvironment',
'Add-ReleaseDefinition',
'Add-TeamAccount',
'Add-Team',
'Clear-DefaultProject',
'Get-Approval',
'Get-Build',
Expand All @@ -102,17 +105,21 @@
'Get-ReleaseDefinition',
'Get-ServiceEndpoint',
'Get-TeamInfo',
'Get-Team',
'Get-TeamMember',
'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
88 changes: 88 additions & 0 deletions src/teammembers.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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,
[parameter(Mandatory = $true)]
[string] $TeamId
)

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/$TeamId/members"
$instance = $env:TEAM_ACCT

# 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(
[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()]
param (
[Parameter()]
[int] $Top,

[Parameter()]
[int] $Skip,

[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName=$true)]
[Alias('name')]
[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

$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 -team $TeamId
}

Write-Output $resp.value
}
}

Export-ModuleMember -Alias * -Function Get-TeamMember
222 changes: 222 additions & 0 deletions src/teams.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
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
)

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

# 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(
[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 {
[CmdletBinding(DefaultParameterSetName = 'List')]
param (
[Parameter(ParameterSetName = 'List')]
[int] $Top,

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

[Parameter(ParameterSetName = 'ByID')]
[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"}
}

_applyTypes -item $resp -ProjectName $ProjectName

Write-Output $resp
}
} else {
# Build the url to list the teams
$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 -ProjectName $ProjectName
}

Write-Output $resp.value
}
}
}

function Add-Team {
[CmdletBinding()]
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"}
}

_applyTypes -item $resp -ProjectName $ProjectName

return $resp
}
}

function Update-Team {
[CmdletBinding()]
param(
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $true)]
[Alias('name')]
[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"}
}

_applyTypes -item $resp -ProjectName $ProjectName

return $resp
}
}

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

[switch]$Force
)
DynamicParam {
_buildProjectNameDynamicParam
}

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

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

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

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

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