functions/github/Assert-GitHubTeam.ps1
function Assert-GitHubTeam { [CmdletBinding()] param ( [Parameter(Mandatory=$true)] [string] $Org, [Parameter(Mandatory=$true)] [string] $Name, [Parameter(Mandatory=$true)] [string] $Description, [Parameter(Mandatory=$true)] [AllowEmptyCollection()] [string[]] $Members, [Parameter(Mandatory=$true)] [AllowEmptyCollection()] [string[]] $Repositories, [Parameter(Mandatory=$true)] [ValidateSet("pull","push","admin","triage","maintain")] [string] $RepoAccess, [Parameter()] [switch] $StrictMode ) $teamResults = @{ team_name = "" team_created = $false team_updated = $false members_added = @() members_removed = @() repos_added = @() repos_updated = @() repos_removed = @() } $headers = @{ Accept = "application/vnd.github.v3+json" } $existingTeams = Invoke-GitHubRestMethod -Uri "https://api.github.com/orgs/$Org/teams" -Headers $headers if (!($Name -in $existingTeams.name)) { # create team Write-Information "Creating team '$Name" $resp = Invoke-GitHubRestMethod ` -Uri "https://api.github.com/orgs/$Org/teams" ` -Verb POST ` -Body @{ name = $Name description = $Description # repo_names = @() privacy = "closed" } $teamResults.team_created = $true } $teamResults.team_name = $Name $ghTeam = Invoke-GitHubRestMethod -Uri "https://api.github.com/orgs/$Org/teams/$Name" if (!$ghTeam) { throw "Something went wrong - the team '$Name' could not be found" } # Synchronise the team membership if ($Members) { $teamMemberResults = $ghTeam | Assert-GitHubTeamMembers -Members $Members -RemoveExtraMembers:$StrictMode $teamResults.members_added = $teamMemberResults.members_added $teamResults.members_removed = $teamMemberResults.members_removed # avoid flagging the team as being updated if it was created above if (!$teamResults.team_created -and ` $teamMemberResults.members_added -gt 0 -or ` $teamMemberResults.members_removed -gt 0 ) { $teamResults.team_updated = $true } } # Configure which repositories the team has access to if ($Repositories) { $teamRepoAccessResults = $ghTeam | Assert-GitHubTeamRepoAccess -Repositories $Repositories -RepoAccess $RepoAccess $teamResults.repos_added = $teamRepoAccessResults.repos_permission_added $teamResults.repos_updated = $teamRepoAccessResults.repos_permission_updated $teamResults.repos_removed = $teamRepoAccessResults.repos_permission_removed } return $teamResults } |