Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverted logic to upload attachments #287

Merged
merged 2 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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