Skip to content

Commit 4fcdb6a

Browse files
[Feature] User - Followers - Use the REST API to get information about followers of authenticated users (#52)
- Fixes #47 - Add-GitHubUserFollowing (Follow-GitHubUser) - Get-GitHubUserFollowers - Get-GitHubUserFollowing - Remove-GitHubUserFollowing (Unfollow-GitHubUser) - Test-GitHubUserFollows
1 parent fedf70a commit 4fcdb6a

12 files changed

+483
-3
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
filter Get-GitHubUserFollowersOfUser {
2+
<#
3+
.SYNOPSIS
4+
List followers of a user
5+
6+
.DESCRIPTION
7+
Lists the people following the specified user.
8+
9+
.EXAMPLE
10+
Get-GitHubUserFollowersOfUser -Username 'octocat'
11+
12+
Gets all followers of user 'octocat'.
13+
14+
.NOTES
15+
https://docs.github.com/rest/users/followers#list-followers-of-a-user
16+
17+
#>
18+
[OutputType([pscustomobject])]
19+
[CmdletBinding()]
20+
param (
21+
# The handle for the GitHub user account.
22+
[Parameter(
23+
Mandatory,
24+
ValueFromPipeline,
25+
ValueFromPipelineByPropertyName
26+
)]
27+
[Alias('login')]
28+
[string] $Username,
29+
30+
# The number of results per page (max 100).
31+
[Parameter()]
32+
[int] $PerPage = 30
33+
)
34+
35+
$body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case
36+
Remove-HashtableEntries -Hashtable $body -RemoveNames 'username'
37+
38+
$inputObject = @{
39+
APIEndpoint = "/users/$Username/followers"
40+
Method = 'GET'
41+
Body = $body
42+
}
43+
44+
(Invoke-GitHubAPI @inputObject).Response
45+
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
filter Get-GitHubUserFollowingMe {
2+
<#
3+
.SYNOPSIS
4+
List the people the authenticated user follows
5+
6+
.DESCRIPTION
7+
Lists the people who the authenticated user follows.
8+
9+
.EXAMPLE
10+
Get-GitHubUserFollowingMe
11+
12+
Gets all people the authenticated user follows.
13+
14+
.NOTES
15+
https://docs.github.com/rest/users/followers#list-the-people-the-authenticated-user-follows
16+
17+
#>
18+
[OutputType([pscustomobject])]
19+
[CmdletBinding()]
20+
param (
21+
# The number of results per page (max 100).
22+
[Parameter()]
23+
[int] $PerPage = 30
24+
)
25+
26+
$body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case
27+
28+
$inputObject = @{
29+
APIEndpoint = '/user/following'
30+
Method = 'GET'
31+
Body = $body
32+
}
33+
34+
(Invoke-GitHubAPI @inputObject).Response
35+
36+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
filter Get-GitHubUserFollowingUser {
2+
<#
3+
.SYNOPSIS
4+
List the people a user follows
5+
6+
.DESCRIPTION
7+
Lists the people who the specified user follows.
8+
9+
.EXAMPLE
10+
Get-GitHubUserFollowingUser -Username 'octocat'
11+
12+
Gets all people that 'octocat' follows.
13+
14+
.NOTES
15+
https://docs.github.com/rest/users/followers#list-the-people-a-user-follows
16+
17+
#>
18+
[OutputType([pscustomobject])]
19+
[CmdletBinding()]
20+
param (
21+
# The handle for the GitHub user account.
22+
[Parameter(
23+
Mandatory,
24+
ValueFromPipeline,
25+
ValueFromPipelineByPropertyName
26+
)]
27+
[Alias('login')]
28+
[string] $Username,
29+
30+
# The number of results per page (max 100).
31+
[Parameter()]
32+
[int] $PerPage = 30
33+
)
34+
35+
$body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case
36+
Remove-HashtableEntries -Hashtable $body -RemoveNames 'username'
37+
38+
$inputObject = @{
39+
APIEndpoint = "/users/$Username/following"
40+
Method = 'GET'
41+
Body = $body
42+
}
43+
44+
(Invoke-GitHubAPI @inputObject).Response
45+
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
filter Get-GitHubUserMyFollowers {
2+
<#
3+
.SYNOPSIS
4+
List followers of the authenticated user
5+
6+
.DESCRIPTION
7+
Lists the people following the authenticated user.
8+
9+
.EXAMPLE
10+
Get-GitHubUserMyFollowers
11+
12+
Gets all followers of the authenticated user.
13+
14+
.NOTES
15+
https://docs.github.com/rest/users/followers#list-followers-of-the-authenticated-user
16+
17+
#>
18+
[OutputType([pscustomobject])]
19+
[CmdletBinding()]
20+
param (
21+
# The number of results per page (max 100).
22+
[Parameter()]
23+
[int] $PerPage = 30
24+
)
25+
26+
$body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case
27+
28+
$inputObject = @{
29+
APIEndpoint = '/user/followers'
30+
Method = 'GET'
31+
Body = $body
32+
}
33+
34+
(Invoke-GitHubAPI @inputObject).Response
35+
36+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
filter Test-GitHubUserFollowedByMe {
2+
<#
3+
.SYNOPSIS
4+
Check if a person is followed by the authenticated user
5+
6+
.DESCRIPTION
7+
Returns a 204 if the given user is followed by the authenticated user.
8+
Returns a 404 if the user is not followed by the authenticated user.
9+
10+
.EXAMPLE
11+
Test-GitHubUserFollowedByMe -Username 'octocat'
12+
13+
Checks if the authenticated user follows the user 'octocat'.
14+
15+
.NOTES
16+
https://docs.github.com/rest/users/followers#check-if-a-person-is-followed-by-the-authenticated-user
17+
18+
#>
19+
[OutputType([bool])]
20+
[CmdletBinding()]
21+
param (
22+
# The handle for the GitHub user account.
23+
[Parameter(
24+
Mandatory,
25+
ValueFromPipelineByPropertyName
26+
)]
27+
[string] $Username
28+
)
29+
30+
$inputObject = @{
31+
APIEndpoint = "/user/following/$Username"
32+
Method = 'GET'
33+
}
34+
35+
try {
36+
$null = (Invoke-GitHubAPI @inputObject)
37+
return $true
38+
} catch {
39+
if ($_.Exception.Response.StatusCode.Value__ -eq 404) {
40+
return $false
41+
} else {
42+
throw $_
43+
}
44+
}
45+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
filter Test-GitHubUserFollowedByUser {
2+
<#
3+
.SYNOPSIS
4+
Check if a user follows another user
5+
6+
.DESCRIPTION
7+
Checks if a user follows another user.
8+
9+
.EXAMPLE
10+
Test-GitHubUserFollowedByUser -Username 'octocat' -Follows 'ratstallion'
11+
12+
Checks if the user 'octocat' follows the user 'ratstallion'.
13+
14+
.NOTES
15+
https://docs.github.com/rest/users/followers#check-if-a-user-follows-another-user
16+
17+
#>
18+
[OutputType([bool])]
19+
[CmdletBinding()]
20+
param (
21+
# The handle for the GitHub user account.
22+
[Parameter(
23+
Mandatory,
24+
ValueFromPipelineByPropertyName
25+
)]
26+
[string] $Username,
27+
28+
# The handle for the GitHub user account we want to check if user specified by $Username is following.
29+
[Parameter(
30+
Mandatory,
31+
ValueFromPipelineByPropertyName
32+
)]
33+
[string] $Follows
34+
)
35+
36+
$inputObject = @{
37+
APIEndpoint = "/users/$Username/following/$Follows"
38+
Method = 'GET'
39+
}
40+
41+
try {
42+
$null = (Invoke-GitHubAPI @inputObject)
43+
return $true
44+
} catch {
45+
if ($_.Exception.Response.StatusCode.Value__ -eq 404) {
46+
return $false
47+
} else {
48+
throw $_
49+
}
50+
}
51+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
filter Add-GitHubUserFollowing {
2+
<#
3+
.SYNOPSIS
4+
Follow a user
5+
6+
.DESCRIPTION
7+
Note that you'll need to set `Content-Length` to zero when calling out to this endpoint.
8+
For more information, see "[HTTP verbs](https://docs.github.com/rest/overview/resources-in-the-rest-api#http-verbs)."
9+
Following a user requires the user to be logged in and authenticated with basic auth or OAuth with the `user:follow` scope.
10+
11+
.EXAMPLE
12+
Follow-GitHubUser -Username 'octocat'
13+
14+
Follows the user with the username 'octocat'.
15+
16+
.NOTES
17+
https://docs.github.com/rest/users/followers#follow-a-user
18+
19+
#>
20+
[OutputType([pscustomobject])]
21+
[Alias('Follow-GitHubUser')]
22+
[CmdletBinding()]
23+
param (
24+
# The handle for the GitHub user account.
25+
[Parameter(
26+
Mandatory,
27+
ValueFromPipeline,
28+
ValueFromPipelineByPropertyName
29+
)]
30+
[string] $Username
31+
)
32+
33+
$inputObject = @{
34+
APIEndpoint = "/user/following/$Username"
35+
Method = 'PUT'
36+
}
37+
38+
$null = (Invoke-GitHubAPI @inputObject).Response
39+
40+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
filter Get-GitHubUserFollowers {
2+
<#
3+
.SYNOPSIS
4+
List followers of a given user or the authenticated user
5+
6+
.DESCRIPTION
7+
Lists the people following a given user or the authenticated user.
8+
9+
.EXAMPLE
10+
Get-GitHubUserFollowers
11+
12+
Gets all followers of the authenticated user.
13+
14+
.EXAMPLE
15+
Get-GitHubUserFollowers -Username 'octocat'
16+
17+
Gets all followers of the user 'octocat'.
18+
19+
.NOTES
20+
https://docs.github.com/rest/users/followers#list-followers-of-the-authenticated-user
21+
22+
#>
23+
[OutputType([pscustomobject])]
24+
[CmdletBinding()]
25+
param (
26+
# The handle for the GitHub user account.
27+
[Parameter(
28+
ValueFromPipeline,
29+
ValueFromPipelineByPropertyName
30+
)]
31+
[Alias('login')]
32+
[string] $Username,
33+
34+
# The number of results per page (max 100).
35+
[Parameter()]
36+
[int] $PerPage = 30
37+
)
38+
39+
if ($Username) {
40+
Get-GitHubUserFollowersOfUser -Username $Username -PerPage $PerPage
41+
} else {
42+
Get-GitHubUserMyFollowers -PerPage $PerPage
43+
}
44+
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
filter Get-GitHubUserFollowing {
2+
<#
3+
.SYNOPSIS
4+
List the people a given user or the authenticated user follows
5+
6+
.DESCRIPTION
7+
Lists the people who a given user or the authenticated user follows.
8+
9+
.EXAMPLE
10+
Get-GitHubUserFollowing
11+
12+
Gets all people the authenticated user follows.
13+
14+
.EXAMPLE
15+
Get-GitHubUserFollowing -Username 'octocat'
16+
17+
Gets all people that 'octocat' follows.
18+
19+
.NOTES
20+
https://docs.github.com/rest/users/followers#list-the-people-the-authenticated-user-follows
21+
https://docs.github.com/rest/users/followers#list-the-people-a-user-follows
22+
23+
#>
24+
[OutputType([pscustomobject])]
25+
[CmdletBinding()]
26+
param (
27+
# The handle for the GitHub user account.
28+
[Parameter(
29+
ValueFromPipeline,
30+
ValueFromPipelineByPropertyName
31+
)]
32+
[Alias('login')]
33+
[string] $Username,
34+
35+
# The number of results per page (max 100).
36+
[Parameter()]
37+
[int] $PerPage = 30
38+
)
39+
40+
if ($Username) {
41+
Get-GitHubUserFollowingUser -Username $Username -PerPage $PerPage
42+
} else {
43+
Get-GitHubUserFollowingMe -PerPage $PerPage
44+
}
45+
46+
}

0 commit comments

Comments
 (0)