Skip to content
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
61 changes: 61 additions & 0 deletions src/classes/public/Teams/GitHubTeam.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class GitHubTeam {
# The name of the team.
[string] $Name

# The slug of the team.
[string] $Slug

# The organization the team belongs to.
[string] $Organization

# The combined slug of the team.
[string] $CombinedSlug

# The ID of the team.
[string] $NodeID

# The database ID of the team.
[string] $DatabaseID

# The description of the team.
[string] $Description

# The notification setting the team has chosen.
# $true = notifications_enabled - team members receive notifications when the team is @mentioned.
# $false = notifications_disabled - no one receives notifications.
[bool] $Notifications = $true

# The privacy setting of the team.
# $true = closed - visible to all members of this organization.
# $false = secret - only visible to organization owners and members of this team.
[bool] $Visible = $true

# The parent team of the team.
[string] $ParentTeam

# The child teams of the team.
[string[]] $ChildTeams

# The date and time the team was created.
[datetime] $CreatedAt

# The date and time the team was last updated.
[datetime] $UpdatedAt

# Simple parameterless constructor
GitHubTeam() {}

# Creates a object from a hashtable of key-vaule pairs.
GitHubTeam([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
$this.$Property = $Properties.$Property
}
}

# Creates a object from a PSCustomObject.
GitHubTeam([PSCustomObject]$Object) {
$Object.PSObject.Properties | ForEach-Object {
$this.($_.Name) = $_.Value
}
}
}
82 changes: 82 additions & 0 deletions src/functions/private/Teams/Get-GitHubRESTTeam.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
function Get-GitHubRESTTeam {
<#
.SYNOPSIS
List teams from an org or get a team by name

.DESCRIPTION
Lists all teams in an organization that are visible to the authenticated user or 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-GitHubRESTTeam -Organization 'GitHub'

Gets all teams in the `github` organization.

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

Gets the team with the slug 'my-team-name' in the `github` organization.

.NOTES
[List teams](https://docs.github.com/rest/teams/teams#list-teams)
[Get team by name](https://docs.github.com/en/rest/teams/teams#get-a-team-by-name)
#>
[OutputType([pscustomobject])]
[CmdletBinding(DefaultParameterSetName = '__AllParameterSets')]
param(
# The organization name. The name is not case sensitive.
# If not provided, the organization from the context is used.
[Parameter()]
[Alias('Org')]
[string] $Organization,

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

# The context to run the command in. Used to get the details for the API call.
# Can be either a string or a GitHubContext object.
[Parameter()]
[object] $Context = (Get-GitHubContext)
)

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
$Context = Resolve-GitHubContext -Context $Context
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT

if ([string]::IsNullOrEmpty($Organization)) {
$Organization = $Context.Owner
}
Write-Debug "Organization: [$Organization]"
}

process {
try {
$params = @{
Organization = $Organization
Context = $Context
}
switch ($PSCmdlet.ParameterSetName) {
'GetByName' {
Get-GitHubTeamByName @params -Name $Name
}
'__AllParameterSets' {
Get-GitHubTeamListByOrg @params
}
}
} catch {
throw $_
}
}

end {
Write-Debug "[$stackPath] - End"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,20 @@

.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 organization name. The name is not case sensitive.
[Parameter(Mandatory)]
[Alias('Org')]
[string] $Organization,

# The context to run the command in. Used to get the details for the API call.
# Can be either a string or a GitHubContext object.
[Parameter()]
Expand Down
121 changes: 121 additions & 0 deletions src/functions/private/Teams/Get-GitHubTeamBySlug.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
function Get-GitHubTeamBySlug {
<#
.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-GitHubTeamBySlug -Organization 'github' -Slug 'my-team-name'
#>
[OutputType([GitHubTeam])]
[CmdletBinding()]
param(
# The slug of the team name.
[Parameter(Mandatory)]
[Alias('team_slug')]
[string] $Slug,

# The organization name. The name is not case sensitive.
# If not provided, the owner from the context will be used.
[Parameter()]
[Alias('Org')]
[string] $Organization,

# The context to run the command in. Used to get the details for the API call.
# Can be either a string or a GitHubContext object.
[Parameter()]
[object] $Context = (Get-GitHubContext)
)

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
$Context = Resolve-GitHubContext -Context $Context
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT

if ([string]::IsNullOrEmpty($Organization)) {
$Organization = $Context.Owner
}
Write-Debug "Organization: [$Organization]"
}

process {
try {
$query = @"
query(`$org: String!, `$teamSlug: String!) {
organization(login: `$org) {
team(slug: `$teamSlug) {
id
name
slug
combinedSlug
databaseId
description
notificationSetting
privacy
parentTeam {
name
slug
}
organization {
login
}
childTeams(first: 100) {
nodes {
name
}
}
createdAt
updatedAt
}
}
}
}
"@

# Variables hash that will be sent with the query
$variables = @{
org = $Organization
teamSlug = $Slug
}

# Send the request to the GitHub GraphQL API
$response = Invoke-GitHubGraphQLQuery -Query $query -Variables $variables

# Extract team data
$team = $response.data.organization.team

# Output the team object
if (-not $team) {
return
}

[GitHubTeam](
@{
Name = $team.name
Slug = $team.slug
NodeID = $team.id
CombinedSlug = $team.CombinedSlug
DatabaseID = $team.DatabaseId
Description = $team.description
Notifications = $team.notificationSetting -eq 'NOTIFICATIONS_ENABLED' ? $true : $false
Visible = $team.privacy -eq 'VISIBLE' ? $true : $false
ParentTeam = $team.parentTeam.slug
Organization = $team.organization.login
ChildTeams = $team.childTeams.nodes.name
CreatedAt = $team.createdAt
UpdatedAt = $team.updatedAt
}
)
} catch {
throw $_
}
}

end {
Write-Debug "[$stackPath] - End"
}
}
Loading