forked from microsoft/PowerShellForGitHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitHubUsers.ps1
277 lines (209 loc) · 9.64 KB
/
GitHubUsers.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
function Get-GitHubUser
{
<#
.SYNOPSIS
Retrieves information about the specified user on GitHub.
.DESCRIPTION
Retrieves information about the specified user on GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER User
The GitHub user to retrieve information for.
If not specified, will retrieve information on all GitHub users (and may take a while to complete).
.PARAMETER Current
If specified, gets information on the current user.
.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.
.NOTES
The email key in the following response is the publicly visible email address from the
user's GitHub profile page. You only see publicly visible email addresses when
authenticated with GitHub.
When setting up your profile, a user can select a primary email address to be public
which provides an email entry for this endpoint. If the user does not set a public
email address for email, then it will have a value of null.
.EXAMPLE
Get-GitHubUser -User octocat
Gets information on just the user named 'octocat'
.EXAMPLE
Get-GitHubUser
Gets information on every GitHub user.
.EXAMPLE
Get-GitHubUser -Current
Gets information on the current authenticated user.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParametersetName='ListAndSearch')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[Parameter(ParameterSetName='ListAndSearch')]
[string] $User,
[Parameter(ParameterSetName='Current')]
[switch] $Current,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$params = @{
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
if ($Current)
{
return Invoke-GHRestMethod -UriFragment "user" -Description "Getting current authenticated user" -Method 'Get' @params
}
elseif ([String]::IsNullOrEmpty($User))
{
return Invoke-GHRestMethodMultipleResult -UriFragment 'users' -Description 'Getting all users' @params
}
else
{
return Invoke-GHRestMethod -UriFragment "users/$User" -Description "Getting user $User" -Method 'Get' @params
}
}
function Get-GitHubUserContextualInformation
{
<#
.SYNOPSIS
Retrieves contextual information about the specified user on GitHub.
.DESCRIPTION
Retrieves contextual information about the specified user on GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER User
The GitHub user to retrieve information for.
.PARAMETER Subject
Identifies which additional information to receive about the user's hovercard.
.PARAMETER SubjectId
The ID for the Subject. Required when Subject has been specified.
.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.
.EXAMPLE
Get-GitHubUserContextualInformation -User octocat
.EXAMPLE
Get-GitHubUserContextualInformation -User octocat -Subject Repository -SubjectId 1300192
#>
[CmdletBinding(SupportsShouldProcess)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[Parameter(Mandatory)]
[string] $User,
[ValidateSet('Organization', 'Repository', 'Issue', 'PullRequest')]
[string] $Subject,
[string] $SubjectId,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$getParams = @()
# Intentionally not using -xor here because we need to know if we're setting the GET parameters as well.
if ((-not [String]::IsNullOrEmpty($Subject)) -or (-not [String]::IsNullOrEmpty($SubjectId)))
{
if ([String]::IsNullOrEmpty($Subject) -or [String]::IsNullOrEmpty($SubjectId))
{
$message = 'If either Subject or SubjectId has been provided, then BOTH must be provided.'
Write-Log -Message $message -Level Error
throw $message
}
$subjectConverter = @{
'Organization' = 'organization'
'Repository' = 'repository'
'Issue' = 'issue'
'PullRequest' = 'pull_request'
}
$getParams += "subject_type=$($subjectConverter[$Subject])"
$getParams += "subject_id=$SubjectId"
}
$params = @{
'UriFragment' = "users/$User/hovercard`?" + ($getParams -join '&')
'Method' = 'Get'
'Description' = "Getting hovercard information for $User"
'AcceptHeader' = 'application/vnd.github.hagar-preview+json'
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
Invoke-GHRestMethod @params
}
function Update-GitHubCurrentUser
{
<#
.SYNOPSIS
Updates information about the current authenticated user on GitHub.
.DESCRIPTION
Updates information about the current authenticated user on GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Name
The new name of the user.
.PARAMETER Email
The publicly visible email address of the user.
.PARAMETER Blog
The new blog URL of the user.
.PARAMETER Company
The new company of the user.
.PARAMETER Location
The new location of the user.
.PARAMETER Bio
The new short biography of the user.
.PARAMETER Hireable
Specify to indicate a change in hireable availability for the current authenticated user's
GitHub profile. To change to "not hireable", specify -Hireable:$false
.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.
.EXAMPLE
Update-GitHubCurrentUser -Location 'Seattle, WA' -Hireable:$false
Updates the current user to indicate that their location is "Seattle, WA" and that they
are not currently hireable.
#>
[CmdletBinding(SupportsShouldProcess)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[string] $Name,
[string] $Email,
[string] $Blog,
[string] $Company,
[string] $Location,
[string] $Bio,
[switch] $Hireable,
[string] $AccessToken,
[switch] $NoStatus
)
Write-InvocationLog
$hashBody = @{}
if ($PSBoundParameters.ContainsKey('Name')) { $hashBody['name'] = $Name }
if ($PSBoundParameters.ContainsKey('Email')) { $hashBody['email'] = $Email }
if ($PSBoundParameters.ContainsKey('Blog')) { $hashBody['blog'] = $Blog }
if ($PSBoundParameters.ContainsKey('Company')) { $hashBody['company'] = $Company }
if ($PSBoundParameters.ContainsKey('Location')) { $hashBody['location'] = $Location }
if ($PSBoundParameters.ContainsKey('Bio')) { $hashBody['bio'] = $Bio }
if ($PSBoundParameters.ContainsKey('Hireable')) { $hashBody['hireable'] = $Hireable.ToBool() }
$params = @{
'UriFragment' = 'user'
'Method' = 'Patch'
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Description' = "Updating current authenticated user"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
return Invoke-GHRestMethod @params
}