Skip to content

Commit 1076239

Browse files
committed
Add support for querying and creating forks
Pull Request #31 was adding support for getting forks using the old module structure. I've gone ahead and added the requested functionality using the new module layout, and additionally added support for creating new forks, along with tests for both.
1 parent 336f69b commit 1076239

6 files changed

+364
-6
lines changed

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
# PowerShellForGitHub PowerShell Module
22
## Changelog
33

4+
## [0.0.2](https://github.com/PowerShell/PowerShellForGitHub/tree/0.3.0) - (2018/11/13)
5+
### Features:
6+
+ Added support for querying forks and creating new ones.
7+
8+
### Fixes:
9+
* Will only perform a retry when receiving a `202` response on a `GET` request. Previously, it would
10+
retry regardless of the method of the request.
11+
12+
More Info: [[pr]](https://github.com/PowerShell/PowerShellForGitHub/pull/41) | [[cl]](https://github.com/PowerShell/PowerHellForGitHub/commit/TODO)
13+
14+
Author: [**@HowardWolosky**](https://github.com/HowardWolosky)
15+
16+
------
17+
418
## [0.0.2](https://github.com/PowerShell/PowerShellForGitHub/tree/0.2.0) - (2018/11/13)
519
### Features:
620
+ Significant restructing and refactoring of entire module to make future expansion easier.

GitHubCore.ps1

+12-5
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,23 @@ function Invoke-GHRestMethod
328328
if ($result.StatusCode -eq $resultNotReadyStatusCode)
329329
{
330330
$retryDelaySeconds = Get-GitHubConfiguration -Name RetryDelaySeconds
331-
if ($retryDelaySeconds -gt 0)
331+
332+
if ($Method -ne 'Get')
332333
{
333-
Write-Log -Message "The server has indicated that the result is not yet ready (received status code of [$($result.StatusCode)]). Will retry in [$retryDelaySeconds] seconds." -Level Warning
334-
Start-Sleep -Seconds ($retryDelaySeconds)
335-
return (Invoke-GHRestMethod @PSBoundParameters)
334+
# We only want to do our retry logic for GET requests...
335+
# We don't want to repeat PUT/PATCH/POST/DELETE.
336+
Write-Log -Message "The server has indicated that the result is not yet ready (received status code of [$($result.StatusCode)])." -Level Warning
336337
}
337-
else
338+
elseif ($retryDelaySeconds -le 0)
338339
{
339340
Write-Log -Message "The server has indicated that the result is not yet ready (received status code of [$($result.StatusCode)]), however the module is currently configured to not retry in this scenario (RetryDelaySeconds is set to 0). Please try this command again later." -Level Warning
340341
}
342+
else
343+
{
344+
Write-Log -Message "The server has indicated that the result is not yet ready (received status code of [$($result.StatusCode)]). Will retry in [$retryDelaySeconds] seconds." -Level Warning
345+
Start-Sleep -Seconds ($retryDelaySeconds)
346+
return (Invoke-GHRestMethod @PSBoundParameters)
347+
}
341348
}
342349

343350
if ($ExtendedResult)

GitHubRepositoryForks.ps1

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
function Get-GitHubRepositoryFork
5+
{
6+
<#
7+
.SYNOPSIS
8+
Gets the list of forks of the specified repository on GitHub.
9+
10+
.DESCRIPTION
11+
Gets the list of forks of the specified repository on GitHub.
12+
13+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
14+
15+
.PARAMETER OwnerName
16+
Owner of the repository.
17+
If not supplied here, the DefaultOwnerName configuration property value will be used.
18+
19+
.PARAMETER RepositoryName
20+
Name of the repository.
21+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
22+
23+
.PARAMETER Uri
24+
Uri for the repository.
25+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
26+
them individually.
27+
28+
.PARAMETER Sort
29+
The sort order for results.
30+
31+
.PARAMETER AccessToken
32+
If provided, this will be used as the AccessToken for authentication with the
33+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
34+
35+
.PARAMETER NoStatus
36+
If this switch is specified, long-running commands will run on the main thread
37+
with no commandline status update. When not specified, those commands run in
38+
the background, enabling the command prompt to provide status information.
39+
If not supplied here, the DefaultNoStatus configuration property value will be used.
40+
41+
.EXAMPLE
42+
Get-GitHubRepositoryFork -OwnerName PowerShell -RepositoryName PowerShellForGitHub
43+
44+
Gets all of the forks for the PowerShell\PowerShellForGitHub repository.
45+
#>
46+
[CmdletBinding(
47+
SupportsShouldProcess,
48+
DefaultParametersetName='Elements')]
49+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
50+
param(
51+
[Parameter(ParameterSetName='Elements')]
52+
[string] $OwnerName,
53+
54+
[Parameter(ParameterSetName='Elements')]
55+
[string] $RepositoryName,
56+
57+
[Parameter(
58+
Mandatory,
59+
ParameterSetName='Uri')]
60+
[string] $Uri,
61+
62+
[ValidateSet('newest', 'oldest', 'stargazers')]
63+
[string] $Sort = 'newest',
64+
65+
[string] $AccessToken,
66+
67+
[switch] $NoStatus
68+
)
69+
70+
Write-InvocationLog -Invocation $MyInvocation
71+
72+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation
73+
$OwnerName = $elements.ownerName
74+
$RepositoryName = $elements.repositoryName
75+
76+
$telemetryProperties = @{
77+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
78+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
79+
'Sort' = $Sort
80+
}
81+
82+
$getParams = @(
83+
"sort=$Sort"
84+
)
85+
86+
$params = @{
87+
'UriFragment' = "repos/$OwnerName/$RepositoryName/forks"
88+
'Description' = "Getting all forks of $RepositoryName"
89+
'AccessToken' = $AccessToken
90+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
91+
'TelemetryProperties' = $telemetryProperties
92+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
93+
}
94+
95+
return Invoke-GHRestMethodMultipleResult @params
96+
}
97+
98+
function New-GitHubRepositoryFork
99+
{
100+
<#
101+
.SYNOPSIS
102+
Creates a new fork of a repository on GitHub.
103+
104+
.DESCRIPTION
105+
Creates a new fork of a repository on GitHub.
106+
107+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
108+
109+
.PARAMETER OwnerName
110+
Owner of the repository.
111+
If not supplied here, the DefaultOwnerName configuration property value will be used.
112+
113+
.PARAMETER RepositoryName
114+
Name of the repository.
115+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
116+
117+
.PARAMETER Uri
118+
Uri for the repository.
119+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
120+
them individually.
121+
122+
.PARAMETER OrganizationName
123+
Name of the organization that the new repository should be created under.
124+
If not specified, will be created under the current authenticated user's account.
125+
126+
.PARAMETER AccessToken
127+
If provided, this will be used as the AccessToken for authentication with the
128+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
129+
130+
.PARAMETER NoStatus
131+
If this switch is specified, long-running commands will run on the main thread
132+
with no commandline status update. When not specified, those commands run in
133+
the background, enabling the command prompt to provide status information.
134+
If not supplied here, the DefaultNoStatus configuration property value will be used.
135+
136+
.EXAMPLE
137+
New-GitHubRepositoryFork -OwnerName PowerShell -RepositoryName PowerShellForGitHub
138+
139+
Creates a fork of this repository under the current authenticated user's account.
140+
141+
.EXAMPLE
142+
New-GitHubRepositoryFork -OwnerName PowerShell -RepositoryName PowerShellForGitHub -OrganizationName OctoLabs
143+
144+
Creates a fork of this repository under the OctoLabs organization.
145+
#>
146+
[CmdletBinding(
147+
SupportsShouldProcess,
148+
DefaultParametersetName='Elements')]
149+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
150+
param(
151+
[Parameter(ParameterSetName='Elements')]
152+
[string] $OwnerName,
153+
154+
[Parameter(ParameterSetName='Elements')]
155+
[string] $RepositoryName,
156+
157+
[Parameter(
158+
Mandatory,
159+
ParameterSetName='Uri')]
160+
[string] $Uri,
161+
162+
[string] $OrganizationName,
163+
164+
[string] $AccessToken,
165+
166+
[switch] $NoStatus
167+
)
168+
169+
Write-InvocationLog -Invocation $MyInvocation
170+
171+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation
172+
$OwnerName = $elements.ownerName
173+
$RepositoryName = $elements.repositoryName
174+
175+
$telemetryProperties = @{
176+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
177+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
178+
}
179+
180+
$getParams = @()
181+
if ($PSBoundParameters.ContainsKey('OrganizationName') -and
182+
(-not [String]::IsNullOrEmpty($OrganizationName)))
183+
{
184+
$telemetryProperties['OrganizationName'] = Get-PiiSafeString -PlainText $OrganizationName
185+
$getParams += "organization=$OrganizationName"
186+
}
187+
188+
$params = @{
189+
'UriFragment' = "repos/$OwnerName/$RepositoryName/forks`?" + ($getParams -join '&')
190+
'Method' = 'Post'
191+
'Description' = "Creating fork of $RepositoryName"
192+
'AccessToken' = $AccessToken
193+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
194+
'TelemetryProperties' = $telemetryProperties
195+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
196+
}
197+
198+
$result = Invoke-GHRestMethod @params
199+
200+
Write-Log -Message 'Forking a repository happens asynchronously. You may have to wait a short period of time (up to 5 minutes) before you can access the git objects.' -Level Warning
201+
return $result
202+
}

PowerShellForGitHub.psd1

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
CompanyName = 'Microsoft Corporation'
88
Copyright = 'Copyright (C) Microsoft Corporation. All rights reserved.'
99

10-
ModuleVersion = '0.2.0'
10+
ModuleVersion = '0.3.0'
1111
Description = 'PowerShell wrapper for GitHub API'
1212

1313
# Script module or binary module file associated with this manifest.
@@ -28,6 +28,7 @@
2828
'GitHubOrganizations.ps1',
2929
'GitHubPullRequests.ps1',
3030
'GitHubRepositories.ps1',
31+
'GitHubRepositoryForks.ps1',
3132
'GitHubTeams.ps1',
3233
'GitHubUsers.ps1',
3334
'NugetTools.ps1',
@@ -56,6 +57,7 @@
5657
'Get-GitHubRepositoryBranch',
5758
'Get-GitHubRepositoryCollaborator',
5859
'Get-GitHubRepositoryContributor',
60+
'Get-GitHubRepositoryFork',
5961
'Get-GitHubRepositoryLanguage',
6062
'Get-GitHubRepositoryTag',
6163
'Get-GitHubRepositoryTopic',
@@ -72,6 +74,7 @@
7274
'New-GitHubIssue',
7375
'New-GitHubLabel',
7476
'New-GitHubRepository',
77+
'New-GitHubRepositoryFork',
7578
'Remove-GitHubLabel',
7679
'Remove-GitHubRepository',
7780
'Reset-GitHubConfiguration',

0 commit comments

Comments
 (0)