From 869d52cf0dd91971661cbc9aec6f6c8e9c9ee1d0 Mon Sep 17 00:00:00 2001 From: Raghav S Date: Sun, 22 Mar 2020 00:03:20 +0530 Subject: [PATCH 1/6] Add commands for Git Refs API This commit adds support for the create/update/delete and Get reference APIs. At this point the test cases for Update alone need to be added --- GitHubReferences.ps1 | 442 +++++++++++++++++++++++++++++++ PowerShellForGitHub.psd1 | 6 + Tests/GitHubReferences.Tests.ps1 | 219 +++++++++++++++ 3 files changed, 667 insertions(+) create mode 100644 GitHubReferences.ps1 create mode 100644 Tests/GitHubReferences.Tests.ps1 diff --git a/GitHubReferences.ps1 b/GitHubReferences.ps1 new file mode 100644 index 00000000..bd3e06a8 --- /dev/null +++ b/GitHubReferences.ps1 @@ -0,0 +1,442 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +function Get-GitHubReference +{ +<# + .SYNOPSIS + Retrieve a reference of a given GitHub repository. + + .DESCRIPTION + Retrieve a reference of a given GitHub repository. + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER Reference + Name of the reference, for example: "heads/" for branches and "tags/" for tags. + Gets all the references (everything in the namespace, not only heads and tags) if not provided + + .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. + + .OUTPUTS + [PSCustomObject] + Details of the git reference in the given repository + + .EXAMPLE + Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Reference heads/master +#> + [CmdletBinding( + SupportsShouldProcess, + DefaultParametersetName='Elements')] + [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='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ParameterSetName='Uri')] + [string] $Uri, + + [string] $Reference, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'ProvidedReference' = $PSBoundParameters.ContainsKey('Reference') + } + + if ($OwnerName -xor $RepositoryName) + { + $message = 'You must specify both Owner Name and Repository Name.' + Write-Log -Message $message -Level Error + throw $message + } + + if (-not [String]::IsNullOrEmpty($RepositoryName)) + { + $uriFragment = "repos/$OwnerName/$RepositoryName/git/matching-refs" + $description = "Getting all references for $RepositoryName" + if ($PSBoundParameters.ContainsKey('Reference')) + { + $uriFragment = $uriFragment + "/$Reference" + $description = "Getting Reference $Reference for $RepositoryName" + } + else + { + # Add a slash at the end. Calling it with only matching-refs without the slash causes a 404 + $uriFragment = $uriFragment + "//" + } + } + + $params = @{ + 'UriFragment' = $uriFragment + 'Description' = $description + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethodMultipleResult @params +} + +function New-GitHubReference +{ + <# + .SYNOPSIS + Create a reference in a given GitHub repository. + + .DESCRIPTION + Create a reference in a given GitHub repository. + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER Reference + The name of the reference to be created (eg: heads/master or tags/myTag) + + .PARAMETER Sha + The SHA1 value for the reference to be created + + .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. + + .OUTPUTS + [PSCustomObject] + Details of the git reference created. Throws an Exception if the reference already exists + + .EXAMPLE + New-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Reference heads/master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + #> + [CmdletBinding( + SupportsShouldProcess, + DefaultParametersetName='Elements')] + [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='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ParameterSetName='Uri')] + [string] $Uri, + + [Parameter(Mandatory)] + [string] $Reference, + + [Parameter(Mandatory)] + [string] $Sha, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + if ($OwnerName -xor $RepositoryName) + { + $message = 'You must specify both Owner Name and Repository Name.' + Write-Log -Message $message -Level Error + throw $message + } + + $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs" + $description = "Creating Reference $Reference for $RepositoryName from SHA $Sha" + + $hashBody = @{ + 'ref' = "refs/" + $Reference + 'sha' = $Sha + } + + $params = @{ + 'UriFragment' = $uriFragment + 'Method' = 'Post' + 'Body' = (ConvertTo-Json -InputObject $hashBody) + 'Description' = $description + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} + +function Update-GitHubReference +{ + <# + .SYNOPSIS + Update a reference in a given GitHub repository. + + .DESCRIPTION + Update a reference in a given GitHub repository. + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER Reference + The name of the reference to be updated (eg: heads/master or tags/myTag) + + .PARAMETER Sha + The updated SHA1 value to be set for this reference + + .PARAMETER Force + Indicates whether to force the update. If not set it will ensure that the update is a fast-forward update. + + .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-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Reference heads/master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + #> + [CmdletBinding( + SupportsShouldProcess, + DefaultParametersetName='Elements')] + [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='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ParameterSetName='Uri')] + [string] $Uri, + + [Parameter(Mandatory)] + [string] $Reference, + + [Parameter(Mandatory)] + [string] $Sha, + + [switch] $Force, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + if ($OwnerName -xor $RepositoryName) + { + $message = 'You must specify both Owner Name and Repository Name.' + Write-Log -Message $message -Level Error + throw $message + } + + $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/$Reference" + $description = "Updating SHA for Reference $Reference in $RepositoryName to $Sha" + + $hashBody = @{ + 'force' = $Force.IsPresent + 'sha' = $Sha + } + + $params = @{ + 'UriFragment' = $uriFragment + 'Method' = 'Patch' + 'Body' = (ConvertTo-Json -InputObject $hashBody) + 'Description' = $description + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} + +function Remove-GitHubReference +{ + <# + .SYNOPSIS + Delete a reference in a given GitHub repository. + + .DESCRIPTION + Delete a reference in a given GitHub repository. + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER Reference + The name of the reference to be deleted (eg: heads/master) + + .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 + Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Reference heads/master + #> + [CmdletBinding( + SupportsShouldProcess, + DefaultParametersetName='Elements')] + [Alias('Delete-GitHubReference')] + [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='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ParameterSetName='Uri')] + [string] $Uri, + + [Parameter(Mandatory)] + [string] $Reference, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + if ($OwnerName -xor $RepositoryName) + { + $message = 'You must specify both Owner Name and Repository Name.' + Write-Log -Message $message -Level Error + throw $message + } + + $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/$Reference" + $description = "Deleting Reference $Reference from repository $RepositoryName" + + $params = @{ + 'UriFragment' = $uriFragment + 'Method' = 'Delete' + 'Body' = (ConvertTo-Json -InputObject $hashBody) + 'Description' = $description + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} \ No newline at end of file diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 67903012..c505cecd 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -33,6 +33,7 @@ 'GitHubMiscellaneous.ps1', 'GitHubOrganizations.ps1', 'GitHubPullRequests.ps1', + 'GitHubReferences.ps1', 'GitHubReleases.ps1', 'GitHubRepositories.ps1', 'GitHubRepositoryForks.ps1', @@ -69,6 +70,7 @@ 'Get-GitHubPathTraffic', 'Get-GitHubPullRequest', 'Get-GitHubRateLimit', + 'Get-GitHubReference', 'Get-GitHubReferrerTraffic', 'Get-GitHubRelease', 'Get-GitHubRepository', @@ -96,6 +98,7 @@ 'New-GitHubLabel', 'New-GitHubMilestone', 'New-GitHubPullRequest', + 'New-GithubReference', 'New-GitHubRepository', 'New-GitHubRepositoryFork', 'Remove-GithubAssignee', @@ -103,6 +106,7 @@ 'Remove-GitHubIssueLabel', 'Remove-GitHubLabel', 'Remove-GitHubMilestone', + 'Remove-GitHubReference', 'Remove-GitHubRepository', 'Rename-GitHubRepository', 'Reset-GitHubConfiguration', @@ -122,6 +126,7 @@ 'Update-GitHubCurrentUser', 'Update-GitHubIssue', 'Update-GitHubLabel', + 'Update-GithubReference', 'Update-GitHubRepository' ) @@ -129,6 +134,7 @@ 'Delete-GitHubComment', 'Delete-GitHubLabel', 'Delete-GitHubMilestone', + 'Delete-GitHubReference', 'Delete-GitHubRepository', 'Get-GitHubBranch', 'Transfer-GitHubRepositoryOwnership' diff --git a/Tests/GitHubReferences.Tests.ps1 b/Tests/GitHubReferences.Tests.ps1 new file mode 100644 index 00000000..9015d679 --- /dev/null +++ b/Tests/GitHubReferences.Tests.ps1 @@ -0,0 +1,219 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +<# +.Synopsis + Tests for GitHubReferences.ps1 module +#> + +# 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') + +try +{ + if ($accessTokenConfigured) + { + Describe 'Create a new reference(branch) in repository' { + $repositoryName = [Guid]::NewGuid() + $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $masterRefName = "heads/master" + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName + $sha = $existingref.object.sha + + Context 'On creating a valid reference in a new repository from a given SHA' { + $refName = "heads/" + [Guid]::NewGuid().ToString() + $result = New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refName -Sha $sha + + It 'Should successfully create the reference' { + $result.ref | Should Be "refs/$refName" + } + } + + Context 'On creating a valid reference in a new repository (specified by Uri) from a given SHA' { + $refName = "heads/" + [Guid]::NewGuid().ToString() + $result = New-GitHubReference -Uri $repo.svn_url -Reference $refName -Sha $sha + + It 'Should successfully create the reference' { + $result.ref | Should Be "refs/$refName" + } + } + + Context 'On trying to create an existing reference in a new repository from a given SHA' { + It 'Should throw an Exception' { + { New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName -Sha $sha } | Should Throw + } + } + + Context 'On creating an existing reference in a new repository (specified by Uri) from a given SHA' { + It 'Should throw an exception' { + { New-GitHubReference -Uri $repo.svn_url -Reference $masterRefName -Sha $sha } | Should Throw + } + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + + Describe 'Getting a reference(branch) from repository' { + $repositoryName = [Guid]::NewGuid() + $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $masterRefName = "heads/master" + $randomRefName = "heads/$([Guid]::NewGuid())" + + Context 'On getting a valid reference from a new repository' { + $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName + + It 'Should return details of the reference' { + $reference.ref | Should be "refs/$masterRefName" + } + } + + Context 'On getting an invalid reference from a new repository' { + $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $randomRefName + + It 'Should not return any details' { + $reference | Should be $null + } + } + + Context 'On getting a valid reference using Uri from a new repository' { + $reference = Get-GitHubReference -Uri $repo.svn_url -Reference $masterRefName + + It 'Should return details of the reference' { + $reference.ref | Should be "refs/$masterRefName" + } + } + + Context 'On getting an invalid reference using Uri from a new repository' { + $reference = Get-GitHubReference -Uri $repo.svn_url -Reference $randomRefName + + It 'Should not return any details' { + $reference | Should be $null + } + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + + Describe 'Getting all references from repository' { + $repositoryName = [Guid]::NewGuid() + $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $masterRefName = "heads/master" + $secondRefName = "heads/branch1" + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $secondRefName -Sha $sha + $refNames = @("refs/$masterRefName", "refs/$secondRefName") + + Context 'On getting all references from a new repository' { + $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName + + It 'Should return all references' { + ($reference.ref | Where-Object {$refNames -Contains $_}).Count | Should be $refNames.Count + } + } + + Context 'On getting all references using Uri from a new repository' { + $reference = Get-GitHubReference -Uri $repo.svn_url + + It 'Should return all references' { + ($reference.ref | Where-Object {$refNames -Contains $_}).Count | Should be $refNames.Count + } + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + + Describe 'Delete a reference(branch) from repository' { + $repositoryName = [Guid]::NewGuid() + New-GitHubRepository -RepositoryName $repositoryName -AutoInit + + Context 'On deleting a newly created reference in a new repository' { + $refname = "heads/myRef" + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master" + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname -Sha $sha + Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname + + It 'Should not return any details when the reference is queried' { + Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname | + Should be $null + } + + It 'Should throw an exception when the same reference is deleted again' { + { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname } | + Should Throw + } + } + + Context 'On deleting an invalid reference from a new repository' { + It 'Should throw an exception' { + { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/$([Guid]::NewGuid())" } | + Should Throw + } + } + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + + Describe 'Update a reference(branch) in repository' { + $masterRefName = "heads/master" + $repositoryName = [Guid]::NewGuid() + New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $refname = "heads/myRef" + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname -Sha $sha + + Context 'On updating a newly created reference to a different SHA' { + It 'Should throw an exception if the SHA is invalid' { + { Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname -Sha "1234" } | + Should Throw + } + } + + Context 'On updating a reference to a different SHA' { + # Add a new file in master in order to create a new commit and hence, an updated SHA + # TODO: Replace this when the Content updation related APIs are made available + $fileContent = "Hello powershell!" + $fileName = "powershell.txt" + $encodedFile = [System.Convert]::ToBase64String(([System.Text.Encoding]::UTF8).GetBytes($fileContent)) + + $fileData = @{ + "message" = "added new file" + "content" = "$encodedFile" + } + + $params = @{ + 'UriFragment' = "repos/$ownerName/$repositoryName/contents/$fileName" + 'Method' = 'Put' + 'Body' = (ConvertTo-Json -InputObject $fileData) + 'AccessToken' = $AccessToken + } + Invoke-GHRestMethod @params + + # Update branch with master SHA + $masterSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName).object.sha + $oldSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname).object.sha + Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname -Sha $masterSHA + + It 'Should return the updated SHA when the reference is queried after update' { + $newSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname).object.sha + $newSHA | Should be $masterSHA + $newSHA | Should Not be $oldSHA + } + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + } +} +catch +{ + if (Test-Path -Path $script:originalConfigFile -PathType Leaf) + { + # Restore the user's configuration to its pre-test state + Restore-GitHubConfiguration -Path $script:originalConfigFile + $script:originalConfigFile = $null + } +} + From f391019bf138da9d48a92394b67cfd9deff4e7af Mon Sep 17 00:00:00 2001 From: Raghav S Date: Sun, 31 May 2020 02:14:43 +0530 Subject: [PATCH 2/6] Refactor methods and add more tests --- GitHubReferences.ps1 | 290 +++++++++++++-------- Tests/GitHubReferences.Tests.ps1 | 433 ++++++++++++++++++++++++------- 2 files changed, 522 insertions(+), 201 deletions(-) diff --git a/GitHubReferences.ps1 b/GitHubReferences.ps1 index bd3e06a8..e175f7d4 100644 --- a/GitHubReferences.ps1 +++ b/GitHubReferences.ps1 @@ -5,10 +5,10 @@ function Get-GitHubReference { <# .SYNOPSIS - Retrieve a reference of a given GitHub repository. + Retrieve a reference from a given GitHub repository. .DESCRIPTION - Retrieve a reference of a given GitHub repository. + Retrieve a reference from a given GitHub repository. The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub .PARAMETER OwnerName @@ -24,9 +24,17 @@ function Get-GitHubReference The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER Reference - Name of the reference, for example: "heads/" for branches and "tags/" for tags. - Gets all the references (everything in the namespace, not only heads and tags) if not provided + .PARAMETER Tag + The name of the Tag to be retrieved. + + .PARAMETER Branch + The name of the Branch to be retrieved. + + .PARAMETER All + If provided, will retrieve both branches and tags for the given repository + + .PARAMETER MatchPrefix + If provided, this will return matching preferences for the given branch/tag name in case an exact match is not found .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the @@ -43,25 +51,54 @@ function Get-GitHubReference Details of the git reference in the given repository .EXAMPLE - Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Reference heads/master + Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag powershellTagV1 + + .EXAMPLE + Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch master + + .EXAMPLE + Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch master -MatchPrefix + + .EXAMPLE + Get-GitHubReference -OwnerName microsoft -RepositoryName -All #> [CmdletBinding( SupportsShouldProcess, - DefaultParametersetName='Elements')] + DefaultParameterSetName='Elements')] [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='Elements')] + [Parameter(Mandatory, ParameterSetName='BranchElements')] + [Parameter(Mandatory, ParameterSetName='TagElements')] + [Parameter(Mandatory, ParameterSetName='Elements')] [string] $OwnerName, - [Parameter(ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='BranchElements')] + [Parameter(Mandatory, ParameterSetName='TagElements')] + [Parameter(Mandatory, ParameterSetName='Elements')] [string] $RepositoryName, - [Parameter( - Mandatory, - ParameterSetName='Uri')] + [Parameter(Mandatory, ParameterSetName='BranchUri')] + [Parameter(Mandatory, ParameterSetName='TagUri')] + [Parameter(Mandatory, ParameterSetName='Uri')] [string] $Uri, - [string] $Reference, + [Parameter(ParameterSetName='TagUri')] + [Parameter(ParameterSetName='TagElements')] + [string] $Tag, + + [Parameter(ParameterSetName='BranchUri')] + [Parameter(ParameterSetName='BranchElements')] + [string] $Branch, + + [Parameter(ParameterSetName='Elements')] + [Parameter(ParameterSetName='Uri')] + [switch] $All, + + [Parameter(ParameterSetName='BranchUri')] + [Parameter(ParameterSetName='BranchElements')] + [Parameter(ParameterSetName='TagUri')] + [Parameter(ParameterSetName='TagElements')] + [switch] $MatchPrefix, [string] $AccessToken, @@ -80,26 +117,25 @@ function Get-GitHubReference 'ProvidedReference' = $PSBoundParameters.ContainsKey('Reference') } - if ($OwnerName -xor $RepositoryName) - { - $message = 'You must specify both Owner Name and Repository Name.' - Write-Log -Message $message -Level Error - throw $message - } + $uriFragment = "repos/$OwnerName/$RepositoryName/git" - if (-not [String]::IsNullOrEmpty($RepositoryName)) - { - $uriFragment = "repos/$OwnerName/$RepositoryName/git/matching-refs" + if ($All) { + # Add a slash at the end as Invoke-GHRestMethod removes the last trailing slash. Calling this API without the slash causes a 404 + $uriFragment = $uriFragment + "/matching-refs//" $description = "Getting all references for $RepositoryName" - if ($PSBoundParameters.ContainsKey('Reference')) - { - $uriFragment = $uriFragment + "/$Reference" - $description = "Getting Reference $Reference for $RepositoryName" + } + else { + $reference = Resolve-Reference $Tag $Branch + + if ($MatchPrefix) { + $uriFragment = $uriFragment + "/matching-refs/$reference" + $description = "Getting references matching $reference for $RepositoryName" } else { - # Add a slash at the end. Calling it with only matching-refs without the slash causes a 404 - $uriFragment = $uriFragment + "//" + # We want to return an exact match, call the 'get single reference' API + $uriFragment = $uriFragment + "/ref/$reference" + $description = "Getting reference $reference for $RepositoryName" } } @@ -112,6 +148,7 @@ function Get-GitHubReference 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } + Write-Host $uriFragment return Invoke-GHRestMethodMultipleResult @params } @@ -138,8 +175,11 @@ function New-GitHubReference The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER Reference - The name of the reference to be created (eg: heads/master or tags/myTag) + .PARAMETER Tag + The name of the Tag to be created. + + .PARAMETER Branch + The name of the Branch to be created. .PARAMETER Sha The SHA1 value for the reference to be created @@ -159,26 +199,33 @@ function New-GitHubReference Details of the git reference created. Throws an Exception if the reference already exists .EXAMPLE - New-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Reference heads/master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + New-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag powershellTagV1 -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + + .EXAMPLE + New-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd #> - [CmdletBinding( - SupportsShouldProcess, - DefaultParametersetName='Elements')] + [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(ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='BranchElements')] + [Parameter(Mandatory, ParameterSetName='TagElements')] [string] $OwnerName, - [Parameter(ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='BranchElements')] + [Parameter(Mandatory, ParameterSetName='TagElements')] [string] $RepositoryName, - [Parameter( - Mandatory, - ParameterSetName='Uri')] + [Parameter(Mandatory, ParameterSetName='BranchUri')] + [Parameter(Mandatory, ParameterSetName='TagUri')] [string] $Uri, - [Parameter(Mandatory)] - [string] $Reference, + [Parameter(Mandatory, ParameterSetName='TagUri')] + [Parameter(Mandatory, ParameterSetName='TagElements')] + [string] $Tag, + + [Parameter(Mandatory, ParameterSetName='BranchUri')] + [Parameter(Mandatory, ParameterSetName='BranchElements')] + [string] $Branch, [Parameter(Mandatory)] [string] $Sha, @@ -199,18 +246,13 @@ function New-GitHubReference 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } - if ($OwnerName -xor $RepositoryName) - { - $message = 'You must specify both Owner Name and Repository Name.' - Write-Log -Message $message -Level Error - throw $message - } + $reference = Resolve-Reference $Tag $Branch $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs" - $description = "Creating Reference $Reference for $RepositoryName from SHA $Sha" + $description = "Creating Reference $reference for $RepositoryName from SHA $Sha" $hashBody = @{ - 'ref' = "refs/" + $Reference + 'ref' = "refs/" + $reference 'sha' = $Sha } @@ -251,12 +293,15 @@ function Update-GitHubReference The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER Reference - The name of the reference to be updated (eg: heads/master or tags/myTag) - + .PARAMETER Tag + The name of the tag to be updated to the given SHA. + + .PARAMETER Branch + The name of the branch to be updated to the given SHA. + .PARAMETER Sha - The updated SHA1 value to be set for this reference - + The updated SHA1 value to be set for this reference. + .PARAMETER Force Indicates whether to force the update. If not set it will ensure that the update is a fast-forward update. @@ -278,19 +323,25 @@ function Update-GitHubReference DefaultParametersetName='Elements')] [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='Elements')] + [Parameter(Mandatory, ParameterSetName='BranchElements')] + [Parameter(Mandatory, ParameterSetName='TagElements')] [string] $OwnerName, - [Parameter(ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='BranchElements')] + [Parameter(Mandatory, ParameterSetName='TagElements')] [string] $RepositoryName, - [Parameter( - Mandatory, - ParameterSetName='Uri')] + [Parameter(Mandatory, ParameterSetName='BranchUri')] + [Parameter(Mandatory, ParameterSetName='TagUri')] [string] $Uri, - [Parameter(Mandatory)] - [string] $Reference, + [Parameter(Mandatory, ParameterSetName='TagUri')] + [Parameter(Mandatory, ParameterSetName='TagElements')] + [string] $Tag, + + [Parameter(Mandatory, ParameterSetName='BranchUri')] + [Parameter(Mandatory, ParameterSetName='BranchElements')] + [string] $Branch, [Parameter(Mandatory)] [string] $Sha, @@ -313,15 +364,10 @@ function Update-GitHubReference 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } - if ($OwnerName -xor $RepositoryName) - { - $message = 'You must specify both Owner Name and Repository Name.' - Write-Log -Message $message -Level Error - throw $message - } + $reference = Resolve-Reference $Tag $Branch - $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/$Reference" - $description = "Updating SHA for Reference $Reference in $RepositoryName to $Sha" + $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/$reference" + $description = "Updating SHA for Reference $reference in $RepositoryName to $Sha" $hashBody = @{ 'force' = $Force.IsPresent @@ -365,8 +411,11 @@ function Remove-GitHubReference The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER Reference - The name of the reference to be deleted (eg: heads/master) + .PARAMETER Tag + The name of the tag to be deleted. + + .PARAMETER Branch + The name of the branch to be deleted. .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the @@ -379,64 +428,89 @@ function Remove-GitHubReference If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Reference heads/master + Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag powershellTagV1 + + .EXAMPLE + Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch master + + .EXAMPLE + Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch milestone1 -Confirm:$false #> - [CmdletBinding( - SupportsShouldProcess, - DefaultParametersetName='Elements')] + [CmdletBinding(SupportsShouldProcess, ConfirmImpact="High")] [Alias('Delete-GitHubReference')] [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='Elements')] + [Parameter(Mandatory, ParameterSetName='BranchElements')] + [Parameter(Mandatory, ParameterSetName='TagElements')] [string] $OwnerName, - [Parameter(ParameterSetName='Elements')] + [Parameter(Mandatory, ParameterSetName='BranchElements')] + [Parameter(Mandatory, ParameterSetName='TagElements')] [string] $RepositoryName, - [Parameter( - Mandatory, - ParameterSetName='Uri')] + [Parameter(Mandatory, ParameterSetName='BranchUri')] + [Parameter(Mandatory, ParameterSetName='TagUri')] [string] $Uri, - [Parameter(Mandatory)] - [string] $Reference, + [Parameter(Mandatory, ParameterSetName='TagUri')] + [Parameter(Mandatory, ParameterSetName='TagElements')] + [string] $Tag, + + [Parameter(Mandatory, ParameterSetName='BranchUri')] + [Parameter(Mandatory, ParameterSetName='BranchElements')] + [string] $Branch, [string] $AccessToken, [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + $repositoryInfoForDisplayMessage = if ($PSCmdlet.ParameterSetName -eq "Uri") { $Uri } else { $OwnerName, $RepositoryName -join "/" } + $reference = Resolve-Reference $Tag $Branch + if ($PSCmdlet.ShouldProcess($repositoryInfoForDisplayMessage, "Remove reference: $reference")) + { + Write-InvocationLog -Invocation $MyInvocation - $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation - $OwnerName = $elements.ownerName - $RepositoryName = $elements.repositoryName + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName - $telemetryProperties = @{ - 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) - 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - } + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } - if ($OwnerName -xor $RepositoryName) - { - $message = 'You must specify both Owner Name and Repository Name.' - Write-Log -Message $message -Level Error - throw $message + $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/$reference" + $description = "Deleting Reference $reference from repository $RepositoryName" + + $params = @{ + 'UriFragment' = $uriFragment + 'Method' = 'Delete' + 'Body' = (ConvertTo-Json -InputObject $hashBody) + 'Description' = $description + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params } - $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/$Reference" - $description = "Deleting Reference $Reference from repository $RepositoryName" +} - $params = @{ - 'UriFragment' = $uriFragment - 'Method' = 'Delete' - 'Body' = (ConvertTo-Json -InputObject $hashBody) - 'Description' = $description - 'AccessToken' = $AccessToken - 'TelemetryEventName' = $MyInvocation.MyCommand.Name - 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) +function Resolve-Reference($Tag, $Branch) +{ + if (-not [String]::IsNullOrEmpty($Tag)) + { + return "tags/$Tag" + } + elseif (-not [String]::IsNullOrEmpty($Branch)) + { + return "heads/$Branch" + } + else + { + return [string]::Empty } - - return Invoke-GHRestMethod @params } \ No newline at end of file diff --git a/Tests/GitHubReferences.Tests.ps1 b/Tests/GitHubReferences.Tests.ps1 index 9015d679..e9a07521 100644 --- a/Tests/GitHubReferences.Tests.ps1 +++ b/Tests/GitHubReferences.Tests.ps1 @@ -10,194 +10,442 @@ $moduleRootPath = Split-Path -Path $PSScriptRoot -Parent . (Join-Path -Path $moduleRootPath -ChildPath 'Tests\Common.ps1') +function Update-MasterSHA() +{ + # Add a new file in master in order to create a new commit and hence, an updated SHA + # TODO: Replace this when the Content updation related APIs are made available + $fileContent = "Hello powershell!" + $fileName = "powershell.txt" + $encodedFile = [System.Convert]::ToBase64String(([System.Text.Encoding]::UTF8).GetBytes($fileContent)) + + $fileData = @{ + "message" = "added new file" + "content" = "$encodedFile" + } + + $params = @{ + 'UriFragment' = "repos/$ownerName/$repositoryName/contents/$fileName" + 'Method' = 'Put' + 'Body' = (ConvertTo-Json -InputObject $fileData) + 'AccessToken' = $AccessToken + } + Invoke-GHRestMethod @params +} + try { if ($accessTokenConfigured) { - Describe 'Create a new reference(branch) in repository' { + $masterBranchName = "master" + Describe 'Create a new branch in repository' { $repositoryName = [Guid]::NewGuid() $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $masterRefName = "heads/master" - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName $sha = $existingref.object.sha - Context 'On creating a valid reference in a new repository from a given SHA' { - $refName = "heads/" + [Guid]::NewGuid().ToString() - $result = New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refName -Sha $sha + Context 'On creating a new branch in a repository from a given SHA' { + $branchName = [Guid]::NewGuid().ToString() + $result = New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $sha - It 'Should successfully create the reference' { - $result.ref | Should Be "refs/$refName" + It 'Should successfully create the new branch' { + $result.ref | Should Be "refs/heads/$branchName" + } + + It 'Should throw an Exception when trying to create it again' { + { New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName -Sha $sha } | Should Throw } } - Context 'On creating a valid reference in a new repository (specified by Uri) from a given SHA' { - $refName = "heads/" + [Guid]::NewGuid().ToString() - $result = New-GitHubReference -Uri $repo.svn_url -Reference $refName -Sha $sha + Context 'On creating a new branch in a repository (specified by Uri) from a given SHA' { + $branchName = [Guid]::NewGuid().ToString() + $result = New-GitHubReference -Uri $repo.svn_url -Branch $branchName -Sha $sha + + It 'Should successfully create the branch' { + $result.ref | Should Be "refs/heads/$branchName" + } + + It 'Should throw an exception when trying to create it again' { + { New-GitHubReference -Uri $repo.svn_url -Branch $masterBranchName -Sha $sha } | Should Throw + } + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + + Describe 'Create a new tag in a repository' { + $repositoryName = [Guid]::NewGuid() + $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName + $sha = $existingref.object.sha + + Context 'On creating a new tag in a repository referring to a given SHA' { + $tagName = [Guid]::NewGuid().ToString() + $result = New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Sha $sha + + It 'Should successfully create the new tag' { + $result.ref | Should Be "refs/tags/$tagName" + } + + It 'Should throw an Exception when trying to create it again' { + { New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Sha $sha } | Should Throw + } + } + + Context 'On creating a new tag in a repository (specified by Uri) from a given SHA' { + $tagName = [Guid]::NewGuid().ToString() + $result = New-GitHubReference -Uri $repo.svn_url -Tag $tagName -Sha $sha It 'Should successfully create the reference' { - $result.ref | Should Be "refs/$refName" + $result.ref | Should Be "refs/tags/$tagName" + } + + It 'Should throw an exception when trying to create it again' { + { New-GitHubReference -Uri $repo.svn_url -Tag $tagName -Sha $sha } | Should Throw } } - Context 'On trying to create an existing reference in a new repository from a given SHA' { - It 'Should throw an Exception' { - { New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName -Sha $sha } | Should Throw + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + + Describe 'Getting branch(es) from repository' { + $repositoryName = [Guid]::NewGuid() + $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $randomBranchName = [Guid]::NewGuid() + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName + $sha = $existingref.object.sha + + Context 'On getting an existing branch from a repository' { + $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName + + It 'Should return details of the branch' { + $reference.ref | Should be "refs/heads/$masterBranchName" + } + } + + Context 'On getting an non-existent branch from a repository' { + + It 'Should throw an exception' { + { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $randomBranchName } | + Should Throw + } + } + + Context 'On getting an existing branch using Uri from a repository' { + $reference = Get-GitHubReference -Uri $repo.svn_url -Branch $masterBranchName + + It 'Should return details of the branch' { + $reference.ref | Should be "refs/heads/$masterBranchName" } } - Context 'On creating an existing reference in a new repository (specified by Uri) from a given SHA' { + Context 'On getting an invalid branch using Uri from a repository' { + It 'Should throw an exception' { - { New-GitHubReference -Uri $repo.svn_url -Reference $masterRefName -Sha $sha } | Should Throw + { Get-GitHubReference -Uri $repo.svn_url -Branch $randomBranchName } | + Should Throw + } + } + + Context 'On getting branches by prefix from a repository' { + $branch1 = "elements_" + $([Guid]::NewGuid().ToString()) + $branch2 = "elements_" + $([Guid]::NewGuid().ToString()) + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branch1 -Sha $sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branch2 -Sha $sha + + $references = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch "elements" -MatchPrefix + + It 'Should return all branches matching the prefix' { + $expected = @("refs/heads/$branch1", "refs/heads/$branch2") + $references.ref | Should Be $expected + } + } + + Context 'On getting branches by prefix using Uri from a repository' { + $branch1 = "uri_" + $([Guid]::NewGuid().ToString()) + $branch2 = "uri_" + $([Guid]::NewGuid().ToString()) + New-GitHubReference -Uri $repo.svn_url -Branch $branch1 -Sha $sha + New-GitHubReference -Uri $repo.svn_url -Branch $branch2 -Sha $sha + + $references = Get-GitHubReference -Uri $repo.svn_url -Branch "uri" -MatchPrefix + + It 'Should return all branches matching the prefix' { + $expected = @("refs/heads/$branch1", "refs/heads/$branch2") + $references.ref | Should Be $expected } } $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - Describe 'Getting a reference(branch) from repository' { + Describe 'Getting tag(s) from repository' { $repositoryName = [Guid]::NewGuid() $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $masterRefName = "heads/master" - $randomRefName = "heads/$([Guid]::NewGuid())" + $randomTagName = [Guid]::NewGuid() + $validTag = "master-tag" + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName + $sha = $existingref.object.sha + New-GitHubReference -Uri $repo.svn_url -Tag $validTag -Sha $sha + + Context 'On getting an existing tag from a repository' { + $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $validTag + + It 'Should return details of the tag' { + $reference.ref | Should be "refs/tags/$validTag" + } + } - Context 'On getting a valid reference from a new repository' { - $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName + Context 'On getting an non-existent tag from a repository' { - It 'Should return details of the reference' { - $reference.ref | Should be "refs/$masterRefName" + It 'Should throw an exception' { + { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $randomTagName } | + Should Throw } } - Context 'On getting an invalid reference from a new repository' { - $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $randomRefName + Context 'On getting an existing tag using Uri from a repository' { + $reference = Get-GitHubReference -Uri $repo.svn_url -Tag $validTag - It 'Should not return any details' { - $reference | Should be $null + It 'Should return details of the tag' { + $reference.ref | Should be "refs/tags/$validTag" } } - Context 'On getting a valid reference using Uri from a new repository' { - $reference = Get-GitHubReference -Uri $repo.svn_url -Reference $masterRefName + Context 'On getting an invalid tag using Uri from a repository' { - It 'Should return details of the reference' { - $reference.ref | Should be "refs/$masterRefName" + It 'Should throw an exception' { + { Get-GitHubReference -Uri $repo.svn_url -Tag $randomTagName } | + Should Throw } } - Context 'On getting an invalid reference using Uri from a new repository' { - $reference = Get-GitHubReference -Uri $repo.svn_url -Reference $randomRefName + Context 'On getting tags by prefix from a repository' { + $tag1 = "elements_" + $([Guid]::NewGuid().ToString()) + $tag2 = "elements_" + $([Guid]::NewGuid().ToString()) + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $tag1 -Sha $sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $tag2 -Sha $sha - It 'Should not return any details' { - $reference | Should be $null + $references = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag "elements" -MatchPrefix + + It 'Should return all tags matching the prefix' { + $expected = @("refs/tags/$tag1", "refs/tags/$tag2") + $references.ref | Should Be $expected + } + } + + Context 'On getting tags by prefix from a repository specified By Uri' { + $tag1 = "uri_" + $([Guid]::NewGuid().ToString()) + $tag2 = "uri_" + $([Guid]::NewGuid().ToString()) + New-GitHubReference -Uri $repo.svn_url -Tag $tag1 -Sha $sha + New-GitHubReference -Uri $repo.svn_url -Tag $tag2 -Sha $sha + + $references = Get-GitHubReference -Uri $repo.svn_url -Tag "uri" -MatchPrefix + + It 'Should return all tags matching the prefix' { + $expected = @("refs/tags/$branch1", "refs/tags/$branch2") + $references.ref | Should Be $expected } } $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - Describe 'Getting all references from repository' { $repositoryName = [Guid]::NewGuid() $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $masterRefName = "heads/master" - $secondRefName = "heads/branch1" - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName + $branchName = "forked-from-master" + $tagName = "master-fork-tag" + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName $sha = $existingref.object.sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $secondRefName -Sha $sha - $refNames = @("refs/$masterRefName", "refs/$secondRefName") + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Sha $sha - Context 'On getting all references from a new repository' { - $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName + $refNames = @("refs/heads/$masterBranchName", "refs/heads/$branchName", "refs/tags/$tagName") - It 'Should return all references' { - ($reference.ref | Where-Object {$refNames -Contains $_}).Count | Should be $refNames.Count + Context 'On getting all references from a repository' { + $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -All + + It 'Should return all branches and tags' { + $reference.ref | Should be $refNames } } - Context 'On getting all references using Uri from a new repository' { - $reference = Get-GitHubReference -Uri $repo.svn_url + Context 'On getting all references using Uri from a repository' { + $reference = Get-GitHubReference -Uri $repo.svn_url -All - It 'Should return all references' { - ($reference.ref | Where-Object {$refNames -Contains $_}).Count | Should be $refNames.Count + It 'Should return all branches and tags' { + $reference.ref | Should be $refNames } } $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - Describe 'Delete a reference(branch) from repository' { + Describe 'Delete a branch from a repository' { $repositoryName = [Guid]::NewGuid() - New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $branchName = "myBranch" + + Context 'On deleting a newly created branch in a repository' { + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch "master" + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $sha + Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Confirm:$false + + It 'Should throw an exception when trying to get the deleted branch' { + { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName } | + Should Throw + } + + It 'Should throw an exception when the same branch is deleted again' { + { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Confirm:$false} | + Should Throw + } + } + + Context 'On deleting an invalid branch from a repository' { + It 'Should throw an exception' { + { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $([Guid]::NewGuid()) -Confirm:$false} | + Should Throw + } + } + + Context 'On deleting a newly created branch in a repository specified by Uri' { + $branchName = "myBranch" + $existingref = Get-GitHubReference -Uri $repo.svn_url -Branch "master" + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $sha + Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Confirm:$false + + It 'Should throw an exception when trying to get the deleted branch' { + { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName} | + Should Throw + } + + It 'Should throw an exception when the same branch is deleted again' { + { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Confirm:$false} | + Should Throw + } + } + + Context 'On deleting an invalid branch from a repository specified by Uri' { + It 'Should throw an exception' { + { Remove-GitHubReference -OwnerName -Uri $repo.svn_url -Branch $([Guid]::NewGuid()) -Confirm:$false } | + Should Throw + } + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + + Describe 'Delete a Tag from a Repository' { + $repositoryName = [Guid]::NewGuid() + $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $tagName = "myTag" + + Context 'On deleting a valid tag in a repository' { + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch "master" + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Sha $sha + Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Confirm:$false + + It 'Should throw an exception when trying to get the deleted tag' { + { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName } | + Should Throw + } + + It 'Should throw an exception when the same tag is deleted again' { + { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Confirm:$false } | + Should Throw + } + } + + Context 'On deleting an invalid tag from a repository' { + It 'Should throw an exception' { + { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $([Guid]::NewGuid()) -Confirm:$false } | + Should Throw + } + } - Context 'On deleting a newly created reference in a new repository' { - $refname = "heads/myRef" - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master" + Context 'On deleting a tag in a repository specified by Uri' { + $existingref = Get-GitHubReference -Uri $repo.svn_url -Branch "master" $sha = $existingref.object.sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname -Sha $sha - Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Sha $sha + Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Confirm:$false - It 'Should not return any details when the reference is queried' { - Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname | - Should be $null + It 'Should throw an exception when trying to get the deleted tag' { + { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName } | + Should Throw } - It 'Should throw an exception when the same reference is deleted again' { - { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname } | + It 'Should throw an exception when the same branch is deleted again' { + { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Confirm:$false } | Should Throw } } - Context 'On deleting an invalid reference from a new repository' { + Context 'On deleting an invalid tag from a repository specified by Uri' { It 'Should throw an exception' { - { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/$([Guid]::NewGuid())" } | + { Remove-GitHubReference -OwnerName -Uri $repo.svn_url -Tag $([Guid]::NewGuid()) -Confirm:$false } | Should Throw } } + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } - Describe 'Update a reference(branch) in repository' { - $masterRefName = "heads/master" + Describe 'Update a branch in a repository' { $repositoryName = [Guid]::NewGuid() New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $refname = "heads/myRef" - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName + $branchName = "myBranch" + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName $sha = $existingref.object.sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname -Sha $sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $sha - Context 'On updating a newly created reference to a different SHA' { + Context 'On updating an existing branch to a different SHA' { It 'Should throw an exception if the SHA is invalid' { - { Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname -Sha "1234" } | + { Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha "1234" } | Should Throw } } - Context 'On updating a reference to a different SHA' { - # Add a new file in master in order to create a new commit and hence, an updated SHA - # TODO: Replace this when the Content updation related APIs are made available - $fileContent = "Hello powershell!" - $fileName = "powershell.txt" - $encodedFile = [System.Convert]::ToBase64String(([System.Text.Encoding]::UTF8).GetBytes($fileContent)) + Context 'On updating a branch to a different SHA' { + Update-MasterSHA + $masterSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch "master").object.sha + $oldSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName).object.sha + Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $masterSHA - $fileData = @{ - "message" = "added new file" - "content" = "$encodedFile" + It 'Should return the updated SHA when the reference is queried after update' { + $newSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName).object.sha + $newSHA | Should be $masterSHA + $newSHA | Should Not be $oldSHA } + } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } - $params = @{ - 'UriFragment' = "repos/$ownerName/$repositoryName/contents/$fileName" - 'Method' = 'Put' - 'Body' = (ConvertTo-Json -InputObject $fileData) - 'AccessToken' = $AccessToken + Describe 'Update a tag in a repository' { + $repositoryName = [Guid]::NewGuid() + New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $tag = "myTag" + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag -Sha $sha + + Context 'On updating an existing tag to a different SHA' { + It 'Should throw an exception if the SHA is invalid' { + { Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag -Sha "1234" } | + Should Throw } - Invoke-GHRestMethod @params + } - # Update branch with master SHA - $masterSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName).object.sha - $oldSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname).object.sha - Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname -Sha $masterSHA + Context 'On updating a tag to a valid SHA' { + Update-MasterSHA + $masterSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch "master").object.sha + $oldSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag).object.sha + Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag -Sha $masterSHA It 'Should return the updated SHA when the reference is queried after update' { - $newSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname).object.sha + $newSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag).object.sha $newSHA | Should be $masterSHA $newSHA | Should Not be $oldSHA } @@ -215,5 +463,4 @@ catch Restore-GitHubConfiguration -Path $script:originalConfigFile $script:originalConfigFile = $null } -} - +} \ No newline at end of file From fa1194edbcb131223c83eaafaf1158018ab60067 Mon Sep 17 00:00:00 2001 From: Raghav S Date: Sun, 7 Jun 2020 21:34:20 +0530 Subject: [PATCH 3/6] Revert Manifest to original --- PowerShellForGitHub.psd1 | 418 +++++++++++++++++++-------------------- 1 file changed, 206 insertions(+), 212 deletions(-) diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 781fb5fe..5ef27773 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -1,212 +1,206 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -@{ - GUID = '9e8dfd44-f782-445a-883c-70614f71519c' - Author = 'Microsoft Corporation' - CompanyName = 'Microsoft Corporation' - Copyright = 'Copyright (C) Microsoft Corporation. All rights reserved.' - - ModuleVersion = '0.14.0' - Description = 'PowerShell wrapper for GitHub API' - - # Script module or binary module file associated with this manifest. - RootModule = 'PowerShellForGitHub.psm1' - - # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess - NestedModules = @( - # Ideally this list would be kept completely alphabetical, but other scripts (like - # GitHubConfiguration.ps1) depend on some of the code in Helpers being around at load time. - 'Helpers.ps1', - 'GitHubConfiguration.ps1', - - 'GitHubAnalytics.ps1', - 'GitHubAssignees.ps1', - 'GitHubBranches.ps1', - 'GitHubCore.ps1', - 'GitHubComments.ps1', - 'GitHubContents.ps1', - 'GitHubEvents.ps1', - 'GitHubIssues.ps1', - 'GitHubLabels.ps1', - 'GitHubMilestones.ps1', - 'GitHubMiscellaneous.ps1', - 'GitHubOrganizations.ps1', - 'GitHubProjects.ps1', - 'GitHubProjectCards.ps1', - 'GitHubProjectColumns.ps1', - 'GitHubPullRequests.ps1', - 'GitHubReferences.ps1', - 'GitHubReleases.ps1', - 'GitHubRepositories.ps1', - 'GitHubRepositoryForks.ps1', - 'GitHubRepositoryTraffic.ps1', - 'GitHubTeams.ps1', - 'GitHubUsers.ps1', - 'Telemetry.ps1', - 'UpdateCheck.ps1') - - # Minimum version of the Windows PowerShell engine required by this module - PowerShellVersion = '4.0' - - # Functions to export from this module - FunctionsToExport = @( - 'Add-GitHubIssueLabel', - 'Backup-GitHubConfiguration', - 'Clear-GitHubAuthentication', - 'ConvertFrom-GitHubMarkdown', - 'Get-GitHubAssignee', - 'Get-GitHubCloneTraffic', - 'Get-GitHubCodeOfConduct', - 'Get-GitHubComment', - 'Get-GitHubConfiguration', - 'Get-GitHubContent', - 'Get-GitHubEmoji', - 'Get-GitHubEvent', - 'Get-GitHubGitIgnore', - 'Get-GitHubIssue', - 'Get-GitHubIssueTimeline', - 'Get-GitHubLabel', - 'Get-GitHubLicense', - 'Get-GitHubMilestone', - 'Get-GitHubOrganizationMember', - 'Get-GitHubPathTraffic', - 'Get-GitHubProject', - 'Get-GitHubProjectCard', - 'Get-GitHubProjectColumn', - 'Get-GitHubPullRequest', - 'Get-GitHubRateLimit', - 'Get-GitHubReference', - 'Get-GitHubReferrerTraffic', - 'Get-GitHubRelease', - 'Get-GitHubRepository', - 'Get-GitHubRepositoryBranch', - 'Get-GitHubRepositoryCollaborator', - 'Get-GitHubRepositoryContributor', - 'Get-GitHubRepositoryFork', - 'Get-GitHubRepositoryLanguage', - 'Get-GitHubRepositoryTag', - 'Get-GitHubRepositoryTopic', - 'Get-GitHubRepositoryUniqueContributor', - 'Get-GitHubTeam', - 'Get-GitHubTeamMember', - 'Get-GitHubUser', - 'Get-GitHubUserContextualInformation', - 'Get-GitHubViewTraffic', - 'Group-GitHubIssue', - 'Group-GitHubPullRequest', - 'Invoke-GHRestMethod', - 'Invoke-GHRestMethodMultipleResult', - 'Lock-GitHubIssue', - 'Move-GitHubProjectCard', - 'Move-GitHubProjectColumn', - 'Move-GitHubRepositoryOwnership', - 'New-GithubAssignee', - 'New-GitHubComment', - 'New-GitHubIssue', - 'New-GitHubLabel', - 'New-GitHubMilestone', - 'New-GitHubProject', - 'New-GitHubProjectCard', - 'New-GitHubProjectColumn', - 'New-GitHubPullRequest', - 'New-GithubReference', - 'New-GitHubRepository', - 'New-GitHubRepositoryFork', - 'Remove-GithubAssignee', - 'Remove-GitHubComment', - 'Remove-GitHubIssueLabel', - 'Remove-GitHubLabel', - 'Remove-GitHubMilestone', - 'Remove-GitHubProject', - 'Remove-GitHubProjectCard', - 'Remove-GitHubProjectColumn', - 'Remove-GitHubReference', - 'Remove-GitHubRepository', - 'Rename-GitHubRepository', - 'Reset-GitHubConfiguration', - 'Restore-GitHubConfiguration', - 'Set-GitHubAuthentication', - 'Set-GitHubComment', - 'Set-GitHubConfiguration', - 'Set-GitHubIssueLabel', - 'Set-GitHubLabel', - 'Set-GitHubMilestone', - 'Set-GitHubProject', - 'Set-GitHubProjectCard', - 'Set-GitHubProjectColumn', - 'Set-GitHubRepositoryTopic', - 'Split-GitHubUri', - 'Test-GitHubAssignee', - 'Test-GitHubAuthenticationConfigured', - 'Test-GitHubOrganizationMember', - 'Unlock-GitHubIssue', - 'Update-GitHubCurrentUser', - 'Update-GitHubIssue', - 'Update-GitHubLabel', - 'Update-GithubReference', - 'Update-GitHubRepository' - ) - - AliasesToExport = @( - 'Delete-GitHubComment', - 'Delete-GitHubLabel', - 'Delete-GitHubMilestone', - 'Delete-GitHubProject', - 'Delete-GitHubProjectCard', - 'Delete-GitHubProjectColumn' - 'Delete-GitHubReference', - 'Delete-GitHubRepository', - 'Get-GitHubBranch', - 'Transfer-GitHubRepositoryOwnership' - ) - - # Cmdlets to export from this module - # CmdletsToExport = '*' - - # Variables to export from this module - # VariablesToExport = '*' - - # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. - PrivateData = @{ - - PSData = @{ - - # Tags applied to this module. These help with module discovery in online galleries. - Tags = @('GitHub', 'API', 'PowerShell') - - # A URL to the license for this module. - LicenseUri = 'https://aka.ms/PowerShellForGitHub_License' - - # A URL to the main website for this project. - ProjectUri = 'https://aka.ms/PowerShellForGitHub' - - # A URL to an icon representing this module. - # IconUri = '' - - # ReleaseNotes of this module - ReleaseNotes = 'https://github.com/microsoft/PowerShellForGitHub/blob/master/CHANGELOG.md' - } - } - - # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. - # DefaultCommandPrefix = 'GH' - - # Modules that must be imported into the global environment prior to importing this module - # RequiredModules = @() - - # Assemblies that must be loaded prior to importing this module - # RequiredAssemblies = @() - - # Script files (.ps1) that are run in the caller's environment prior to importing this module. - # ScriptsToProcess = @() - - # List of all modules packaged with this module - # ModuleList = @() - - # List of all files packaged with this module - # FileList = @() - - # HelpInfo URI of this module - # HelpInfoURI = '' -} +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +@{ + GUID = '9e8dfd44-f782-445a-883c-70614f71519c' + Author = 'Microsoft Corporation' + CompanyName = 'Microsoft Corporation' + Copyright = 'Copyright (C) Microsoft Corporation. All rights reserved.' + + ModuleVersion = '0.14.0' + Description = 'PowerShell wrapper for GitHub API' + + # Script module or binary module file associated with this manifest. + RootModule = 'PowerShellForGitHub.psm1' + + # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess + NestedModules = @( + # Ideally this list would be kept completely alphabetical, but other scripts (like + # GitHubConfiguration.ps1) depend on some of the code in Helpers being around at load time. + 'Helpers.ps1', + 'GitHubConfiguration.ps1', + + 'GitHubAnalytics.ps1', + 'GitHubAssignees.ps1', + 'GitHubBranches.ps1', + 'GitHubCore.ps1', + 'GitHubComments.ps1', + 'GitHubContents.ps1', + 'GitHubEvents.ps1', + 'GitHubIssues.ps1', + 'GitHubLabels.ps1', + 'GitHubMilestones.ps1', + 'GitHubMiscellaneous.ps1', + 'GitHubOrganizations.ps1', + 'GitHubProjects.ps1', + 'GitHubProjectCards.ps1', + 'GitHubProjectColumns.ps1', + 'GitHubPullRequests.ps1', + 'GitHubReleases.ps1', + 'GitHubRepositories.ps1', + 'GitHubRepositoryForks.ps1', + 'GitHubRepositoryTraffic.ps1', + 'GitHubTeams.ps1', + 'GitHubUsers.ps1', + 'Telemetry.ps1', + 'UpdateCheck.ps1') + + # Minimum version of the Windows PowerShell engine required by this module + PowerShellVersion = '4.0' + + # Functions to export from this module + FunctionsToExport = @( + 'Add-GitHubIssueLabel', + 'Backup-GitHubConfiguration', + 'Clear-GitHubAuthentication', + 'ConvertFrom-GitHubMarkdown', + 'Get-GitHubAssignee', + 'Get-GitHubCloneTraffic', + 'Get-GitHubCodeOfConduct', + 'Get-GitHubComment', + 'Get-GitHubConfiguration', + 'Get-GitHubContent', + 'Get-GitHubEmoji', + 'Get-GitHubEvent', + 'Get-GitHubGitIgnore', + 'Get-GitHubIssue', + 'Get-GitHubIssueTimeline', + 'Get-GitHubLabel', + 'Get-GitHubLicense', + 'Get-GitHubMilestone', + 'Get-GitHubOrganizationMember', + 'Get-GitHubPathTraffic', + 'Get-GitHubProject', + 'Get-GitHubProjectCard', + 'Get-GitHubProjectColumn', + 'Get-GitHubPullRequest', + 'Get-GitHubRateLimit', + 'Get-GitHubReferrerTraffic', + 'Get-GitHubRelease', + 'Get-GitHubRepository', + 'Get-GitHubRepositoryBranch', + 'Get-GitHubRepositoryCollaborator', + 'Get-GitHubRepositoryContributor', + 'Get-GitHubRepositoryFork', + 'Get-GitHubRepositoryLanguage', + 'Get-GitHubRepositoryTag', + 'Get-GitHubRepositoryTopic', + 'Get-GitHubRepositoryUniqueContributor', + 'Get-GitHubTeam', + 'Get-GitHubTeamMember', + 'Get-GitHubUser', + 'Get-GitHubUserContextualInformation', + 'Get-GitHubViewTraffic', + 'Group-GitHubIssue', + 'Group-GitHubPullRequest', + 'Invoke-GHRestMethod', + 'Invoke-GHRestMethodMultipleResult', + 'Lock-GitHubIssue', + 'Move-GitHubProjectCard', + 'Move-GitHubProjectColumn', + 'Move-GitHubRepositoryOwnership', + 'New-GithubAssignee', + 'New-GitHubComment', + 'New-GitHubIssue', + 'New-GitHubLabel', + 'New-GitHubMilestone', + 'New-GitHubProject', + 'New-GitHubProjectCard', + 'New-GitHubProjectColumn', + 'New-GitHubPullRequest', + 'New-GitHubRepository', + 'New-GitHubRepositoryFork', + 'Remove-GithubAssignee', + 'Remove-GitHubComment', + 'Remove-GitHubIssueLabel', + 'Remove-GitHubLabel', + 'Remove-GitHubMilestone', + 'Remove-GitHubProject', + 'Remove-GitHubProjectCard', + 'Remove-GitHubProjectColumn', + 'Remove-GitHubRepository', + 'Rename-GitHubRepository', + 'Reset-GitHubConfiguration', + 'Restore-GitHubConfiguration', + 'Set-GitHubAuthentication', + 'Set-GitHubComment', + 'Set-GitHubConfiguration', + 'Set-GitHubIssueLabel', + 'Set-GitHubLabel', + 'Set-GitHubMilestone', + 'Set-GitHubProject', + 'Set-GitHubProjectCard', + 'Set-GitHubProjectColumn', + 'Set-GitHubRepositoryTopic', + 'Split-GitHubUri', + 'Test-GitHubAssignee', + 'Test-GitHubAuthenticationConfigured', + 'Test-GitHubOrganizationMember', + 'Unlock-GitHubIssue', + 'Update-GitHubCurrentUser', + 'Update-GitHubIssue', + 'Update-GitHubLabel', + 'Update-GitHubRepository' + ) + + AliasesToExport = @( + 'Delete-GitHubComment', + 'Delete-GitHubLabel', + 'Delete-GitHubMilestone', + 'Delete-GitHubProject', + 'Delete-GitHubProjectCard', + 'Delete-GitHubProjectColumn' + 'Delete-GitHubRepository', + 'Get-GitHubBranch', + 'Transfer-GitHubRepositoryOwnership' + ) + + # Cmdlets to export from this module + # CmdletsToExport = '*' + + # Variables to export from this module + # VariablesToExport = '*' + + # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. + PrivateData = @{ + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + Tags = @('GitHub', 'API', 'PowerShell') + + # A URL to the license for this module. + LicenseUri = 'https://aka.ms/PowerShellForGitHub_License' + + # A URL to the main website for this project. + ProjectUri = 'https://aka.ms/PowerShellForGitHub' + + # A URL to an icon representing this module. + # IconUri = '' + + # ReleaseNotes of this module + ReleaseNotes = 'https://github.com/microsoft/PowerShellForGitHub/blob/master/CHANGELOG.md' + } + } + + # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. + # DefaultCommandPrefix = 'GH' + + # Modules that must be imported into the global environment prior to importing this module + # RequiredModules = @() + + # Assemblies that must be loaded prior to importing this module + # RequiredAssemblies = @() + + # Script files (.ps1) that are run in the caller's environment prior to importing this module. + # ScriptsToProcess = @() + + # List of all modules packaged with this module + # ModuleList = @() + + # List of all files packaged with this module + # FileList = @() + + # HelpInfo URI of this module + # HelpInfoURI = '' +} From 03ac63c9d6459adaf870c229b14233237c9ef97a Mon Sep 17 00:00:00 2001 From: Raghav S Date: Sun, 7 Jun 2020 23:54:22 +0530 Subject: [PATCH 4/6] Fix tests --- GitHubReferences.ps1 | 25 +++++++++++++++++-------- PowerShellForGitHub.psd1 | 1 + Tests/GitHubReferences.Tests.ps1 | 24 ++++++++++++------------ 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/GitHubReferences.ps1 b/GitHubReferences.ps1 index e175f7d4..5626aae5 100644 --- a/GitHubReferences.ps1 +++ b/GitHubReferences.ps1 @@ -57,10 +57,14 @@ function Get-GitHubReference Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch master .EXAMPLE - Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch master -MatchPrefix + Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch powershell -MatchPrefix + + Get the branch 'powershell' and if it doesn't exist, get all branches beginning with 'powershell' .EXAMPLE - Get-GitHubReference -OwnerName microsoft -RepositoryName -All + Get-GitHubReference -Uri https://github.com/You/YourRepo -All + + Get all references in the repository specified by URI #> [CmdletBinding( SupportsShouldProcess, @@ -128,7 +132,7 @@ function Get-GitHubReference $reference = Resolve-Reference $Tag $Branch if ($MatchPrefix) { - $uriFragment = $uriFragment + "/matching-refs/$reference" + $uriFragment = $uriFragment + "/matching-refs/$reference" $description = "Getting references matching $reference for $RepositoryName" } else @@ -148,7 +152,6 @@ function Get-GitHubReference 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } - Write-Host $uriFragment return Invoke-GHRestMethodMultipleResult @params } @@ -202,7 +205,7 @@ function New-GitHubReference New-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag powershellTagV1 -Sha aa218f56b14c9653891f9e74264a383fa43fefbd .EXAMPLE - New-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + New-GitHubReference -Uri https://github.com/You/YourRepo -Branch master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd #> [CmdletBinding(SupportsShouldProcess)] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] @@ -316,7 +319,11 @@ function Update-GitHubReference If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Update-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Reference heads/master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + Update-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch myBranch -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + + .EXAMPLE + Update-GithubReference -Uri https://github.com/You/YourRepo -Tag myTag -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + #> [CmdletBinding( SupportsShouldProcess, @@ -431,10 +438,12 @@ function Remove-GitHubReference Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag powershellTagV1 .EXAMPLE - Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch master + Remove-GitHubReference -Uri https://github.com/You/YourRepo -Branch master .EXAMPLE - Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch milestone1 -Confirm:$false + Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag milestone1 -Confirm:$false + + Remove the tag milestone1 without prompting for confirmation #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact="High")] [Alias('Delete-GitHubReference')] diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 5ef27773..7910789d 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -77,6 +77,7 @@ 'Get-GitHubRateLimit', 'Get-GitHubReferrerTraffic', 'Get-GitHubRelease', + 'Get-GitHubReference', 'Get-GitHubRepository', 'Get-GitHubRepositoryBranch', 'Get-GitHubRepositoryCollaborator', diff --git a/Tests/GitHubReferences.Tests.ps1 b/Tests/GitHubReferences.Tests.ps1 index e9a07521..99663f3d 100644 --- a/Tests/GitHubReferences.Tests.ps1 +++ b/Tests/GitHubReferences.Tests.ps1 @@ -69,7 +69,7 @@ try } } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false } Describe 'Create a new tag in a repository' { @@ -104,7 +104,7 @@ try } } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false } Describe 'Getting branch(es) from repository' { @@ -174,7 +174,7 @@ try } } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false } Describe 'Getting tag(s) from repository' { @@ -221,8 +221,8 @@ try Context 'On getting tags by prefix from a repository' { $tag1 = "elements_" + $([Guid]::NewGuid().ToString()) $tag2 = "elements_" + $([Guid]::NewGuid().ToString()) - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $tag1 -Sha $sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $tag2 -Sha $sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag1 -Sha $sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag2 -Sha $sha $references = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag "elements" -MatchPrefix @@ -241,12 +241,12 @@ try $references = Get-GitHubReference -Uri $repo.svn_url -Tag "uri" -MatchPrefix It 'Should return all tags matching the prefix' { - $expected = @("refs/tags/$branch1", "refs/tags/$branch2") + $expected = @("refs/tags/$tag1", "refs/tags/$tag2") $references.ref | Should Be $expected } } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false } Describe 'Getting all references from repository' { $repositoryName = [Guid]::NewGuid() @@ -276,7 +276,7 @@ try } } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false } Describe 'Delete a branch from a repository' { @@ -333,7 +333,7 @@ try } } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false } Describe 'Delete a Tag from a Repository' { @@ -389,7 +389,7 @@ try } } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false } Describe 'Update a branch in a repository' { @@ -420,7 +420,7 @@ try } } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false } Describe 'Update a tag in a repository' { @@ -451,7 +451,7 @@ try } } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false } } } From 7317e943d891e006290fb5f961eb2447b51516a9 Mon Sep 17 00:00:00 2001 From: Raghav S Date: Sun, 7 Jun 2020 23:56:48 +0530 Subject: [PATCH 5/6] Add methods to manifest --- PowerShellForGitHub.psd1 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 7910789d..bee85d84 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -37,6 +37,7 @@ 'GitHubProjectColumns.ps1', 'GitHubPullRequests.ps1', 'GitHubReleases.ps1', + 'GitHubReferences.ps1', 'GitHubRepositories.ps1', 'GitHubRepositoryForks.ps1', 'GitHubRepositoryTraffic.ps1', @@ -109,6 +110,7 @@ 'New-GitHubProjectCard', 'New-GitHubProjectColumn', 'New-GitHubPullRequest', + 'New-GitHubReference', 'New-GitHubRepository', 'New-GitHubRepositoryFork', 'Remove-GithubAssignee', @@ -119,6 +121,7 @@ 'Remove-GitHubProject', 'Remove-GitHubProjectCard', 'Remove-GitHubProjectColumn', + 'Remove-GitHubReference', 'Remove-GitHubRepository', 'Rename-GitHubRepository', 'Reset-GitHubConfiguration', @@ -141,6 +144,7 @@ 'Update-GitHubCurrentUser', 'Update-GitHubIssue', 'Update-GitHubLabel', + 'Update-GitHubReference', 'Update-GitHubRepository' ) @@ -150,7 +154,8 @@ 'Delete-GitHubMilestone', 'Delete-GitHubProject', 'Delete-GitHubProjectCard', - 'Delete-GitHubProjectColumn' + 'Delete-GitHubProjectColumn', + 'Delete-GitHubReference', 'Delete-GitHubRepository', 'Get-GitHubBranch', 'Transfer-GitHubRepositoryOwnership' From 458e6dda5db38680ea19618c8076cdb9be84714c Mon Sep 17 00:00:00 2001 From: Raghav S Date: Sun, 19 Jul 2020 21:52:25 +0530 Subject: [PATCH 6/6] Fix review comments and add pipeline support This change adds pipeline support for the GitHubReference module and in addition * Fixes naming of variables and methods * Adds comment based help for all methods * Removes the temporary method used in testing for updating a branch and uses Set-Content instead * Updates test case to add pipeline scenarios * Verified by running test cases and ScriptAnalyzer --- GitHubReferences.ps1 | 566 ++++++++++++++---- PowerShellForGitHub.psd1 | 2 +- Tests/GitHubReferences.Tests.ps1 | 981 +++++++++++++++++++++---------- 3 files changed, 1106 insertions(+), 443 deletions(-) diff --git a/GitHubReferences.ps1 b/GitHubReferences.ps1 index 5626aae5..5dc6cecf 100644 --- a/GitHubReferences.ps1 +++ b/GitHubReferences.ps1 @@ -1,7 +1,13 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -function Get-GitHubReference +@{ + GitHubReferenceTypeName = 'GitHub.Reference' + }.GetEnumerator() | ForEach-Object { + Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value + } + +filter Get-GitHubReference { <# .SYNOPSIS @@ -9,6 +15,7 @@ function Get-GitHubReference .DESCRIPTION Retrieve a reference from a given GitHub repository. + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub .PARAMETER OwnerName @@ -24,15 +31,12 @@ function Get-GitHubReference The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER Tag + .PARAMETER TagName The name of the Tag to be retrieved. - .PARAMETER Branch + .PARAMETER BranchName The name of the Branch to be retrieved. - .PARAMETER All - If provided, will retrieve both branches and tags for the given repository - .PARAMETER MatchPrefix If provided, this will return matching preferences for the given branch/tag name in case an exact match is not found @@ -42,61 +46,109 @@ function Get-GitHubReference .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 + 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. + .INPUTS + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Reaction + GitHub.Release + GitHub.Repository + .OUTPUTS - [PSCustomObject] + GitHub.Reference Details of the git reference in the given repository .EXAMPLE - Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag powershellTagV1 + Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -TagName powershellTagV1 .EXAMPLE - Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch master + Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -BranchName master .EXAMPLE - Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch powershell -MatchPrefix + Get-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -BranchName powershell -MatchPrefix Get the branch 'powershell' and if it doesn't exist, get all branches beginning with 'powershell' .EXAMPLE - Get-GitHubReference -Uri https://github.com/You/YourRepo -All + $repo = Get-GitHubRepository -OwnerName microsoft -RepositoryName PowerShellForGitHub + $repo | Get-GitHubReference -BranchName powershell + + Get details of the "powershell" branch in the repository - Get all references in the repository specified by URI #> [CmdletBinding( SupportsShouldProcess, - DefaultParameterSetName='Elements')] + DefaultParameterSetName='Uri')] [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, ParameterSetName='BranchElements')] - [Parameter(Mandatory, ParameterSetName='TagElements')] - [Parameter(Mandatory, ParameterSetName='Elements')] + [Parameter( + Mandatory, + ParameterSetName='BranchElements')] + [Parameter( + Mandatory, + ParameterSetName='TagElements')] + [Parameter( + Mandatory, + ParameterSetName='Elements')] [string] $OwnerName, - [Parameter(Mandatory, ParameterSetName='BranchElements')] - [Parameter(Mandatory, ParameterSetName='TagElements')] - [Parameter(Mandatory, ParameterSetName='Elements')] + [Parameter( + Mandatory, + ParameterSetName='BranchElements')] + [Parameter( + Mandatory, + ParameterSetName='TagElements')] + [Parameter( + Mandatory, + ParameterSetName='Elements')] [string] $RepositoryName, - [Parameter(Mandatory, ParameterSetName='BranchUri')] - [Parameter(Mandatory, ParameterSetName='TagUri')] - [Parameter(Mandatory, ParameterSetName='Uri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='BranchUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='TagUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='Uri')] + [Alias('RepositoryUrl')] [string] $Uri, - [Parameter(ParameterSetName='TagUri')] - [Parameter(ParameterSetName='TagElements')] - [string] $Tag, - - [Parameter(ParameterSetName='BranchUri')] - [Parameter(ParameterSetName='BranchElements')] - [string] $Branch, - - [Parameter(ParameterSetName='Elements')] - [Parameter(ParameterSetName='Uri')] - [switch] $All, + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='TagUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='TagElements')] + [string] $TagName, + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='BranchUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='BranchElements')] + [string] $BranchName, [Parameter(ParameterSetName='BranchUri')] [Parameter(ParameterSetName='BranchElements')] @@ -109,37 +161,38 @@ function Get-GitHubReference [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog - $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'ProvidedReference' = $PSBoundParameters.ContainsKey('Reference') } $uriFragment = "repos/$OwnerName/$RepositoryName/git" + $reference = Resolve-GitHubReference -TagName $TagName -BranchName $BranchName - if ($All) { + if ([String]::IsNullOrEmpty($reference)) + { # Add a slash at the end as Invoke-GHRestMethod removes the last trailing slash. Calling this API without the slash causes a 404 $uriFragment = $uriFragment + "/matching-refs//" - $description = "Getting all references for $RepositoryName" + $description = "Getting all references for $RepositoryName" } - else { - $reference = Resolve-Reference $Tag $Branch - - if ($MatchPrefix) { + else + { + if ($MatchPrefix) + { $uriFragment = $uriFragment + "/matching-refs/$reference" - $description = "Getting references matching $reference for $RepositoryName" + $description = "Getting references matching $reference for $RepositoryName" } else { # We want to return an exact match, call the 'get single reference' API $uriFragment = $uriFragment + "/ref/$reference" - $description = "Getting reference $reference for $RepositoryName" + $description = "Getting reference $reference for $RepositoryName" } } @@ -152,10 +205,10 @@ function Get-GitHubReference 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } - return Invoke-GHRestMethodMultipleResult @params + return (Invoke-GHRestMethodMultipleResult @params | Add-GitHubBranchAdditionalProperties) } -function New-GitHubReference +filter New-GitHubReference { <# .SYNOPSIS @@ -163,6 +216,7 @@ function New-GitHubReference .DESCRIPTION Create a reference in a given GitHub repository. + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub .PARAMETER OwnerName @@ -178,10 +232,10 @@ function New-GitHubReference The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER Tag + .PARAMETER TagName The name of the Tag to be created. - .PARAMETER Branch + .PARAMETER BranchName The name of the Branch to be created. .PARAMETER Sha @@ -197,38 +251,83 @@ function New-GitHubReference 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.Reaction + GitHub.Release + GitHub.Repository + .OUTPUTS - [PSCustomObject] + GitHub.Reference Details of the git reference created. Throws an Exception if the reference already exists .EXAMPLE - New-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag powershellTagV1 -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + New-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -TagName powershellTagV1 -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + + .EXAMPLE + New-GitHubReference -Uri https://github.com/You/YourRepo -BranchName master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd .EXAMPLE - New-GitHubReference -Uri https://github.com/You/YourRepo -Branch master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + $repo = Get-GitHubRepository -OwnerName microsoft -RepositoryName PowerShellForGitHub + $repo | New-GitHubReference -BranchName powershell -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + + Create a new branch named "powershell" in the given 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, ParameterSetName='BranchElements')] - [Parameter(Mandatory, ParameterSetName='TagElements')] + [Parameter( + Mandatory, + ParameterSetName='BranchElements')] + [Parameter( + Mandatory, + ParameterSetName='TagElements')] [string] $OwnerName, - [Parameter(Mandatory, ParameterSetName='BranchElements')] - [Parameter(Mandatory, ParameterSetName='TagElements')] + [Parameter( + Mandatory, + ParameterSetName='BranchElements')] + [Parameter( + Mandatory, + ParameterSetName='TagElements')] [string] $RepositoryName, - [Parameter(Mandatory, ParameterSetName='BranchUri')] - [Parameter(Mandatory, ParameterSetName='TagUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='BranchUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='TagUri')] + [Alias('RepositoryUrl')] [string] $Uri, - [Parameter(Mandatory, ParameterSetName='TagUri')] - [Parameter(Mandatory, ParameterSetName='TagElements')] - [string] $Tag, - - [Parameter(Mandatory, ParameterSetName='BranchUri')] - [Parameter(Mandatory, ParameterSetName='BranchElements')] - [string] $Branch, + [Parameter( + Mandatory, + ParameterSetName='TagUri')] + [Parameter( + Mandatory, + ParameterSetName='TagElements')] + [string] $TagName, + + [Parameter( + Mandatory, + ParameterSetName='BranchUri')] + [Parameter( + Mandatory, + ParameterSetName='BranchElements')] + [string] $BranchName, [Parameter(Mandatory)] [string] $Sha, @@ -238,9 +337,9 @@ function New-GitHubReference [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog - $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName @@ -249,10 +348,10 @@ function New-GitHubReference 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } - $reference = Resolve-Reference $Tag $Branch + $reference = Resolve-GitHubReference -TagName $TagName -BranchName $BranchName $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs" - $description = "Creating Reference $reference for $RepositoryName from SHA $Sha" + $description = "Creating Reference $reference for $RepositoryName from SHA $Sha" $hashBody = @{ 'ref' = "refs/" + $reference @@ -270,10 +369,10 @@ function New-GitHubReference 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } - return Invoke-GHRestMethod @params + return (Invoke-GHRestMethod @params | Add-GitHubBranchAdditionalProperties) } -function Update-GitHubReference +filter Set-GithubReference { <# .SYNOPSIS @@ -281,6 +380,7 @@ function Update-GitHubReference .DESCRIPTION Update a reference in a given GitHub repository. + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub .PARAMETER OwnerName @@ -296,17 +396,18 @@ function Update-GitHubReference The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER Tag + .PARAMETER TagName The name of the tag to be updated to the given SHA. - .PARAMETER Branch + .PARAMETER BranchName The name of the branch to be updated to the given SHA. .PARAMETER Sha The updated SHA1 value to be set for this reference. .PARAMETER Force - Indicates whether to force the update. If not set it will ensure that the update is a fast-forward update. + If not set, the update will only occur if it is a fast-forward update. + Not specifying this (or setting it to $false) will make sure you're not overwriting work. .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the @@ -318,37 +419,94 @@ function Update-GitHubReference 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.Reference + GitHub.Repository + + .OUTPUTS + GitHub.Reference + + .EXAMPLE + Set-GithubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -BranchName myBranch -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + + .EXAMPLE + Set-GithubReference -Uri https://github.com/You/YourRepo -TagName myTag -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + .EXAMPLE - Update-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Branch myBranch -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + Set-GithubReference -Uri https://github.com/You/YourRepo -TagName myTag -Sha aa218f56b14c9653891f9e74264a383fa43fefbd -Force + + Force an update even if it is not a fast-forward update .EXAMPLE - Update-GithubReference -Uri https://github.com/You/YourRepo -Tag myTag -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + $repo = Get-GitHubRepository -OwnerName microsoft -RepositoryName PowerShellForGitHub + $ref = $repo | Get-GitHubReference -BranchName powershell + $ref | Set-GithubReference -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + Get the "powershell" branch from the given repo and update its SHA #> [CmdletBinding( SupportsShouldProcess, - DefaultParametersetName='Elements')] + DefaultParametersetName='Uri')] [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, ParameterSetName='BranchElements')] - [Parameter(Mandatory, ParameterSetName='TagElements')] + [Parameter( + Mandatory, + ParameterSetName='BranchElements')] + [Parameter( + Mandatory, + ParameterSetName='TagElements')] [string] $OwnerName, - [Parameter(Mandatory, ParameterSetName='BranchElements')] - [Parameter(Mandatory, ParameterSetName='TagElements')] + [Parameter( + Mandatory, + ParameterSetName='BranchElements')] + [Parameter( + Mandatory, + ParameterSetName='TagElements')] [string] $RepositoryName, - [Parameter(Mandatory, ParameterSetName='BranchUri')] - [Parameter(Mandatory, ParameterSetName='TagUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='BranchUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='TagUri')] + [Alias('RepositoryUrl')] [string] $Uri, - [Parameter(Mandatory, ParameterSetName='TagUri')] - [Parameter(Mandatory, ParameterSetName='TagElements')] - [string] $Tag, - - [Parameter(Mandatory, ParameterSetName='BranchUri')] - [Parameter(Mandatory, ParameterSetName='BranchElements')] - [string] $Branch, + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='TagUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='TagElements')] + [string] $TagName, + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='BranchUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='BranchElements')] + [string] $BranchName, [Parameter(Mandatory)] [string] $Sha, @@ -360,9 +518,9 @@ function Update-GitHubReference [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog - $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName @@ -371,10 +529,10 @@ function Update-GitHubReference 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } - $reference = Resolve-Reference $Tag $Branch + $reference = Resolve-GitHubReference -TagName $TagName -BranchName $BranchName $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/$reference" - $description = "Updating SHA for Reference $reference in $RepositoryName to $Sha" + $description = "Updating SHA for Reference $reference in $RepositoryName to $Sha" $hashBody = @{ 'force' = $Force.IsPresent @@ -392,10 +550,10 @@ function Update-GitHubReference 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } - return Invoke-GHRestMethod @params + return (Invoke-GHRestMethod @params | Add-GitHubBranchAdditionalProperties) } -function Remove-GitHubReference +filter Remove-GitHubReference { <# .SYNOPSIS @@ -403,6 +561,7 @@ function Remove-GitHubReference .DESCRIPTION Delete a reference in a given GitHub repository. + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub .PARAMETER OwnerName @@ -418,12 +577,15 @@ function Remove-GitHubReference The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER Tag + .PARAMETER TagName The name of the tag to be deleted. - .PARAMETER Branch + .PARAMETER BranchName The name of the branch to be deleted. + .PARAMETER Force + If this switch is specified, you will not be prompted for confirmation of command execution. + .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. @@ -434,40 +596,98 @@ function Remove-GitHubReference 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.Reference + GitHub.Repository + + .OUTPUTS + None + .EXAMPLE - Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag powershellTagV1 + Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -TagName powershellTagV1 .EXAMPLE - Remove-GitHubReference -Uri https://github.com/You/YourRepo -Branch master + Remove-GitHubReference -Uri https://github.com/You/YourRepo -BranchName master .EXAMPLE - Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag milestone1 -Confirm:$false + Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -TagName milestone1 -Confirm:$false + Remove the tag milestone1 without prompting for confirmation + .EXAMPLE + Remove-GitHubReference -OwnerName microsoft -RepositoryName PowerShellForGitHub -TagName milestone1 -Force Remove the tag milestone1 without prompting for confirmation + + .EXAMPLE + $repo = Get-GitHubRepository -OwnerName microsoft -RepositoryName PowerShellForGitHub + $ref = $repo | Get-GitHubReference -TagName powershellV1 + $ref | Remove-GithubReference + + Get a reference to the "powershellV1" tag using Get-GithubReference on the repository. Pass it to this method in order to remove it #> - [CmdletBinding(SupportsShouldProcess, ConfirmImpact="High")] + [CmdletBinding( + SupportsShouldProcess, + ConfirmImpact="High")] [Alias('Delete-GitHubReference')] [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, ParameterSetName='BranchElements')] - [Parameter(Mandatory, ParameterSetName='TagElements')] + [Parameter( + Mandatory, + ParameterSetName='BranchElements')] + [Parameter( + Mandatory, + ParameterSetName='TagElements')] [string] $OwnerName, - [Parameter(Mandatory, ParameterSetName='BranchElements')] - [Parameter(Mandatory, ParameterSetName='TagElements')] + [Parameter( + Mandatory, + ParameterSetName='BranchElements')] + [Parameter( + Mandatory, + ParameterSetName='TagElements')] [string] $RepositoryName, - [Parameter(Mandatory, ParameterSetName='BranchUri')] - [Parameter(Mandatory, ParameterSetName='TagUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='BranchUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='TagUri')] + [Alias('RepositoryUrl')] [string] $Uri, - [Parameter(Mandatory, ParameterSetName='TagUri')] - [Parameter(Mandatory, ParameterSetName='TagElements')] - [string] $Tag, - - [Parameter(Mandatory, ParameterSetName='BranchUri')] - [Parameter(Mandatory, ParameterSetName='BranchElements')] - [string] $Branch, + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='TagUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='TagElements')] + [string] $TagName, + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='BranchUri')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='BranchElements')] + [string] $BranchName, [string] $AccessToken, @@ -475,12 +695,16 @@ function Remove-GitHubReference ) $repositoryInfoForDisplayMessage = if ($PSCmdlet.ParameterSetName -eq "Uri") { $Uri } else { $OwnerName, $RepositoryName -join "/" } - $reference = Resolve-Reference $Tag $Branch + $reference = Resolve-GitHubReference -TagName $TagName -BranchName $BranchName + if ($Force -and (-not $Confirm)) + { + $ConfirmPreference = 'None' + } if ($PSCmdlet.ShouldProcess($repositoryInfoForDisplayMessage, "Remove reference: $reference")) { - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog - $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName @@ -490,7 +714,7 @@ function Remove-GitHubReference } $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/$reference" - $description = "Deleting Reference $reference from repository $RepositoryName" + $description = "Deleting Reference $reference from repository $RepositoryName" $params = @{ 'UriFragment' = $uriFragment @@ -505,21 +729,109 @@ function Remove-GitHubReference return Invoke-GHRestMethod @params } - } -function Resolve-Reference($Tag, $Branch) +filter Add-GitHubBranchAdditionalProperties { - if (-not [String]::IsNullOrEmpty($Tag)) +<# + .SYNOPSIS + Adds type name and additional properties to ease pipelining to GitHub Branch objects. + + .PARAMETER InputObject + The GitHub object to add additional properties to. + + .PARAMETER TypeName + The type that should be assigned to the object. + + .INPUTS + [PSCustomObject] + + .OUTPUTS + GitHub.Reference +#> + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification="Internal helper that is definitely adding more than one property.")] + param( + [Parameter( + Mandatory, + ValueFromPipeline)] + [AllowNull()] + [AllowEmptyCollection()] + [PSCustomObject[]] $InputObject, + + [ValidateNotNullOrEmpty()] + [string] $TypeName = $script:GitHubReferenceTypeName + ) + + foreach ($item in $InputObject) { - return "tags/$Tag" + $item.PSObject.TypeNames.Insert(0, $TypeName) + + if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport)) + { + if ($null -ne $item.url) + { + $elements = Split-GitHubUri -Uri $item.url + } + else + { + $elements = Split-GitHubUri -Uri $item.commit.url + } + $repositoryUrl = Join-GitHubUri @elements + + Add-Member -InputObject $item -Name 'RepositoryUrl' -Value $repositoryUrl -MemberType NoteProperty -Force + + if ($item.ref.StartsWith('refs/heads/')) + { + $branchName = $item.ref -replace ('refs/heads/', '') + Add-Member -InputObject $item -Name 'BranchName' -Value $branchName -MemberType NoteProperty -Force + } + if ($item.ref.StartsWith('refs/tags')) + { + $tagName = $item.ref -replace ('refs/tags/', '') + Add-Member -InputObject $item -Name 'TagName' -Value $tagName -MemberType NoteProperty -Force + } + } + + Write-Output $item } - elseif (-not [String]::IsNullOrEmpty($Branch)) +} + +filter Resolve-GitHubReference +{ + <# + .SYNOPSIS + Get the given tag or branch in the form of a Github reference + + .DESCRIPTION + Get the given tag or branch in the form of a Github reference i.e. tags/ for a tag and heads/ for a branch + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER TagName + The tag for which we need the reference string + + .PARAMETER BranchName + The branch for which we need the reference string + + .EXAMPLE + Resolve-GitHubReference -TagName powershellTag + + .EXAMPLE + Resolve-GitHubReference -BranchName powershellBranch + #> + param( + [string] $TagName, + [string] $BranchName + ) + + if (-not [String]::IsNullOrEmpty($TagName)) { - return "heads/$Branch" + return "tags/$TagName" } - else + elseif (-not [String]::IsNullOrEmpty($BranchName)) { - return [string]::Empty + return "heads/$BranchName" } + return [String]::Empty } \ No newline at end of file diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 5e44463d..675b5239 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -167,7 +167,7 @@ 'Update-GitHubCurrentUser', 'Update-GitHubIssue', 'Update-GitHubLabel', - 'Update-GitHubReference', + 'Set-GithubReference', 'Update-GitHubRepository' ) diff --git a/Tests/GitHubReferences.Tests.ps1 b/Tests/GitHubReferences.Tests.ps1 index 99663f3d..977f43c4 100644 --- a/Tests/GitHubReferences.Tests.ps1 +++ b/Tests/GitHubReferences.Tests.ps1 @@ -5,457 +5,808 @@ .Synopsis Tests for GitHubReferences.ps1 module #> +[CmdletBinding()] +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', + Justification='Suppress false positives in Pester code blocks')] +param() # 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') -function Update-MasterSHA() +try { - # Add a new file in master in order to create a new commit and hence, an updated SHA - # TODO: Replace this when the Content updation related APIs are made available - $fileContent = "Hello powershell!" - $fileName = "powershell.txt" - $encodedFile = [System.Convert]::ToBase64String(([System.Text.Encoding]::UTF8).GetBytes($fileContent)) - - $fileData = @{ - "message" = "added new file" - "content" = "$encodedFile" - } - - $params = @{ - 'UriFragment' = "repos/$ownerName/$repositoryName/contents/$fileName" - 'Method' = 'Put' - 'Body' = (ConvertTo-Json -InputObject $fileData) - 'AccessToken' = $AccessToken + # Script-scoped, hidden, readonly variables. + @{ + repoGuid = [Guid]::NewGuid().Guid + masterBranchName = "master" + branchName = [Guid]::NewGuid().ToString() + tagName = [Guid]::NewGuid().ToString() + }.GetEnumerator() | ForEach-Object { + Set-Variable -Force -Scope Script -Option ReadOnly -Visibility Private -Name $_.Key -Value $_.Value } - Invoke-GHRestMethod @params -} -try -{ - if ($accessTokenConfigured) - { - $masterBranchName = "master" - Describe 'Create a new branch in repository' { - $repositoryName = [Guid]::NewGuid() - $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName + Describe 'Create a new branch in repository' { + BeforeAll { + # AutoInit will create a readme with the GUID of the repo name + $repo = New-GitHubRepository -RepositoryName ($repoGuid) -AutoInit + $existingref = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $masterBranchName $sha = $existingref.object.sha + } - Context 'On creating a new branch in a repository from a given SHA' { - $branchName = [Guid]::NewGuid().ToString() - $result = New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $sha + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } - It 'Should successfully create the new branch' { - $result.ref | Should Be "refs/heads/$branchName" - } + Context 'On creating a new branch in a repository from a valid SHA' { + $branch = [Guid]::NewGuid().ToString() + $reference = New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branch -Sha $sha - It 'Should throw an Exception when trying to create it again' { - { New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName -Sha $sha } | Should Throw - } + It 'Should successfully create the new branch and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.BranchName | Should -Be $branch } - Context 'On creating a new branch in a repository (specified by Uri) from a given SHA' { - $branchName = [Guid]::NewGuid().ToString() - $result = New-GitHubReference -Uri $repo.svn_url -Branch $branchName -Sha $sha + It 'Should throw an Exception when trying to create it again' { + { New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branch -Sha $sha } | + Should -Throw + } + } - It 'Should successfully create the branch' { - $result.ref | Should Be "refs/heads/$branchName" - } + Context 'On creating a new branch in a repository (specified by Uri) from a valid SHA' { + $branch = [Guid]::NewGuid().ToString() - It 'Should throw an exception when trying to create it again' { - { New-GitHubReference -Uri $repo.svn_url -Branch $masterBranchName -Sha $sha } | Should Throw - } + $reference = New-GitHubReference -Uri $repo.svn_url -BranchName $branch -Sha $sha + + It 'Should successfully create the new branch and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.BranchName | Should -Be $branch } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false + It 'Should throw an exception when trying to create it again' { + { New-GitHubReference -Uri $repo.svn_url -BranchName $branch -Sha $sha } | Should -Throw + } } - Describe 'Create a new tag in a repository' { - $repositoryName = [Guid]::NewGuid() - $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName - $sha = $existingref.object.sha + Context 'On creating a new branch in a repository with the repo on the pipeline' { + $branch = [Guid]::NewGuid().ToString() - Context 'On creating a new tag in a repository referring to a given SHA' { - $tagName = [Guid]::NewGuid().ToString() - $result = New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Sha $sha + $reference = $repo | New-GitHubReference -BranchName $branch -Sha $sha - It 'Should successfully create the new tag' { - $result.ref | Should Be "refs/tags/$tagName" - } + It 'Should successfully create the new branch and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.BranchName | Should -Be $branch + } - It 'Should throw an Exception when trying to create it again' { - { New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Sha $sha } | Should Throw - } + It 'Should throw an exception when trying to create it again' { + { $repo | New-GitHubReference -BranchName $branch -Sha $sha } | Should -Throw } + } + } - Context 'On creating a new tag in a repository (specified by Uri) from a given SHA' { - $tagName = [Guid]::NewGuid().ToString() - $result = New-GitHubReference -Uri $repo.svn_url -Tag $tagName -Sha $sha + Describe 'Create a new tag in a repository' { + BeforeAll { + # AutoInit will create a readme with the GUID of the repo name + $repo = New-GitHubRepository -RepositoryName ($repoGuid) -AutoInit + $existingref = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $masterBranchName + $sha = $existingref.object.sha + } + + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } - It 'Should successfully create the reference' { - $result.ref | Should Be "refs/tags/$tagName" - } + Context 'On creating a new tag in a repository referring to a given SHA' { + $tag = [Guid]::NewGuid().ToString() + $reference = New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag -Sha $sha - It 'Should throw an exception when trying to create it again' { - { New-GitHubReference -Uri $repo.svn_url -Tag $tagName -Sha $sha } | Should Throw - } + It 'Should successfully create the new tag and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.TagName | Should -Be $tag } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false + It 'Should throw an Exception when trying to create it again' { + { New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag -Sha $sha } | Should -Throw + } } - Describe 'Getting branch(es) from repository' { - $repositoryName = [Guid]::NewGuid() - $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $randomBranchName = [Guid]::NewGuid() - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName - $sha = $existingref.object.sha + Context 'On creating a new tag in a repository (specified by Uri) from a given SHA' { + $tag = [Guid]::NewGuid().ToString() - Context 'On getting an existing branch from a repository' { - $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName + $reference = New-GitHubReference -Uri $repo.svn_url -TagName $tag -Sha $sha - It 'Should return details of the branch' { - $reference.ref | Should be "refs/heads/$masterBranchName" - } + It 'Should successfully create the new tag and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.TagName | Should -Be $tag } - Context 'On getting an non-existent branch from a repository' { + It 'Should throw an exception when trying to create it again' { + { New-GitHubReference -Uri $repo.svn_url -TagName $tag -Sha $sha } | Should -Throw + } + } + + Context 'On creating a new tag in a repository with the repo on the pipeline' { + $tag = [Guid]::NewGuid().ToString() - It 'Should throw an exception' { - { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $randomBranchName } | - Should Throw - } + $reference = $repo | New-GitHubReference -TagName $tag -Sha $sha + + It 'Should successfully create the new tag and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.TagName | Should -Be $tag } - Context 'On getting an existing branch using Uri from a repository' { - $reference = Get-GitHubReference -Uri $repo.svn_url -Branch $masterBranchName + It 'Should throw an exception when trying to create it again' { + { $repo | New-GitHubReference -TagName $tag -Sha $sha } | Should -Throw + } + } + } - It 'Should return details of the branch' { - $reference.ref | Should be "refs/heads/$masterBranchName" - } + Describe 'Getting branch(es) from repository' { + BeforeAll { + # AutoInit will create a readme with the GUID of the repo name + $repo = New-GitHubRepository -RepositoryName ($repoGuid) -AutoInit + $existingref = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $masterBranchName + $sha = $existingref.object.sha + $randomBranchName = [Guid]::NewGuid() + } + + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + + Context 'On getting an existing branch from a repository' { + $reference = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $masterBranchName + + It 'Should return details for that branch and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.BranchName | Should -Be $masterBranchName } + } + + Context 'On getting an non-existent branch from a repository' { + It 'Should throw an exception' { + { Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $randomBranchName } | + Should -Throw + } + } - Context 'On getting an invalid branch using Uri from a repository' { + Context 'On getting an existing branch using Uri from a repository' { + $reference = Get-GitHubReference -Uri $repo.svn_url -BranchName $masterBranchName - It 'Should throw an exception' { - { Get-GitHubReference -Uri $repo.svn_url -Branch $randomBranchName } | - Should Throw - } + It 'Should return details for that branch and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.BranchName | Should -Be $masterBranchName } + } - Context 'On getting branches by prefix from a repository' { - $branch1 = "elements_" + $([Guid]::NewGuid().ToString()) - $branch2 = "elements_" + $([Guid]::NewGuid().ToString()) - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branch1 -Sha $sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branch2 -Sha $sha + Context 'On getting an invalid branch using Uri from a repository' { + It 'Should throw an exception' { + { Get-GitHubReference -Uri $repo.svn_url -BranchName $randomBranchName } | + Should -Throw + } + } - $references = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch "elements" -MatchPrefix + Context 'On getting an existing branch with the repo on the pipeline' { + $reference = $repo | Get-GitHubReference -BranchName $masterBranchName - It 'Should return all branches matching the prefix' { - $expected = @("refs/heads/$branch1", "refs/heads/$branch2") - $references.ref | Should Be $expected - } + It 'Should return details for that branch and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.BranchName | Should -Be $masterBranchName } + } - Context 'On getting branches by prefix using Uri from a repository' { - $branch1 = "uri_" + $([Guid]::NewGuid().ToString()) - $branch2 = "uri_" + $([Guid]::NewGuid().ToString()) - New-GitHubReference -Uri $repo.svn_url -Branch $branch1 -Sha $sha - New-GitHubReference -Uri $repo.svn_url -Branch $branch2 -Sha $sha + Context 'On getting an invalid branch with the repo on the pipeline' { + It 'Should throw an exception' { + { $repo | Get-GitHubReference -BranchName $randomBranchName } | + Should -Throw + } + } - $references = Get-GitHubReference -Uri $repo.svn_url -Branch "uri" -MatchPrefix + Context 'On getting branches by prefix from a repository' { + $branch1 = "elements_A" + $branch2 = "elements_B" + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branch1 -Sha $sha + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branch2 -Sha $sha + + $references = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName "elements" -MatchPrefix + + It 'Should return all branches matching the prefix when an exact match is not found' { + $expected = @($branch1, $branch2) + $references.BranchName | Sort-Object | Should -Be $expected + $references[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[1].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[0].RepositoryUrl | Should -Be $repo.RepositoryUrl + $references[1].RepositoryUrl | Should -Be $repo.RepositoryUrl + } + } - It 'Should return all branches matching the prefix' { - $expected = @("refs/heads/$branch1", "refs/heads/$branch2") - $references.ref | Should Be $expected - } + Context 'On getting branches by prefix using Uri from a repository' { + $branch1 = "uri_A" + $branch2 = "uri_B" + New-GitHubReference -Uri $repo.svn_url -BranchName $branch1 -Sha $sha + New-GitHubReference -Uri $repo.svn_url -BranchName $branch2 -Sha $sha + + $references = Get-GitHubReference -Uri $repo.svn_url -BranchName "uri" -MatchPrefix + + It 'Should return all branches matching the prefix when an exact match is not found' { + $expected = @($branch1, $branch2) + $references.BranchName | Sort-Object | Should -Be $expected + $references[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[1].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[0].RepositoryUrl | Should -Be $repo.RepositoryUrl + $references[1].RepositoryUrl | Should -Be $repo.RepositoryUrl } + } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false + Context 'On getting branches by prefix with the repo on the pipeline' { + $branch1 = "pipeline_A" + $branch2 = "pipeline_B" + $repo | New-GitHubReference -BranchName $branch1 -Sha $sha + $repo | New-GitHubReference -BranchName $branch2 -Sha $sha + + $references = $repo | Get-GitHubReference -BranchName "pipeline" -MatchPrefix + + It 'Should return all branches matching the prefix when an exact match is not found' { + $expected = @($branch1, $branch2) + $references.BranchName | Sort-Object | Should -Be $expected + $references[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[1].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[0].RepositoryUrl | Should -Be $repo.RepositoryUrl + $references[1].RepositoryUrl | Should -Be $repo.RepositoryUrl + } } + } - Describe 'Getting tag(s) from repository' { - $repositoryName = [Guid]::NewGuid() - $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $randomTagName = [Guid]::NewGuid() - $validTag = "master-tag" - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName + Describe 'Getting tag(s) from repository' { + BeforeAll { + # AutoInit will create a readme with the GUID of the repo name + $repo = New-GitHubRepository -RepositoryName ($repoGuid) -AutoInit + $existingref = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $masterBranchName $sha = $existingref.object.sha - New-GitHubReference -Uri $repo.svn_url -Tag $validTag -Sha $sha + $randomTagName = [Guid]::NewGuid() + New-GitHubReference -Uri $repo.svn_url -TagName $tagName -Sha $sha + } - Context 'On getting an existing tag from a repository' { - $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $validTag + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } - It 'Should return details of the tag' { - $reference.ref | Should be "refs/tags/$validTag" - } - } + Context 'On getting an existing tag from a repository' { + $reference = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName - Context 'On getting an non-existent tag from a repository' { + It 'Should return details for that tag and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.TagName | Should -Be $tagName + } + } - It 'Should throw an exception' { - { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $randomTagName } | - Should Throw - } + Context 'On getting an non-existent tag from a repository' { + It 'Should throw an exception' { + { Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $randomTagName } | + Should -Throw } + } - Context 'On getting an existing tag using Uri from a repository' { - $reference = Get-GitHubReference -Uri $repo.svn_url -Tag $validTag + Context 'On getting an existing tag using Uri from a repository' { + $reference = Get-GitHubReference -Uri $repo.svn_url -TagName $tagName + + It 'Should return details for that tag and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.TagName | Should -Be $tagName + } + } - It 'Should return details of the tag' { - $reference.ref | Should be "refs/tags/$validTag" - } + Context 'On getting an invalid tag using Uri from a repository' { + It 'Should throw an exception' { + { Get-GitHubReference -Uri $repo.svn_url -TagName $randomTagName } | + Should -Throw } + } - Context 'On getting an invalid tag using Uri from a repository' { + Context 'On getting an existing tag with the repo on the pipeline' { + $reference = $repo | Get-GitHubReference -TagName $tagName - It 'Should throw an exception' { - { Get-GitHubReference -Uri $repo.svn_url -Tag $randomTagName } | - Should Throw - } + It 'Should return details for that tag and have expected additional properties' { + $reference.PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $reference.RepositoryUrl | Should -Be $repo.RepositoryUrl + $reference.TagName | Should -Be $tagName } + } - Context 'On getting tags by prefix from a repository' { - $tag1 = "elements_" + $([Guid]::NewGuid().ToString()) - $tag2 = "elements_" + $([Guid]::NewGuid().ToString()) - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag1 -Sha $sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag2 -Sha $sha + Context 'On getting an invalid tag using Uri from a repository' { + It 'Should throw an exception' { + { $repo | Get-GitHubReference -TagName $randomTagName } | + Should -Throw + } + } - $references = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag "elements" -MatchPrefix + Context 'On getting tags by prefix from a repository' { + $tag1 = "elements_A" + $tag2 = "elements_B" + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag1 -Sha $sha + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag2 -Sha $sha + + $references = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName "elements" -MatchPrefix + + It 'Should return all branches matching the prefix when an exact match is not found' { + $expected = @($tag1, $tag2) + $references.TagName | Sort-Object | Should -Be $expected + $references[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[1].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[0].RepositoryUrl | Should -Be $repo.RepositoryUrl + $references[1].RepositoryUrl | Should -Be $repo.RepositoryUrl + } + } - It 'Should return all tags matching the prefix' { - $expected = @("refs/tags/$tag1", "refs/tags/$tag2") - $references.ref | Should Be $expected - } + Context 'On getting tags by prefix from a repository specified By Uri' { + $tag1 = "uri_A" + $tag2 = "uri_B" + New-GitHubReference -Uri $repo.svn_url -TagName $tag1 -Sha $sha + New-GitHubReference -Uri $repo.svn_url -TagName $tag2 -Sha $sha + + $references = Get-GitHubReference -Uri $repo.svn_url -TagName "uri" -MatchPrefix + + It 'Should return all branches matching the prefix when an exact match is not found' { + $expected = @($tag1, $tag2) + $references.TagName | Sort-Object | Should -Be $expected + $references[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[1].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[0].RepositoryUrl | Should -Be $repo.RepositoryUrl + $references[1].RepositoryUrl | Should -Be $repo.RepositoryUrl } + } - Context 'On getting tags by prefix from a repository specified By Uri' { - $tag1 = "uri_" + $([Guid]::NewGuid().ToString()) - $tag2 = "uri_" + $([Guid]::NewGuid().ToString()) - New-GitHubReference -Uri $repo.svn_url -Tag $tag1 -Sha $sha - New-GitHubReference -Uri $repo.svn_url -Tag $tag2 -Sha $sha + Context 'On getting tags by prefix with the repo on the pipeline' { + $tag1 = "pipeline_A" + $tag2 = "pipeline_B" + $repo | New-GitHubReference -TagName $tag1 -Sha $sha + $repo | New-GitHubReference -TagName $tag2 -Sha $sha + + $references = Get-GitHubReference -Uri $repo.svn_url -TagName "pipeline" -MatchPrefix + + It 'Should return all branches matching the prefix when an exact match is not found' { + $expected = @($tag1, $tag2) + $references.TagName | Sort-Object | Should -Be $expected + $references[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[1].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[0].RepositoryUrl | Should -Be $repo.RepositoryUrl + $references[1].RepositoryUrl | Should -Be $repo.RepositoryUrl + } + } + } + Describe 'Getting all references from repository' { + BeforeAll { + # AutoInit will create a readme with the GUID of the repo name + $repo = New-GitHubRepository -RepositoryName ($repoGuid) -AutoInit + $existingref = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $masterBranchName + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName -Sha $sha + } - $references = Get-GitHubReference -Uri $repo.svn_url -Tag "uri" -MatchPrefix + AfterAll { + # Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + + Context 'On getting all references from a repository' { + $references = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name + It 'Should return all branches matching the prefix when an exact match is not found' { + $references.Count | Should -Be 2 + $references.TagName.Contains($tagName) | Should -Be True + $references.BranchName.Contains($masterBranchName) | Should -Be True + $references[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[1].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[0].RepositoryUrl | Should -Be $repo.RepositoryUrl + $references[1].RepositoryUrl | Should -Be $repo.RepositoryUrl + } + } - It 'Should return all tags matching the prefix' { - $expected = @("refs/tags/$tag1", "refs/tags/$tag2") - $references.ref | Should Be $expected - } + Context 'On getting all references using Uri from a repository' { + $references = Get-GitHubReference -Uri $repo.svn_url + + It 'Should return all branches matching the prefix when an exact match is not found' { + $references.Count | Should -Be 2 + $references.TagName.Contains($tagName) | Should -Be True + $references.BranchName.Contains($masterBranchName) | Should -Be True + $references[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[1].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[0].RepositoryUrl | Should -Be $repo.RepositoryUrl + $references[1].RepositoryUrl | Should -Be $repo.RepositoryUrl } + } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false + Context 'On getting all references with repo on pipeline' { + $references = $repo | Get-GitHubReference + + It 'Should return all branches matching the prefix when an exact match is not found' { + $references.Count | Should -Be 2 + $references.TagName.Contains($tagName) | Should -Be True + $references.BranchName.Contains($masterBranchName) | Should -Be True + $references[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[1].PSObject.TypeNames[0] | Should -Be 'GitHub.Reference' + $references[0].RepositoryUrl | Should -Be $repo.RepositoryUrl + $references[1].RepositoryUrl | Should -Be $repo.RepositoryUrl + } } - Describe 'Getting all references from repository' { - $repositoryName = [Guid]::NewGuid() - $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $branchName = "forked-from-master" - $tagName = "master-fork-tag" - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName + } + + Describe 'Delete a branch from a repository' { + BeforeAll { + # AutoInit will create a readme with the GUID of the repo name + $repo = New-GitHubRepository -RepositoryName ($repoGuid) -AutoInit + } + + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + + Context 'On deleting a branch in a repository' { + $existingref = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName "master" $sha = $existingref.object.sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Sha $sha + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Sha $sha + Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Confirm:$false - $refNames = @("refs/heads/$masterBranchName", "refs/heads/$branchName", "refs/tags/$tagName") + It 'Should throw an exception when trying to get the deleted branch' { + { Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName } | + Should -Throw + } - Context 'On getting all references from a repository' { - $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -All + It 'Should throw an exception when the same branch is deleted again' { + { Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Confirm:$false} | + Should -Throw + } + } - It 'Should return all branches and tags' { - $reference.ref | Should be $refNames - } + Context 'On deleting an invalid branch from a repository' { + It 'Should throw an exception' { + { Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $([Guid]::NewGuid()) -Confirm:$false} | + Should -Throw } + } - Context 'On getting all references using Uri from a repository' { - $reference = Get-GitHubReference -Uri $repo.svn_url -All + Context 'On deleting a branch in a repository specified by Uri' { + $existingref = Get-GitHubReference -Uri $repo.svn_url -BranchName "master" + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Sha $sha + Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Confirm:$false - It 'Should return all branches and tags' { - $reference.ref | Should be $refNames - } + It 'Should throw an exception when trying to get the deleted branch' { + { Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName} | + Should -Throw } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false + It 'Should throw an exception when the same branch is deleted again' { + { Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Confirm:$false} | + Should -Throw + } } - Describe 'Delete a branch from a repository' { - $repositoryName = [Guid]::NewGuid() - $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $branchName = "myBranch" + Context 'On deleting a branch in a repository with the repo on pipeline' { + $existingref = Get-GitHubReference -Uri $repo.svn_url -BranchName "master" + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Sha $sha + $repo | Remove-GitHubReference -BranchName $branchName -Confirm:$false - Context 'On deleting a newly created branch in a repository' { - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch "master" - $sha = $existingref.object.sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $sha - Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Confirm:$false + It 'Should throw an exception when trying to get the deleted branch' { + { Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName} | + Should -Throw + } - It 'Should throw an exception when trying to get the deleted branch' { - { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName } | - Should Throw - } + It 'Should throw an exception when the same branch is deleted again' { + { Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Confirm:$false} | + Should -Throw + } + } + + Context 'On deleting a branch in a repository with the branch reference on pipeline' { + $existingref = Get-GitHubReference -Uri $repo.svn_url -BranchName "master" + $sha = $existingref.object.sha + $branchRef = New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Sha $sha + $branchRef | Remove-GitHubReference -Confirm:$false - It 'Should throw an exception when the same branch is deleted again' { - { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Confirm:$false} | - Should Throw - } + It 'Should throw an exception when trying to get the deleted branch' { + { Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName} | + Should -Throw } - Context 'On deleting an invalid branch from a repository' { - It 'Should throw an exception' { - { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $([Guid]::NewGuid()) -Confirm:$false} | - Should Throw - } + It 'Should throw an exception when the same branch is deleted again' { + { Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Confirm:$false} | + Should -Throw } + } - Context 'On deleting a newly created branch in a repository specified by Uri' { - $branchName = "myBranch" - $existingref = Get-GitHubReference -Uri $repo.svn_url -Branch "master" - $sha = $existingref.object.sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $sha - Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Confirm:$false + Context 'On deleting an invalid branch from a repository specified by Uri' { + It 'Should throw an exception' { + { Remove-GitHubReference -OwnerName -Uri $repo.svn_url -BranchName $([Guid]::NewGuid()) -Confirm:$false } | + Should -Throw + } + } - It 'Should throw an exception when trying to get the deleted branch' { - { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName} | - Should Throw - } + } - It 'Should throw an exception when the same branch is deleted again' { - { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Confirm:$false} | - Should Throw - } - } + Describe 'Delete a Tag from a Repository' { + BeforeAll { + # AutoInit will create a readme with the GUID of the repo name + $repo = New-GitHubRepository -RepositoryName ($repoGuid) -AutoInit + } + + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } - Context 'On deleting an invalid branch from a repository specified by Uri' { - It 'Should throw an exception' { - { Remove-GitHubReference -OwnerName -Uri $repo.svn_url -Branch $([Guid]::NewGuid()) -Confirm:$false } | - Should Throw - } + Context 'On deleting a valid tag in a repository' { + $existingref = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName "master" + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName -Sha $sha + Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName -Confirm:$false + + It 'Should throw an exception when trying to get the deleted tag' { + { Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName } | + Should -Throw } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false + It 'Should throw an exception when the same tag is deleted again' { + { Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName -Confirm:$false } | + Should -Throw + } } - Describe 'Delete a Tag from a Repository' { - $repositoryName = [Guid]::NewGuid() - $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $tagName = "myTag" + Context 'On deleting an invalid tag from a repository' { + It 'Should throw an exception' { + { Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $([Guid]::NewGuid()) -Confirm:$false } | + Should -Throw + } + } - Context 'On deleting a valid tag in a repository' { - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch "master" - $sha = $existingref.object.sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Sha $sha - Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Confirm:$false + Context 'On deleting a tag in a repository specified by Uri' { + $existingref = Get-GitHubReference -Uri $repo.svn_url -BranchName "master" + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName -Sha $sha + Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName -Confirm:$false - It 'Should throw an exception when trying to get the deleted tag' { - { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName } | - Should Throw - } + It 'Should throw an exception when trying to get the deleted tag' { + { Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName } | + Should -Throw + } - It 'Should throw an exception when the same tag is deleted again' { - { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Confirm:$false } | - Should Throw - } + It 'Should throw an exception when the same branch is deleted again' { + { Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName -Confirm:$false } | + Should -Throw } + } - Context 'On deleting an invalid tag from a repository' { - It 'Should throw an exception' { - { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $([Guid]::NewGuid()) -Confirm:$false } | - Should Throw - } + Context 'On deleting a tag in a repository with repo on pipeline' { + $existingref = Get-GitHubReference -Uri $repo.svn_url -BranchName "master" + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName -Sha $sha + $repo | Remove-GitHubReference -TagName $tagName -Confirm:$false + + It 'Should throw an exception when trying to get the deleted tag' { + { Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName } | + Should -Throw } - Context 'On deleting a tag in a repository specified by Uri' { - $existingref = Get-GitHubReference -Uri $repo.svn_url -Branch "master" - $sha = $existingref.object.sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Sha $sha - Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Confirm:$false + It 'Should throw an exception when the same branch is deleted again' { + { Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName -Confirm:$false } | + Should -Throw + } + } - It 'Should throw an exception when trying to get the deleted tag' { - { Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName } | - Should Throw - } + Context 'On deleting a tag in a repository with tag reference on pipeline' { + $existingref = Get-GitHubReference -Uri $repo.svn_url -BranchName "master" + $sha = $existingref.object.sha + $tagRef = New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName -Sha $sha + $tagRef | Remove-GitHubReference -Confirm:$false - It 'Should throw an exception when the same branch is deleted again' { - { Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tagName -Confirm:$false } | - Should Throw - } + It 'Should throw an exception when trying to get the deleted tag' { + { Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName } | + Should -Throw } - Context 'On deleting an invalid tag from a repository specified by Uri' { - It 'Should throw an exception' { - { Remove-GitHubReference -OwnerName -Uri $repo.svn_url -Tag $([Guid]::NewGuid()) -Confirm:$false } | - Should Throw - } + It 'Should throw an exception when the same branch is deleted again' { + { Remove-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tagName -Confirm:$false } | + Should -Throw } + } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false + Context 'On deleting an invalid tag from a repository specified by Uri' { + It 'Should throw an exception' { + { Remove-GitHubReference -OwnerName -Uri $repo.svn_url -TagName $([Guid]::NewGuid()) -Confirm:$false } | + Should -Throw + } } - Describe 'Update a branch in a repository' { - $repositoryName = [Guid]::NewGuid() - New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $branchName = "myBranch" - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName + } + + Describe 'Update a branch in a repository' { + BeforeAll { + # AutoInit will create a readme with the GUID of the repo name + $repo = New-GitHubRepository -RepositoryName ($repoGuid) -AutoInit + $existingref = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $masterBranchName $sha = $existingref.object.sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $sha + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Sha $sha + } - Context 'On updating an existing branch to a different SHA' { - It 'Should throw an exception if the SHA is invalid' { - { Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha "1234" } | - Should Throw - } + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + + Context 'On updating an existing branch to a different SHA' { + It 'Should throw an exception if the SHA is invalid' { + { Set-GithubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Sha "1234" } | + Should -Throw + } + } + + Context 'On updating a branch to a different SHA' { + $setGitHubContentParams = @{ + Path = 'test.txt' + CommitMessage = 'Commit Message' + Branch = $masterBranchName + Content = 'This is the content for test.txt' + Uri = $repo.svn_url } - Context 'On updating a branch to a different SHA' { - Update-MasterSHA - $masterSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch "master").object.sha - $oldSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName).object.sha - Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName -Sha $masterSHA + Set-GitHubContent @setGitHubContentParams + + $masterSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName "master").object.sha + $oldSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName).object.sha + Set-GithubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName -Sha $masterSHA - It 'Should return the updated SHA when the reference is queried after update' { - $newSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $branchName).object.sha - $newSHA | Should be $masterSHA - $newSHA | Should Not be $oldSHA - } + It 'Should return the updated SHA when the reference is queried after update' { + $newSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName).object.sha + $newSHA | Should -Be $masterSHA + $newSHA | Should -Not -Be $oldSHA } + } + + Context 'On updating a branch to a different SHA with repo on pipeline' { + $setGitHubContentParams = @{ + Path = 'test.txt' + CommitMessage = 'Commit Message' + Branch = $masterBranchName + Content = 'This is the content for test.txt' + Uri = $repo.svn_url + } + + Set-GitHubContent @setGitHubContentParams + + $masterSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName "master").object.sha + $oldSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName).object.sha + $repo | Set-GithubReference -BranchName $branchName -Sha $masterSHA - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false + It 'Should return the updated SHA when the reference is queried after update' { + $newSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName).object.sha + $newSHA | Should -Be $masterSHA + $newSHA | Should -Not -Be $oldSHA + } + } + + Context 'On updating a branch to a different SHA with branch reference on pipeline' { + $setGitHubContentParams = @{ + Path = 'test.txt' + CommitMessage = 'Commit Message' + Branch = $masterBranchName + Content = 'This is the content for test.txt' + Uri = $repo.svn_url + } + + Set-GitHubContent @setGitHubContentParams + + $masterSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName "master").object.sha + $branchRef = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName) + $oldSHA = $branchRef.object.sha + $branchRef | Set-GithubReference -Sha $masterSHA + + It 'Should return the updated SHA when the reference is queried after update' { + $newSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $branchName).object.sha + $newSHA | Should -Be $masterSHA + $newSHA | Should -Not -Be $oldSHA + } } - Describe 'Update a tag in a repository' { - $repositoryName = [Guid]::NewGuid() - New-GitHubRepository -RepositoryName $repositoryName -AutoInit + } + + Describe 'Update a tag in a repository' { + BeforeAll { + # AutoInit will create a readme with the GUID of the repo name + $repo = New-GitHubRepository -RepositoryName ($repoGuid) -AutoInit $tag = "myTag" - $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch $masterBranchName + $existingref = Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName $masterBranchName $sha = $existingref.object.sha - New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag -Sha $sha + New-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag -Sha $sha + } + + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + + Context 'On updating an existing tag to a different SHA' { + It 'Should throw an exception if the SHA is invalid' { + { Set-GithubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag -Sha "1234" } | + Should -Throw + } + } + + Context 'On updating a tag to a valid SHA' { + $setGitHubContentParams = @{ + Path = 'test.txt' + CommitMessage = 'Commit Message' + Branch = $masterBranchName + Content = 'This is the content for test.txt' + Uri = $repo.svn_url + } + + Set-GitHubContent @setGitHubContentParams + + $masterSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName "master").object.sha + $oldSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag).object.sha + Set-GithubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag -Sha $masterSHA - Context 'On updating an existing tag to a different SHA' { - It 'Should throw an exception if the SHA is invalid' { - { Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag -Sha "1234" } | - Should Throw - } + It 'Should return the updated SHA when the reference is queried after update' { + $newSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag).object.sha + $newSHA | Should -Be $masterSHA + $newSHA | Should -Not -Be $oldSHA } + } + + Context 'On updating a tag to a valid SHA with repo on pipeline' { + $setGitHubContentParams = @{ + Path = 'test.txt' + CommitMessage = 'Commit Message' + Branch = $masterBranchName + Content = 'This is the content for test.txt' + Uri = $repo.svn_url + } + + Set-GitHubContent @setGitHubContentParams + + $masterSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName "master").object.sha + $oldSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag).object.sha + $repo | Set-GithubReference -TagName $tag -Sha $masterSHA - Context 'On updating a tag to a valid SHA' { - Update-MasterSHA - $masterSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Branch "master").object.sha - $oldSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag).object.sha - Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag -Sha $masterSHA + It 'Should return the updated SHA when the reference is queried after update' { + $newSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag).object.sha + $newSHA | Should -Be $masterSHA + $newSHA | Should -Not -Be $oldSHA + } + } - It 'Should return the updated SHA when the reference is queried after update' { - $newSHA = $(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Tag $tag).object.sha - $newSHA | Should be $masterSHA - $newSHA | Should Not be $oldSHA - } + Context 'On updating a tag to a valid SHA with tag reference on pipeline' { + $setGitHubContentParams = @{ + Path = 'test.txt' + CommitMessage = 'Commit Message' + Branch = $masterBranchName + Content = 'This is the content for test.txt' + Uri = $repo.svn_url } - $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName -Confirm:$false + Set-GitHubContent @setGitHubContentParams + + $masterSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -BranchName "master").object.sha + $tagRef = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag) + $oldSHA = $tagRef.object.sha + $tagRef | Set-GithubReference -Sha $masterSHA + + It 'Should return the updated SHA when the reference is queried after update' { + $newSHA = $(Get-GitHubReference -OwnerName $script:ownerName -RepositoryName $repo.name -TagName $tag).object.sha + $newSHA | Should -Be $masterSHA + $newSHA | Should -Not -Be $oldSHA + } } } } -catch +finally { if (Test-Path -Path $script:originalConfigFile -PathType Leaf) {