Skip to content

Commit 680696a

Browse files
joseartriveraHowardWolosky
authored andcommitted
Add support for assignee APIs (#54)
Adds support for the [Issues Assignees](https://developer.github.com./v3/issues/assignees/) API's
1 parent 6cf344f commit 680696a

File tree

5 files changed

+561
-5
lines changed

5 files changed

+561
-5
lines changed

GitHubAssignees.ps1

+386
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,386 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
function Get-GitHubAssignee
5+
{
6+
<#
7+
.DESCRIPTION
8+
Lists the available assignees for issues in a repository.
9+
10+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
11+
12+
.PARAMETER OwnerName
13+
Owner of the repository.
14+
If not supplied here, the DefaultOwnerName configuration property value will be used.
15+
16+
.PARAMETER RepositoryName
17+
Name of the repository.
18+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
19+
20+
.PARAMETER Uri
21+
Uri for the repository.
22+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
23+
them individually.
24+
25+
.PARAMETER AccessToken
26+
If provided, this will be used as the AccessToken for authentication with the
27+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
28+
29+
.PARAMETER NoStatus
30+
If this switch is specified, long-running commands will run on the main thread
31+
with no commandline status update. When not specified, those commands run in
32+
the background, enabling the command prompt to provide status information.
33+
If not supplied here, the DefaultNoStatus configuration property value will be used.
34+
35+
.EXAMPLE
36+
Get-GitHubAsigneeList -OwnerName Powershell -RepositoryName PowerShellForGitHub
37+
38+
Lists the available assignees for issues from the PowerShell\PowerShellForGitHub project.
39+
#>
40+
[CmdletBinding(
41+
SupportsShouldProcess,
42+
DefaultParametersetName='Elements')]
43+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
44+
param(
45+
[Parameter(ParameterSetName='Elements')]
46+
[string] $OwnerName,
47+
48+
[Parameter(ParameterSetName='Elements')]
49+
[string] $RepositoryName,
50+
51+
[Parameter(
52+
Mandatory,
53+
ParameterSetName='Uri')]
54+
[string] $Uri,
55+
56+
[string] $AccessToken,
57+
58+
[switch] $NoStatus
59+
)
60+
61+
Write-InvocationLog
62+
63+
$elements = Resolve-RepositoryElements
64+
$OwnerName = $elements.ownerName
65+
$RepositoryName = $elements.repositoryName
66+
67+
$telemetryProperties = @{
68+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
69+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
70+
}
71+
72+
$params = @{
73+
'UriFragment' = "repos/$OwnerName/$RepositoryName/assignees"
74+
'Description' = "Getting assignee list for $RepositoryName"
75+
'AccessToken' = $AccessToken
76+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
77+
'TelemetryProperties' = $telemetryProperties
78+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
79+
}
80+
81+
return Invoke-GHRestMethodMultipleResult @params
82+
}
83+
84+
function Test-GitHubAssignee
85+
{
86+
<#
87+
.DESCRIPTION
88+
Checks if a user has permission to be assigned to an issue in this repository. Returns a boolean.
89+
90+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
91+
92+
.PARAMETER OwnerName
93+
Owner of the repository.
94+
If not supplied here, the DefaultOwnerName configuration property value will be used.
95+
96+
.PARAMETER RepositoryName
97+
Name of the repository.
98+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
99+
100+
.PARAMETER Uri
101+
Uri for the repository.
102+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
103+
them individually.
104+
105+
.PARAMETER Assignee
106+
Username for the assignee
107+
108+
.PARAMETER AccessToken
109+
If provided, this will be used as the AccessToken for authentication with the
110+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
111+
112+
.PARAMETER NoStatus
113+
If this switch is specified, long-running commands will run on the main thread
114+
with no commandline status update. When not specified, those commands run in
115+
the background, enabling the command prompt to provide status information.
116+
If not supplied here, the DefaultNoStatus configuration property value will be used.
117+
118+
.OUTPUTS
119+
[bool] If the assignee can be assigned to issues in the repository.
120+
121+
.EXAMPLE
122+
Test-GitHubAssignee -OwnerName Powershell -RepositoryName PowerShellForGitHub -Assignee "LoginID123"
123+
124+
Checks if a user has permission to be assigned to an issue from the PowerShell\PowerShellForGitHub project.
125+
#>
126+
[CmdletBinding(
127+
SupportsShouldProcess,
128+
DefaultParametersetName='Elements')]
129+
[OutputType([bool])]
130+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
131+
param(
132+
[Parameter(ParameterSetName='Elements')]
133+
[string] $OwnerName,
134+
135+
[Parameter(ParameterSetName='Elements')]
136+
[string] $RepositoryName,
137+
138+
[Parameter(
139+
Mandatory,
140+
ParameterSetName='Uri')]
141+
[string] $Uri,
142+
143+
[string] $Assignee,
144+
145+
[string] $AccessToken,
146+
147+
[switch] $NoStatus
148+
)
149+
150+
Write-InvocationLog
151+
152+
$elements = Resolve-RepositoryElements
153+
$OwnerName = $elements.ownerName
154+
$RepositoryName = $elements.repositoryName
155+
156+
$telemetryProperties = @{
157+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
158+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
159+
'Asignee' = (Get-PiiSafeString -PlainText $Assignee)
160+
}
161+
162+
$params = @{
163+
'UriFragment' = "repos/$OwnerName/$RepositoryName/assignees/$Assignee"
164+
'Method' = 'Get'
165+
'Description' = "Checking permission for $Assignee for $RepositoryName"
166+
'AccessToken' = $AccessToken
167+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
168+
'TelemetryProperties' = $telemetryProperties
169+
'ExtendedResult'= $true
170+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
171+
}
172+
173+
try
174+
{
175+
$response = Invoke-GHRestMethod @params
176+
return $response.StatusCode -eq 204
177+
}
178+
catch
179+
{
180+
return $false
181+
}
182+
}
183+
184+
function New-GithubAssignee
185+
{
186+
<#
187+
.DESCRIPTION
188+
Adds a list of assignees to a Github Issue for the given repository.
189+
190+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
191+
192+
.PARAMETER OwnerName
193+
Owner of the repository.
194+
If not supplied here, the DefaultOwnerName configuration property value will be used.
195+
196+
.PARAMETER RepositoryName
197+
Name of the repository.
198+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
199+
200+
.PARAMETER Uri
201+
Uri for the repository.
202+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
203+
them individually.
204+
205+
.PARAMETER Issue
206+
Issue number to add the assignees to.
207+
208+
.PARAMETER Assignee
209+
Usernames of users to assign this issue to. NOTE: Only users with push access can add assignees to an issue.
210+
Assignees are silently ignored otherwise.
211+
212+
.PARAMETER AccessToken
213+
If provided, this will be used as the AccessToken for authentication with the
214+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
215+
216+
.PARAMETER NoStatus
217+
If this switch is specified, long-running commands will run on the main thread
218+
with no commandline status update. When not specified, those commands run in
219+
the background, enabling the command prompt to provide status information.
220+
If not supplied here, the DefaultNoStatus configuration property value will be used.
221+
222+
.EXAMPLE
223+
New-GithubAssignee -OwnerName Powershell -RepositoryName PowerShellForGitHub -Assignee $assignee
224+
225+
Lists the available assignees for issues from the PowerShell\PowerShellForGitHub project.
226+
#>
227+
[CmdletBinding(
228+
SupportsShouldProcess,
229+
DefaultParametersetName='Elements')]
230+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
231+
param(
232+
[Parameter(ParameterSetName='Elements')]
233+
[string] $OwnerName,
234+
235+
[Parameter(ParameterSetName='Elements')]
236+
[string] $RepositoryName,
237+
238+
[Parameter(
239+
Mandatory,
240+
ParameterSetName='Uri')]
241+
[string] $Uri,
242+
243+
[Parameter(Mandatory)]
244+
[int] $Issue,
245+
246+
[Parameter(Mandatory)]
247+
[ValidateCount(1, 10)]
248+
[string[]] $Assignee,
249+
250+
[string] $AccessToken,
251+
252+
[switch] $NoStatus
253+
)
254+
255+
Write-InvocationLog
256+
257+
$elements = Resolve-RepositoryElements
258+
$OwnerName = $elements.ownerName
259+
$RepositoryName = $elements.repositoryName
260+
261+
$telemetryProperties = @{
262+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
263+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
264+
'AssigneeCount' = $Assignee.Count
265+
'Issue' = (Get-PiiSafeString -PlainText $Issue)
266+
}
267+
268+
$hashBody = @{
269+
'assignees' = $Assignee
270+
}
271+
272+
$params = @{
273+
'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$Issue/assignees"
274+
'Body' = (ConvertTo-Json -InputObject $hashBody)
275+
'Method' = 'Post'
276+
'Description' = "Add assignees to issue $Issue for $RepositoryName"
277+
'AccessToken' = $AccessToken
278+
'AcceptHeader' = 'application/vnd.github.symmetra-preview+json'
279+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
280+
'TelemetryProperties' = $telemetryProperties
281+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
282+
}
283+
284+
return Invoke-GHRestMethod @params
285+
}
286+
287+
function Remove-GithubAssignee
288+
{
289+
<#
290+
.DESCRIPTION
291+
Removes an assignee from a Github issue.
292+
293+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
294+
295+
.PARAMETER OwnerName
296+
Owner of the repository.
297+
If not supplied here, the DefaultOwnerName configuration property value will be used.
298+
299+
.PARAMETER RepositoryName
300+
Name of the repository.
301+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
302+
303+
.PARAMETER Uri
304+
Uri for the repository.
305+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
306+
them individually.
307+
308+
.PARAMETER Issue
309+
Issue number to remove the assignees from.
310+
311+
.PARAMETER Assignee
312+
Usernames of assignees to remove from an issue. NOTE: Only users with push access can remove assignees from an issue. Assignees are silently ignored otherwise.
313+
314+
.PARAMETER AccessToken
315+
If provided, this will be used as the AccessToken for authentication with the
316+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
317+
318+
.PARAMETER NoStatus
319+
If this switch is specified, long-running commands will run on the main thread
320+
with no commandline status update. When not specified, those commands run in
321+
the background, enabling the command prompt to provide status information.
322+
If not supplied here, the DefaultNoStatus configuration property value will be used.
323+
324+
.EXAMPLE
325+
Remove-GithubAssignee -OwnerName Powershell -RepositoryName PowerShellForGitHub -Assignee $assignees
326+
327+
Lists the available assignees for issues from the PowerShell\PowerShellForGitHub project.
328+
#>
329+
[CmdletBinding(
330+
SupportsShouldProcess,
331+
DefaultParametersetName='Elements')]
332+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
333+
param(
334+
[Parameter(ParameterSetName='Elements')]
335+
[string] $OwnerName,
336+
337+
[Parameter(ParameterSetName='Elements')]
338+
[string] $RepositoryName,
339+
340+
[Parameter(
341+
Mandatory,
342+
ParameterSetName='Uri')]
343+
[string] $Uri,
344+
345+
[Parameter(Mandatory)]
346+
[int] $Issue,
347+
348+
[Parameter(Mandatory)]
349+
[string[]] $Assignee,
350+
351+
[string] $AccessToken,
352+
353+
[switch] $NoStatus
354+
)
355+
356+
Write-InvocationLog
357+
358+
$elements = Resolve-RepositoryElements
359+
$OwnerName = $elements.ownerName
360+
$RepositoryName = $elements.repositoryName
361+
362+
$telemetryProperties = @{
363+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
364+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
365+
'AssigneeCount' = $Assignee.Count
366+
'Issue' = (Get-PiiSafeString -PlainText $Issue)
367+
}
368+
369+
$hashBody = @{
370+
'assignees' = $Assignee
371+
}
372+
373+
$params = @{
374+
'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$Issue/assignees"
375+
'Body' = (ConvertTo-Json -InputObject $hashBody)
376+
'Method' = 'Delete'
377+
'Description' = "Removing assignees from issue $Issue for $RepositoryName"
378+
'AccessToken' = $AccessToken
379+
'AcceptHeader' = 'application/vnd.github.symmetra-preview+json'
380+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
381+
'TelemetryProperties' = $telemetryProperties
382+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
383+
}
384+
385+
return Invoke-GHRestMethod @params
386+
}

0 commit comments

Comments
 (0)