From adb705108056fb10392d007247322f0f15343807 Mon Sep 17 00:00:00 2001 From: Howard Wolosky Date: Wed, 12 Aug 2020 11:15:53 -0700 Subject: [PATCH] Add rename --- GitHubTeams.ps1 | 126 +++++++++++++++++++++++++++++++++++++++ PowerShellForGitHub.psd1 | 1 + 2 files changed, 127 insertions(+) diff --git a/GitHubTeams.ps1 b/GitHubTeams.ps1 index b1ef4a2a..43471f38 100644 --- a/GitHubTeams.ps1 +++ b/GitHubTeams.ps1 @@ -527,6 +527,11 @@ filter Set-GitHubTeam .PARAMETER TeamName The name of the team. + When TeamSlug is specified, specifying a name here that is different from the existing + name will cause the team to be renamed. TeamSlug and TeamName are specified for you + automatically when piping in a GitHub.Team object, so a rename would only occur if + intentionally specify this parameter and provide a different name. + .PARAMETER TeamSlug The slug (a unique key based on the team name) of the team to update. @@ -668,6 +673,127 @@ filter Set-GitHubTeam return (Invoke-GHRestMethod @params | Add-GitHubTeamAdditionalProperties) } +filter Rename-GitHubTeam +{ +<# + .SYNOPSIS + Renames a team within an organization on GitHub. + + .DESCRIPTION + Renames a team within an organization on GitHub. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OrganizationName + The name of the team's organization. + + .PARAMETER TeamName + The existing name of the team. + + .PARAMETER TeamSlug + The slug (a unique key based on the team name) of the team to update. + + .PARAMETER NewTeamName + The new name for the team. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .INPUTS + GitHub.Organization + GitHub.Team + + .OUTPUTS + GitHub.Team + + .EXAMPLE + Rename-GitHubTeam -OrganizationName PowerShell -TeamName Developers -NewTeamName DeveloperTeam + + Renames the 'Developers' GitHub team in the 'PowerShell' organization to be 'DeveloperTeam'. + + .EXAMPLE + $team = Get-GitHubTeam -OrganizationName PowerShell -TeamName Developers + $team | Rename-GitHubTeam -NewTeamName 'DeveloperTeam' + + You can also pipe in a GitHub team that was returned from a previous command. +#> + [CmdletBinding( + SupportsShouldProcess, + PositionalBinding = $false + )] + [OutputType( { $script:GitHubTeamTypeName } )] + param + ( + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + Position = 1)] + [ValidateNotNullOrEmpty()] + [string] $OrganizationName, + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + Position = 2, + ParameterSetName='TeamName')] + [ValidateNotNullOrEmpty()] + [string] $TeamName, + + [Parameter( + ValueFromPipelineByPropertyName, + ParameterSetName='TeamSlug')] + [ValidateNotNullOrEmpty()] + [string] $TeamSlug, + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + Position = 3)] + [ValidateNotNullOrEmpty()] + [string] $NewTeamName, + + [string] $AccessToken + ) + + Write-InvocationLog + + $telemetryProperties = @{ + OrganizationName = (Get-PiiSafeString -PlainText $OrganizationName) + TeamSlug = (Get-PiiSafeString -PlainText $TeamSlug) + TeamName = (Get-PiiSafeString -PlainText $TeamName) + } + + if ($PSBoundParameters.ContainsKey('TeamName')) + { + $team = Get-GitHubTeam -OrganizationName $OrganizationName -TeamName $TeamName -AccessToken:$AccessToken + $TeamSlug = $team.slug + } + + $uriFragment = "/orgs/$OrganizationName/teams/$TeamSlug" + + $hashBody = @{ + name = $NewTeamName + } + + if (-not $PSCmdlet.ShouldProcess($NewTeamName, "Rename GitHub Team ($TeamSlug) to")) + { + return + } + + $params = @{ + UriFragment = $uriFragment + Body = (ConvertTo-Json -InputObject $hashBody) + Method = 'Patch' + Description = "Renaming $TeamSlug" + AccessToken = $AccessToken + TelemetryEventName = $MyInvocation.MyCommand.Name + TelemetryProperties = $telemetryProperties + } + + return (Invoke-GHRestMethod @params | Add-GitHubTeamAdditionalProperties) +} + filter Remove-GitHubTeam { <# diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 10944b72..a17a3e7d 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -165,6 +165,7 @@ 'Remove-GitHubTeam', 'Rename-GitHubGistFile', 'Rename-GitHubRepository', + 'Rename-GitHubTeam', 'Reset-GitHubConfiguration', 'Restore-GitHubConfiguration', 'Set-GitHubAuthentication',