Skip to content

Add Test-GitHubOrganizationMember #90

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

Merged
merged 1 commit into from
Mar 15, 2019
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
78 changes: 78 additions & 0 deletions GitHubOrganizations.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,81 @@ function Get-GitHubOrganizationMember

return Invoke-GHRestMethodMultipleResult @params
}

function Test-GitHubOrganizationMember
{
<#
.SYNOPSIS
Check to see if a user is a member of an organization.

.DESCRIPTION
Check to see if a user is a member of an organization.

The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub

.PARAMETER OrganizationName
The name of the organization.

.PARAMETER UserName
The name of the user being inquired about.

.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.

.PARAMETER NoStatus
If this switch is specified, long-running commands will run on the main thread
with no commandline status update. When not specified, those commands run in
the background, enabling the command prompt to provide status information.
If not supplied here, the DefaultNoStatus configuration property value will be used.

.OUTPUTS
[Bool]

.EXAMPLE
Test-GitHubOrganizationMember -OrganizationName PowerShell -UserName Octocat
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String] $OrganizationName,

[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String] $UserName,

[string] $AccessToken,

[switch] $NoStatus
)

Write-InvocationLog

$telemetryProperties = @{
'OrganizationName' = (Get-PiiSafeString -PlainText $OrganizationName)
}

$params = @{
'UriFragment' = "orgs/$OrganizationName/members/$UserName"
'Description' = "Checking if $UserName is a member of $OrganizationName"
'Method' = 'Get'
'ExtendedResult' = $true
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

try
{
$result = Invoke-GHRestMethod @params
return ($result.statusCode -eq 204)
}
catch
{
return $false
}
}
38 changes: 28 additions & 10 deletions GitHubTeams.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ function Get-GitHubTeamMember
.PARAMETER TeamName
The name of the team in the organization

.PARAMETER TeamId
The ID of the team in the organization

.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
Expand All @@ -161,17 +164,26 @@ function Get-GitHubTeamMember
$members = Get-GitHubTeamMember -Organization PowerShell -TeamName Everybody
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
[CmdletBinding(SupportsShouldProcess)]
[CmdletBinding(
SupportsShouldProcess,
DefaultParametersetName='ID')]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String] $OrganizationName,

[Parameter(Mandatory)]
[Parameter(
Mandatory,
ParameterSetName='Name')]
[ValidateNotNullOrEmpty()]
[String] $TeamName,

[Parameter(
Mandatory,
ParameterSetName='ID')]
[int] $TeamId,

[string] $AccessToken,

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

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

$teams = Get-GitHubTeam -OrganizationName $OrganizationName -AccessToken $AccessToken -NoStatus:$NoStatus
$team = $teams | Where-Object {$_.name -eq $TeamName}
if ($null -eq $team)
if ($PSCmdlet.ParameterSetName -eq 'Name')
{
$message = "Unable to find the team [$TeamName] within the organization [$OrganizationName]."
Write-Log -Message $message -Level Error
throw $message
$teams = Get-GitHubTeam -OrganizationName $OrganizationName -AccessToken $AccessToken -NoStatus:$NoStatus
$team = $teams | Where-Object {$_.name -eq $TeamName}
if ($null -eq $team)
{
$message = "Unable to find the team [$TeamName] within the organization [$OrganizationName]."
Write-Log -Message $message -Level Error
throw $message
}

$TeamId = $team.id
}

$telemetryProperties = @{
'OrganizationName' = (Get-PiiSafeString -PlainText $OrganizationName)
'TeamName' = (Get-PiiSafeString -PlainText $TeamName)
'TeamId' = (Get-PiiSafeString -PlainText $TeamId)
}

$params = @{
'UriFragment' = "teams/$($team.id)/members"
'Description' = "Getting members of the team $TeamName $($team.id)"
'UriFragment' = "teams/$TeamId/members"
'Description' = "Getting members of team $TeamId"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
Expand Down
1 change: 1 addition & 0 deletions PowerShellForGitHub.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
'Split-GitHubUri',
'Test-GitHubAssignee',
'Test-GitHubAuthenticationConfigured',
'Test-GitHubOrganizationMember',
'Unlock-GitHubIssue',
'Update-GitHubCurrentUser',
'Update-GitHubIssue',
Expand Down