Skip to content

Commit c60bb29

Browse files
committed
Add Test-GitHubOrganizationMember
Also updated Get-GitHubTeamMember to optionally work directly with a TeamId.
1 parent 592167d commit c60bb29

File tree

3 files changed

+107
-10
lines changed

3 files changed

+107
-10
lines changed

GitHubOrganizations.ps1

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,81 @@ function Get-GitHubOrganizationMember
6161

6262
return Invoke-GHRestMethodMultipleResult @params
6363
}
64+
65+
function Test-GitHubOrganizationMember
66+
{
67+
<#
68+
.SYNOPSIS
69+
Check to see if a user is a member of an organization.
70+
71+
.DESCRIPTION
72+
Check to see if a user is a member of an organization.
73+
74+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
75+
76+
.PARAMETER OrganizationName
77+
The name of the organization.
78+
79+
.PARAMETER UserName
80+
The name of the user being inquired about.
81+
82+
.PARAMETER AccessToken
83+
If provided, this will be used as the AccessToken for authentication with the
84+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
85+
86+
.PARAMETER NoStatus
87+
If this switch is specified, long-running commands will run on the main thread
88+
with no commandline status update. When not specified, those commands run in
89+
the background, enabling the command prompt to provide status information.
90+
If not supplied here, the DefaultNoStatus configuration property value will be used.
91+
92+
.OUTPUTS
93+
[Bool]
94+
95+
.EXAMPLE
96+
Test-GitHubOrganizationMember -OrganizationName PowerShell -UserName Octocat
97+
#>
98+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
99+
[CmdletBinding(SupportsShouldProcess)]
100+
param
101+
(
102+
[Parameter(Mandatory)]
103+
[ValidateNotNullOrEmpty()]
104+
[String] $OrganizationName,
105+
106+
[Parameter(Mandatory)]
107+
[ValidateNotNullOrEmpty()]
108+
[String] $UserName,
109+
110+
[string] $AccessToken,
111+
112+
[switch] $NoStatus
113+
)
114+
115+
Write-InvocationLog
116+
117+
$telemetryProperties = @{
118+
'OrganizationName' = (Get-PiiSafeString -PlainText $OrganizationName)
119+
}
120+
121+
$params = @{
122+
'UriFragment' = "orgs/$OrganizationName/members/$UserName"
123+
'Description' = "Checking if $UserName is a member of $OrganizationName"
124+
'Method' = 'Get'
125+
'ExtendedResult' = $true
126+
'AccessToken' = $AccessToken
127+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
128+
'TelemetryProperties' = $telemetryProperties
129+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
130+
}
131+
132+
try
133+
{
134+
$result = Invoke-GHRestMethod @params
135+
return ($result.statusCode -eq 204)
136+
}
137+
catch
138+
{
139+
return $false
140+
}
141+
}

GitHubTeams.ps1

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ function Get-GitHubTeamMember
144144
.PARAMETER TeamName
145145
The name of the team in the organization
146146
147+
.PARAMETER TeamId
148+
The ID of the team in the organization
149+
147150
.PARAMETER AccessToken
148151
If provided, this will be used as the AccessToken for authentication with the
149152
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
@@ -161,17 +164,26 @@ function Get-GitHubTeamMember
161164
$members = Get-GitHubTeamMember -Organization PowerShell -TeamName Everybody
162165
#>
163166
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
164-
[CmdletBinding(SupportsShouldProcess)]
167+
[CmdletBinding(
168+
SupportsShouldProcess,
169+
DefaultParametersetName='ID')]
165170
param
166171
(
167172
[Parameter(Mandatory)]
168173
[ValidateNotNullOrEmpty()]
169174
[String] $OrganizationName,
170175

171-
[Parameter(Mandatory)]
176+
[Parameter(
177+
Mandatory,
178+
ParameterSetName='Name')]
172179
[ValidateNotNullOrEmpty()]
173180
[String] $TeamName,
174181

182+
[Parameter(
183+
Mandatory,
184+
ParameterSetName='ID')]
185+
[int] $TeamId,
186+
175187
[string] $AccessToken,
176188

177189
[switch] $NoStatus
@@ -181,23 +193,29 @@ function Get-GitHubTeamMember
181193

182194
$NoStatus = Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus
183195

184-
$teams = Get-GitHubTeam -OrganizationName $OrganizationName -AccessToken $AccessToken -NoStatus:$NoStatus
185-
$team = $teams | Where-Object {$_.name -eq $TeamName}
186-
if ($null -eq $team)
196+
if ($PSCmdlet.ParameterSetName -eq 'Name')
187197
{
188-
$message = "Unable to find the team [$TeamName] within the organization [$OrganizationName]."
189-
Write-Log -Message $message -Level Error
190-
throw $message
198+
$teams = Get-GitHubTeam -OrganizationName $OrganizationName -AccessToken $AccessToken -NoStatus:$NoStatus
199+
$team = $teams | Where-Object {$_.name -eq $TeamName}
200+
if ($null -eq $team)
201+
{
202+
$message = "Unable to find the team [$TeamName] within the organization [$OrganizationName]."
203+
Write-Log -Message $message -Level Error
204+
throw $message
205+
}
206+
207+
$TeamId = $team.id
191208
}
192209

193210
$telemetryProperties = @{
194211
'OrganizationName' = (Get-PiiSafeString -PlainText $OrganizationName)
195212
'TeamName' = (Get-PiiSafeString -PlainText $TeamName)
213+
'TeamId' = (Get-PiiSafeString -PlainText $TeamId)
196214
}
197215

198216
$params = @{
199-
'UriFragment' = "teams/$($team.id)/members"
200-
'Description' = "Getting members of the team $TeamName $($team.id)"
217+
'UriFragment' = "teams/$TeamId/members"
218+
'Description' = "Getting members of team $TeamId"
201219
'AccessToken' = $AccessToken
202220
'TelemetryEventName' = $MyInvocation.MyCommand.Name
203221
'TelemetryProperties' = $telemetryProperties

PowerShellForGitHub.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
'Split-GitHubUri',
112112
'Test-GitHubAssignee',
113113
'Test-GitHubAuthenticationConfigured',
114+
'Test-GitHubOrganizationMember',
114115
'Unlock-GitHubIssue',
115116
'Update-GitHubCurrentUser',
116117
'Update-GitHubIssue',

0 commit comments

Comments
 (0)