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

Find-JiraFilter #365

Merged
merged 9 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
89 changes: 89 additions & 0 deletions JiraPS/Public/Find-JiraFilter.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
function Find-JiraFilter {
# .ExternalHelp ..\JiraPS-help.xml
[CmdletBinding( SupportsPaging )]
param(
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string[]]$Name,

[Parameter(ValueFromPipelineByPropertyName)]
[string]$AccountId,

lipkau marked this conversation as resolved.
Show resolved Hide resolved
[Parameter(ValueFromPipelineByPropertyName)]
[string]$GroupName,

[Parameter(ValueFromPipelineByPropertyName)]
[Object]$Project,
lipkau marked this conversation as resolved.
Show resolved Hide resolved

[Validateset('description','favourite','favouritedCount','jql','owner','searchUrl','sharePermissions','subscriptions','viewUrl')]
[String[]]
$Fields = @('description','favourite','favouritedCount','jql','owner','searchUrl','sharePermissions','subscriptions','viewUrl'),

[Validateset('description','favourite_count','is_favourite','id','name','owner')]
[string]$Sort,

[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

$searchURi = "$server/rest/api/latest/filter/search"

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

process {
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"
$parameter = @{
URI = $searchURi
Method = 'GET'
GetParameter = @{
expand = $Fields
}
Paging = $true
Credential = $Credential
}
if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('AccountId')) {
$parameter['GetParameter']['accountId'] = $AccountId
}
if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('GroupName')) {
$parameter['GetParameter']['groupName'] = $GroupName
}
if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('Project')) {
$projectObj = Get-JiraProject -Project $Project -Credential $Credential -ErrorAction Stop
$parameter['GetParameter']['projectId'] = $projectObj.Id
}
if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('Sort')) {
$parameter['GetParameter']['orderBy'] = $Sort
}
# Paging
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | ForEach-Object {
$parameter[$_] = $PSCmdlet.PagingParameters.$_
}
if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('Name')) {
foreach($_name in $Name) {
$parameter['GetParameter']['filterName'] = $_name
Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter"

$result = Invoke-JiraMethod @parameter
lipkau marked this conversation as resolved.
Show resolved Hide resolved

Write-Output (ConvertTo-JiraFilter -InputObject $result)
}
}
else {
Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter"

$result = Invoke-JiraMethod @parameter
lipkau marked this conversation as resolved.
Show resolved Hide resolved

Write-Output (ConvertTo-JiraFilter -InputObject $result)
}
}

end {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
}
}
290 changes: 290 additions & 0 deletions Tests/Functions/Find-JiraFilter.Unit.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
#requires -modules BuildHelpers
#requires -modules @{ ModuleName = 'Pester'; ModuleVersion = '4.4.0' }

Describe 'Find-JiraFilter' -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 = 'https://jira.example.com'

$owner = 'c62dde3418235be1c8424950'
$ownerEscaped = ConvertTo-URLEncoded $owner
$group = 'groupA'
$groupEscaped = ConvertTo-URLEncoded $group
$response = @'
{
'expand': 'schema,names',
'startAt': 0,
'maxResults': 25,
'total': 1,
'filters': [
{
"SearchUrl": "https://jira.example.com/rest/api/2/search?jql=id+in+(TEST-001,+TEST-002,+TEST-003)",
"ID": "1",
"FilterPermissions": [],
"Name": "Test filter",
"JQL": "id in (TEST-001, TEST-002, TEST-003)",
"SharePermission": {
"project": {
"id": 1,
"key": 'ABC'
}
},
"Owner": {
"Name": "test_owner",
"AccountId": "c62dde3418235be1c8424950"
},
"Favourite": true,
"Description": "This is a test filter",
"RestUrl": "https://jira.example.com/rest/api/2/filter/1",
"ViewUrl": "https://jira.example.com/issues/?filter=1",
"Favorite": true
}
]
}
'@

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

Mock Get-JiraProject -ModuleName JiraPS {
ShowMockInfo 'Get-JiraProject' 'Project'
[PSCustomObject]@{
Id = '1'
Key = 'ABC'
}
}

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

Mock Invoke-JiraMethod -ModuleName JiraPS {
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri'
throw 'Unidentified call to Invoke-JiraMethod'
}
#endregion Mocks

Context 'Sanity checking' {
$command = Get-Command -Name Find-JiraFilter

defParam $command 'Name'
defParam $command 'AccountId'
defParam $command 'GroupName'
defParam $command 'Project'
defParam $command 'Fields'
defParam $command 'Sort'
defParam $command 'Credential'
}

Context 'Behavior testing' {

It 'Finds a JIRA filter by Name' {
{ Find-JiraFilter -Name 'Test Filter' } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$URI -like '*/rest/api/*/filter/search*'
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat
}

It 'Uses accountId to find JIRA filters if the -AccountId parameter is used' {
{ Find-JiraFilter -Name 'Test Filter' -AccountId $owner } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$URI -like '*/rest/api/*/filter/search*' -and
$GetParameter['accountId'] -eq $ownerEscaped
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat
}

It 'Uses groupName to find JIRA filters if the -GroupName parameter is used' {
{ Find-JiraFilter -Name 'Test Filter' -GroupName $group } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$URI -like '*/rest/api/*/filter/search*' -and
$GetParameter['groupName'] -eq $groupEscaped
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat
}

It 'Uses projectId to find JIRA filters if a -Project id parameter is used' {
{ Find-JiraFilter -Name 'Test Filter' -Project 1 } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$URI -like '*/rest/api/*/filter/search*' -and
$GetParameter['projectId'] -eq '1'
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat
}

It 'Uses orderBy to sort JIRA filters found if the -Sort parmaeter is used' {
{ Find-JiraFilter -Name 'Test Filter' -Sort 'name' } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$URI -like '*/rest/api/*/filter/search*' -and
$GetParameter['orderBy'] -eq 'name'
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat
}

It 'Expands only the fields required with -Fields' {
{ Find-JiraFilter -Name 'Test Filter' } | Should -Not -Throw
{ Find-JiraFilter -Name 'Test Filter' -Fields 'description' } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$URI -like '*/rest/api/*/filter/search*' -and
$GetParameter['expand'] -eq 'description,favourite,favouritedCount,jql,owner,searchUrl,sharePermissions,subscriptions,viewUrl'
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$URI -like '*/rest/api/*/filter/search*' -and
$GetParameter['expand'] -eq 'description'
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat
}
}

Context 'Input testing' {
It 'Accepts a project key for the -Project parameter' {
{ Find-JiraFilter -Project ABC } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$URI -like '*/rest/api/*/filter/search*' -and
$GetParameter['projectId'] -eq '1'
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat
}

It 'Accepts AccountId, GroupName, and Project parameter values from pipeline by property name' {
$searchObject = [PSCustomObject]@{
AccountId = $owner
GroupName = $group
Project = 'ABC'
}

# Should call Find-JiraFilter using the -Key parameter, so our URL should reflect the key we provided
{ $searchObject | Find-JiraFilter } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$URI -like '*/rest/api/*/filter/search*' -and
$GetParameter['accountId'] -eq $ownerEscaped -and
$GetParameter['groupName'] -eq $groupEscaped -and
$GetParameter['projectId'] -eq '1'
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat
}
}
}
}
Loading