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

🚀 [Feature]: Implement GitHub team management functions #163

Merged
merged 2 commits into from
Nov 25, 2024
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
45 changes: 45 additions & 0 deletions src/functions/public/Teams/Delete-GitHubTeam.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function Remove-GitHubTeam {
<#
.SYNOPSIS
Delete a team

.DESCRIPTION
To delete a team, the authenticated user must be an organization owner or team maintainer.
If you are an organization owner, deleting a parent team will delete all of its child teams as well.

.EXAMPLE
An example

.NOTES
[Delete a team](https://docs.github.com/en/rest/teams/teams?apiVersion=2022-11-28#delete-a-team)
#>
[OutputType([void])]
[CmdletBinding(SupportsShouldProcess)]
param (
# The organization name. The name is not case sensitive.
[Parameter(Mandatory)]
[Alias('Org')]
[string] $Organization,

# The slug of the team name.
[Parameter(Mandatory)]
[Alias('Team', 'TeamName', 'slug', 'team_slug')]
[string] $Name,

# The context to run the command in
[Parameter()]
[string] $Context = (Get-GitHubConfig -Name DefaultContext)
)

$inputObject = @{
Context = $Context
Method = 'Delete'
APIEndpoint = "/orgs/$Organization/teams/$Name"
}

if ($PSCmdlet.ShouldProcess("$Organization/$Name", 'Delete')) {
Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}
}
1 change: 0 additions & 1 deletion src/functions/public/Teams/Get-GitHubRepoTeam.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ filter Get-GitHubRepoTeam {
Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}

}
43 changes: 43 additions & 0 deletions src/functions/public/Teams/Get-GitHubTeamByName.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function Get-GitHubTeamByName {
<#
.SYNOPSIS
Get a team by name

.DESCRIPTION
Gets a team using the team's slug. To create the slug, GitHub replaces special characters in the name string, changes all words to lowercase,
and replaces spaces with a - separator. For example, "My TEam Näme" would become my-team-name.

.EXAMPLE
Get-GitHubTeamByName -Organization 'github' -Name 'my-team-name'

.NOTES
[Get team by name](https://docs.github.com/en/rest/teams/teams#get-a-team-by-name)
#>
[OutputType([void])]
[CmdletBinding()]
param (
# The organization name. The name is not case sensitive.
[Parameter(Mandatory)]
[Alias('Org')]
[string] $Organization,

# The slug of the team name.
[Parameter(Mandatory)]
[Alias('Team', 'TeamName', 'slug', 'team_slug')]
[string] $Name,

# The context to run the command in
[Parameter()]
[string] $Context = (Get-GitHubConfig -Name DefaultContext)
)

$inputObject = @{
Context = $Context
Method = 'Get'
APIEndpoint = "/orgs/$Organization/teams/$Name"
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}
37 changes: 37 additions & 0 deletions src/functions/public/Teams/Get-GitHubTeamListByOrg.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function Get-GitHubTeamListByOrg {
<#
.SYNOPSIS
List teams

.DESCRIPTION
Lists all teams in an organization that are visible to the authenticated user.

.EXAMPLE
Get-GitHubTeamListByOrg -Organization 'github'

.NOTES
[List teams](https://docs.github.com/rest/teams/teams#list-teams)
#>
[OutputType([pscustomobject])]
[CmdletBinding()]
param (
# The organization name. The name is not case sensitive.
[Parameter(Mandatory)]
[Alias('Org')]
[string] $Organization,

# The context to run the command in
[Parameter()]
[string] $Context = (Get-GitHubConfig -Name DefaultContext)
)

$inputObject = @{
Context = $Context
Method = 'Get'
APIEndpoint = "/orgs/$Organization/teams"
}

Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}
96 changes: 96 additions & 0 deletions src/functions/public/Teams/New-GitHubTeam.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
function New-GitHubTeam {
<#
.SYNOPSIS
Create a team

.DESCRIPTION
To create a team, the authenticated user must be a member or owner of `{org}`. By default, organization members can create teams.
Organization owners can limit team creation to organization owners. For more information, see
"[Setting team creation permissions](https://docs.github.com/articles/setting-team-creation-permissions-in-your-organization)."

When you create a new team, you automatically become a team maintainer without explicitly adding yourself to the optional array of
`maintainers`. For more information, see
"[About teams](https://docs.github.com/github/setting-up-and-managing-organizations-and-teams/about-teams)".

.NOTES
[Create a team](https://docs.github.com/rest/teams/teams#create-a-team)
#>
[CmdletBinding(SupportsShouldProcess)]
param(
# The organization name. The name is not case sensitive.
[Parameter(Mandatory)]
[Alias('Org')]
[string] $Organization,

# The name of the team.
[Parameter(Mandatory)]
[string] $Name,

# The description of the team.
[Parameter()]
[string] $Description,

# List GitHub IDs for organization members who will become team maintainers.
[Parameter()]
[string[]] $Maintainers,

# The full name (e.g., "organization-name/repository-name") of repositories to add the team to.
[Parameter()]
[string[]] $RepoNames,

# The level of privacy this team should have. The options are:
# For a non-nested team:
# - secret - only visible to organization owners and members of this team.
# - closed - visible to all members of this organization.
# Default: secret
# For a parent or child team:
# - closed - visible to all members of this organization.
# Default for child team: closed
[Parameter()]
[ValidateSet('secret', 'closed')]
[string] $Privacy = 'closed',

# The notification setting the team has chosen. The options are:
# notifications_enabled - team members receive notifications when the team is @mentioned.
# notifications_disabled - no one receives notifications.
# Default: notifications_enabled
[Parameter()]
[ValidateSet('notifications_enabled', 'notifications_disabled')]
[string] $NotificationSetting,

# Closing down notice. The permission that new repositories will be added to the team with when none is specified.
[Parameter()]
[ValidateSet('pull', 'push')]
[string] $Permission = 'pull',

# The ID of a team to set as the parent team.
[Parameter()]
[int] $ParentTeamID
)

$body = @{
name = $Name
description = $Description
maintainers = $Maintainers
repo_names = $RepoNames
privacy = $Privacy
notification_setting = $NotificationSetting
permission = $Permission
parent_team_id = $ParentTeamID
}

$body | Remove-HashtableEntry -NullOrEmptyValues

$inputObject = @{
Context = $Context
Method = 'POST'
Body = $body
APIEndpoint = "/orgs/$Organization/teams"
}

if ($PSCmdlet.ShouldProcess("'$Name' in '$Organization'", 'Create team')) {
Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}
}
94 changes: 94 additions & 0 deletions src/functions/public/Teams/Update-GitHubTeam.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
function Update-GitHubTeam {
<#
.SYNOPSIS
Update a team

.DESCRIPTION
To edit a team, the authenticated user must either be an organization owner or a team maintainer.

.EXAMPLE


.NOTES
[Update a team](https://docs.github.com/en/rest/teams/teams?apiVersion=2022-11-28#update-a-team)
#>
[OutputType([pscustomobject])]
[CmdletBinding(SupportsShouldProcess)]
param (
# The organization name. The name is not case sensitive.
[Parameter(Mandatory)]
[Alias('Org')]
[string] $Organization,

# The slug of the team name.
[Parameter(Mandatory)]
[Alias('Team', 'TeamName', 'slug', 'team_slug')]
[string] $Name,

# The context to run the command in
[Parameter()]
[string] $Context = (Get-GitHubConfig -Name DefaultContext),

# The new team name.
[Parameter()]
[Alias()]
[string] $NewName,

# The description of the team.
[Parameter()]
[string] $Description,

# The level of privacy this team should have. The options are:
# For a non-nested team:
# - secret - only visible to organization owners and members of this team.
# - closed - visible to all members of this organization.
# Default: secret
# For a parent or child team:
# - closed - visible to all members of this organization.
# Default for child team: closed
[Parameter()]
[ValidateSet('secret', 'closed')]
[string] $Privacy = 'closed',

# The notification setting the team has chosen. The options are:
# notifications_enabled - team members receive notifications when the team is @mentioned.
# notifications_disabled - no one receives notifications.
# Default: notifications_enabled
[Parameter()]
[ValidateSet('notifications_enabled', 'notifications_disabled')]
[string] $NotificationSetting,

# Closing down notice. The permission that new repositories will be added to the team with when none is specified.
[Parameter()]
[ValidateSet('pull', 'push')]
[string] $Permission = 'pull',

# The ID of a team to set as the parent team.
[Parameter()]
[int] $ParentTeamID
)

$body = @{
name = $NewName
description = $Description
privacy = $Privacy
notification_setting = $NotificationSetting
permission = $Permission
parent_team_id = $ParentTeamID
}

$body | Remove-HashtableEntry -NullOrEmptyValues

$inputObject = @{
Context = $Context
Method = 'Patch'
APIEndpoint = "/orgs/$Organization/teams/$Name"
Body = $body
}

if ($PSCmdlet.ShouldProcess("$Organization/$Name", 'Update')) {
Invoke-GitHubAPI @inputObject | ForEach-Object {
Write-Output $_.Response
}
}
}
4 changes: 2 additions & 2 deletions tools/utilities/GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get
# @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, `
# @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table

$path = '/app/installations/{installation_id}/access_tokens'
$method = 'post'
$path = '/orgs/{org}/teams'
$method = 'get'
$response.paths.$path.$method
$response.paths.$path.$method.tags | clip # -> Namespace/foldername
$response.paths.$path.$method.operationId | clip # -> FunctionName
Expand Down
Loading