Skip to content

Commit

Permalink
Simplified Rename and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HowardWolosky committed Aug 12, 2020
1 parent 49d41a8 commit 8b3e3b0
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 49 deletions.
50 changes: 20 additions & 30 deletions GitHubTeams.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,17 @@ filter Set-GitHubTeam
}
elseif ($PSBoundParameters.ContainsKey('ParentTeamId'))
{
$hashBody['parent_team_id'] = $ParentTeamId
if ($ParentTeamId -gt 0)
{
$hashBody['parent_team_id'] = $ParentTeamId
}
else
{
$hashBody['parent_team_id'] = $null
}
}

if (-not $PSCmdlet.ShouldProcess($TeamName, 'Set GitHub Team'))
if (-not $PSCmdlet.ShouldProcess($TeamSlug, 'Set GitHub Team'))
{
return
}
Expand Down Expand Up @@ -717,11 +724,13 @@ filter Rename-GitHubTeam
$team | Rename-GitHubTeam -NewTeamName 'DeveloperTeam'
You can also pipe in a GitHub team that was returned from a previous command.
.NOTES
This is a helper/wrapper for Set-GitHubTeam which can also rename a GitHub Team.
#>
[CmdletBinding(
SupportsShouldProcess,
PositionalBinding = $false
)]
PositionalBinding = $false,
DefaultParameterSetName = 'TeamSlug')]
[OutputType( { $script:GitHubTeamTypeName } )]
param
(
Expand All @@ -741,6 +750,7 @@ filter Rename-GitHubTeam
[string] $TeamName,

[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='TeamSlug')]
[ValidateNotNullOrEmpty()]
Expand All @@ -758,40 +768,20 @@ filter Rename-GitHubTeam

Write-InvocationLog

$telemetryProperties = @{
OrganizationName = (Get-PiiSafeString -PlainText $OrganizationName)
TeamSlug = (Get-PiiSafeString -PlainText $TeamSlug)
TeamName = (Get-PiiSafeString -PlainText $TeamName)
}

if ($PSBoundParameters.ContainsKey('TeamName'))
if (-not $PSBoundParameters.ContainsKey('TeamSlug'))
{
$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"
OrganizationName = $OrganizationName
TeamSlug = $TeamSlug
TeamName = $NewTeamName
AccessToken = $AccessToken
TelemetryEventName = $MyInvocation.MyCommand.Name
TelemetryProperties = $telemetryProperties
}

return (Invoke-GHRestMethod @params | Add-GitHubTeamAdditionalProperties)
return Set-GitHubTeam @params
}

