Skip to content

Commit

Permalink
Merge pull request #300 from tuxgoose/master
Browse files Browse the repository at this point in the history
Added field parameter
  • Loading branch information
lipkau authored Jul 8, 2018
2 parents da7c99b + e324728 commit 925cc9b
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 35 deletions.
36 changes: 29 additions & 7 deletions JiraPS/Public/Get-JiraIssue.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function Get-JiraIssue {
[Object]
$Filter,

[Parameter()]
[ValidateNotNullOrEmpty()]
[String[]]
$Fields = "*all",

[Parameter( ParameterSetName = 'ByJQL' )]
[Parameter( ParameterSetName = 'ByFilter' )]
[UInt32]
Expand All @@ -91,8 +96,10 @@ function Get-JiraIssue {

$server = Get-JiraConfigServer -ErrorAction Stop

$resourceURi = "$server/rest/api/latest/issue/{0}?expand=transitions"
$searchURi = "$server/rest/api/latest/search"
$resourceURi = "$server/rest/api/latest/issue/{0}"

[String]$Fields = $Fields -join ","
}

process {
Expand All @@ -105,11 +112,18 @@ function Get-JiraIssue {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Processing [$_key]"
Write-Debug "[$($MyInvocation.MyCommand.Name)] Processing `$_key [$_key]"

$getParameter = @{ expand = "transitions" }
if ($Fields) {
$getParameter["fields"] = $Fields
}

$parameter = @{
URI = $resourceURi -f $_key
Method = "GET"
Credential = $Credential
URI = $resourceURi -f $_key
Method = "GET"
GetParameter = $getParameter
Credential = $Credential
}

Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter"
$result = Invoke-JiraMethod @parameter

Expand All @@ -122,7 +136,7 @@ function Get-JiraIssue {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Processing [$_issue]"
Write-Debug "[$($MyInvocation.MyCommand.Name)] Processing `$_issue [$_issue]"

Write-Output (Get-JiraIssue -Key $_issue.Key -Credential $Credential)
Write-Output (Get-JiraIssue -Key $_issue.Key -Fields $Fields -Credential $Credential)
}
}
'ByJQL' {
Expand All @@ -134,11 +148,15 @@ function Get-JiraIssue {
validateQuery = $true
expand = "transitions"
maxResults = $PageSize

}
OutputType = "JiraIssue"
Paging = $true
Credential = $Credential
}
if ($Fields) {
$parameter["GetParameter"]["fields"] = $Fields
}
# Paging
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | ForEach-Object {
$parameter[$_] = $PSCmdlet.PagingParameters.$_
Expand All @@ -158,14 +176,14 @@ function Get-JiraIssue {
Invoke-JiraMethod @parameter
}
'ByFilter' {
$filterObj = Get-JiraFilter -InputObject $Filter -Credential $Credential -ErrorAction Stop
$filterObj = (Get-JiraFilter -InputObject $Filter -Credential $Credential -ErrorAction Stop).searchurl
<#
#ToDo:CustomClass
Once we have custom classes, this will no longer be necessary
#>

$parameter = @{
URI = $filterObj.SearchUrl
URI = $filterObj
Method = "GET"
GetParameter = @{
validateQuery = $true
Expand All @@ -175,6 +193,10 @@ function Get-JiraIssue {
OutputType = "JiraIssue"
Paging = $true
Credential = $Credential

}
if ($Fields) {
$parameter["GetParameter"]["fields"] = $Fields
}
# Paging
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | ForEach-Object {
Expand Down
131 changes: 112 additions & 19 deletions Tests/Get-JiraIssue.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Describe "Get-JiraIssue" {

Import-Module "$PSScriptRoot/../JiraPS" -Force -ErrorAction Stop
BeforeAll {
Remove-Module JiraPS -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot/../JiraPS" -Force -ErrorAction Stop
}

InModuleScope JiraPS {

Expand Down Expand Up @@ -28,31 +30,54 @@
'@

#region Mocks
Mock Get-JiraConfigServer {
Mock Get-JiraConfigServer -ModuleName JiraPS {
$jiraServer
}

Mock Invoke-JiraMethod -ParameterFilter { $Method -eq 'Get' -and $URI -like "$jiraServer/rest/api/latest/issue/TEST-001*" } {
Mock Get-JiraUser -ModuleName JiraPS {
$object = [PSCustomObject] @{
'Name' = 'username'
}
$object.PSObject.TypeNames.Insert(0, 'JiraPS.User')
return $object
}

Mock Get-JiraFilter -ModuleName JiraPS {
[PSCustomObject]@{
PSTypeName = "JiraPS.Filter"
Id = 12345
SearchUrl = "https://jira.example.com/rest/api/latest/filter/12345"
}
}

Mock Invoke-JiraMethod -ModuleName JiraPS -ParameterFilter {
$Method -eq 'Get' -and
$URI -like "$jiraServer/rest/api/*/issue/TEST-001*"
} {
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri'
ConvertFrom-Json $response
}

Mock Invoke-JiraMethod -ParameterFilter { $Method -eq 'Get' -and $URI -like "$jiraServer/rest/api/latest/search" -and $GetParameter["jql"] -eq $jqlEscaped } {
Mock Invoke-JiraMethod -ModuleName JiraPS -ParameterFilter {
$Method -eq 'Get' -and
$URI -like "$jiraServer/rest/api/*/search" -and
$GetParameter["jql"] -eq $jqlEscaped
} {
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri'
ConvertFrom-Json $response
}

Mock Invoke-JiraMethod {
Mock Invoke-JiraMethod -ModuleName JiraPS -ParameterFilter {
$Method -eq 'Get' -and
$URI -like "$jiraServer/rest/api/*/filter/*"
} {
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri'
throw "Unidentified call to Invoke-JiraMethod"
ConvertFrom-Json $response
}

Mock Get-JiraUser {
$object = [PSCustomObject] @{
'Name' = 'username'
}
$object.PSObject.TypeNames.Insert(0, 'JiraPS.User')
return $object
Mock Invoke-JiraMethod -ModuleName JiraPS {
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri'
throw "Unidentified call to Invoke-JiraMethod"
}
#endregion Mocks

Expand All @@ -63,6 +88,7 @@
defParam $command 'InputObject'
defParam $command 'Query'
defParam $command 'Filter'
defParam $command 'Fields'
defParam $command 'StartIndex'
defParam $command 'MaxResults'
defParam $command 'PageSize'
Expand All @@ -72,7 +98,7 @@
Context "Behavior testing" {

It "Obtains information about a provided issue in JIRA" {
{ Get-JiraIssue -Key TEST-001 } | Should Not Throw
{ Get-JiraIssue -Key TEST-001 } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand All @@ -89,7 +115,7 @@
}

It "Uses JQL to search for issues if the -Query parameter is used" {
{ Get-JiraIssue -Query $jql } | Should Not Throw
{ Get-JiraIssue -Query $jql } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand All @@ -107,7 +133,7 @@
}

It "Supports the -StartIndex and -MaxResults parameters to page through search results" {
{ Get-JiraIssue -Query $jql -StartIndex 10 -MaxResults 50 } | Should Not Throw
{ Get-JiraIssue -Query $jql -StartIndex 10 -MaxResults 50 } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand All @@ -127,7 +153,7 @@
}

It "Returns all issues via looping if -MaxResults is not specified" {
{ Get-JiraIssue -Query $jql -PageSize 25 } | Should Not Throw
{ Get-JiraIssue -Query $jql -PageSize 25 } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand All @@ -144,11 +170,78 @@
}
Assert-MockCalled @assertMockCalledSplat
}

It "Returns only the fields required with -Fields" {
$issue = [PSCustomObject]@{
PSTypeName = "JiraPS.Issue"
Key = "TEST-001"
}

{ Get-JiraIssue -Key TEST-001 } | Should -Not -Throw
{ Get-JiraIssue -Key TEST-001 -Fields "key" } | Should -Not -Throw
{ Get-JiraIssue -Key TEST-001 -Fields "-summary" } | Should -Not -Throw
{ Get-JiraIssue -Key TEST-001 -Fields "key", "summary", "status" } | Should -Not -Throw
{ Get-JiraIssue -InputObject $issue -Fields "key", "summary", "status" } | Should -Not -Throw
{ Get-JiraIssue -Query $jql -Fields "key", "summary", "status" } | Should -Not -Throw
{ Get-JiraIssue -Filter "12345" -Fields "key", "summary", "status" } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$GetParameter["fields"] -eq "*all"
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$GetParameter["fields"] -eq "key"
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$GetParameter["fields"] -eq "-summary"
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$GetParameter["fields"] -eq "key,summary,status"
}
Scope = 'It'
Exactly = $true
Times = 4
}
Assert-MockCalled @assertMockCalledSplat
}
}

Context "Input testing" {
It "Accepts an issue key for the -Key parameter" {
{ Get-JiraIssue -Key TEST-001 } | Should Not Throw
{ Get-JiraIssue -Key TEST-001 } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand All @@ -172,7 +265,7 @@
$issue.PSObject.TypeNames.Insert(0, 'JiraPS.Issue')

# Should call Get-JiraIssue using the -Key parameter, so our URL should reflect the key we provided
{ Get-JiraIssue -InputObject $Issue } | Should Not Throw
{ Get-JiraIssue -InputObject $Issue } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand Down
Loading

0 comments on commit 925cc9b

Please sign in to comment.