Public/Add-JiraIssueAttachment.ps1
function Add-JiraIssueAttachment { [CmdletBinding( SupportsShouldProcess )] param( [Parameter( Mandatory )] [ValidateNotNullOrEmpty()] [ValidateScript( { if (("JiraPS.Issue" -notin $_.PSObject.TypeNames) -and (($_ -isnot [String]))) { $errorItem = [System.Management.Automation.ErrorRecord]::new( ([System.ArgumentException]"Invalid Type for Parameter"), 'ParameterType.NotJiraIssue', [System.Management.Automation.ErrorCategory]::InvalidArgument, $_ ) $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, <# #ToDo:CustomClass Once we have custom classes, this can also accept ValueFromPipeline #> [Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )] [ValidateScript( { if (-not (Test-Path $_ -PathType Leaf)) { $errorItem = [System.Management.Automation.ErrorRecord]::new( ([System.ArgumentException]"File not found"), 'ParameterValue.FileNotFound', [System.Management.Automation.ErrorCategory]::ObjectNotFound, $_ ) $errorItem.ErrorDetails = "No file could be found with the provided path '$_'." $PSCmdlet.ThrowTerminatingError($errorItem) } else { return $true } } )] [Alias('InFile', 'FullName', 'Path')] [String[]] $FilePath, [PSCredential] $Credential, [Switch] $PassThru ) begin { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started" $resourceURi = "{0}/attachments" } process { Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)" Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)" if (@($Issue).Count -ne 1) { $errorItem = [System.Management.Automation.ErrorRecord]::new( ([System.ArgumentException]"invalid Issue provided"), 'ParameterValue.JiraIssue', [System.Management.Automation.ErrorCategory]::InvalidArgument, $_ ) $errorItem.ErrorDetails = "Only one Issue can be provided at a time." $PSCmdlet.ThrowTerminatingError($errorItem) } # Find the proper object for the Issue $issueObj = Resolve-JiraIssueObject -InputObject $Issue -Credential $Credential foreach ($file in $FilePath) { $fileName = Split-Path -Path $file -Leaf $readFile = [System.IO.File]::ReadAllBytes($file) $enc = [System.Text.Encoding]::GetEncoding("iso-8859-1") $fileEnc = $enc.GetString($readFile) $boundary = [System.Guid]::NewGuid().ToString() $mimeType = [System.Web.MimeMapping]::GetMimeMapping($file) if ($mimeType) { $ContentType = $mimeType } else { $ContentType = "application/octet-stream" } $bodyLines = @' --{0} Content-Disposition: form-data; name="file"; filename="{1}" Content-Type: {2} {3} --{0}-- '@ -f $boundary, $fileName, $mimeType, $fileEnc $headers = @{ 'X-Atlassian-Token' = 'nocheck' 'Content-Type' = "multipart/form-data; boundary=`"$boundary`"" } $parameter = @{ URI = $resourceURi -f $issueObj.RestURL Method = "POST" Body = $bodyLines Headers = $headers RawBody = $true Credential = $Credential } Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter" if ($PSCmdlet.ShouldProcess($IssueObj.Key, "Adding attachment '$($fileName)'.")) { $rawResult = Invoke-JiraMethod @parameter if ($PassThru) { Write-Output (ConvertTo-JiraAttachment -InputObject $rawResult) } } } } end { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete" } } |