Skip to content

Commit

Permalink
Merge pull request #287 from lipkau/revert/UploadOfAttachments
Browse files Browse the repository at this point in the history
Reverted logic to upload attachments
  • Loading branch information
lipkau authored Jun 14, 2018
2 parents e44a4a8 + adb9960 commit 6cc9209
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
35 changes: 35 additions & 0 deletions JiraPS/Private/Resolve-FilePath.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function Resolve-FilePath {
<#
.SYNOPSIS
Resolve a path to it's full path
.DESCRIPTION
Resolve relative paths and PSDrive paths to the full path
.LINK
https://github.com/pester/Pester/blob/5796c95e4d6ff5528b8e14865e3f25e40f01bd65/Functions/TestResults.ps1#L13-L27
#>
[CmdletBinding()]
param(
# Path to be resolved
[Parameter( Mandatory, ValueFromPipeline )]
[ValidateNotNullOrEmpty()]
[Alias("PSPath", "LiteralPath")]
[String]
$Path
)

process {
$folder = Split-Path -Path $Path -Parent
$file = Split-Path -Path $Path -Leaf

if ( -not ([String]::IsNullOrEmpty($folder))) {
$folderResolved = Resolve-Path -Path $folder
}
else {
$folderResolved = Resolve-Path -Path $ExecutionContext.SessionState.Path.CurrentFileSystemLocation
}

Join-Path -Path $folderResolved.ProviderPath -ChildPath $file
}
}
37 changes: 30 additions & 7 deletions JiraPS/Public/Add-JiraIssueAttachment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,38 @@ function Add-JiraIssueAttachment {
# Find the proper object for the Issue
$issueObj = Resolve-JiraIssueObject -InputObject $Issue -Credential $Credential

$parameter = @{
URI = $resourceURi -f $issueObj.RestURL
Method = "POST"
Credential = $Credential
}

foreach ($file in $FilePath) {
$parameter["InFile"] = $file
$file = Resolve-FilePath -Path $file

$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
$boundary = [System.Guid]::NewGuid().ToString()

$fileName = Split-Path -Path $file -Leaf
$readFile = [System.IO.File]::ReadAllBytes($file)
$fileEnc = $enc.GetString($readFile)

$bodyLines = @'
--{0}
Content-Disposition: form-data; name="file"; filename="{1}"
Content-Type: application/octet-stream
{2}
--{0}--
'@ -f $boundary, $fileName, $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
Expand Down

0 comments on commit 6cc9209

Please sign in to comment.