-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from kb-cs/move-jiraversion
Add Move-JiraVersion method
- Loading branch information
Showing
9 changed files
with
528 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
function Move-JiraVersion { | ||
# .ExternalHelp ..\JiraPS-help.xml | ||
[CmdletBinding( DefaultParameterSetName = 'ByAfter' )] | ||
param( | ||
[Parameter( Mandatory, ValueFromPipeline )] | ||
[ValidateNotNullOrEmpty()] | ||
[ValidateScript( | ||
{ | ||
if (("JiraPS.Version" -notin $_.PSObject.TypeNames) -and (($_ -isnot [Int]))) { | ||
$exception = ([System.ArgumentException]"Invalid Type for Parameter") #fix code highlighting] | ||
$errorId = 'ParameterType.NotJiraVersion' | ||
$errorCategory = 'InvalidArgument' | ||
$errorTarget = $_ | ||
$errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget | ||
$errorItem.ErrorDetails = "Wrong object type provided for Version. Expected [JiraPS.Version] or [Int], but was $($_.GetType().Name)" | ||
$PSCmdlet.ThrowTerminatingError($errorItem) | ||
<# | ||
#ToDo:CustomClass | ||
Once we have custom classes, this check can be done with Type declaration | ||
#> | ||
} | ||
else { | ||
return $true | ||
} | ||
} | ||
)] | ||
[Object] | ||
$Version, | ||
|
||
[Parameter( Mandatory, ParameterSetName = 'ByPosition' )] | ||
[ValidateSet('First', 'Last', 'Earlier', 'Later')] | ||
[String]$Position, | ||
|
||
[Parameter( Mandatory, ParameterSetName = 'ByAfter' )] | ||
[ValidateNotNullOrEmpty()] | ||
[ValidateScript( | ||
{ | ||
if (("JiraPS.Version" -notin $_.PSObject.TypeNames) -and (($_ -isnot [Int]))) { | ||
$exception = ([System.ArgumentException]"Invalid Type for Parameter") #fix code highlighting] | ||
$errorId = 'ParameterType.NotJiraVersion' | ||
$errorCategory = 'InvalidArgument' | ||
$errorTarget = $_ | ||
$errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget | ||
$errorItem.ErrorDetails = "Wrong object type provided for Version. Expected [JiraPS.Version] or [Int], but was $($_.GetType().Name)" | ||
$PSCmdlet.ThrowTerminatingError($errorItem) | ||
<# | ||
#ToDo:CustomClass | ||
Once we have custom classes, this check can be done with Type declaration | ||
#> | ||
} | ||
else { | ||
return $true | ||
} | ||
} | ||
)] | ||
[Object] | ||
$After, | ||
|
||
[Parameter()] | ||
[System.Management.Automation.PSCredential] | ||
[System.Management.Automation.Credential()] | ||
$Credential = [System.Management.Automation.PSCredential]::Empty | ||
) | ||
|
||
begin { | ||
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started" | ||
|
||
$server = Get-JiraConfigServer -ErrorAction Stop | ||
|
||
$versionResourceUri = "$server/rest/api/latest/version/{0}/move" | ||
} | ||
|
||
process { | ||
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)" | ||
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)" | ||
|
||
$requestBody = @{ } | ||
switch ($PsCmdlet.ParameterSetName) { | ||
'ByPosition' { | ||
$requestBody["position"] = $Position | ||
} | ||
'ByAfter' { | ||
$afterSelfUri = '' | ||
if ($After -is [Int]) { | ||
$versionObj = Get-JiraVersion -Id $After -Credential $Credential -ErrorAction Stop | ||
$afterSelfUri = $versionObj.RestUrl | ||
} | ||
else { | ||
$afterSelfUri = $After.RestUrl | ||
} | ||
|
||
$requestBody["after"] = $afterSelfUri | ||
} | ||
} | ||
|
||
if ($Version.Id) { | ||
$versionId = $Version.Id | ||
} else { | ||
$versionId = $Version | ||
} | ||
|
||
$parameter = @{ | ||
URI = $versionResourceUri -f $versionId | ||
Method = "POST" | ||
Body = ConvertTo-Json $requestBody | ||
Credential = $Credential | ||
} | ||
|
||
Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter" | ||
Invoke-JiraMethod @parameter | ||
} | ||
|
||
end { | ||
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
#requires -modules BuildHelpers | ||
#requires -modules @{ ModuleName = "Pester"; ModuleVersion = "4.4.0" } | ||
|
||
Describe "Move-JiraVersion" -Tag 'Unit' { | ||
|
||
BeforeAll { | ||
Remove-Item -Path Env:\BH* | ||
$projectRoot = (Resolve-Path "$PSScriptRoot/../..").Path | ||
if ($projectRoot -like "*Release") { | ||
$projectRoot = (Resolve-Path "$projectRoot/..").Path | ||
} | ||
|
||
Import-Module BuildHelpers | ||
Set-BuildEnvironment -BuildOutput '$ProjectPath/Release' -Path $projectRoot -ErrorAction SilentlyContinue | ||
|
||
$env:BHManifestToTest = $env:BHPSModuleManifest | ||
$script:isBuild = $PSScriptRoot -like "$env:BHBuildOutput*" | ||
if ($script:isBuild) { | ||
$Pattern = [regex]::Escape($env:BHProjectPath) | ||
|
||
$env:BHBuildModuleManifest = $env:BHPSModuleManifest -replace $Pattern, $env:BHBuildOutput | ||
$env:BHManifestToTest = $env:BHBuildModuleManifest | ||
} | ||
|
||
Import-Module "$env:BHProjectPath/Tools/BuildTools.psm1" | ||
|
||
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue | ||
Import-Module $env:BHManifestToTest | ||
} | ||
AfterAll { | ||
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue | ||
Remove-Module BuildHelpers -ErrorAction SilentlyContinue | ||
Remove-Item -Path Env:\BH* | ||
} | ||
|
||
InModuleScope JiraPS { | ||
|
||
. "$PSScriptRoot/../Shared.ps1" | ||
|
||
$jiraServer = 'http://jiraserver.example.com' | ||
$versionID1 = 16840 | ||
$versionID2 = 16940 | ||
$projectKey = 'LDD' | ||
$projectId = '12101' | ||
|
||
$JiraProjectData = @" | ||
[ | ||
{ | ||
"Key" : "$projectKey", | ||
"Id": "$projectId" | ||
}, | ||
{ | ||
"Key" : "foo", | ||
"Id": "99" | ||
} | ||
] | ||
"@ | ||
|
||
#region Mock | ||
Mock Get-JiraConfigServer -ModuleName JiraPS { | ||
Write-Output $jiraServer | ||
} | ||
|
||
Mock Get-JiraProject -ModuleName JiraPS { | ||
$Projects = ConvertFrom-Json $JiraProjectData | ||
$Projects.PSObject.TypeNames.Insert(0, 'JiraPS.Project') | ||
$Projects | Where-Object {$_.Key -in $Project} | ||
} | ||
|
||
Mock Get-JiraVersion -ModuleName JiraPS { | ||
foreach ($_id in $Id) { | ||
$Version = [PSCustomObject]@{ | ||
Id = $_Id | ||
Name = "v1" | ||
Description = "My Desccription" | ||
Project = (Get-JiraProject -Project $projectKey) | ||
ReleaseDate = (Get-Date "2017-12-01") | ||
StartDate = (Get-Date "2017-01-01") | ||
RestUrl = "$jiraServer/rest/api/latest/version/$_Id" | ||
} | ||
$Version.PSObject.TypeNames.Insert(0, 'JiraPS.Version') | ||
$Version | ||
} | ||
} | ||
|
||
Mock Invoke-JiraMethod -ModuleName JiraPS -ParameterFilter { $Method -eq 'POST' -and $URI -like "$jiraServer/rest/api/*/version/$versionID1/move" } { | ||
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri' | ||
} | ||
|
||
Mock Invoke-JiraMethod -ModuleName JiraPS -ParameterFilter { $Method -eq 'POST' -and $URI -like "$jiraServer/rest/api/*/version/$versionID2/move" } { | ||
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri' | ||
} | ||
|
||
# Generic catch-all. This will throw an exception if we forgot to mock something. | ||
Mock Invoke-JiraMethod -ModuleName JiraPS { | ||
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri' | ||
throw "Unidentified call to Invoke-JiraMethod" | ||
} | ||
#endregion Mock | ||
|
||
Context "Sanity checking" { | ||
$command = Get-Command -Name Move-JiraVersion | ||
|
||
defParam $command 'Version' | ||
defParam $command 'Position' | ||
defParam $command 'After' | ||
defParam $command 'Credential' | ||
} | ||
|
||
Context "ByPosition behavior checking" { | ||
It 'moves a Version using its ID and Last Position' { | ||
{ Move-JiraVersion -Version $versionID1 -Position Last -ErrorAction Stop } | Should Not Throw | ||
Assert-MockCalled 'Get-JiraVersion' -Times 0 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Get-JiraConfigServer' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Invoke-JiraMethod' -Times 1 -Scope It -ModuleName JiraPS -Exactly -ParameterFilter { | ||
$Method -eq 'POST' -and | ||
$URI -like "$jiraServer/rest/api/latest/version/$versionID1/move" -and | ||
$Body -match '"position":\s*"Last"' | ||
} | ||
} | ||
It 'moves a Version using its ID and Earlier Position' { | ||
{ Move-JiraVersion -Version $versionID1 -Position Earlier -ErrorAction Stop } | Should Not Throw | ||
Assert-MockCalled 'Get-JiraVersion' -Times 0 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Get-JiraConfigServer' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Invoke-JiraMethod' -Times 1 -Scope It -ModuleName JiraPS -Exactly -ParameterFilter { | ||
$Method -eq 'POST' -and | ||
$URI -like "$jiraServer/rest/api/latest/version/$versionID1/move" -and | ||
$Body -match '"position":\s*"Earlier"' | ||
} | ||
} | ||
It 'moves a Version using a JiraPS.Version object and Later Position' { | ||
{ | ||
$version = Get-JiraVersion -ID $versionID2 | ||
Move-JiraVersion -Version $version -Position Later -ErrorAction Stop | ||
} | Should Not Throw | ||
Assert-MockCalled 'Get-JiraVersion' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Get-JiraConfigServer' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Invoke-JiraMethod' -Times 1 -Scope It -ModuleName JiraPS -Exactly -ParameterFilter { | ||
$Method -eq 'POST' -and | ||
$URI -like "$jiraServer/rest/api/latest/version/$versionID2/move" -and | ||
$Body -match '"position":\s*"Later"' | ||
} | ||
} | ||
It 'moves a Version using JiraPS.Version object and First Position' { | ||
{ | ||
$version = Get-JiraVersion -ID $versionID2 | ||
Move-JiraVersion -Version $version -Position First -ErrorAction Stop | ||
} | Should Not Throw | ||
Assert-MockCalled 'Get-JiraVersion' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Get-JiraConfigServer' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Invoke-JiraMethod' -Times 1 -Scope It -ModuleName JiraPS -Exactly -ParameterFilter { | ||
$Method -eq 'POST' -and | ||
$URI -like "$jiraServer/rest/api/latest/version/$versionID2/move" -and | ||
$Body -match '"position":\s*"First"' | ||
} | ||
} | ||
It 'moves a Version using JiraPS.Version object over pipeline and First Position' { | ||
{ | ||
$version = Get-JiraVersion -ID $versionID2 | ||
$version | Move-JiraVersion -Position First -ErrorAction Stop | ||
} | Should Not Throw | ||
Assert-MockCalled 'Get-JiraVersion' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Get-JiraConfigServer' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Invoke-JiraMethod' -Times 1 -Scope It -ModuleName JiraPS -Exactly -ParameterFilter { | ||
$Method -eq 'POST' -and | ||
$URI -like "$jiraServer/rest/api/latest/version/$versionID2/move" -and | ||
$Body -match '"position":\s*"First"' | ||
} | ||
} | ||
It 'moves a Version using its ID over pipeline and First Position' { | ||
{ | ||
$versionID1 | Move-JiraVersion -Position First -ErrorAction Stop | ||
} | Should Not Throw | ||
Assert-MockCalled 'Get-JiraVersion' -Times 0 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Get-JiraConfigServer' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Invoke-JiraMethod' -Times 1 -Scope It -ModuleName JiraPS -Exactly -ParameterFilter { | ||
$Method -eq 'POST' -and | ||
$URI -like "$jiraServer/rest/api/latest/version/$versionID1/move" -and | ||
$Body -match '"position":\s*"First"' | ||
} | ||
} | ||
} | ||
Context "ByAfter behavior checking" { | ||
It 'moves a Version using its ID and other Version ID' { | ||
$restUrl = (Get-JiraVersion -Id $versionID2).RestUrl | ||
{ Move-JiraVersion -Version $versionID1 -After $versionID2 -ErrorAction Stop } | Should Not Throw | ||
Assert-MockCalled 'Get-JiraVersion' -Times 2 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Get-JiraConfigServer' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Invoke-JiraMethod' -Times 1 -Scope It -ModuleName JiraPS -Exactly -ParameterFilter { | ||
$Method -eq 'POST' -and | ||
$URI -like "$jiraServer/rest/api/latest/version/$versionID1/move" -and | ||
$Body -match """after"":\s*""$restUrl""" | ||
} | ||
} | ||
It 'moves a Version using JiraPS.Version object and other Version ID' { | ||
$restUrl = (Get-JiraVersion -Id $versionID2).RestUrl | ||
$version1 = Get-JiraVersion -ID $versionID1 | ||
{ Move-JiraVersion -Version $version1 -After $versionID2 -ErrorAction Stop } | Should Not Throw | ||
Assert-MockCalled 'Get-JiraVersion' -Times 3 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Get-JiraConfigServer' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Invoke-JiraMethod' -Times 1 -Scope It -ModuleName JiraPS -Exactly -ParameterFilter { | ||
$Method -eq 'POST' -and | ||
$URI -like "$jiraServer/rest/api/latest/version/$versionID1/move" -and | ||
$Body -match """after"":\s*""$restUrl""" | ||
} | ||
} | ||
It 'moves a Version using its ID and other Version JiraPS.Version object' { | ||
$version2 = Get-JiraVersion -ID $versionID2 | ||
{ Move-JiraVersion -Version $versionID1 -After $version2 -ErrorAction Stop } | Should Not Throw | ||
Assert-MockCalled 'Get-JiraVersion' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Get-JiraConfigServer' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Invoke-JiraMethod' -Times 1 -Scope It -ModuleName JiraPS -Exactly -ParameterFilter { | ||
$Method -eq 'POST' -and | ||
$URI -like "$jiraServer/rest/api/latest/version/$versionID1/move" -and | ||
$Body -match """after"":\s*""$($version2.RestUrl)""" | ||
} | ||
} | ||
It 'moves a Version using its ID over pipeline and other Version JiraPS.Version object' { | ||
$version2 = Get-JiraVersion -ID $versionID2 | ||
{ $versionID1 | Move-JiraVersion -After $version2 -ErrorAction Stop } | Should Not Throw | ||
Assert-MockCalled 'Get-JiraVersion' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Get-JiraConfigServer' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Invoke-JiraMethod' -Times 1 -Scope It -ModuleName JiraPS -Exactly -ParameterFilter { | ||
$Method -eq 'POST' -and | ||
$URI -like "$jiraServer/rest/api/latest/version/$versionID1/move" -and | ||
$Body -match """after"":\s*""$($version2.RestUrl)""" | ||
} | ||
} | ||
It 'moves a Version using JiraPS.Version object over pipeline and other Version JiraPS.Version object' { | ||
$version1 = Get-JiraVersion -ID $versionID1 | ||
$version2 = Get-JiraVersion -ID $versionID2 | ||
{ $version1 | Move-JiraVersion -After $version2 -ErrorAction Stop } | Should Not Throw | ||
Assert-MockCalled 'Get-JiraVersion' -Times 2 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Get-JiraConfigServer' -Times 1 -Scope It -ModuleName JiraPS -Exactly | ||
Assert-MockCalled 'Invoke-JiraMethod' -Times 1 -Scope It -ModuleName JiraPS -Exactly -ParameterFilter { | ||
$Method -eq 'POST' -and | ||
$URI -like "$jiraServer/rest/api/latest/version/$versionID1/move" -and | ||
$Body -match """after"":\s*""$($version2.RestUrl)""" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.