From d13f3f2029846d7595dcb885e58f8c59806522d7 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 3 Oct 2023 22:21:39 +0200 Subject: [PATCH 1/8] Added ability to get socials for users --- .../private/Users/Get-GitHubMyUserSocials.ps1 | 35 ++++++++++++++++++ .../private/Users/Get-GitHubUserByName.ps1 | 1 - .../Users/Get-GitHubUserSocialsByName.ps1 | 37 +++++++++++++++++++ src/GitHub/public/Users/Get-GitHubUser.ps1 | 10 ++++- 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/GitHub/private/Users/Get-GitHubMyUserSocials.ps1 create mode 100644 src/GitHub/private/Users/Get-GitHubUserSocialsByName.ps1 diff --git a/src/GitHub/private/Users/Get-GitHubMyUserSocials.ps1 b/src/GitHub/private/Users/Get-GitHubMyUserSocials.ps1 new file mode 100644 index 000000000..fde1e83c2 --- /dev/null +++ b/src/GitHub/private/Users/Get-GitHubMyUserSocials.ps1 @@ -0,0 +1,35 @@ +filter Get-GitHubMyUserSocials { + <# + .SYNOPSIS + List social accounts for the authenticated user + + .DESCRIPTION + Lists all of your social accounts. + + .EXAMPLE + Get-GitHubMyUserSocials + + Lists all of your social accounts. + + .NOTES + https://docs.github.com/rest/users/social-accounts#list-social-accounts-for-the-authenticated-user + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The number of results per page (max 100). + [Parameter()] + [int] $PerPage = 30 + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + + $inputObject = @{ + APIEndpoint = '/user/social_accounts' + Method = 'GET' + Body = $body + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/src/GitHub/private/Users/Get-GitHubUserByName.ps1 b/src/GitHub/private/Users/Get-GitHubUserByName.ps1 index 6d78f9716..c4f008817 100644 --- a/src/GitHub/private/Users/Get-GitHubUserByName.ps1 +++ b/src/GitHub/private/Users/Get-GitHubUserByName.ps1 @@ -30,7 +30,6 @@ [string] $Username ) - $inputObject = @{ APIEndpoint = "/users/$Username" Method = 'GET' diff --git a/src/GitHub/private/Users/Get-GitHubUserSocialsByName.ps1 b/src/GitHub/private/Users/Get-GitHubUserSocialsByName.ps1 new file mode 100644 index 000000000..aea79703d --- /dev/null +++ b/src/GitHub/private/Users/Get-GitHubUserSocialsByName.ps1 @@ -0,0 +1,37 @@ +filter Get-GitHubUserSocialsByName { + <# + .SYNOPSIS + List social accounts for a user + + .DESCRIPTION + Lists social media accounts for a user. This endpoint is accessible by anyone. + + .EXAMPLE + Get-GitHubUserSocialsByName -Username 'octocat' + + Lists social media accounts for the user 'octocat'. + + .NOTES + https://docs.github.com/rest/users/social-accounts#list-social-accounts-for-a-user + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('login')] + [string] $Username + ) + + $inputObject = @{ + APIEndpoint = "/users/$Username/social_accounts" + Method = 'GET' + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/src/GitHub/public/Users/Get-GitHubUser.ps1 b/src/GitHub/public/Users/Get-GitHubUser.ps1 index 8efa351fa..d505df4c1 100644 --- a/src/GitHub/public/Users/Get-GitHubUser.ps1 +++ b/src/GitHub/public/Users/Get-GitHubUser.ps1 @@ -55,10 +55,16 @@ switch ($PSCmdlet.ParameterSetName) { '__DefaultSet' { - Get-GitHubMyUser + $user = Get-GitHubMyUser + $social_accounts = Get-GitHubMyUserSocials + $user | Add-Member -MemberType NoteProperty -Name 'social_accounts' -Value $social_accounts -Force + $user } 'NamedUser' { - Get-GitHubUserByName -Username $Username + $user = Get-GitHubUserByName -Username $Username + $social_accounts = Get-GitHubUserSocialsByName -Username $Username + $user | Add-Member -MemberType NoteProperty -Name 'social_accounts' -Value $social_accounts -Force + $user } 'AllUsers' { Get-GitHubAllUsers -Since $Since -PerPage $PerPage From 4bf519c5c99caab54a3c0e513e199aace1ef99ff Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 3 Oct 2023 22:41:47 +0200 Subject: [PATCH 2/8] Add and remove socials for the authenticated user --- .../public/Users/Remove-GitHubUserSocials.ps1 | 37 +++++++++++++++++++ .../public/Users/Set-GitHubUserSocials.ps1 | 36 ++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/GitHub/public/Users/Remove-GitHubUserSocials.ps1 create mode 100644 src/GitHub/public/Users/Set-GitHubUserSocials.ps1 diff --git a/src/GitHub/public/Users/Remove-GitHubUserSocials.ps1 b/src/GitHub/public/Users/Remove-GitHubUserSocials.ps1 new file mode 100644 index 000000000..c7ca4de98 --- /dev/null +++ b/src/GitHub/public/Users/Remove-GitHubUserSocials.ps1 @@ -0,0 +1,37 @@ +filter Remove-GitHubUserSocials { + <# + .SYNOPSIS + Delete social accounts for the authenticated user + + .DESCRIPTION + Deletes one or more social accounts from the authenticated user's profile. This endpoint is accessible with the `user` scope. + + .PARAMETER AccountUrls + Parameter description + + .EXAMPLE + Remove-GitHubUserSocials -AccountUrls 'https://twitter.com/MyTwitterAccount' + + .NOTES + https://docs.github.com/rest/users/social-accounts#delete-social-accounts-for-the-authenticated-user + #> + [OutputType([void])] + [CmdletBinding()] + param ( + # Full URLs for the social media profiles to add. + [Parameter(Mandatory)] + [Alias('account_urls')] + [string[]] $AccountUrls + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + + $inputObject = @{ + APIEndpoint = '/user/social_accounts' + Body = $body + Method = 'DELETE' + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/src/GitHub/public/Users/Set-GitHubUserSocials.ps1 b/src/GitHub/public/Users/Set-GitHubUserSocials.ps1 new file mode 100644 index 000000000..c2aa649ea --- /dev/null +++ b/src/GitHub/public/Users/Set-GitHubUserSocials.ps1 @@ -0,0 +1,36 @@ +filter Set-GitHubUserSocials { + <# + .SYNOPSIS + Add social accounts for the authenticated user + + .DESCRIPTION + Add one or more social accounts to the authenticated user's profile. This endpoint is accessible with the `user` scope. + + .EXAMPLE + Set-GitHubUserSocials -AccountUrls 'https://twitter.com/MyTwitterAccount', 'https://www.linkedin.com/company/MyCompany' + + Adds the Twitter and LinkedIn accounts to the authenticated user's profile. + + .NOTES + https://docs.github.com/rest/users/social-accounts#add-social-accounts-for-the-authenticated-user + #> + [OutputType([void])] + [CmdletBinding()] + param ( + # Full URLs for the social media profiles to add. + [Parameter(Mandatory)] + [Alias('account_urls')] + [string[]] $AccountUrls + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + + $inputObject = @{ + APIEndpoint = '/user/social_accounts' + Body = $body + Method = 'POST' + } + + (Invoke-GitHubAPI @inputObject).Response + +} From db466b01cdda8339ad334ed855ddaa1a13c2fa09 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 3 Oct 2023 22:50:15 +0200 Subject: [PATCH 3/8] test commands --- .../{private => public}/Users/Get-GitHubMyUserSocials.ps1 | 0 .../{private => public}/Users/Get-GitHubUserSocialsByName.ps1 | 0 tools/utilities/GitHubAPI.ps1 | 4 ++-- tools/utilities/Local-Testing.ps1 | 3 +++ 4 files changed, 5 insertions(+), 2 deletions(-) rename src/GitHub/{private => public}/Users/Get-GitHubMyUserSocials.ps1 (100%) rename src/GitHub/{private => public}/Users/Get-GitHubUserSocialsByName.ps1 (100%) diff --git a/src/GitHub/private/Users/Get-GitHubMyUserSocials.ps1 b/src/GitHub/public/Users/Get-GitHubMyUserSocials.ps1 similarity index 100% rename from src/GitHub/private/Users/Get-GitHubMyUserSocials.ps1 rename to src/GitHub/public/Users/Get-GitHubMyUserSocials.ps1 diff --git a/src/GitHub/private/Users/Get-GitHubUserSocialsByName.ps1 b/src/GitHub/public/Users/Get-GitHubUserSocialsByName.ps1 similarity index 100% rename from src/GitHub/private/Users/Get-GitHubUserSocialsByName.ps1 rename to src/GitHub/public/Users/Get-GitHubUserSocialsByName.ps1 diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index 49bfe0808..cd8396c37 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -21,8 +21,8 @@ $response.paths.psobject.Properties | Select-Object ` @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table -$path = '/user/blocks/{username}' -$method = 'get' +$path = '/user/social_accounts' +$method = 'delete' $response.paths.$path.$method $response.paths.$path.$method.tags | clip # -> Namespace/foldername $response.paths.$path.$method.operationId | clip # -> FunctionName diff --git a/tools/utilities/Local-Testing.ps1 b/tools/utilities/Local-Testing.ps1 index cc9a1aa28..52b614011 100644 --- a/tools/utilities/Local-Testing.ps1 +++ b/tools/utilities/Local-Testing.ps1 @@ -12,6 +12,7 @@ Get-Module -Name GitHub -ListAvailable $VerbosePreference = 'Continue' Install-Module -Name GitHub -Force -Verbose -AllowPrerelease +Get-Module -Name GitHub -ListAvailable # $env:PSModulePath += ';C:\Repos\GitHub\PSModule\Modules\GitHub\outputs' # Import-Module -Name 'C:\Repos\GitHub\PSModule\Modules\GitHub\src\GitHub\GitHub.psm1' -Verbose -Force @@ -50,6 +51,8 @@ Set-GitHubOrganization -OrganizationName 'PSModule' -Blog '' Set-GitHubOrganization -OrganizationName 'PSModule' -Company 'PSModule123' -DefaultRepositoryPermission admin Set-GitHubOrganization -OrganizationName 'PSModule' -Company 'PSModule' -DefaultRepositoryPermission read + +Get-GitHubUser Set-GitHubUser -Name 'Marius Storhaug' -Company '@DNBBank' -Email 'marstor@hotmail.com' -Blog 'https://www.github.com/MariusStorhaug' -TwitterUsername MariusStorhaug -Location 'Norway' -Hireable $false -Bio 'DevOps Engineer at DNB Bank. I ❤️ PowerShell and automation.' Set-GitHubUser -Name 'MariusStorhaug' -Company ' ' Set-GitHubUser -Name 'MariusStorhaug' -Hireable $true From a775fd3e61bc0ffe70cb52529c44c392084bbf58 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 4 Oct 2023 13:25:25 +0200 Subject: [PATCH 4/8] Moved functions to private + rename assert -> test --- .../Users/Social-Accounts}/Get-GitHubMyUserSocials.ps1 | 0 .../Social-Accounts}/Get-GitHubUserSocialsByName.ps1 | 0 ...kedUser.ps1 => Test-GitHubOrganizationBlockedUser.ps1} | 7 ++++--- ...t-GitHubBlockedUser.ps1 => Test-GitHubBlockedUser.ps1} | 8 ++++++-- .../Add-GitHubUserSocials.ps1} | 4 ++-- .../{ => Social-Accounts}/Remove-GitHubUserSocials.ps1 | 0 6 files changed, 12 insertions(+), 7 deletions(-) rename src/GitHub/{public/Users => private/Users/Social-Accounts}/Get-GitHubMyUserSocials.ps1 (100%) rename src/GitHub/{public/Users => private/Users/Social-Accounts}/Get-GitHubUserSocialsByName.ps1 (100%) rename src/GitHub/public/Organization/Blocking/{Assert-GitHubOrganizationBlockedUser.ps1 => Test-GitHubOrganizationBlockedUser.ps1} (83%) rename src/GitHub/public/Users/Blocking/{Assert-GitHubBlockedUser.ps1 => Test-GitHubBlockedUser.ps1} (81%) rename src/GitHub/public/Users/{Set-GitHubUserSocials.ps1 => Social-Accounts/Add-GitHubUserSocials.ps1} (91%) rename src/GitHub/public/Users/{ => Social-Accounts}/Remove-GitHubUserSocials.ps1 (100%) diff --git a/src/GitHub/public/Users/Get-GitHubMyUserSocials.ps1 b/src/GitHub/private/Users/Social-Accounts/Get-GitHubMyUserSocials.ps1 similarity index 100% rename from src/GitHub/public/Users/Get-GitHubMyUserSocials.ps1 rename to src/GitHub/private/Users/Social-Accounts/Get-GitHubMyUserSocials.ps1 diff --git a/src/GitHub/public/Users/Get-GitHubUserSocialsByName.ps1 b/src/GitHub/private/Users/Social-Accounts/Get-GitHubUserSocialsByName.ps1 similarity index 100% rename from src/GitHub/public/Users/Get-GitHubUserSocialsByName.ps1 rename to src/GitHub/private/Users/Social-Accounts/Get-GitHubUserSocialsByName.ps1 diff --git a/src/GitHub/public/Organization/Blocking/Assert-GitHubOrganizationBlockedUser.ps1 b/src/GitHub/public/Organization/Blocking/Test-GitHubOrganizationBlockedUser.ps1 similarity index 83% rename from src/GitHub/public/Organization/Blocking/Assert-GitHubOrganizationBlockedUser.ps1 rename to src/GitHub/public/Organization/Blocking/Test-GitHubOrganizationBlockedUser.ps1 index 9e2a73803..880fe5512 100644 --- a/src/GitHub/public/Organization/Blocking/Assert-GitHubOrganizationBlockedUser.ps1 +++ b/src/GitHub/public/Organization/Blocking/Test-GitHubOrganizationBlockedUser.ps1 @@ -1,4 +1,4 @@ -filter Assert-GitHubOrganizationBlockedUser { +filter Test-GitHubOrganizationBlockedUser { <# .SYNOPSIS Check if a user is blocked by an organization @@ -7,9 +7,10 @@ Returns a 204 if the given user is blocked by the given organization. Returns a 404 if the organization is not blocking the user, or if the user account has been identified as spam by GitHub. .EXAMPLE - Get-GitHubOrganizationBlockedUser -OrganizationName 'github' + Test-GitHubOrganizationBlockedUser -OrganizationName 'PSModule' -Username 'octocat' - Lists all users blocked by the organization `github`. + Checks if the user `octocat` is blocked by the organization `PSModule`. + Returns true if the user is blocked, false if not. .NOTES https://docs.github.com/rest/orgs/blocking#check-if-a-user-is-blocked-by-an-organization diff --git a/src/GitHub/public/Users/Blocking/Assert-GitHubBlockedUser.ps1 b/src/GitHub/public/Users/Blocking/Test-GitHubBlockedUser.ps1 similarity index 81% rename from src/GitHub/public/Users/Blocking/Assert-GitHubBlockedUser.ps1 rename to src/GitHub/public/Users/Blocking/Test-GitHubBlockedUser.ps1 index 9bf24e819..ecf5b70a5 100644 --- a/src/GitHub/public/Users/Blocking/Assert-GitHubBlockedUser.ps1 +++ b/src/GitHub/public/Users/Blocking/Test-GitHubBlockedUser.ps1 @@ -1,4 +1,4 @@ -filter Assert-GitHubBlockedUser { +filter Test-GitHubBlockedUser { <# .SYNOPSIS Check if a user is blocked by the authenticated user @@ -7,11 +7,15 @@ Returns a 204 if the given user is blocked by the authenticated user. Returns a 404 if the given user is not blocked by the authenticated user, or if the given user account has been identified as spam by GitHub. .EXAMPLE + Test-GitHubBlockedUser -Username 'octocat' + + Checks if the user `octocat` is blocked by the authenticated user. + Returns true if the user is blocked, false if not. .NOTES https://docs.github.com/rest/users/blocking#check-if-a-user-is-blocked-by-the-authenticated-user #> - [OutputType([pscustomobject])] + [OutputType([bool])] [CmdletBinding()] param ( # The number of results per page (max 100). diff --git a/src/GitHub/public/Users/Set-GitHubUserSocials.ps1 b/src/GitHub/public/Users/Social-Accounts/Add-GitHubUserSocials.ps1 similarity index 91% rename from src/GitHub/public/Users/Set-GitHubUserSocials.ps1 rename to src/GitHub/public/Users/Social-Accounts/Add-GitHubUserSocials.ps1 index c2aa649ea..89cfdcddf 100644 --- a/src/GitHub/public/Users/Set-GitHubUserSocials.ps1 +++ b/src/GitHub/public/Users/Social-Accounts/Add-GitHubUserSocials.ps1 @@ -1,4 +1,4 @@ -filter Set-GitHubUserSocials { +filter Add-GitHubUserSocials { <# .SYNOPSIS Add social accounts for the authenticated user @@ -7,7 +7,7 @@ Add one or more social accounts to the authenticated user's profile. This endpoint is accessible with the `user` scope. .EXAMPLE - Set-GitHubUserSocials -AccountUrls 'https://twitter.com/MyTwitterAccount', 'https://www.linkedin.com/company/MyCompany' + Add-GitHubUserSocials -AccountUrls 'https://twitter.com/MyTwitterAccount', 'https://www.linkedin.com/company/MyCompany' Adds the Twitter and LinkedIn accounts to the authenticated user's profile. diff --git a/src/GitHub/public/Users/Remove-GitHubUserSocials.ps1 b/src/GitHub/public/Users/Social-Accounts/Remove-GitHubUserSocials.ps1 similarity index 100% rename from src/GitHub/public/Users/Remove-GitHubUserSocials.ps1 rename to src/GitHub/public/Users/Social-Accounts/Remove-GitHubUserSocials.ps1 From d9f11d4caa34a67a77769c731d55b50f34c3c49c Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 4 Oct 2023 14:37:07 +0200 Subject: [PATCH 5/8] Fix Test-GitHubBlockedUser adding param for username --- .../public/Users/Blocking/Test-GitHubBlockedUser.ps1 | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/GitHub/public/Users/Blocking/Test-GitHubBlockedUser.ps1 b/src/GitHub/public/Users/Blocking/Test-GitHubBlockedUser.ps1 index ecf5b70a5..0b1901971 100644 --- a/src/GitHub/public/Users/Blocking/Test-GitHubBlockedUser.ps1 +++ b/src/GitHub/public/Users/Blocking/Test-GitHubBlockedUser.ps1 @@ -18,6 +18,15 @@ [OutputType([bool])] [CmdletBinding()] param ( + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('login')] + [string] $Username, + # The number of results per page (max 100). [Parameter()] [int] $PerPage = 30 @@ -26,7 +35,7 @@ $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case $inputObject = @{ - APIEndpoint = "/user/blocks" + APIEndpoint = "/user/blocks/$Username" Method = 'GET' Body = $body } From b0f5695509b9a384051277480035d0ee073bc5dd Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 4 Oct 2023 14:59:14 +0200 Subject: [PATCH 6/8] Change output to bool --- .../Test-GitHubOrganizationBlockedUser.ps1 | 2 +- tools/utilities/Local-Testing.ps1 | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/GitHub/public/Organization/Blocking/Test-GitHubOrganizationBlockedUser.ps1 b/src/GitHub/public/Organization/Blocking/Test-GitHubOrganizationBlockedUser.ps1 index 880fe5512..ba15aaa99 100644 --- a/src/GitHub/public/Organization/Blocking/Test-GitHubOrganizationBlockedUser.ps1 +++ b/src/GitHub/public/Organization/Blocking/Test-GitHubOrganizationBlockedUser.ps1 @@ -15,7 +15,7 @@ .NOTES https://docs.github.com/rest/orgs/blocking#check-if-a-user-is-blocked-by-an-organization #> - [OutputType([pscustomobject])] + [OutputType([bool])] [Alias('Is-GitHubOrganizationBlockedUser')] [Alias('Check-GitHubOrganizationBlockedUser')] [CmdletBinding()] diff --git a/tools/utilities/Local-Testing.ps1 b/tools/utilities/Local-Testing.ps1 index 52b614011..9f4d3fb27 100644 --- a/tools/utilities/Local-Testing.ps1 +++ b/tools/utilities/Local-Testing.ps1 @@ -39,7 +39,7 @@ $date = [datetime]::ParseExact($str, $format, $null) $date -Get-GitHubOrganization +Get-GitHubOrganization | Select-Object Name, login, id Get-GitHubOrganization -OrganizationName 'PowerShell' Get-GitHubOrganization -OrganizationName 'PSModule' @@ -48,11 +48,18 @@ Get-GitHubOrganizationAppInstallation -OrganizationName 'PSModule' Set-GitHubOrganization -OrganizationName 'PSModule' -Blog 'https://www.psmodule.io' Set-GitHubOrganization -OrganizationName 'PSModule' -Blog '' -Set-GitHubOrganization -OrganizationName 'PSModule' -Company 'PSModule123' -DefaultRepositoryPermission admin -Set-GitHubOrganization -OrganizationName 'PSModule' -Company 'PSModule' -DefaultRepositoryPermission read - +Set-GitHubOrganization -OrganizationName 'PSModule' -Company 'PSModule123' -DefaultRepositoryPermission admin | Select-Object name, company, default_repository_permission +Set-GitHubOrganization -OrganizationName 'PSModule' -Company 'PSModule' -DefaultRepositoryPermission read | Select-Object name, company, default_repository_permission Get-GitHubUser -Set-GitHubUser -Name 'Marius Storhaug' -Company '@DNBBank' -Email 'marstor@hotmail.com' -Blog 'https://www.github.com/MariusStorhaug' -TwitterUsername MariusStorhaug -Location 'Norway' -Hireable $false -Bio 'DevOps Engineer at DNB Bank. I ❤️ PowerShell and automation.' -Set-GitHubUser -Name 'MariusStorhaug' -Company ' ' -Set-GitHubUser -Name 'MariusStorhaug' -Hireable $true +Get-GitHubUser | Select-Object Name, login, id, company, blog, twitter_username, location, hireable, bio + +$user = Get-GitHubUser +$user.social_accounts + +Set-GitHubUser -Company '@DNBBank' -Email 'marstor@hotmail.com' -Blog 'https://www.github.com/MariusStorhaug' -TwitterUsername MariusStorhaug -Location 'Norway' -Hireable $false -Bio 'DevOps Engineer at DNB Bank. I ❤️ PowerShell and automation.' +Set-GitHubUser -Company ' ' +Set-GitHubUser -Hireable $true | Select-Object login, hireable +Set-GitHubUser -Hireable $false | Select-Object login, hireable + +Add-GitHubUserSocials -AccountUrls 'https://www.github.com/MariusStorhaug' From 816c095c9034380eef59486e4bdaaab174eb0d42 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 4 Oct 2023 22:25:48 +0200 Subject: [PATCH 7/8] Add block/unblcok user --- .../Users/Blocking/Block-GitHubUser.ps1 | 50 +++++++++++++++++++ .../Users/Blocking/Unblock-GitHubUser.ps1 | 42 ++++++++++++++++ tools/utilities/GitHubAPI.ps1 | 2 +- 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/GitHub/public/Users/Blocking/Block-GitHubUser.ps1 create mode 100644 src/GitHub/public/Users/Blocking/Unblock-GitHubUser.ps1 diff --git a/src/GitHub/public/Users/Blocking/Block-GitHubUser.ps1 b/src/GitHub/public/Users/Blocking/Block-GitHubUser.ps1 new file mode 100644 index 000000000..bb5e63469 --- /dev/null +++ b/src/GitHub/public/Users/Blocking/Block-GitHubUser.ps1 @@ -0,0 +1,50 @@ +filter Block-GitHubUser { + <# + .SYNOPSIS + Block a user + + .DESCRIPTION + Blocks the given user and returns a 204. If the authenticated user cannot block the given user a 422 is returned. + + .PARAMETER Username + Parameter description + + .EXAMPLE + Block-GitHubUser -Username 'octocat' + + Blocks the user 'octocat' for the authenticated user. + + .NOTES + https://docs.github.com/rest/users/blocking#block-a-user + #> + [OutputType([bool])] + [CmdletBinding()] + param ( + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('login')] + [string] $Username + ) + + $inputObject = @{ + APIEndpoint = "/user/blocks/$Username" + Method = 'PUT' + } + + try { + $null = (Invoke-GitHubAPI @inputObject) + # Should we check if user is already blocked and return true if so? + return $true + } catch { + if ($_.Exception.Response.StatusCode.Value__ -eq 422) { + return $false + } else { + Write-Error $_.Exception.Response + throw $_ + } + } +} diff --git a/src/GitHub/public/Users/Blocking/Unblock-GitHubUser.ps1 b/src/GitHub/public/Users/Blocking/Unblock-GitHubUser.ps1 new file mode 100644 index 000000000..9ebddfd0f --- /dev/null +++ b/src/GitHub/public/Users/Blocking/Unblock-GitHubUser.ps1 @@ -0,0 +1,42 @@ +filter Unblock-GitHubUser { + <# + .SYNOPSIS + Unblock a user + + .DESCRIPTION + Unblocks the given user and returns a 204. + + .EXAMPLE + Unblock-GitHubUser -Username 'octocat' + + Unblocks the user 'octocat' for the authenticated user. + + .NOTES + https://docs.github.com/rest/users/blocking#unblock-a-user + #> + [OutputType([bool])] + [CmdletBinding()] + param ( + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('login')] + [string] $Username + ) + + $inputObject = @{ + APIEndpoint = "/user/blocks/$Username" + Method = 'DELETE' + } + + try { + $null = (Invoke-GitHubAPI @inputObject) + return $true + } catch { + Write-Error $_.Exception.Response + throw $_ + } +} diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index cd8396c37..ea217b64f 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -21,7 +21,7 @@ $response.paths.psobject.Properties | Select-Object ` @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table -$path = '/user/social_accounts' +$path = '/user/blocks/{username}' $method = 'delete' $response.paths.$path.$method $response.paths.$path.$method.tags | clip # -> Namespace/foldername From 21174f9dd9792694b7a20da152e082a6102f0454 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 4 Oct 2023 23:32:31 +0200 Subject: [PATCH 8/8] Add general blocking cmds wrapping for org --- .../Block-GitHubUserByOrganization.ps1 | 59 +++++++++++++++++++ .../Get-GitHubBlockedUserByOrganization.ps1} | 4 +- .../Test-GitHubBlockedUserByOrganization.ps1} | 6 +- .../Unblock-GitHubUserByOrganization.ps1 | 54 +++++++++++++++++ .../Users/Blocking/Block-GitHubUserByUser.ps1 | 48 +++++++++++++++ .../Blocking/Get-GitHubBlockedUserByUser.ps1 | 35 +++++++++++ .../Blocking/Test-GitHubBlockedUserByUser.ps1 | 53 +++++++++++++++++ .../Blocking/Unblock-GitHubUserByUser.ps1 | 43 ++++++++++++++ .../Users/Blocking/Block-GitHubUser.ps1 | 47 ++++++++------- .../Users/Blocking/Get-GitHubBlockedUser.ps1 | 35 +++++++---- .../Users/Blocking/Test-GitHubBlockedUser.ps1 | 42 +++++++------ .../Users/Blocking/Unblock-GitHubUser.ps1 | 37 ++++++++---- tools/utilities/GitHubAPI.ps1 | 2 +- 13 files changed, 396 insertions(+), 69 deletions(-) create mode 100644 src/GitHub/private/Organization/Blocking/Block-GitHubUserByOrganization.ps1 rename src/GitHub/{public/Organization/Blocking/Get-GitHubOrganizationBlockedUser.ps1 => private/Organization/Blocking/Get-GitHubBlockedUserByOrganization.ps1} (88%) rename src/GitHub/{public/Organization/Blocking/Test-GitHubOrganizationBlockedUser.ps1 => private/Organization/Blocking/Test-GitHubBlockedUserByOrganization.ps1} (86%) create mode 100644 src/GitHub/private/Organization/Blocking/Unblock-GitHubUserByOrganization.ps1 create mode 100644 src/GitHub/private/Users/Blocking/Block-GitHubUserByUser.ps1 create mode 100644 src/GitHub/private/Users/Blocking/Get-GitHubBlockedUserByUser.ps1 create mode 100644 src/GitHub/private/Users/Blocking/Test-GitHubBlockedUserByUser.ps1 create mode 100644 src/GitHub/private/Users/Blocking/Unblock-GitHubUserByUser.ps1 diff --git a/src/GitHub/private/Organization/Blocking/Block-GitHubUserByOrganization.ps1 b/src/GitHub/private/Organization/Blocking/Block-GitHubUserByOrganization.ps1 new file mode 100644 index 000000000..14ffea29f --- /dev/null +++ b/src/GitHub/private/Organization/Blocking/Block-GitHubUserByOrganization.ps1 @@ -0,0 +1,59 @@ +filter Block-GitHubUserByOrganization { + <# + .SYNOPSIS + Block a user from an organization + + .DESCRIPTION + Blocks the given user on behalf of the specified organization and returns a 204. If the organization cannot block the given user a 422 is returned. + + .EXAMPLE + Block-GitHubUserByOrganization -OrganizationName 'github' -Username 'octocat' + + Blocks the user 'octocat' from the organization 'github'. + Returns $true if successful, $false if not. + + .NOTES + https://docs.github.com/rest/orgs/blocking#block-a-user-from-an-organization + #> + [OutputType([bool])] + [CmdletBinding()] + param ( + # The organization name. The name is not case sensitive. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('org')] + [Alias('owner')] + [Alias('login')] + [string] $OrganizationName, + + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('login')] + [string] $Username + ) + + $inputObject = @{ + APIEndpoint = "/orgs/$OrganizationName/blocks/$Username" + Method = 'PUT' + } + + try { + $null = (Invoke-GitHubAPI @inputObject) + # Should we check if user is already blocked and return true if so? + return $true + } catch { + if ($_.Exception.Response.StatusCode.Value__ -eq 422) { + return $false + } else { + Write-Error $_.Exception.Response + throw $_ + } + } +} diff --git a/src/GitHub/public/Organization/Blocking/Get-GitHubOrganizationBlockedUser.ps1 b/src/GitHub/private/Organization/Blocking/Get-GitHubBlockedUserByOrganization.ps1 similarity index 88% rename from src/GitHub/public/Organization/Blocking/Get-GitHubOrganizationBlockedUser.ps1 rename to src/GitHub/private/Organization/Blocking/Get-GitHubBlockedUserByOrganization.ps1 index 587152403..96d690692 100644 --- a/src/GitHub/public/Organization/Blocking/Get-GitHubOrganizationBlockedUser.ps1 +++ b/src/GitHub/private/Organization/Blocking/Get-GitHubBlockedUserByOrganization.ps1 @@ -1,4 +1,4 @@ -filter Get-GitHubOrganizationBlockedUser { +filter Get-GitHubBlockedUserByOrganization { <# .SYNOPSIS List users blocked by an organization @@ -7,7 +7,7 @@ List the users blocked by an organization. .EXAMPLE - Get-GitHubOrganizationBlockedUser -OrganizationName 'github' + Get-GitHubBlockedUserByOrganization -OrganizationName 'github' Lists all users blocked by the organization `github`. diff --git a/src/GitHub/public/Organization/Blocking/Test-GitHubOrganizationBlockedUser.ps1 b/src/GitHub/private/Organization/Blocking/Test-GitHubBlockedUserByOrganization.ps1 similarity index 86% rename from src/GitHub/public/Organization/Blocking/Test-GitHubOrganizationBlockedUser.ps1 rename to src/GitHub/private/Organization/Blocking/Test-GitHubBlockedUserByOrganization.ps1 index ba15aaa99..231a29ea3 100644 --- a/src/GitHub/public/Organization/Blocking/Test-GitHubOrganizationBlockedUser.ps1 +++ b/src/GitHub/private/Organization/Blocking/Test-GitHubBlockedUserByOrganization.ps1 @@ -1,4 +1,4 @@ -filter Test-GitHubOrganizationBlockedUser { +filter Test-GitHubBlockedUserByOrganization { <# .SYNOPSIS Check if a user is blocked by an organization @@ -7,7 +7,7 @@ Returns a 204 if the given user is blocked by the given organization. Returns a 404 if the organization is not blocking the user, or if the user account has been identified as spam by GitHub. .EXAMPLE - Test-GitHubOrganizationBlockedUser -OrganizationName 'PSModule' -Username 'octocat' + Test-GitHubBlockedUserByOrganization -OrganizationName 'PSModule' -Username 'octocat' Checks if the user `octocat` is blocked by the organization `PSModule`. Returns true if the user is blocked, false if not. @@ -16,8 +16,6 @@ https://docs.github.com/rest/orgs/blocking#check-if-a-user-is-blocked-by-an-organization #> [OutputType([bool])] - [Alias('Is-GitHubOrganizationBlockedUser')] - [Alias('Check-GitHubOrganizationBlockedUser')] [CmdletBinding()] param ( # The organization name. The name is not case sensitive. diff --git a/src/GitHub/private/Organization/Blocking/Unblock-GitHubUserByOrganization.ps1 b/src/GitHub/private/Organization/Blocking/Unblock-GitHubUserByOrganization.ps1 new file mode 100644 index 000000000..3e8d208cd --- /dev/null +++ b/src/GitHub/private/Organization/Blocking/Unblock-GitHubUserByOrganization.ps1 @@ -0,0 +1,54 @@ +filter Unblock-GitHubUserByOrganization { + <# + .SYNOPSIS + Unblock a user from an organization + + .DESCRIPTION + Unblocks the given user on behalf of the specified organization. + + .EXAMPLE + Unblock-GitHubUserByOrganization -OrganizationName 'github' -Username 'octocat' + + Unblocks the user 'octocat' from the organization 'github'. + Returns $true if successful. + + .NOTES + https://docs.github.com/rest/orgs/blocking#unblock-a-user-from-an-organization + #> + [OutputType([bool])] + [CmdletBinding()] + param ( + # The organization name. The name is not case sensitive. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('org')] + [Alias('owner')] + [Alias('login')] + [string] $OrganizationName, + + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('login')] + [string] $Username + ) + + $inputObject = @{ + APIEndpoint = "/user/blocks/$Username" + Method = 'DELETE' + } + + try { + $null = (Invoke-GitHubAPI @inputObject) + return $true + } catch { + Write-Error $_.Exception.Response + throw $_ + } +} diff --git a/src/GitHub/private/Users/Blocking/Block-GitHubUserByUser.ps1 b/src/GitHub/private/Users/Blocking/Block-GitHubUserByUser.ps1 new file mode 100644 index 000000000..5afca493d --- /dev/null +++ b/src/GitHub/private/Users/Blocking/Block-GitHubUserByUser.ps1 @@ -0,0 +1,48 @@ +filter Block-GitHubUserByUser { + <# + .SYNOPSIS + Block a user + + .DESCRIPTION + Blocks the given user and returns a 204. If the authenticated user cannot block the given user a 422 is returned. + + .EXAMPLE + Block-GitHubUserByUser -Username 'octocat' + + Blocks the user 'octocat' for the authenticated user. + Returns $true if successful, $false if not. + + .NOTES + https://docs.github.com/rest/users/blocking#block-a-user + #> + [OutputType([bool])] + [CmdletBinding()] + param ( + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('login')] + [string] $Username + ) + + $inputObject = @{ + APIEndpoint = "/user/blocks/$Username" + Method = 'PUT' + } + + try { + $null = (Invoke-GitHubAPI @inputObject) + # Should we check if user is already blocked and return true if so? + return $true + } catch { + if ($_.Exception.Response.StatusCode.Value__ -eq 422) { + return $false + } else { + Write-Error $_.Exception.Response + throw $_ + } + } +} diff --git a/src/GitHub/private/Users/Blocking/Get-GitHubBlockedUserByUser.ps1 b/src/GitHub/private/Users/Blocking/Get-GitHubBlockedUserByUser.ps1 new file mode 100644 index 000000000..e11fb208e --- /dev/null +++ b/src/GitHub/private/Users/Blocking/Get-GitHubBlockedUserByUser.ps1 @@ -0,0 +1,35 @@ +filter Get-GitHubBlockedUserByUser { + <# + .SYNOPSIS + List users blocked by the authenticated user + + .DESCRIPTION + List the users you've blocked on your personal account. + + .EXAMPLE + Get-GitHubBlockedUserByUser + + Returns a list of users blocked by the authenticated user. + + .NOTES + https://docs.github.com/rest/users/blocking#list-users-blocked-by-the-authenticated-user + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The number of results per page (max 100). + [Parameter()] + [int] $PerPage = 30 + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + + $inputObject = @{ + APIEndpoint = "/user/blocks" + Method = 'GET' + Body = $body + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/src/GitHub/private/Users/Blocking/Test-GitHubBlockedUserByUser.ps1 b/src/GitHub/private/Users/Blocking/Test-GitHubBlockedUserByUser.ps1 new file mode 100644 index 000000000..843905aab --- /dev/null +++ b/src/GitHub/private/Users/Blocking/Test-GitHubBlockedUserByUser.ps1 @@ -0,0 +1,53 @@ +filter Test-GitHubBlockedUserByUser { + <# + .SYNOPSIS + Check if a user is blocked by the authenticated user + + .DESCRIPTION + Returns a 204 if the given user is blocked by the authenticated user. Returns a 404 if the given user is not blocked by the authenticated user, or if the given user account has been identified as spam by GitHub. + + .EXAMPLE + Test-GitHubBlockedUserByUser -Username 'octocat' + + Checks if the user `octocat` is blocked by the authenticated user. + Returns true if the user is blocked, false if not. + + .NOTES + https://docs.github.com/rest/users/blocking#check-if-a-user-is-blocked-by-the-authenticated-user + #> + [OutputType([bool])] + [CmdletBinding()] + param ( + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('login')] + [string] $Username, + + # The number of results per page (max 100). + [Parameter()] + [int] $PerPage = 30 + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + + $inputObject = @{ + APIEndpoint = "/user/blocks/$Username" + Method = 'GET' + Body = $body + } + + try { + (Invoke-GitHubAPI @inputObject).StatusCode -eq 204 + } catch { + if ($_.Exception.Response.StatusCode.Value__ -eq 404) { + return $false + } else { + throw $_ + } + } + +} diff --git a/src/GitHub/private/Users/Blocking/Unblock-GitHubUserByUser.ps1 b/src/GitHub/private/Users/Blocking/Unblock-GitHubUserByUser.ps1 new file mode 100644 index 000000000..6192d8ea7 --- /dev/null +++ b/src/GitHub/private/Users/Blocking/Unblock-GitHubUserByUser.ps1 @@ -0,0 +1,43 @@ +filter Unblock-GitHubUserByUser { + <# + .SYNOPSIS + Unblock a user + + .DESCRIPTION + Unblocks the given user and returns a 204. + + .EXAMPLE + Unblock-GitHubUserByUser -Username 'octocat' + + Unblocks the user 'octocat' for the authenticated user. + Returns $true if successful. + + .NOTES + https://docs.github.com/rest/users/blocking#unblock-a-user + #> + [OutputType([bool])] + [CmdletBinding()] + param ( + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('login')] + [string] $Username + ) + + $inputObject = @{ + APIEndpoint = "/user/blocks/$Username" + Method = 'DELETE' + } + + try { + $null = (Invoke-GitHubAPI @inputObject) + return $true + } catch { + Write-Error $_.Exception.Response + throw $_ + } +} diff --git a/src/GitHub/public/Users/Blocking/Block-GitHubUser.ps1 b/src/GitHub/public/Users/Blocking/Block-GitHubUser.ps1 index bb5e63469..1a469cf4b 100644 --- a/src/GitHub/public/Users/Blocking/Block-GitHubUser.ps1 +++ b/src/GitHub/public/Users/Blocking/Block-GitHubUser.ps1 @@ -1,21 +1,27 @@ filter Block-GitHubUser { <# .SYNOPSIS - Block a user + Block a user from user or an organization. .DESCRIPTION - Blocks the given user and returns a 204. If the authenticated user cannot block the given user a 422 is returned. - - .PARAMETER Username - Parameter description + Blocks the given user and returns true. + If the user cannot be blocked false is returned. .EXAMPLE Block-GitHubUser -Username 'octocat' Blocks the user 'octocat' for the authenticated user. + Returns $true if successful, $false if not. + + .EXAMPLE + Block-GitHubUser -OrganizationName 'github' -Username 'octocat' + + Blocks the user 'octocat' from the organization 'github'. + Returns $true if successful, $false if not. .NOTES https://docs.github.com/rest/users/blocking#block-a-user + https://docs.github.com/rest/orgs/blocking#block-a-user-from-an-organization #> [OutputType([bool])] [CmdletBinding()] @@ -27,24 +33,23 @@ ValueFromPipelineByPropertyName )] [Alias('login')] - [string] $Username + [string] $Username, + + # The organization name. The name is not case sensitive. + [Parameter( + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('org')] + [Alias('owner')] + [Alias('login')] + [string] $OrganizationName ) - $inputObject = @{ - APIEndpoint = "/user/blocks/$Username" - Method = 'PUT' + if ($OrganizationName) { + Block-GitHubUserByOrganization -OrganizationName $OrganizationName -Username $Username + } else { + Block-GitHubUserByUser -Username $Username } - try { - $null = (Invoke-GitHubAPI @inputObject) - # Should we check if user is already blocked and return true if so? - return $true - } catch { - if ($_.Exception.Response.StatusCode.Value__ -eq 422) { - return $false - } else { - Write-Error $_.Exception.Response - throw $_ - } - } } diff --git a/src/GitHub/public/Users/Blocking/Get-GitHubBlockedUser.ps1 b/src/GitHub/public/Users/Blocking/Get-GitHubBlockedUser.ps1 index 3f5187976..664fea731 100644 --- a/src/GitHub/public/Users/Blocking/Get-GitHubBlockedUser.ps1 +++ b/src/GitHub/public/Users/Blocking/Get-GitHubBlockedUser.ps1 @@ -1,32 +1,47 @@ filter Get-GitHubBlockedUser { <# .SYNOPSIS - List users blocked by the authenticated user + List blocked users. .DESCRIPTION - List the users you've blocked on your personal account. + List the users that are blocked on your personal account or a given organization. .EXAMPLE + Get-GitHubBlockedUser + + Returns a list of users blocked by the authenticated user. + + .EXAMPLE + Get-GitHubBlockedUser -OrganizationName 'github' + + Lists all users blocked by the organization `github`. .NOTES https://docs.github.com/rest/users/blocking#list-users-blocked-by-the-authenticated-user + https://docs.github.com/rest/orgs/blocking#list-users-blocked-by-an-organization #> [OutputType([pscustomobject])] [CmdletBinding()] param ( + # The organization name. The name is not case sensitive. + [Parameter( + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('org')] + [Alias('owner')] + [Alias('login')] + [string] $OrganizationName, + # The number of results per page (max 100). [Parameter()] [int] $PerPage = 30 ) - $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case - - $inputObject = @{ - APIEndpoint = "/user/blocks" - Method = 'GET' - Body = $body + if ($OrganizationName) { + Get-GitHubBlockedUserByOrganization -OrganizationName $OrganizationName -PerPage $PerPage + } else { + Get-GitHubBlockedUserByUser -PerPage $PerPage } - (Invoke-GitHubAPI @inputObject).Response - } diff --git a/src/GitHub/public/Users/Blocking/Test-GitHubBlockedUser.ps1 b/src/GitHub/public/Users/Blocking/Test-GitHubBlockedUser.ps1 index 0b1901971..7484d2be2 100644 --- a/src/GitHub/public/Users/Blocking/Test-GitHubBlockedUser.ps1 +++ b/src/GitHub/public/Users/Blocking/Test-GitHubBlockedUser.ps1 @@ -1,10 +1,11 @@ filter Test-GitHubBlockedUser { <# .SYNOPSIS - Check if a user is blocked by the authenticated user + Check if a user is blocked by the authenticated user or an organization. .DESCRIPTION - Returns a 204 if the given user is blocked by the authenticated user. Returns a 404 if the given user is not blocked by the authenticated user, or if the given user account has been identified as spam by GitHub. + Returns a 204 if the given user is blocked by the authenticated user or organization. + Returns a 404 if the given user is not blocked, or if the given user account has been identified as spam by GitHub. .EXAMPLE Test-GitHubBlockedUser -Username 'octocat' @@ -12,8 +13,15 @@ Checks if the user `octocat` is blocked by the authenticated user. Returns true if the user is blocked, false if not. + .EXAMPLE + Test-GitHubBlockedUser -OrganizationName 'github' -Username 'octocat' + + Checks if the user `octocat` is blocked by the organization `github`. + Returns true if the user is blocked, false if not. + .NOTES https://docs.github.com/rest/users/blocking#check-if-a-user-is-blocked-by-the-authenticated-user + https://docs.github.com/rest/orgs/blocking#check-if-a-user-is-blocked-by-an-organization #> [OutputType([bool])] [CmdletBinding()] @@ -27,27 +35,25 @@ [Alias('login')] [string] $Username, + # The organization name. The name is not case sensitive. + [Parameter( + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('org')] + [Alias('owner')] + [Alias('login')] + [string] $OrganizationName, + # The number of results per page (max 100). [Parameter()] [int] $PerPage = 30 ) - $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case - - $inputObject = @{ - APIEndpoint = "/user/blocks/$Username" - Method = 'GET' - Body = $body - } - - try { - (Invoke-GitHubAPI @inputObject).StatusCode -eq 204 - } catch { - if ($_.Exception.Response.StatusCode.Value__ -eq 404) { - return $false - } else { - throw $_ - } + if ($OrganizationName) { + Test-GitHubBlockedUserByOrganization -OrganizationName $OrganizationName -Username $Username -PerPage $PerPage + } else { + Test-GitHubBlockedUserByUser -Username $Username -PerPage $PerPage } } diff --git a/src/GitHub/public/Users/Blocking/Unblock-GitHubUser.ps1 b/src/GitHub/public/Users/Blocking/Unblock-GitHubUser.ps1 index 9ebddfd0f..854a1ef70 100644 --- a/src/GitHub/public/Users/Blocking/Unblock-GitHubUser.ps1 +++ b/src/GitHub/public/Users/Blocking/Unblock-GitHubUser.ps1 @@ -4,15 +4,23 @@ Unblock a user .DESCRIPTION - Unblocks the given user and returns a 204. + Unblocks the given user and returns true. .EXAMPLE Unblock-GitHubUser -Username 'octocat' Unblocks the user 'octocat' for the authenticated user. + Returns $true if successful. + + .EXAMPLE + Unblock-GitHubUser -OrganizationName 'github' -Username 'octocat' + + Unblocks the user 'octocat' from the organization 'github'. + Returns $true if successful. .NOTES https://docs.github.com/rest/users/blocking#unblock-a-user + https://docs.github.com/rest/orgs/blocking#unblock-a-user-from-an-organization #> [OutputType([bool])] [CmdletBinding()] @@ -24,19 +32,22 @@ ValueFromPipelineByPropertyName )] [Alias('login')] - [string] $Username - ) + [string] $Username, - $inputObject = @{ - APIEndpoint = "/user/blocks/$Username" - Method = 'DELETE' - } + # The organization name. The name is not case sensitive. + [Parameter( + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('org')] + [Alias('owner')] + [Alias('login')] + [string] $OrganizationName + ) - try { - $null = (Invoke-GitHubAPI @inputObject) - return $true - } catch { - Write-Error $_.Exception.Response - throw $_ + if ($OrganizationName) { + Unblock-GitHubUserByOrganization -OrganizationName $OrganizationName -Username $Username + } else { + Unblock-GitHubUserByUser -Username $Username } } diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index ea217b64f..dfdec0398 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -21,7 +21,7 @@ $response.paths.psobject.Properties | Select-Object ` @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table -$path = '/user/blocks/{username}' +$path = '/orgs/{org}/blocks/{username}' $method = 'delete' $response.paths.$path.$method $response.paths.$path.$method.tags | clip # -> Namespace/foldername