Skip to content

Commit

Permalink
Merge pull request #89 from padgers/issue-38
Browse files Browse the repository at this point in the history
Adding fields, comment and assignee parameters to Invoke-JiraIssueTransition. #38
  • Loading branch information
lipkau committed May 24, 2017
2 parents 9362313 + d5d2fcd commit 328c90e
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 5 deletions.
114 changes: 111 additions & 3 deletions PSJira/Public/Invoke-JiraIssueTransition.ps1
Expand Up @@ -16,6 +16,16 @@ function Invoke-JiraIssueTransition
.EXAMPLE
Invoke-JiraIssueTransition -Issue TEST-01 -Transition 11
Invokes transition ID 11 on issue TEST-01.
.EXAMPLE
Invoke-JiraIssueTransition -Issue TEST-01 -Transition 11 -Comment 'Transition comment'
Invokes transition ID 11 on issue TEST-01 with a comment. Requires the comment field to be configured visible for transition.
.EXAMPLE
Invoke-JiraIssueTransition -Issue TEST-01 -Transition 11 -Assignee 'joe.bloggs'
Invokes transition ID 11 on issue TEST-01 and assigns to user 'Joe Blogs'. Requires the assignee field to be configured as visible for transition.
.EXAMPLE
$transitionFields = @{'customfield_12345' = 'example'}
Invoke-JiraIssueTransition -Issue TEST-01 -Transition 11 -Fields $transitionFields
Invokes transition ID 11 on issue TEST-01 and configures a custom field value. Requires fields to be configured as visible for transition.
.EXAMPLE
$transition = Get-JiraIssue -Issue TEST-01 | Select-Object -ExpandProperty Transition | ? {$_.ResultStatus.Name -eq 'In Progress'}
Invoke-JiraIssueTransition -Issue TEST-01 -Transition $transition
Expand All @@ -40,6 +50,17 @@ function Invoke-JiraIssueTransition
Position = 1)]
[Object] $Transition,

# Any additional fields that should be updated. Fields must be configured to appear on the transition screen to use this parameter.
[System.Collections.Hashtable] $Fields,

# New assignee of the issue. Enter 'Unassigned' to unassign the issue. Assignee field must be configured to appear on the transition screen to use this parameter.
[Parameter(Mandatory = $false)]
[Object] $Assignee,

# Comment that should be added to JIRA. Comment field must be configured to appear on the transition screen to use this parameter.
[Parameter(Mandatory = $false)]
[String] $Comment,

# Credentials to use to connect to Jira
[Parameter(Mandatory = $false)]
[System.Management.Automation.PSCredential] $Credential
Expand Down Expand Up @@ -95,7 +116,96 @@ function Invoke-JiraIssueTransition
'id' = $transitionId;
}
}
$json = ConvertTo-Json -InputObject $props -Depth 3

if ($Assignee)
{
Write-Debug "[Invoke-JiraIssueTransition] Testing Assignee type"
if ($Assignee -eq 'Unassigned')
{
Write-Debug "[Invoke-JiraIssueTransition] 'Unassigned' String passed. Issue will be assigned to no one."
$assigneeString = ""
$validAssignee = $true
} else {
Write-Debug "[Invoke-JiraIssueTransition] Attempting to obtain Jira user [$Assignee]"
$assigneeObj = Get-JiraUser -InputObject $Assignee -Credential $Credential
if ($assigneeObj)
{
Write-Debug "[Invoke-JiraIssueTransition] User found (name=[$($assigneeObj.Name)],RestUrl=[$($assigneeObj.RestUrl)])"
$assigneeString = $assigneeObj.Name
$validAssignee = $true
} else {
Write-Debug "[Invoke-JiraIssueTransition] Unable to obtain Assignee. Exception will be thrown."
throw "Unable to validate Jira user [$Assignee]. Use Get-JiraUser for more details."
}
}
}


if ($validAssignee)
{
Write-Debug "[Invoke-JiraIssueTransition] Updating Assignee"
$props += @{
'fields' = @{
'assignee' = @{
'name' = $assigneeString;
}
}
}
}


