From dfe1fffbfb79ff0440b359b1ceeb3038f4a4774c Mon Sep 17 00:00:00 2001 From: Marco Borm Date: Mon, 31 Aug 2020 12:08:18 +0200 Subject: [PATCH 1/2] original Add-JiraRemoteLink from "https://github.com/AtlassianPS/JiraPS/issues/375" created by "j9850s" --- JiraPS/Public/Add-JiraRemoteLink.ps1 | 105 +++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 JiraPS/Public/Add-JiraRemoteLink.ps1 diff --git a/JiraPS/Public/Add-JiraRemoteLink.ps1 b/JiraPS/Public/Add-JiraRemoteLink.ps1 new file mode 100644 index 00000000..860dd62e --- /dev/null +++ b/JiraPS/Public/Add-JiraRemoteLink.ps1 @@ -0,0 +1,105 @@ +function Add-JiraRemoteLink { +# .ExternalHelp ..\JiraPS-help.xml + [CmdletBinding( SupportsShouldProcess )] + param( + [Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )] + [ValidateNotNullOrEmpty()] + [ValidateScript( + { + if (("JiraPS.Issue" -notin $_.PSObject.TypeNames) -and (($_ -isnot [String]))) { + $exception = ([System.ArgumentException]"Invalid Type for Parameter") #fix code highlighting] + $errorId = 'ParameterType.NotJiraIssue' + $errorCategory = 'InvalidArgument' + $errorTarget = $_ + $errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget + $errorItem.ErrorDetails = "Wrong object type provided for Issue. Expected [JiraPS.Issue] or [String], 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 + } + } + )] + [Alias('Key')] + [Object[]] + $Issue, + + [Parameter( Mandatory )] + [ValidateScript( + { + $objectProperties = Get-Member -InputObject $_ -MemberType *Property + if (-not( + ($objectProperties.Name -contains "url") -and + ($objectProperties.Name -contains "title") + )) { + $exception = ([System.ArgumentException]"Invalid Parameter") #fix code highlighting] + $errorId = 'ParameterProperties.Incomplete' + $errorCategory = 'InvalidArgument' + $errorTarget = $_ + $errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget + $errorItem.ErrorDetails = "The RemoteLink provided does not contain the information needed." + $PSCmdlet.ThrowTerminatingError($errorItem) + <# + #ToDo:CustomClass + Once we have custom classes, this check can be done with Type declaration + #> + } + else { + return $true + } + } + )] + [Object[]] + $RemoteLink, + + [Parameter()] + [System.Management.Automation.PSCredential] + [System.Management.Automation.Credential()] + $Credential = [System.Management.Automation.PSCredential]::Empty + ) + + begin { + Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started" + } + + + process { + Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)" + Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)" + + foreach ($_issue in $Issue) { + # Find the proper object for the Issue + $issueObj = Resolve-JiraIssueObject -InputObject $_issue -Credential $Credential + + foreach ($_remoteLink in $RemoteLink) { + + $body = @{ + object = @{ + url = $_remoteLink.url + title = $_remoteLink.title + } + } + + $parameter = @{ + URI = "{0}/remotelink" -f $issueObj.RestUrl + Method = "POST" + Body = ConvertTo-Json -InputObject $body + Credential = $Credential + } + + Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter" + if ($PSCmdlet.ShouldProcess($issueObj.Key)) { + Invoke-JiraMethod @parameter + } + } + } + } + + end { + Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete" + } +} From e169fea1518e8cf17fad84e90214c38782dd5c97 Mon Sep 17 00:00:00 2001 From: Marco Borm Date: Wed, 2 Sep 2020 16:14:59 +0200 Subject: [PATCH 2/2] - created counterpart for ConvertTo-JiraLink - extended Add-JiraRemoteLink, so it can accept the result of for example Get-JiraRemoteLink if id and Rest is removed before --- JiraPS/Private/ConvertFrom-JiraLink.ps1 | 95 +++++++++++++++++++++++++ JiraPS/Public/Add-JiraRemoteLink.ps1 | 30 +++----- 2 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 JiraPS/Private/ConvertFrom-JiraLink.ps1 diff --git a/JiraPS/Private/ConvertFrom-JiraLink.ps1 b/JiraPS/Private/ConvertFrom-JiraLink.ps1 new file mode 100644 index 00000000..858d32b3 --- /dev/null +++ b/JiraPS/Private/ConvertFrom-JiraLink.ps1 @@ -0,0 +1,95 @@ +function ConvertFrom-JiraLink { + [CmdletBinding()] + param( + [Parameter( ValueFromPipeline )] + [PSObject[]] + $InputObject + ) + + process { + foreach ($i in $InputObject) { + Write-Debug "[$($MyInvocation.MyCommand.Name)] Converting `$InputObject to custom object" + + $props = @{} + if ($i.Id) { + $props.Add('id', $i.Id) + } + if ($i.RestUrl) { + $props.Add('self', $i.RestUrl) + } + + if ($i.globalId) { + $props.globalId = $i.globalId + } + + if ($i.application -and ($i.application.type -or $i.application.name) ) { + $props.application = New-Object PSObject -Prop @{ + type = $i.application.type + name = $i.application.name + } + } + + if ($i.relationship) { + $props.relationship = $i.relationship + } + + if ($i.object) { + if ($i.object.icon -and ($i.object.icon.title -or $i.object.icon.url16x16)) { + $icon = New-Object PSObject -Prop @{ + url16x16 = $i.object.icon.url16x16 + } + if ( $i.object.icon.title ) { + $icon = $icon | Add-Member -MemberType NoteProperty -Name "title" -Value $i.object.icon.title -PassThru + } + } + else { $icon = $null } + + if ($i.object.status.icon -and ($i.object.status.icon.link -or $i.object.status.icon.title -or $i.object.status.icon.url16x16)) { + $statusIcon = New-Object PSObject + if ( $i.object.status.icon.title ) { + $statusIcon = $statusIcon | Add-Member -MemberType NoteProperty -Name "title" -Value $i.object.status.icon.title -PassThru + } + if ( $i.object.status.icon.link ) { + $statusIcon = $statusIcon | Add-Member -MemberType NoteProperty -Name "link" -Value $i.object.status.icon.link -PassThru + } + if ( $i.object.status.icon.url16x16 ) { + $statusIcon = $statusIcon | Add-Member -MemberType NoteProperty -Name "url16x16" -Value $i.object.status.icon.url16x16 -PassThru + } + } + else { $statusIcon = $null } + + if ($i.object.status -and ($i.object.status.resolved -or $statusIcon)) { + $status = New-Object PSObject -Prop @{ + resolved = $i.object.status.resolved + icon = $statusIcon + } + } + else { $status = $null } + + $props.object = New-Object PSObject -Prop @{ + url = $i.object.url + } + if ( $icon ) { + $props.object = $props.object | Add-Member -MemberType NoteProperty -Name "icon" -Value $icon -PassThru + } + if ( $status ) { + $props.object = $props.object | Add-Member -MemberType NoteProperty -Name "status" -Value $status -PassThru + } + if ( $i.object.title ) { + $props.object = $props.object | Add-Member -MemberType NoteProperty -Name "title" -Value $i.object.title -PassThru + } + if ( $i.object.summary ) { + $props.object = $props.object | Add-Member -MemberType NoteProperty -Name "summary" -Value $i.object.summary -PassThru + } + } + + $result = New-Object -TypeName PSObject -Property $props + $result.PSObject.TypeNames.Insert(0, 'JiraPS.Link') + $result | Add-Member -MemberType ScriptMethod -Name "ToString" -Force -Value { + Write-Output "$($this.Id)" + } + + Write-Output $result + } + } +} diff --git a/JiraPS/Public/Add-JiraRemoteLink.ps1 b/JiraPS/Public/Add-JiraRemoteLink.ps1 index 860dd62e..78e13529 100644 --- a/JiraPS/Public/Add-JiraRemoteLink.ps1 +++ b/JiraPS/Public/Add-JiraRemoteLink.ps1 @@ -32,21 +32,16 @@ function Add-JiraRemoteLink { [ValidateScript( { $objectProperties = Get-Member -InputObject $_ -MemberType *Property - if (-not( - ($objectProperties.Name -contains "url") -and - ($objectProperties.Name -contains "title") - )) { - $exception = ([System.ArgumentException]"Invalid Parameter") #fix code highlighting] - $errorId = 'ParameterProperties.Incomplete' + if (($objectProperties.Name -contains "RestUrl") -or + ($objectProperties.Name -contains "Id") + ) { + $exception = ([System.ArgumentException]"Invalid Parameter") + $errorId = 'ParameterProperties.Invalid' $errorCategory = 'InvalidArgument' $errorTarget = $_ $errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget - $errorItem.ErrorDetails = "The RemoteLink provided does not contain the information needed." + $errorItem.ErrorDetails = "The RemoteLink provided contains a Id and or a RestUrl which is not allowed for adding RemoteLinks." $PSCmdlet.ThrowTerminatingError($errorItem) - <# - #ToDo:CustomClass - Once we have custom classes, this check can be done with Type declaration - #> } else { return $true @@ -65,7 +60,7 @@ function Add-JiraRemoteLink { begin { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started" } - + process { Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)" @@ -77,20 +72,13 @@ function Add-JiraRemoteLink { foreach ($_remoteLink in $RemoteLink) { - $body = @{ - object = @{ - url = $_remoteLink.url - title = $_remoteLink.title - } - } - $parameter = @{ URI = "{0}/remotelink" -f $issueObj.RestUrl Method = "POST" - Body = ConvertTo-Json -InputObject $body + Body = ConvertTo-Json -InputObject (ConvertFrom-JiraLink $_remoteLink) Credential = $Credential } - + Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter" if ($PSCmdlet.ShouldProcess($issueObj.Key)) { Invoke-JiraMethod @parameter