filter Remove-GitHubTeam
Expand Down
139 changes: 120 additions & 19 deletions Tests/GitHubTeams.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ try
$team.description | Should -Be $description
$team.parent | Should -BeNullOrEmpty
$team.privacy | Should -Be $privacy
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand All @@ -79,7 +79,7 @@ try
$team.updated_at | Should -Not -BeNullOrEmpty
$team.members_count | Should -Be 1
$team.repos_count | Should -Be 0
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand All @@ -97,7 +97,7 @@ try
$team.description | Should -Be $description
$team.parent | Should -BeNullOrEmpty
$team.privacy | Should -Be $privacy
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -141,7 +141,7 @@ try
$team.description | Should -Be $description
$team.parent | Should -BeNullOrEmpty
$team.privacy | Should -Be $privacy
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand All @@ -167,7 +167,7 @@ try
$team.updated_at | Should -Not -BeNullOrEmpty
$team.members_count | Should -Be 1
$team.repos_count | Should -Be 1
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand All @@ -190,7 +190,7 @@ try
$team.members_count | Should -Be 1
$team.repos_count | Should -Be 1
$team.privacy | Should -Be $privacy
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -239,7 +239,7 @@ try
$team.members_count | Should -Be 1
$team.repos_count | Should -Be 0
$team.privacy | Should -Be $privacy
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand All @@ -256,7 +256,7 @@ try
$team.members_count | Should -Be 1
$team.repos_count | Should -Be 0
$team.privacy | Should -Be $privacy
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -294,7 +294,7 @@ try
$team.parent | Should -BeNullOrEmpty
$team.members_count | Should -Be 1
$team.repos_count | Should -Be 0
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -345,7 +345,7 @@ try
$team.members_count | Should -Be 1
$team.repos_count | Should -Be 1
$team.privacy | Should -Be $privacy
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -394,7 +394,7 @@ try
$childTeam.organization.login | Should -Be $organizationName
$childTeam.parent.name | Should -Be $parentTeamName
$childTeam.privacy | Should -Be $privacy
$childTeam.TeamName | Should -Be $childTeamName
$childTeam.TeamName | Should -Be $childTeam.name
$childTeam.TeamId | Should -Be $childTeam.id
$childTeam.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -443,7 +443,7 @@ try
$childTeam.organization.login | Should -Be $organizationName
$childTeam.parent.name | Should -Be $parentTeamName
$childTeam.privacy | Should -Be $privacy
$childTeam.TeamName | Should -Be $childTeamName
$childTeam.TeamName | Should -Be $childTeam.name
$childTeam.TeamId | Should -Be $childTeam.id
$childTeam.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -490,7 +490,7 @@ try
$childTeam.organization.login | Should -Be $organizationName
$childTeam.parent.name | Should -Be $parentTeamName
$childTeam.privacy | Should -Be $privacy
$childTeam.TeamName | Should -Be $childTeamName
$childTeam.TeamName | Should -Be $childTeam.name
$childTeam.TeamId | Should -Be $childTeam.id
$childTeam.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -524,7 +524,7 @@ try
$team.updated_at | Should -Not -BeNullOrEmpty
$team.members_count | Should -Be 1
$team.repos_count | Should -Be 0
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -554,7 +554,7 @@ try
$team.updated_at | Should -Not -BeNullOrEmpty
$team.members_count | Should -Be 1
$team.repos_count | Should -Be 0
$team.TeamName | Should -Be $teamName
$team.TeamName | Should -Be $team.name
$team.TeamId | Should -Be $team.id
$team.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -614,7 +614,7 @@ try
$updatedTeam.description | Should -Be $description
$updatedTeam.parent.name | Should -Be $parentTeamName
$updatedTeam.privacy | Should -Be $privacy
$updatedTeam.TeamName | Should -Be $teamName
$updatedTeam.TeamName | Should -Be $team.name
$updatedTeam.TeamId | Should -Be $team.id
$updatedTeam.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -667,7 +667,7 @@ try
$updatedTeam.updated_at | Should -Not -BeNullOrEmpty
$updatedTeam.members_count | Should -Be 1
$updatedTeam.repos_count | Should -Be 0
$updatedTeam.TeamName | Should -Be $teamName
$updatedTeam.TeamName | Should -Be $team.name
$updatedTeam.TeamId | Should -Be $team.id
$updatedTeam.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -721,7 +721,7 @@ try
$updatedTeam.description | Should -Be $description
$updatedTeam.parent.name | Should -Be $parentTeamName
$updatedTeam.privacy | Should -Be $privacy
$updatedTeam.TeamName | Should -Be $teamName
$updatedTeam.TeamName | Should -Be $team.name
$updatedTeam.TeamId | Should -Be $team.id
$updatedTeam.OrganizationName | Should -Be $organizationName
}
Expand Down Expand Up @@ -764,7 +764,7 @@ try
$updatedTeam.updated_at | Should -Not -BeNullOrEmpty
$updatedTeam.members_count | Should -Be 1
$updatedTeam.repos_count | Should -Be 0
$updatedTeam.TeamName | Should -Be $teamName
$updatedTeam.TeamName | Should -Be $team.name
$updatedTeam.TeamId | Should -Be $updatedTeam.id
$updatedTeam.OrganizationName | Should -Be $organizationName
}
Expand All @@ -778,6 +778,107 @@ try
}
}

