Skip to content

Commit 536b762

Browse files
authored
Add ability to rename a repository: Rename-GitHubRepository (#145)
This adds the ability to easily rename a repository via the new cmdlet `Rename-GitHubRepository`. Resolves #144.
1 parent 8ead733 commit 536b762

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

CONTRIBUTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ Thank you to all of our contributors, no matter how big or small the contributio
481481
- **[Darío Hereñú (@kant)](https://github.com/kant)**
482482
- **[@wikijm](https://github.com/wikijm)**
483483
- **[Przemysław Kłys (@PrzemyslawKlys)](https://github.com/PrzemyslawKlys)**
484+
- **[Matt Boren (@mtboren)](http://github.com/mtboren)**
484485

485486

486487

GitHubRepositories.ps1

+108
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,114 @@ function Get-GitHubRepository
461461
return Invoke-GHRestMethodMultipleResult @params
462462
}
463463

464+
function Rename-GitHubRepository
465+
{
466+
<#
467+
.SYNOPSIS
468+
Rename a GitHub repository
469+
470+
.DESCRIPTION
471+
Renames a GitHub repository with the new name provided.
472+
473+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
474+
475+
.PARAMETER OwnerName
476+
Owner of the repository.
477+
If not supplied here, the DefaultOwnerName configuration property value will be used.
478+
479+
.PARAMETER RepositoryName
480+
Name of the repository.
481+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
482+
483+
.PARAMETER Uri
484+
Uri for the repository to rename. You can supply this directly, or more easily by
485+
using Get-GitHubRepository to get the repository as you please, and then piping the result to this cmdlet
486+
487+
.PARAMETER NewName
488+
The new name to set for the given GitHub repository
489+
490+
.PARAMETER AccessToken
491+
If provided, this will be used as the AccessToken for authentication with the
492+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
493+
494+
.PARAMETER NoStatus
495+
If this switch is specified, long-running commands will run on the main thread
496+
with no commandline status update. When not specified, those commands run in
497+
the background, enabling the command prompt to provide status information.
498+
If not supplied here, the DefaultNoStatus configuration property value will be used.
499+
500+
.EXAMPLE
501+
Get-GitHubRepository -Owner octocat -RepositoryName hello-world | Rename-GitHubRepository -NewName hello-again-world
502+
Get the given 'hello-world' repo from the user 'octocat' and rename it to be https://github.com/octocat/hello-again-world.
503+
.EXAMPLE
504+
Get-GitHubRepository -Uri https://github.com/octocat/hello-world | Rename-GitHubRepository -NewName hello-again-world -Confirm:$false
505+
Get the repository at https://github.com/octocat/hello-world and then rename it https://github.com/octocat/hello-again-world. Will not prompt for confirmation, as -Confirm:$false was specified.
506+
507+
.EXAMPLE
508+
Rename-GitHubRepository -Uri https://github.com/octocat/hello-world -NewName hello-again-world
509+
Rename the repository at https://github.com/octocat/hello-world to https://github.com/octocat/hello-again-world.
510+
511+
.EXAMPLE
512+
New-GitHubRepositoryFork -Uri https://github.com/octocat/hello-world | Foreach-Object {$_ | Rename-GitHubRepository -NewName "$($_.name)_fork"}
513+
Fork the `hello-world` repository from the user 'octocat', and then rename the newly forked repository by appending '_fork'.
514+
#>
515+
[CmdletBinding(
516+
SupportsShouldProcess,
517+
DefaultParametersetName='Uri',
518+
ConfirmImpact="High")]
519+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
520+
param(
521+
[Parameter(Mandatory=$true, ParameterSetName='Elements')]
522+
[string] $OwnerName,
523+
524+
[Parameter(Mandatory=$true, ParameterSetName='Elements')]
525+
[string] $RepositoryName,
526+
527+
[Parameter(
528+
Mandatory,
529+
ValueFromPipelineByPropertyName,
530+
ParameterSetName='Uri')]
531+
[Alias("html_url")]
532+
[string] $Uri,
533+
534+
[parameter(Mandatory)][String]$NewName,
535+
536+
[string] $AccessToken,
537+
538+
[switch] $NoStatus
539+
)
540+
541+
process
542+
{
543+
$repositoryInfoForDisplayMessage = if ($PSCmdlet.ParameterSetName -eq "Uri") { $Uri } else { $OwnerName, $RepositoryName -join "/" }
544+
if ($PSCmdlet.ShouldProcess($strRepositoryInfoForDisplayMessage, "Rename repository to '$NewName'"))
545+
{
546+
Write-InvocationLog -Invocation $MyInvocation
547+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters
548+
$OwnerName = $elements.ownerName
549+
$RepositoryName = $elements.repositoryName
550+
551+
$telemetryProperties = @{
552+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
553+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
554+
}
555+
556+
$params = @{
557+
'UriFragment' = "repos/$OwnerName/$RepositoryName"
558+
'Method' = 'Patch'
559+
Body = ConvertTo-Json -InputObject @{name = $NewName}
560+
'Description' = "Renaming repository at '$repositoryInfoForDisplayMessage' to '$NewName'"
561+
'AccessToken' = $AccessToken
562+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
563+
'TelemetryProperties' = $telemetryProperties
564+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
565+
}
566+
567+
return Invoke-GHRestMethod @params
568+
}
569+
}
570+
}
571+
464572
function Update-GitHubRepository
465573
{
466574
<#

PowerShellForGitHub.psd1

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
'Remove-GitHubLabel',
103103
'Remove-GitHubMilestone',
104104
'Remove-GitHubRepository',
105+
'Rename-GitHubRepository',
105106
'Reset-GitHubConfiguration',
106107
'Restore-GitHubConfiguration',
107108
'Set-GitHubAuthentication',

Tests/GitHubRepositories.tests.ps1

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
<#
5+
.Synopsis
6+
Tests for GitHubRepositories.ps1 module
7+
.Description
8+
Many cmdlets are indirectly tested in the course of other tests (New-GitHubRepository, Remove-GitHubRepository), and may not have explicit tests here
9+
#>
10+
11+
# This is common test code setup logic for all Pester test files
12+
$moduleRootPath = Split-Path -Path $PSScriptRoot -Parent
13+
. (Join-Path -Path $moduleRootPath -ChildPath 'Tests\Common.ps1')
14+
15+
try
16+
{
17+
Describe 'Modifying repositories' {
18+
19+
Context -Name 'For renaming a repository' -Fixture {
20+
BeforeEach -Scriptblock {
21+
$repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit
22+
$suffixToAddToRepo = "_renamed"
23+
$newRepoName = "$($repo.Name)$suffixToAddToRepo"
24+
Write-Verbose "New repo name shall be: '$newRepoName'"
25+
}
26+
It "Should have the expected new repository name - by URI" {
27+
$renamedRepo = $repo | Rename-GitHubRepository -NewName $newRepoName -Confirm:$false
28+
$renamedRepo.Name | Should be $newRepoName
29+
}
30+
31+
It "Should have the expected new repository name - by Elements" {
32+
$renamedRepo = Rename-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name -NewName $newRepoName -Confirm:$false
33+
$renamedRepo.Name | Should be $newRepoName
34+
}
35+
## cleanup temp testing repository
36+
AfterEach -Scriptblock {
37+
## variables from BeforeEach scriptblock are accessible here, but not variables from It scriptblocks, so need to make URI (instead of being able to use $renamedRepo variable from It scriptblock)
38+
Remove-GitHubRepository -Uri "$($repo.svn_url)$suffixToAddToRepo" -Verbose
39+
}
40+
}
41+
}
42+
}
43+
finally
44+
{
45+
if (Test-Path -Path $script:originalConfigFile -PathType Leaf)
46+
{
47+
# Restore the user's configuration to its pre-test state
48+
Restore-GitHubConfiguration -Path $script:originalConfigFile
49+
$script:originalConfigFile = $null
50+
}
51+
}

0 commit comments

Comments
 (0)