Skip to content

Commit 66f8a99

Browse files
🚀 [Feature]: Refactor Teams (#225)
## Description This pull request introduces significant changes to the GitHub Teams functions, including the addition of new functions, modifications to existing functions, and the creation of a new class to represent GitHub teams. - Fixes #218 - Fixes #219 ### New Functionality * [`src/classes/public/Teams/GitHubTeam.ps1`](diffhunk://#diff-eea277d020f22d0ad02d67c0dd63a7d10659e9c00f6c17ab1d67309a96822c13R1-R61): Added a new `GitHubTeam` class to represent a GitHub team with properties such as `Name`, `Slug`, `Organization`, `NodeID`, `DatabaseID`, `Description`, `Notifications`, `Visible`, `ParentTeam`, `ChildTeams`, `CreatedAt`, and `UpdatedAt`. ### Rename and hide old functions * [`src/functions/private/Teams/Get-GitHubRESTTeam.ps1`](diffhunk://#diff-efacd3612f7ed73e0426147e0fac909035328b1ea1a8f9a4a30ec5222b8666b1R1-R82): Renamed the `Get-GitHubTeam` function to `Get-GitHubRESTTeam` and made it private, as the GraphQL versions makes it redundant. ### New GraphQL based Team functions * [`src/functions/private/Teams/Get-GitHubTeamListByOrg.ps1`](diffhunk://#diff-af6abadf6c54e780d7919db9bcc9d74cd401faab345af0b4e80c46ab76b269baR1-R133): Added the `Get-GitHubTeamListByOrg` function to list all teams in an organization that are visible to the authenticated user. * [`src/functions/private/Teams/Get-GitHubTeamByName.ps1`](diffhunk://#diff-d7c8b1210daa6952403080b9c5cbb962955174e2f60a04c50322ff83e46be201R1-R121): Added the `Get-GitHubTeamByName` function to retrieve a team using the team's slug. ### Function Modifications * [`src/functions/public/Teams/New-GitHubTeam.ps1`](diffhunk://#diff-3a654816801e01cb0ee48030cee55f9e0b3a5d5df9d3692eed3f49d17d9e8281R30-R42): Updated the `New-GitHubTeam` function to include output of the `GitHubTeam` type and modified parameters for `Visible`, `Notifications`, and `ParentTeamID`. [[1]](diffhunk://#diff-3a654816801e01cb0ee48030cee55f9e0b3a5d5df9d3692eed3f49d17d9e8281R30-R42) [[2]](diffhunk://#diff-3a654816801e01cb0ee48030cee55f9e0b3a5d5df9d3692eed3f49d17d9e8281L62-R71) [[3]](diffhunk://#diff-3a654816801e01cb0ee48030cee55f9e0b3a5d5df9d3692eed3f49d17d9e8281L80-R80) [[4]](diffhunk://#diff-3a654816801e01cb0ee48030cee55f9e0b3a5d5df9d3692eed3f49d17d9e8281R98-R101) [[5]](diffhunk://#diff-3a654816801e01cb0ee48030cee55f9e0b3a5d5df9d3692eed3f49d17d9e8281L107-R112) [[6]](diffhunk://#diff-3a654816801e01cb0ee48030cee55f9e0b3a5d5df9d3692eed3f49d17d9e8281L123-R144) * [`src/functions/public/Teams/Remove-GitHubTeam.ps1`](diffhunk://#diff-4f0443d26d0cbd49de232f07f7b81c2b2fbf29badb81ff98bba2a2a4bfeb7b72R19-L28): Changed the `Remove-GitHubTeam` function to use the `Slug` parameter instead of `Name` for identifying the team. [[1]](diffhunk://#diff-4f0443d26d0cbd49de232f07f7b81c2b2fbf29badb81ff98bba2a2a4bfeb7b72R19-L28) [[2]](diffhunk://#diff-4f0443d26d0cbd49de232f07f7b81c2b2fbf29badb81ff98bba2a2a4bfeb7b72L52-R61) ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [x] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent b0a7735 commit 66f8a99

File tree

11 files changed

+576
-57
lines changed

11 files changed

+576
-57
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class GitHubTeam {
2+
# The name of the team.
3+
[string] $Name
4+
5+
# The slug of the team.
6+
[string] $Slug
7+
8+
# The organization the team belongs to.
9+
[string] $Organization
10+
11+
# The combined slug of the team.
12+
[string] $CombinedSlug
13+
14+
# The ID of the team.
15+
[string] $NodeID
16+
17+
# The database ID of the team.
18+
[string] $DatabaseID
19+
20+
# The description of the team.
21+
[string] $Description
22+
23+
# The notification setting the team has chosen.
24+
# $true = notifications_enabled - team members receive notifications when the team is @mentioned.
25+
# $false = notifications_disabled - no one receives notifications.
26+
[bool] $Notifications = $true
27+
28+
# The privacy setting of the team.
29+
# $true = closed - visible to all members of this organization.
30+
# $false = secret - only visible to organization owners and members of this team.
31+
[bool] $Visible = $true
32+
33+
# The parent team of the team.
34+
[string] $ParentTeam
35+
36+
# The child teams of the team.
37+
[string[]] $ChildTeams
38+
39+
# The date and time the team was created.
40+
[datetime] $CreatedAt
41+
42+
# The date and time the team was last updated.
43+
[datetime] $UpdatedAt
44+
45+
# Simple parameterless constructor
46+
GitHubTeam() {}
47+
48+
# Creates a object from a hashtable of key-vaule pairs.
49+
GitHubTeam([hashtable]$Properties) {
50+
foreach ($Property in $Properties.Keys) {
51+
$this.$Property = $Properties.$Property
52+
}
53+
}
54+
55+
# Creates a object from a PSCustomObject.
56+
GitHubTeam([PSCustomObject]$Object) {
57+
$Object.PSObject.Properties | ForEach-Object {
58+
$this.($_.Name) = $_.Value
59+
}
60+
}
61+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
function Get-GitHubRESTTeam {
2+
<#
3+
.SYNOPSIS
4+
List teams from an org or get a team by name
5+
6+
.DESCRIPTION
7+
Lists all teams in an organization that are visible to the authenticated user or gets a team using the team's slug.
8+
To create the slug, GitHub replaces special characters in the name string, changes all words to lowercase,
9+
and replaces spaces with a - separator. For example, "My TEam Näme" would become my-team-name.
10+
11+
.EXAMPLE
12+
Get-GitHubRESTTeam -Organization 'GitHub'
13+
14+
Gets all teams in the `github` organization.
15+
16+
.EXAMPLE
17+
Get-GitHubRESTTeam -Organization 'github' -Name 'my-team-name'
18+
19+
Gets the team with the slug 'my-team-name' in the `github` organization.
20+
21+
.NOTES
22+
[List teams](https://docs.github.com/rest/teams/teams#list-teams)
23+
[Get team by name](https://docs.github.com/en/rest/teams/teams#get-a-team-by-name)
24+
#>
25+
[OutputType([pscustomobject])]
26+
[CmdletBinding(DefaultParameterSetName = '__AllParameterSets')]
27+
param(
28+
# The organization name. The name is not case sensitive.
29+
# If not provided, the organization from the context is used.
30+
[Parameter()]
31+
[Alias('Org')]
32+
[string] $Organization,
33+
34+
# The slug of the team name.
35+
[Parameter(
36+
Mandatory,
37+
ParameterSetName = 'GetByName'
38+
)]
39+
[Alias('Team', 'TeamName', 'slug', 'team_slug')]
40+
[string] $Name,
41+
42+
# The context to run the command in. Used to get the details for the API call.
43+
# Can be either a string or a GitHubContext object.
44+
[Parameter()]
45+
[object] $Context = (Get-GitHubContext)
46+
)
47+
48+
begin {
49+
$stackPath = Get-PSCallStackPath
50+
Write-Debug "[$stackPath] - Start"
51+
$Context = Resolve-GitHubContext -Context $Context
52+
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
53+
54+
if ([string]::IsNullOrEmpty($Organization)) {
55+
$Organization = $Context.Owner
56+
}
57+
Write-Debug "Organization: [$Organization]"
58+
}
59+
60+
process {
61+
try {
62+
$params = @{
63+
Organization = $Organization
64+
Context = $Context
65+
}
66+
switch ($PSCmdlet.ParameterSetName) {
67+
'GetByName' {
68+
Get-GitHubTeamByName @params -Name $Name
69+
}
70+
'__AllParameterSets' {
71+
Get-GitHubTeamListByOrg @params
72+
}
73+
}
74+
} catch {
75+
throw $_
76+
}
77+
}
78+
79+
end {
80+
Write-Debug "[$stackPath] - End"
81+
}
82+
}

src/functions/public/Teams/Get-GitHubTeamByName.ps1 renamed to src/functions/private/Teams/Get-GitHubRESTTeamByName.ps1

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@
99
1010
.EXAMPLE
1111
Get-GitHubTeamByName -Organization 'github' -Name 'my-team-name'
12-
13-
.NOTES
14-
[Get team by name](https://docs.github.com/en/rest/teams/teams#get-a-team-by-name)
1512
#>
1613
[OutputType([void])]
1714
[CmdletBinding()]
1815
param(
19-
# The organization name. The name is not case sensitive.
20-
[Parameter(Mandatory)]
21-
[Alias('Org')]
22-
[string] $Organization,
23-
2416
# The slug of the team name.
2517
[Parameter(Mandatory)]
2618
[Alias('Team', 'TeamName', 'slug', 'team_slug')]
2719
[string] $Name,
2820

21+
# The organization name. The name is not case sensitive.
22+
[Parameter(Mandatory)]
23+
[Alias('Org')]
24+
[string] $Organization,
25+
2926
# The context to run the command in. Used to get the details for the API call.
3027
# Can be either a string or a GitHubContext object.
3128
[Parameter()]
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
function Get-GitHubTeamBySlug {
2+
<#
3+
.SYNOPSIS
4+
Get a team by name
5+
6+
.DESCRIPTION
7+
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,
8+
and replaces spaces with a - separator. For example, "My TEam Näme" would become my-team-name.
9+
10+
.EXAMPLE
11+
Get-GitHubTeamBySlug -Organization 'github' -Slug 'my-team-name'
12+
#>
13+
[OutputType([GitHubTeam])]
14+
[CmdletBinding()]
15+
param(
16+
# The slug of the team name.
17+
[Parameter(Mandatory)]
18+
[Alias('team_slug')]
19+
[string] $Slug,
20+
21+
# The organization name. The name is not case sensitive.
22+
# If not provided, the owner from the context will be used.
23+
[Parameter()]
24+
[Alias('Org')]
25+
[string] $Organization,
26+
27+
# The context to run the command in. Used to get the details for the API call.
28+
# Can be either a string or a GitHubContext object.
29+
[Parameter()]
30+
[object] $Context = (Get-GitHubContext)
31+
)
32+
33+
begin {
34+
$stackPath = Get-PSCallStackPath
35+
Write-Debug "[$stackPath] - Start"
36+
$Context = Resolve-GitHubContext -Context $Context
37+
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
38+
39+
if ([string]::IsNullOrEmpty($Organization)) {
40+
$Organization = $Context.Owner
41+
}
42+
Write-Debug "Organization: [$Organization]"
43+
}
44+
45+
process {
46+
try {
47+
$query = @"
48+
query(`$org: String!, `$teamSlug: String!) {
49+
organization(login: `$org) {
50+
team(slug: `$teamSlug) {
51+
id
52+
name
53+
slug
54+
combinedSlug
55+
databaseId
56+
description
57+
notificationSetting
58+
privacy
59+
parentTeam {
60+
name
61+
slug
62+
}
63+
organization {
64+
login
65+
}
66+
childTeams(first: 100) {
67+
nodes {
68+
name
69+
}
70+
}
71+
createdAt
72+
updatedAt
73+
}
74+
}
75+
}
76+
}
77+
"@
78+
79+
# Variables hash that will be sent with the query
80+
$variables = @{
81+
org = $Organization
82+
teamSlug = $Slug
83+
}
84+
85+
# Send the request to the GitHub GraphQL API
86+
$response = Invoke-GitHubGraphQLQuery -Query $query -Variables $variables
87+
88+
# Extract team data
89+
$team = $response.data.organization.team
90+
91+
# Output the team object
92+
if (-not $team) {
93+
return
94+
}
95+
96+
[GitHubTeam](
97+
@{
98+
Name = $team.name
99+
Slug = $team.slug
100+
NodeID = $team.id
101+
CombinedSlug = $team.CombinedSlug
102+
DatabaseID = $team.DatabaseId
103+
Description = $team.description
104+
Notifications = $team.notificationSetting -eq 'NOTIFICATIONS_ENABLED' ? $true : $false
105+
Visible = $team.privacy -eq 'VISIBLE' ? $true : $false
106+
ParentTeam = $team.parentTeam.slug
107+
Organization = $team.organization.login
108+
ChildTeams = $team.childTeams.nodes.name
109+
CreatedAt = $team.createdAt
110+
UpdatedAt = $team.updatedAt
111+
}
112+
)
113+
} catch {
114+
throw $_
115+
}
116+
}
117+
118+
end {
119+
Write-Debug "[$stackPath] - End"
120+
}
121+
}

0 commit comments

Comments
 (0)