From cdfa66942e78435365ef72243810e29b8be7809c Mon Sep 17 00:00:00 2001 From: Oliver Lipkau Date: Wed, 12 Dec 2018 17:49:15 +0100 Subject: [PATCH 1/2] Implemented the retrieval of an issue's History --- JiraPS/Private/ConvertFrom-Json.ps1 | 2 +- JiraPS/Private/ConvertTo-JiraIssue.ps1 | 8 ++- .../ConvertTo-JiraIssueHistoryEntry.ps1 | 29 +++++++++ JiraPS/Public/Get-JiraIssue.ps1 | 6 +- JiraPS/Public/Get-JiraStatus.ps1 | 61 +++++++++++++++++++ 5 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 JiraPS/Private/ConvertTo-JiraIssueHistoryEntry.ps1 create mode 100644 JiraPS/Public/Get-JiraStatus.ps1 diff --git a/JiraPS/Private/ConvertFrom-Json.ps1 b/JiraPS/Private/ConvertFrom-Json.ps1 index 5824483b..91e75d86 100644 --- a/JiraPS/Private/ConvertFrom-Json.ps1 +++ b/JiraPS/Private/ConvertFrom-Json.ps1 @@ -37,7 +37,7 @@ if ($PSVersionTable.PSVersion.Major -lt 6) { $pairObjectValue = ConvertFrom-Collection $pairObjectValue } - $returnObject | Add-Member Noteproperty $key $pairObjectValue + $returnObject | Add-Member Noteproperty $key $pairObjectValue -ErrorAction Ignore } return $returnObject diff --git a/JiraPS/Private/ConvertTo-JiraIssue.ps1 b/JiraPS/Private/ConvertTo-JiraIssue.ps1 index fcf591a7..5863cf18 100644 --- a/JiraPS/Private/ConvertTo-JiraIssue.ps1 +++ b/JiraPS/Private/ConvertTo-JiraIssue.ps1 @@ -45,7 +45,13 @@ function ConvertTo-JiraIssue { } if ($i.fields.project) { - $props.Project = ConvertTo-JiraProject -InputObject $i.fields.project + $props["Project"] = ConvertTo-JiraProject -InputObject $i.fields.project + } + + if ($i.changelog) { + $props["History"] = foreach ($entry in $i.changelog.histories) { + ConvertTo-JiraIssueHistoryEntry -InputObject $entry + } } foreach ($field in $userFields) { diff --git a/JiraPS/Private/ConvertTo-JiraIssueHistoryEntry.ps1 b/JiraPS/Private/ConvertTo-JiraIssueHistoryEntry.ps1 new file mode 100644 index 00000000..766636e1 --- /dev/null +++ b/JiraPS/Private/ConvertTo-JiraIssueHistoryEntry.ps1 @@ -0,0 +1,29 @@ +function ConvertTo-JiraIssueHistoryEntry { + [CmdletBinding()] + param( + [Parameter( ValueFromPipeline )] + [PSObject[]] + $InputObject + ) + + process { + foreach ($i in $InputObject) { + Write-Debug "[$($MyInvocation.MyCommand.Name)] Converting `$InputObject to custom object" + + $props = @{ + 'ID' = $i.id + 'Author' = ConvertTo-JiraUser $i.author + 'Created' = Get-Date $i.created + 'Items' = $i.items + } + + $result = New-Object -TypeName PSObject -Property $props + $result.PSObject.TypeNames.Insert(0, 'JiraPS.IssueHistoryEntry') + $result | Add-Member -MemberType ScriptMethod -Name 'ToString' -Force -Value { + Write-Output "$($this.Id)" + } + + Write-Output $result + } + } +} diff --git a/JiraPS/Public/Get-JiraIssue.ps1 b/JiraPS/Public/Get-JiraIssue.ps1 index 1c12f4b4..e6d17840 100644 --- a/JiraPS/Public/Get-JiraIssue.ps1 +++ b/JiraPS/Public/Get-JiraIssue.ps1 @@ -112,7 +112,7 @@ function Get-JiraIssue { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Processing [$_key]" Write-Debug "[$($MyInvocation.MyCommand.Name)] Processing `$_key [$_key]" - $getParameter = @{ expand = "transitions" } + $getParameter = @{ expand = "transitions,changelog" } if ($Fields) { $getParameter["fields"] = $Fields } @@ -146,7 +146,7 @@ function Get-JiraIssue { GetParameter = @{ jql = (ConvertTo-URLEncoded $Query) validateQuery = $true - expand = "transitions" + expand = "transitions,changelog" maxResults = $PageSize } @@ -187,7 +187,7 @@ function Get-JiraIssue { Method = "GET" GetParameter = @{ validateQuery = $true - expand = "transitions" + expand = "transitions,changelog" maxResults = $PageSize } OutputType = "JiraIssue" diff --git a/JiraPS/Public/Get-JiraStatus.ps1 b/JiraPS/Public/Get-JiraStatus.ps1 new file mode 100644 index 00000000..bcdcc0de --- /dev/null +++ b/JiraPS/Public/Get-JiraStatus.ps1 @@ -0,0 +1,61 @@ +function Get-JiraStatus { + # .ExternalHelp ..\JiraPS-help.xml + [CmdletBinding( DefaultParameterSetName = '_All' )] + param( + [Parameter( Mandatory, ValueFromPipeline, ParameterSetName = '_Search' )] + [String[]] + $IdOrName, + + [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 + + $resourceURi = "$server/rest/api/latest/status{0}" + } + + process { + Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)" + Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)" + + switch ($PSCmdlet.ParameterSetName) { + '_All' { + $parameter = @{ + URI = $resourceURi -f "" + Method = "GET" + Credential = $Credential + } + Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter" + $result = Invoke-JiraMethod @parameter + + Write-Output (ConvertTo-JiraStatus -InputObject $result) + } + '_Search' { + foreach ($item in $IdOrName) { + Write-Verbose "[$($MyInvocation.MyCommand.Name)] Processing [$item]" + Write-Debug "[$($MyInvocation.MyCommand.Name)] Processing `$item [$item]" + + $parameter = @{ + URI = $resourceURi -f "/$item" + Method = "GET" + Credential = $Credential + } + Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter" + $result = Invoke-JiraMethod @parameter + + Write-Output (ConvertTo-JiraStatus -InputObject $result) + } + } + } + } + + end { + Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete" + } +} From 3e80c8295ccf8d0ceedacfd74324e05bcafc2651 Mon Sep 17 00:00:00 2001 From: Oluf Nissen Date: Thu, 15 Jul 2021 11:05:23 -0700 Subject: [PATCH 2/2] Add switch parameter (GetHistory) to Get-JiraIssue to allow control of getting the history of issues (or not). --- JiraPS/Public/Get-JiraIssue.ps1 | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/JiraPS/Public/Get-JiraIssue.ps1 b/JiraPS/Public/Get-JiraIssue.ps1 index e6d17840..a05999a2 100644 --- a/JiraPS/Public/Get-JiraIssue.ps1 +++ b/JiraPS/Public/Get-JiraIssue.ps1 @@ -88,7 +88,10 @@ function Get-JiraIssue { [Parameter()] [System.Management.Automation.PSCredential] [System.Management.Automation.Credential()] - $Credential = [System.Management.Automation.PSCredential]::Empty + $Credential = [System.Management.Automation.PSCredential]::Empty, + + [Parameter()] + [Switch][Bool]$GetHistory ) begin { @@ -112,7 +115,8 @@ function Get-JiraIssue { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Processing [$_key]" Write-Debug "[$($MyInvocation.MyCommand.Name)] Processing `$_key [$_key]" - $getParameter = @{ expand = "transitions,changelog" } + $getParameter = @{ expand = "transitions" } + if ($GetHistory -eq $true) { $getParameter = @{ expand = "transitions,changelog" }} if ($Fields) { $getParameter["fields"] = $Fields } @@ -140,13 +144,15 @@ function Get-JiraIssue { } } 'ByJQL' { + $expandValue = "transitions" + if ($GetHistory -eq $true) { $expandValue = "transitions,changelog" } $parameter = @{ URI = $searchURi Method = "GET" GetParameter = @{ jql = (ConvertTo-URLEncoded $Query) validateQuery = $true - expand = "transitions,changelog" + expand = $expandValue maxResults = $PageSize } @@ -181,13 +187,14 @@ function Get-JiraIssue { #ToDo:CustomClass Once we have custom classes, this will no longer be necessary #> - + $expandValue = "transitions" + if ($GetHistory -eq $true) { $expandValue = "transitions,changelog" } $parameter = @{ URI = $filterObj Method = "GET" GetParameter = @{ validateQuery = $true - expand = "transitions,changelog" + expand = $expandvalue maxResults = $PageSize } OutputType = "JiraIssue"