Describe 'GitHubTeams\Rename-GitHubTeam' {
BeforeAll {
$organizationName = $script:organizationName
$teamName = [Guid]::NewGuid().Guid
$newTeamName = [Guid]::NewGuid().Guid
}

Context 'When renaming a GitHub team with the TeamName' {
BeforeAll {
$team = New-GitHubTeam -OrganizationName $organizationName -TeamName $teamName
}

$updatedTeam = Rename-GitHubTeam -OrganizationName $organizationName -TeamName $teamName -NewTeamName $newTeamName
It 'Should have the expected type and additional properties' {
$updatedTeam.PSObject.TypeNames[0] | Should -Be 'GitHub.Team'
$updatedTeam.name | Should -Be $newTeamName
$updatedTeam.organization.login | Should -Be $OrganizationName
$updatedTeam.description | Should -BeNullOrEmpty
$updatedTeam.parent.name | Should -BeNullOrEmpty
$updatedTeam.created_at | Should -Not -BeNullOrEmpty
$updatedTeam.updated_at | Should -Not -BeNullOrEmpty
$updatedTeam.members_count | Should -Be 1
$updatedTeam.repos_count | Should -Be 0
$updatedTeam.TeamName | Should -Be $updatedTeam.name
$updatedTeam.TeamId | Should -Be $updatedTeam.id
$updatedTeam.OrganizationName | Should -Be $organizationName
}

It 'Should find the renamed team' {
{ Get-GitHubTeam -OrganizationName $organizationName -TeamName $newTeamName } |
Should -Not -Throw
}

AfterAll {
Remove-GitHubTeam -OrganizationName $organizationName -TeamName $newTeamName -Force
}
}

Context 'When renaming a GitHub team with the TeamSlug' {
BeforeAll {
$team = New-GitHubTeam -OrganizationName $organizationName -TeamName $teamName
}

$updatedTeam = Rename-GitHubTeam -OrganizationName $organizationName -TeamSlug $team.slug -NewTeamName $newTeamName
It 'Should have the expected type and additional properties' {
$updatedTeam.PSObject.TypeNames[0] | Should -Be 'GitHub.Team'
$updatedTeam.name | Should -Be $newTeamName
$updatedTeam.organization.login | Should -Be $OrganizationName
$updatedTeam.description | Should -BeNullOrEmpty
$updatedTeam.parent.name | Should -BeNullOrEmpty
$updatedTeam.created_at | Should -Not -BeNullOrEmpty
$updatedTeam.updated_at | Should -Not -BeNullOrEmpty
$updatedTeam.members_count | Should -Be 1
$updatedTeam.repos_count | Should -Be 0
$updatedTeam.TeamName | Should -Be $updatedTeam.name
$updatedTeam.TeamId | Should -Be $updatedTeam.id
$updatedTeam.OrganizationName | Should -Be $organizationName
}

It 'Should find the renamed team' {
{ Get-GitHubTeam -OrganizationName $organizationName -TeamName $newTeamName } |
Should -Not -Throw
}

AfterAll {
Remove-GitHubTeam -OrganizationName $organizationName -TeamName $newTeamName -Force
}
}

Context 'When renaming a GitHub team with the TeamSlug on the pipeline' {
BeforeAll {
$team = New-GitHubTeam -OrganizationName $organizationName -TeamName $teamName
}

$updatedTeam = $team | Rename-GitHubTeam -NewTeamName $newTeamName
It 'Should have the expected type and additional properties' {
$updatedTeam.PSObject.TypeNames[0] | Should -Be 'GitHub.Team'
$updatedTeam.name | Should -Be $newTeamName
$updatedTeam.organization.login | Should -Be $OrganizationName
$updatedTeam.description | Should -BeNullOrEmpty
$updatedTeam.parent.name | Should -BeNullOrEmpty
$updatedTeam.created_at | Should -Not -BeNullOrEmpty
$updatedTeam.updated_at | Should -Not -BeNullOrEmpty
$updatedTeam.members_count | Should -Be 1
$updatedTeam.repos_count | Should -Be 0
$updatedTeam.TeamName | Should -Be $updatedTeam.name
$updatedTeam.TeamId | Should -Be $updatedTeam.id
$updatedTeam.OrganizationName | Should -Be $organizationName
}

It 'Should find the renamed team' {
{ Get-GitHubTeam -OrganizationName $organizationName -TeamName $newTeamName } |
Should -Not -Throw
}

AfterAll {
Remove-GitHubTeam -OrganizationName $organizationName -TeamName $newTeamName -Force
}
}
}

Describe 'GitHubTeams\Remove-GitHubTeam' {
BeforeAll {
$organizationName = $script:organizationName
Expand Down

0 comments on commit 8b3e3b0

Please sign in to comment.