if ($Fields)
{
Write-Debug "[Invoke-JiraIssueTransition] Validating field names"
$props += @{
'update' = @{}
}

foreach ($k in $Fields.Keys)
{
$name = $k
$value = $Fields.$k
Write-Debug "[Invoke-JiraIssueTransition] Attempting to identify field (name=[$name], value=[$value])"

$f = Get-JiraField -Field $name -Credential $Credential
if ($f)
{
# For some reason, this was coming through as a hashtable instead of a String,
# which was causing ConvertTo-Json to crash later.
# Not sure why, but this forces $id to be a String and not a hashtable.
$id = "$($f.ID)"
Write-Debug "[Invoke-JiraIssueTransition] Field [$name] was identified as ID [$id]"
$props.update.$id = @()
$props.update.$id += @{
'set' = $value;
}
} else {
Write-Debug "[Invoke-JiraIssueTransition] Field [$name] could not be identified in Jira"
throw "Unable to identify field [$name] from -Fields hashtable. Use Get-JiraField for more information."
}
}
}


if ($Comment)
{
Write-Debug "[Invoke-JiraIssueTransition] Adding comment"
if (-not $Fields)
{
Write-Debug "[Invoke-JiraIssueTransition] Create 'update' hashtable since not already created"
$props += @{
'update' = @{}
}
}

$props.update.comment += ,@{
'add' = @{
'body' = $Comment
}
}
}

$json = ConvertTo-Json -InputObject $props -Depth 4
Write-Debug "[Invoke-JiraIssueTransition] Converted properties to JSON"

Write-Debug "[Invoke-JiraIssueTransition] Preparing for blastoff!"
Expand All @@ -118,5 +228,3 @@ function Invoke-JiraIssueTransition
Write-Debug "Complete"
}
}


36 changes: 34 additions & 2 deletions Tests/Invoke-JiraIssueTransition.Tests.ps1
Expand Up @@ -83,7 +83,39 @@ InModuleScope PSJira {
Assert-MockCalled Get-JiraIssue -ModuleName PSJira -Exactly -Times 2 -Scope It
Assert-MockCalled Invoke-JiraMethod -ModuleName PSJira -Exactly -Times 1 -Scope It
}
}
}


It "Updates custom fields if provided to the -Fields parameter" {
Mock Get-JiraField {
[PSCustomObject] @{
'Name' = $Field;
'ID' = $Field;
}
}
{ Invoke-JiraIssueTransition -Issue $issueKey -Transition 11 -Fields @{'customfield_12345'='foo'; 'customfield_67890'='bar'} } | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -ModuleName PSJira -Times 1 -Scope It -ParameterFilter { $Method -eq 'Post' -and $URI -like "*/rest/api/latest/issue/$issueID/transitions" -and $Body -like '*customfield_12345*set*foo*' }
Assert-MockCalled -CommandName Invoke-JiraMethod -ModuleName PSJira -Times 1 -Scope It -ParameterFilter { $Method -eq 'Post' -and $URI -like "*/rest/api/latest/issue/$issueID/transitions" -and $Body -like '*customfield_67890*set*bar*' }
}

It "Updates assignee name if provided to the -Assignee parameter"{
Mock Get-JiraUser {
[PSCustomObject] @{
'Name' = 'powershell-user';
'RestUrl' = "$jiraServer/rest/api/2/user?username=powershell-user";
}
}
{ $result = Invoke-JiraIssueTransition -Issue $issueKey -Transition 11 -Assignee 'powershell-user'} | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -ModuleName PSJira -Times 1 -Scope It -ParameterFilter { $Method -eq 'Post' -and $URI -like "*/rest/api/latest/issue/$issueID/transitions" -and $Body -like '*name*powershell-user*' }
}

It "Unassigns an issue if 'Unassigned' is passed to the -Assignee parameter"{
{ $result = Invoke-JiraIssueTransition -Issue $issueKey -Transition 11 -Assignee 'Unassigned'} | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -ModuleName PSJira -Times 1 -Scope It -ParameterFilter { $Method -eq 'Post' -and $URI -like "*/rest/api/latest/issue/$issueID/transitions" -and $Body -like '*name*""*' }
}

It "Adds a comment if provide to the -Comment parameter"{
{ $result = Invoke-JiraIssueTransition -Issue $issueKey -Transition 11 -Comment 'test comment'} | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -ModuleName PSJira -Times 1 -Scope It -ParameterFilter { $Method -eq 'Post' -and $URI -like "*/rest/api/latest/issue/$issueID/transitions" -and $Body -like '*body*test comment*' }
}
}
}

0 comments on commit 328c90e

Please sign in to comment.