Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Move-JiraVersion method #363

Merged
merged 5 commits into from
Jul 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion JiraPS/Public/Get-JiraUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function Get-JiraUser {
[CmdletBinding( DefaultParameterSetName = 'Self' )]
param(
[Parameter( Position = 0, Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'ByUserName' )]
[ValidateNotNullOrEmpty()]
[AllowEmptyString()]
[Alias('User', 'Name')]
[String[]]
$UserName,
Expand Down
116 changes: 116 additions & 0 deletions JiraPS/Public/Move-JiraVersion.ps1
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"
}
}
243 changes: 243 additions & 0 deletions Tests/Functions/Move-JiraVersion.Unit.Tests.ps1
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)"""
}
}
}
}
}
8 changes: 8 additions & 0 deletions docs/en-US/commands/Get-JiraUser.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ Get-JiraUser -UserName user1 -Exact

Returns information about user user1

### EXAMPLE 5

```powershell
Get-JiraUser -UserName ""
```

Returns information about all users. The empty string "" matches all users.

## PARAMETERS

### -UserName
Expand Down
2 changes: 2 additions & 0 deletions docs/en-US/commands/Get-JiraVersion.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,5 @@ If neither are supplied, this function will run with anonymous access to JIRA.
[Remove-JiraVersion](../Remove-JiraVersion/)

[Set-JiraVersion](../Set-JiraVersion/)

[Move-JiraVersion](../Move-JiraVersion/)
Loading