From 3b8bb4c3e509694f575be04eb80c858c665d635d Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Thu, 4 Jun 2020 20:52:48 +0100 Subject: [PATCH 01/12] New-GitHubRepositoryFromTemplate --- GitHubRepositories.ps1 | 109 +++++++++++++++++++++++++++++++++++++++ PowerShellForGitHub.psd1 | 1 + 2 files changed, 110 insertions(+) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index c59e33e5..fe3f3993 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -227,6 +227,115 @@ filter New-GitHubRepository return (Invoke-GHRestMethod @params | Add-GitHubRepositoryAdditionalProperties) } +function New-GitHubRepositoryFromTemplate +{ +<# + .SYNOPSIS + Creates a new repository on GitHub from a template repository. + + .DESCRIPTION + Creates a new repository on GitHub from a template repository. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER RepositoryName + Name of the repository to be created. + + .PARAMETER TemplateOwnerName + Owner of the template repository. + + .PARAMETER TeamplateRepositoryName + Name of the template repository. + + .PARAMETER OwnerName + Owner of the repository to be created. If not specified, the DefaultOwnerName configuration + property value will be used. + + .PARAMETER Description + A short description of the repository. + + .PARAMETER Private + By default, this repository will created Public. Specify this to create a private + repository. + + .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 + New-GitHubRepositoryFromTemplate -RepositoryName MyNewRepo -OwnerName Me -TemplateOwnerName MyOrg -TemplateRepositoryName MyTemplateRepo + + Creates a new GitHub repository from the specified template repository +#> + [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)] + [ValidateNotNullOrEmpty()] + [string] $RepositoryName, + + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [string] $TemplateOwnerName, + + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [string] $TemplateRepositoryName, + + [string] $OwnerName, + + [string] $Description, + + [switch] $Private, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + + $telemetryProperties = @{ + RepositoryName = (Get-PiiSafeString -PlainText $RepositoryName) + OwnerName = (Get-PiiSafeString -PlainText $OwnerName) + } + + $uriFragment = "repos/$TemplateOwnerName/$TemplateRepositoryName/generate" + + $hashBody = @{ + owner = $OwnerName + name = $RepositoryName + } + + if ($PSBoundParameters.ContainsKey('Description')) { $hashBody['description'] = $Description } + if ($PSBoundParameters.ContainsKey('Private')) { $hashBody['private'] = $Private.ToBool() } + + $params = @{ + 'UriFragment' = $uriFragment + 'Body' = (ConvertTo-Json -InputObject $hashBody) + 'Method' = 'Post' + 'Description' = "Creating $RepositoryName from Template" + 'AcceptHeader' = $script:baptisteAcceptHeader + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue ` + -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} + filter Remove-GitHubRepository { <# diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index c7f51c82..a22cc69b 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -110,6 +110,7 @@ 'New-GitHubProjectColumn', 'New-GitHubPullRequest', 'New-GitHubRepository', + 'New-GitHubRepositoryFromTemplate', 'New-GitHubRepositoryFork', 'Remove-GitHubAssignee', 'Remove-GitHubIssueComment', From d4e432f03ce930b1ff176fe272a3ec69e31b9881 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Thu, 4 Jun 2020 21:30:51 +0100 Subject: [PATCH 02/12] Add Repositories Usage --- USAGE.md | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 59cc3954..ec42724e 100644 --- a/USAGE.md +++ b/USAGE.md @@ -29,6 +29,11 @@ * [Updating the current authenticated user](#updating-the-current-authenticated-user) * [Getting any user](#getting-any-user) * [Getting all users](#getting-all-users) + * [Repositories](#Repositories]) + * [Create a repository](#Create-a-repository) + * [Create a repository in an organization](#Create-a-repository-in-an-organization) + * [Create a repository in an organization and grant access to a team](#Create-a-repository-in-an-organization-and-grant-access-to-a-team) + * [Create a repository from a template repository](#Create-a-repository-from-a-template-repository) * [Forks](#forks) * [Get all the forks for a repository](#get-all-the-forks-for-a-repository) * [Create a new fork](#create-a-new-fork) @@ -97,7 +102,9 @@ The logging is affected by configuration properties (which can be checked with different log file for each PowerShell process. An easy way to view the filtered entries for a session is (replacing `PID` with the PID that you are interested in): - Get-Content -Path -Encoding UTF8 | Where { $_ -like '*[[]PID[]]*' } +```powershell +Get-Content -Path -Encoding UTF8 | Where { $_ -like '*[[]PID[]]*' } +``` ---------- @@ -414,6 +421,35 @@ Get-GitHubUser ---------- +### Repositories + +#### Create a repository + +```powershell +New-GitHubRepository -RepositoryName TestRepo +``` + +#### Create a repository in an organization + +```powershell +New-GitHubRepository -RepositoryName TestRepo -OrganizationName MyOrg +``` + +#### Create a repository in an organization and grant access to a team + +```powershell +$myTeam = Get-GitHubTeam -OrganizationName MyOrg | Where-Object -Property name -eq MyTeam +New-GitHubRepository -RepositoryName TestRepo -OrganizationName MyOrg -TeamId $myTeam.id +``` + +#### Create a repository from a template repository + +```powershell +New-GitHubRepositoryFromTemplate -RepositoryName MyNewRepo -OwnerName MyOrg -TemplateOwnerName MyOrg -TemplateRepositoryName MyTemplateRepo +``` + +---------- + ### Forks #### Get all the forks for a repository From cd2b4d4ccb47c8336cab3a885cf1dd6ae0fbe9f4 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Thu, 4 Jun 2020 21:33:00 +0100 Subject: [PATCH 03/12] Update contributors --- CONTRIBUTING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1f690f71..79b006d3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -553,6 +553,7 @@ Thank you to all of our contributors, no matter how big or small the contributio - **[Jess Pomfret (@jpomfret)](https://github.com/jpomfret)** - **[Giuseppe Campanelli (@themilanfan)](https://github.com/themilanfan)** - **[Christoph Bergmeister (@bergmeister)](https://github.com/bergmeister)** +- **[Simon Heather (@X-Guardian)](https://github.com/X-Guardian)** ---------- From 8b9dcae6c3bab98bac6ad13a7a599df25f72b9ed Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Fri, 5 Jun 2020 07:50:45 +0100 Subject: [PATCH 04/12] Add tests --- GitHubRepositories.ps1 | 2 +- Tests/GitHubRepositories.tests.ps1 | 82 ++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index fe3f3993..1513d71f 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -244,7 +244,7 @@ function New-GitHubRepositoryFromTemplate .PARAMETER TemplateOwnerName Owner of the template repository. - .PARAMETER TeamplateRepositoryName + .PARAMETER TemplateRepositoryName Name of the template repository. .PARAMETER OwnerName diff --git a/Tests/GitHubRepositories.tests.ps1 b/Tests/GitHubRepositories.tests.ps1 index 3dc1bf81..fe5767d6 100644 --- a/Tests/GitHubRepositories.tests.ps1 +++ b/Tests/GitHubRepositories.tests.ps1 @@ -290,6 +290,88 @@ try } } + Describe 'GitHubRepositories\New-GitHubRepositoryFromTemplate' { + BeforeAll -ScriptBlock { + $templateRepoName = ([Guid]::NewGuid().Guid) + $testGitIgnoreTemplate=(Get-GitHubGitIgnore)[0] + $testLicenseTemplate=(Get-GitHubLicense)[0].key + + $newGitHubRepositoryParms = @{ + RepositoryName = $templateRepoName + Description = $defaultRepoDesc + HomePage = $defaultRepoHomePage + NoIssues = $true + NoProjects = $true + NoWiki = $true + DisallowSquashMerge = $true + DisallowMergeCommit = $true + DisallowRebaseMerge = $false + DeleteBranchOnMerge = $true + GitIgnoreTemplate = $testGitIgnoreTemplate + LicenseTemplate = $testLicenseTemplate + IsTemplate = $true + } + $templateRepo = New-GitHubRepository @newGitHubRepositoryParms + } + + Context -Name 'When creating a public repository from a template' -Fixture { + BeforeAll -ScriptBlock { + $repoName = ([Guid]::NewGuid().Guid) + $newRepoDesc = 'New Repo Description' + $newGitHubRepositoryFromTemplateParms = @{ + RepositoryName = $repoName + OwnerName = $script:ownerName + TemplateOwnerName = $templateRepoName.owner.login + TemplateRepositoryName = $templateRepoName + Description = $newRepoDesc + } + $repo = New-GitHubRepositoryFromTemplate @newGitHubRepositoryFromTemplateParms + } + + It 'Should return an object of the correct type' { + $repo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $repo.name | Should -Be $repoName + $repo.private | Should -BeFalse + $repo.owner.login | Should -Be $script:ownerName + $repo.description | Should -Be $newRepoDesc + $repo.homepage | Should -Be $templateRepo.homepage + $repo.has_issues | Should -BeFalse + $repo.has_projects | Should -BeFalse + $repo.has_Wiki | Should -BeFalse + $repo.allow_squash_merge | Should -BeFalse + $repo.allow_merge_commit | Should -BeFalse + $repo.allow_rebase_merge | Should -BeTrue + $repo.delete_branch_on_merge | Should -BeTrue + $repo.is_template | Should -BeFalse + } + + It 'Should have created a .gitignore file' { + { Get-GitHubContent -Uri $repo.svn_url -Path '.gitignore' } | Should -Not -Throw + } + + It 'Should have created a LICENSE file' { + { Get-GitHubContent -Uri $repo.svn_url -Path 'LICENSE' } | Should -Not -Throw + } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } + } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $templateRepo.svn_url -Confirm:$false + } + } + } + Describe 'Getting repositories' { Context 'For authenticated user' { BeforeAll -Scriptblock { From 2a2221609b379faa1b269fd179e8e91aba3646f2 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 7 Jun 2020 14:26:55 +0100 Subject: [PATCH 05/12] Add PositionalBinding --- GitHubRepositories.ps1 | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index 1513d71f..d077cf00 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -251,7 +251,7 @@ function New-GitHubRepositoryFromTemplate Owner of the repository to be created. If not specified, the DefaultOwnerName configuration property value will be used. - .PARAMETER Description + .PARAMETER Description A short description of the repository. .PARAMETER Private @@ -269,23 +269,33 @@ function New-GitHubRepositoryFromTemplate If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - New-GitHubRepositoryFromTemplate -RepositoryName MyNewRepo -OwnerName Me -TemplateOwnerName MyOrg -TemplateRepositoryName MyTemplateRepo + New-GitHubRepositoryFromTemplate -RepositoryName MyNewRepo -TemplateOwnerName MyOrg -TemplateRepositoryName MyTemplateRepo -OwnerName Me Creates a new GitHub repository from the specified template repository #> - [CmdletBinding(SupportsShouldProcess)] + [CmdletBinding( + SupportsShouldProcess, + PositionalBinding = $false + )] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", - Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + Justification="Methods called within here make use of PSShouldProcess, and the switch is + passed on to them inherently.")] param( - [Parameter(Mandatory)] + [Parameter( + Mandatory, + Position = 1)] [ValidateNotNullOrEmpty()] [string] $RepositoryName, - [Parameter(Mandatory)] + [Parameter( + Mandatory, + Position = 2)] [ValidateNotNullOrEmpty()] [string] $TemplateOwnerName, - [Parameter(Mandatory)] + [Parameter( + Mandatory, + Position = 3)] [ValidateNotNullOrEmpty()] [string] $TemplateRepositoryName, From 66cf239c2b5b5396f32854fe331c1bbe5355c106 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Tue, 9 Jun 2020 23:53:23 +0100 Subject: [PATCH 06/12] Update tests --- Tests/GitHubRepositories.tests.ps1 | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/Tests/GitHubRepositories.tests.ps1 b/Tests/GitHubRepositories.tests.ps1 index fe5767d6..2ad8b2e1 100644 --- a/Tests/GitHubRepositories.tests.ps1 +++ b/Tests/GitHubRepositories.tests.ps1 @@ -299,14 +299,6 @@ try $newGitHubRepositoryParms = @{ RepositoryName = $templateRepoName Description = $defaultRepoDesc - HomePage = $defaultRepoHomePage - NoIssues = $true - NoProjects = $true - NoWiki = $true - DisallowSquashMerge = $true - DisallowMergeCommit = $true - DisallowRebaseMerge = $false - DeleteBranchOnMerge = $true GitIgnoreTemplate = $testGitIgnoreTemplate LicenseTemplate = $testLicenseTemplate IsTemplate = $true @@ -321,7 +313,7 @@ try $newGitHubRepositoryFromTemplateParms = @{ RepositoryName = $repoName OwnerName = $script:ownerName - TemplateOwnerName = $templateRepoName.owner.login + TemplateOwnerName = $templateRepo.owner.login TemplateRepositoryName = $templateRepoName Description = $newRepoDesc } @@ -337,14 +329,6 @@ try $repo.private | Should -BeFalse $repo.owner.login | Should -Be $script:ownerName $repo.description | Should -Be $newRepoDesc - $repo.homepage | Should -Be $templateRepo.homepage - $repo.has_issues | Should -BeFalse - $repo.has_projects | Should -BeFalse - $repo.has_Wiki | Should -BeFalse - $repo.allow_squash_merge | Should -BeFalse - $repo.allow_merge_commit | Should -BeFalse - $repo.allow_rebase_merge | Should -BeTrue - $repo.delete_branch_on_merge | Should -BeTrue $repo.is_template | Should -BeFalse } From f0d7482bf688b27c6a42b76746cf61bfb3e62c8a Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Wed, 10 Jun 2020 20:58:29 +0100 Subject: [PATCH 07/12] Remove redundandant Invocation parameter --- GitHubRepositories.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index d077cf00..5e9731b6 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -310,7 +310,7 @@ function New-GitHubRepositoryFromTemplate [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters $OwnerName = $elements.ownerName From d7901d07a6f77a88e7b0806b2f3b81123c2d9616 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Fri, 19 Jun 2020 20:18:52 +0100 Subject: [PATCH 08/12] Fix review comments and add Pipeline support --- GitHubRepositories.ps1 | 88 +++++++++++++++++++++++------- Tests/GitHubRepositories.tests.ps1 | 43 +++++++++------ 2 files changed, 94 insertions(+), 37 deletions(-) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index 5e9731b6..04e24622 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -227,7 +227,7 @@ filter New-GitHubRepository return (Invoke-GHRestMethod @params | Add-GitHubRepositoryAdditionalProperties) } -function New-GitHubRepositoryFromTemplate +filter New-GitHubRepositoryFromTemplate { <# .SYNOPSIS @@ -238,18 +238,21 @@ function New-GitHubRepositoryFromTemplate The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub - .PARAMETER RepositoryName - Name of the repository to be created. - - .PARAMETER TemplateOwnerName + .PARAMETER OwnerName Owner of the template repository. + If no value is specified, the DefaultOwnerName configuration property value will be used, + and if there is no configuration value defined, the current authenticated user will be used. - .PARAMETER TemplateRepositoryName + .PARAMETER RepositoryName Name of the template repository. - .PARAMETER OwnerName - Owner of the repository to be created. If not specified, the DefaultOwnerName configuration - property value will be used. + .PARAMETER TargetRepositoryName + Name of the repository to be created. + + .PARAMETER TargetOwnerName + The organization or person who will own the new repository. + To create a new repository in an organization, the authenticated user must be a member + of the specified organization. .PARAMETER Description A short description of the repository. @@ -268,37 +271,78 @@ function New-GitHubRepositoryFromTemplate the background, enabling the command prompt to provide status information. If not supplied here, the DefaultNoStatus configuration property value will be used. + .INPUTS + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Release + GitHub.Repository + + .OUTPUTS + GitHub.Repository + + .NOTES + The authenticated user must own or be a member of an organization that owns the repository. + + To check if a repository is available to use as a template, call `Get-GitHubRepository` on the + repository in question and check that the is_template property is $true. + .EXAMPLE - New-GitHubRepositoryFromTemplate -RepositoryName MyNewRepo -TemplateOwnerName MyOrg -TemplateRepositoryName MyTemplateRepo -OwnerName Me + New-GitHubRepositoryFromTemplate -OwnerName MyOrg -RepositoryName MyTemplateRepo -TargetRepositoryName MyNewRepo -TargetOwnerName Me - Creates a new GitHub repository from the specified template repository + Creates a new GitHub repository from the specified template repository. + + .EXAMPLE + $repo = Get-GitHubRepository -OwnerName MyOrg -RepositoryName MyTemplateRepo + $repo | New-GitHubRepositoryFromTemplate -TargetRepositoryName MyNewRepo -TargetOwnerName Me + + You can also pipe in a repo that was returned from a previous command. #> [CmdletBinding( SupportsShouldProcess, PositionalBinding = $false )] + [OutputType({$script:GitHubRepositoryTypeName})] [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, - Position = 1)] + Position = 1, + ParameterSetName='Elements')] [ValidateNotNullOrEmpty()] [string] $RepositoryName, [Parameter( Mandatory, - Position = 2)] - [ValidateNotNullOrEmpty()] - [string] $TemplateOwnerName, + Position = 2, + ValueFromPipelineByPropertyName, + ParameterSetName='Uri')] + [Alias('RepositoryUrl')] + [string] $Uri, [Parameter( Mandatory, Position = 3)] [ValidateNotNullOrEmpty()] - [string] $TemplateRepositoryName, + [string] $TargetOwnerName, + [Parameter( + Mandatory, + Position = 4)] + [ValidateNotNullOrEmpty()] + [string] $TargetRepositoryName, + + [Parameter(ParameterSetName='Elements')] [string] $OwnerName, [string] $Description, @@ -318,13 +362,15 @@ function New-GitHubRepositoryFromTemplate $telemetryProperties = @{ RepositoryName = (Get-PiiSafeString -PlainText $RepositoryName) OwnerName = (Get-PiiSafeString -PlainText $OwnerName) + TargetRepositoryName = (Get-PiiSafeString -PlainText $TargetRepositoryName) + TargetOwnerName = (Get-PiiSafeString -PlainText $TargetOwnerName) } - $uriFragment = "repos/$TemplateOwnerName/$TemplateRepositoryName/generate" + $uriFragment = "repos/$OwnerName/$RepositoryName/generate" $hashBody = @{ - owner = $OwnerName - name = $RepositoryName + owner = $TargetOwnerName + name = $TargetRepositoryName } if ($PSBoundParameters.ContainsKey('Description')) { $hashBody['description'] = $Description } @@ -334,7 +380,7 @@ function New-GitHubRepositoryFromTemplate 'UriFragment' = $uriFragment 'Body' = (ConvertTo-Json -InputObject $hashBody) 'Method' = 'Post' - 'Description' = "Creating $RepositoryName from Template" + 'Description' = "Creating $TargetName from Template" 'AcceptHeader' = $script:baptisteAcceptHeader 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name @@ -343,7 +389,7 @@ function New-GitHubRepositoryFromTemplate -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) } - return Invoke-GHRestMethod @params + return (Invoke-GHRestMethod @params | Add-GitHubRepositoryAdditionalProperties) } filter Remove-GitHubRepository diff --git a/Tests/GitHubRepositories.tests.ps1 b/Tests/GitHubRepositories.tests.ps1 index 2ad8b2e1..c70748ef 100644 --- a/Tests/GitHubRepositories.tests.ps1 +++ b/Tests/GitHubRepositories.tests.ps1 @@ -11,6 +11,8 @@ Justification='Suppress false positives in Pester code blocks')] param() +Set-StrictMode -Version 1.0 + # This is common test code setup logic for all Pester test files $moduleRootPath = Split-Path -Path $PSScriptRoot -Parent . (Join-Path -Path $moduleRootPath -ChildPath 'Tests\Common.ps1') @@ -291,10 +293,11 @@ try } Describe 'GitHubRepositories\New-GitHubRepositoryFromTemplate' { - BeforeAll -ScriptBlock { + BeforeAll { $templateRepoName = ([Guid]::NewGuid().Guid) - $testGitIgnoreTemplate=(Get-GitHubGitIgnore)[0] - $testLicenseTemplate=(Get-GitHubLicense)[0].key + $ownerName = $script:ownerName + $testGitIgnoreTemplate = (Get-GitHubGitIgnore)[0] + $testLicenseTemplate = (Get-GitHubLicense)[0].key $newGitHubRepositoryParms = @{ RepositoryName = $templateRepoName @@ -306,30 +309,38 @@ try $templateRepo = New-GitHubRepository @newGitHubRepositoryParms } - Context -Name 'When creating a public repository from a template' -Fixture { - BeforeAll -ScriptBlock { + Context 'When creating a public repository from a template' { + BeforeAll { $repoName = ([Guid]::NewGuid().Guid) $newRepoDesc = 'New Repo Description' $newGitHubRepositoryFromTemplateParms = @{ - RepositoryName = $repoName - OwnerName = $script:ownerName - TemplateOwnerName = $templateRepo.owner.login - TemplateRepositoryName = $templateRepoName + RepositoryName = $templateRepoName + OwnerName = $templateRepo.owner.login + TargetOwnerName = $ownerName + TargetRepositoryName = $repoName Description = $newRepoDesc } $repo = New-GitHubRepositoryFromTemplate @newGitHubRepositoryFromTemplateParms } - It 'Should return an object of the correct type' { - $repo | Should -BeOfType PSCustomObject + It 'Should support pipeline input for the uri parameter' { + $newGitHubRepositoryFromTemplateParms = @{ + TargetOwnerName = $ownerName + TargetRepositoryName = $repoName + } + { $templateRepo | New-GitHubRepositoryFromTemplate @newGitHubRepositoryFromTemplateParms -WhatIf } | + Should -Not -Throw } - It 'Should return the correct properties' { + It 'Should have the expected type and addititional properties' { + $repo.PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' $repo.name | Should -Be $repoName $repo.private | Should -BeFalse $repo.owner.login | Should -Be $script:ownerName $repo.description | Should -Be $newRepoDesc $repo.is_template | Should -BeFalse + $repo.RepositoryId | Should -Be $repo.id + $repo.RepositoryUrl | Should -Be $repo.html_url } It 'Should have created a .gitignore file' { @@ -340,16 +351,16 @@ try { Get-GitHubContent -Uri $repo.svn_url -Path 'LICENSE' } | Should -Not -Throw } - AfterAll -ScriptBlock { - if ($repo) + AfterAll { + if (Get-Variable -Name repo -ErrorAction SilentlyContinue) { Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false } } } - AfterAll -ScriptBlock { - if ($repo) + AfterAll { + if (Get-Variable -Name templateRepo -ErrorAction SilentlyContinue) { Remove-GitHubRepository -Uri $templateRepo.svn_url -Confirm:$false } From 0fbd8adaf2a7bd00014badd2cb52396d96b66c87 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sat, 20 Jun 2020 00:23:37 +0100 Subject: [PATCH 09/12] Remove Set-StrictMode from test file --- Tests/GitHubRepositories.tests.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/Tests/GitHubRepositories.tests.ps1 b/Tests/GitHubRepositories.tests.ps1 index c70748ef..df79a1ce 100644 --- a/Tests/GitHubRepositories.tests.ps1 +++ b/Tests/GitHubRepositories.tests.ps1 @@ -11,8 +11,6 @@ Justification='Suppress false positives in Pester code blocks')] param() -Set-StrictMode -Version 1.0 - # This is common test code setup logic for all Pester test files $moduleRootPath = Split-Path -Path $PSScriptRoot -Parent . (Join-Path -Path $moduleRootPath -ChildPath 'Tests\Common.ps1') From 68ea0498328d5b9c7fe2191aa68f08d17418d3c5 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Wed, 24 Jun 2020 17:41:55 +0100 Subject: [PATCH 10/12] Fix review comments --- GitHubRepositories.ps1 | 19 ++++++++++++------- Tests/GitHubRepositories.tests.ps1 | 3 +++ USAGE.md | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index e9a35b7f..85b46b4d 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -246,14 +246,19 @@ filter New-GitHubRepositoryFromTemplate .PARAMETER RepositoryName Name of the template repository. - .PARAMETER TargetRepositoryName - Name of the repository to be created. + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. .PARAMETER TargetOwnerName The organization or person who will own the new repository. To create a new repository in an organization, the authenticated user must be a member of the specified organization. + .PARAMETER TargetRepositoryName + Name of the repository to be created. + .PARAMETER Description A short description of the repository. @@ -315,10 +320,13 @@ filter New-GitHubRepositoryFromTemplate Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] param( + [Parameter(ParameterSetName = 'Elements')] + [string] $OwnerName, + [Parameter( Mandatory, Position = 1, - ParameterSetName='Elements')] + ParameterSetName = 'Elements')] [ValidateNotNullOrEmpty()] [string] $RepositoryName, @@ -326,7 +334,7 @@ filter New-GitHubRepositoryFromTemplate Mandatory, Position = 2, ValueFromPipelineByPropertyName, - ParameterSetName='Uri')] + ParameterSetName = 'Uri')] [Alias('RepositoryUrl')] [string] $Uri, @@ -342,9 +350,6 @@ filter New-GitHubRepositoryFromTemplate [ValidateNotNullOrEmpty()] [string] $TargetRepositoryName, - [Parameter(ParameterSetName='Elements')] - [string] $OwnerName, - [string] $Description, [switch] $Private, diff --git a/Tests/GitHubRepositories.tests.ps1 b/Tests/GitHubRepositories.tests.ps1 index df79a1ce..0ee2ff6c 100644 --- a/Tests/GitHubRepositories.tests.ps1 +++ b/Tests/GitHubRepositories.tests.ps1 @@ -304,6 +304,7 @@ try LicenseTemplate = $testLicenseTemplate IsTemplate = $true } + $templateRepo = New-GitHubRepository @newGitHubRepositoryParms } @@ -318,6 +319,7 @@ try TargetRepositoryName = $repoName Description = $newRepoDesc } + $repo = New-GitHubRepositoryFromTemplate @newGitHubRepositoryFromTemplateParms } @@ -326,6 +328,7 @@ try TargetOwnerName = $ownerName TargetRepositoryName = $repoName } + { $templateRepo | New-GitHubRepositoryFromTemplate @newGitHubRepositoryFromTemplateParms -WhatIf } | Should -Not -Throw } diff --git a/USAGE.md b/USAGE.md index ec42724e..5d62c678 100644 --- a/USAGE.md +++ b/USAGE.md @@ -445,7 +445,7 @@ New-GitHubRepository -RepositoryName TestRepo -OrganizationName MyOrg -TeamId $m #### Create a repository from a template repository ```powershell -New-GitHubRepositoryFromTemplate -RepositoryName MyNewRepo -OwnerName MyOrg -TemplateOwnerName MyOrg -TemplateRepositoryName MyTemplateRepo +New-GitHubRepositoryFromTemplate -OwnerName MyOrg -RepositoryName MyNewRepo-TemplateOwnerName MyOrg -TemplateRepositoryName MyTemplateRepo ``` ---------- From 7d061697a59d515cb22caef579f5758464af4370 Mon Sep 17 00:00:00 2001 From: Simon Heather <32168619+X-Guardian@users.noreply.github.com> Date: Wed, 24 Jun 2020 20:44:54 +0100 Subject: [PATCH 11/12] Update GitHubRepositories.ps1 Co-authored-by: Howard Wolosky --- GitHubRepositories.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index 85b46b4d..1d714240 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -313,8 +313,7 @@ filter New-GitHubRepositoryFromTemplate #> [CmdletBinding( SupportsShouldProcess, - PositionalBinding = $false - )] + PositionalBinding = $false)] [OutputType({$script:GitHubRepositoryTypeName})] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is From 8c58a55f797df42b677450a4a85168afc6768733 Mon Sep 17 00:00:00 2001 From: Simon Heather <32168619+X-Guardian@users.noreply.github.com> Date: Wed, 24 Jun 2020 20:47:02 +0100 Subject: [PATCH 12/12] Update GitHubRepositories.ps1 Co-authored-by: Howard Wolosky --- GitHubRepositories.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index f929bdf6..13dac603 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -384,7 +384,7 @@ filter New-GitHubRepositoryFromTemplate 'UriFragment' = $uriFragment 'Body' = (ConvertTo-Json -InputObject $hashBody) 'Method' = 'Post' - 'Description' = "Creating $TargetName from Template" + 'Description' = "Creating $TargetRepositoryName from Template" 'AcceptHeader' = $script:baptisteAcceptHeader 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name