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..a29a4aef 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 in 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 in 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 in 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 in 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 in 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 in 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 in 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 in 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 in 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) {