PowerShellForGitHub/GitHubOrganizations.ps1

190 строки
5.0 KiB
PowerShell

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
@{
GitHubOrganizationTypeName = 'GitHub.Organization'
}.GetEnumerator() | ForEach-Object {
Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value
}
filter Get-GitHubOrganizationMember
{
<#
.SYNOPSIS
Retrieve list of members within an organization.
.DESCRIPTION
Retrieve list of members within an organization.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER OrganizationName
The name of 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.
.INPUTS
[String]
.OUTPUTS
GitHub.User
List of members within the organization.
.EXAMPLE
Get-GitHubOrganizationMember -OrganizationName PowerShell
#>
[CmdletBinding()]
[OutputType({$script:GitHubUserTypeName})]
param
(
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[String] $OrganizationName,
[string] $AccessToken
)
Write-InvocationLog
$telemetryProperties = @{
'OrganizationName' = (Get-PiiSafeString -PlainText $OrganizationName)
}
$params = @{
'UriFragment' = "orgs/$OrganizationName/members"
'Description' = "Getting members for $OrganizationName"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
return (Invoke-GHRestMethodMultipleResult @params | Add-GitHubUserAdditionalProperties)
}
filter 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.
.INPUTS
[String]
.OUTPUTS
[Bool]
.EXAMPLE
Test-GitHubOrganizationMember -OrganizationName PowerShell -UserName Octocat
#>
[CmdletBinding()]
[OutputType([bool])]
param
(
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[String] $OrganizationName,
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[String] $UserName,
[string] $AccessToken
)
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
}
try
{
$result = Invoke-GHRestMethod @params
return ($result.statusCode -eq 204)
}
catch
{
return $false
}
}
filter Add-GitHubOrganizationAdditionalProperties
{
<#
.SYNOPSIS
Adds type name and additional properties to ease pipelining to GitHub Organization objects.
.PARAMETER InputObject
The GitHub object to add additional properties to.
.PARAMETER TypeName
The type that should be assigned to the object.
.INPUTS
[PSCustomObject]
.OUTPUTS
GitHub.Organization
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification="Internal helper that is definitely adding more than one property.")]
param(
[Parameter(
Mandatory,
ValueFromPipeline)]
[AllowNull()]
[AllowEmptyCollection()]
[PSCustomObject[]] $InputObject,
[ValidateNotNullOrEmpty()]
[string] $TypeName = $script:GitHubOrganizationTypeName
)
foreach ($item in $InputObject)
{
$item.PSObject.TypeNames.Insert(0, $TypeName)
if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport))
{
Add-Member -InputObject $item -Name 'OrganizationName' -Value $item.login -MemberType NoteProperty -Force
Add-Member -InputObject $item -Name 'OrganizationId' -Value $item.id -MemberType NoteProperty -Force
}
Write-Output $item
}
}