AtlassianPS / JiraPS

PowerShell module to interact with Atlassian JIRA
https://AtlassianPS.org/module/JiraPS
MIT License
323 stars 131 forks source link

Download attachment from Jira task to local filesystem #318

Closed npatgiri closed 5 years ago

npatgiri commented 5 years ago

Context

Want to download attachment file(s) from a Jira Task to local machine

Description

Post getting attachment details Get-JiraIssueAttachment should be able to direct them to download to local filesystem

Additional Information

nickrobbo commented 5 years ago

Hi, I would like to support this feature.
I am not sure if it is already possible using:

#Invoke-WebRequest -Uri $JiraAttachment.Content

I have been playing around with it and so far been unsuccessful.

lipkau commented 5 years ago

This can be done with current resources:

Invoke-JiraMethod -Uri (Get-JiraIssueAttachment -Issue TV-14).Content[0] -OutFile foo.txt
dir foo.txt

    Directory Projects:\JiraPS

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-11-02     22:45            202 foo.txt

but... ConfluencePS has a dedicated command for this: https://github.com/AtlassianPS/ConfluencePS/blob/master/ConfluencePS/Public/Get-AttachmentFile.ps1

If you want to contribute, you can submit a PR implementing this for JiraPS :-)

nickrobbo commented 5 years ago

I must be doing something incorrectly in my script, hoping you can help.

I am looping through all $JiraObjects I find in a specific filter, and then trying to find all ".PNG" attachments for a given issue and download them to my desktop to then use later on.

It seems nothing is found when I use the following code, I amended it to include what you suggested.

        try {
            $JiraIssueAttachment = Get-JiraIssueAttachment -Issue $JiraObject.Key -FileName "*.png" -Credential $Credentials
            if ($JiraIssueAttachment.Count -ne 0) {
               Invoke-JiraMethod -uri (Get-JiraIssueAttachment -Issue $JiraObject.Key -FileName "*.png" -Credential $Credentials).Content[0] `
                -OutFile "$Env:USERPROFILE\Desktop\" + "$JiraObject.Id" + ".png"
            }            
        }
        catch {
            Write-Verbose -Message "No Jira Attachments found for Jira Issue"
        }
lipkau commented 5 years ago

Yeah... that function needs some work... not the most user friendly. but try this:

Get-JiraIssue "TV-13", "TV-14" | Get-JiraIssueAttachment -ea 0 | % { Invoke-JiraMethod -Uri $_.content -OutFile $
_.filename }
nickrobbo commented 5 years ago

Hi, could you advise on why it is not so user friendly? I would be glad to take your advice on board.

I have tried your suggestion and I did not have any success. I removed the "ea" switch so I could see the errors.

I need only .PNG attachments, so I added a filter for only these types of attachments. I also had to add the $Credentials to stop it from throwing the permission error.

I end up with this line: Get-JiraIssue $JiraObject.Key -Credential $Credentials | Get-JiraIssueAttachment -Credential $Credentials -FileName "*.png" | % { Invoke-JiraMethod -Uri $_.content -OutFile $_.filename }

One thing is, I am not sure where these images would be saved on my machine by just supplying $_.filename?

My aim with this script is to aid our developers, so we don't have to manually create Release Notes. The script will just grab the content I need from Jira, and add them to a Word file or PDF. This will then be manually checked over before sending them out.

This is the error I got when running the script:

Invoke-JiraMethod : Oops, you've made a malformed request. - Atlassian accountwindow.baseUrl = 'https:\/\/aid-static-assets.prod.atl-paas.net\/atlassian-id\/14.3.29';window.AJS = {}; window.AJS.EventQueue = []; require(['internal/browser-metrics', 'herment'], function (metrics, herment) {var config =
{storage_key: 'aid', product: 'aid', subproduct: 'signup', save_interval: 20};herment(config).start();});Oops, you've made a malformed request.Often, clearing your browsers cache and restarting your browser will solve this problem.If that doesn't work, please contact support.Terms of UseSupportPrivacy
PolicyAtlassian
At C:\Users\MyUserName\Development\PowerShell\Create-ReleaseNotesFromJiraIssues.ps1:23 char:139
+ ... *.png" | % { Invoke-JiraMethod -Uri $_.content -OutFile $_.filename }
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidResult: (:) [Invoke-JiraMethod], RuntimeException
    + FullyQualifiedErrorId : InvalidResponse.Status400,Invoke-JiraMethod

Edit - I have resolved this, the Invoke-JiraMethod needed my credentials passing too. I would still be interesting in your thoughts on making this more user friendly.

lipkau commented 5 years ago

hey @nickrobbo

could you advise on why it is not so user friendly?

sure. here a the ones I can identify without investigating too much:

  1. The function should not be writing an error just because it did not find a result. This is discussed here: https://github.com/PoshCode/PowerShellPracticeAndStyle/issues/113
  2. The function is not resolving correctly a collection of JiraPS.Issue provided to -Issue
  3. The function is not resolving correctly an input like "TV-13", "TV-14" | Get-JiraIssueAttachment

One thing is, I am not sure where these images would be saved on my machine by just supplying $_.filename?

In the current directory ($pwd.path)

This is the error I got when running the script:

This looks strange. can you run the command with -verbose and show the output?

nickrobbo commented 5 years ago

I added -Credentials $Credential to the Invoke-JiraMethod command, and this resolved that error. It seems I have to pass these to each command.

Thanks for your input, I will go back and refactor the script. I guess I should share my full script for you to comment on, as I think it will make it clearer. I have just left out the part which prompts me for my credentials.

$JiraObjects = Get-JiraIssue -Filter $issuefilter -Credential $Credentials
if ($JiraObjects.Count -ne 0) {
    $FilePath = "$Env:USERPROFILE\Desktop\Test.docx"
    $WordDocument = New-WordDocument $FilePath
    foreach ($JiraObject in $JiraObjects) { #Loop through all issues found in filter
        $TextKey = $JiraObject.Key 
        $TextSummary = $JiraObject.Summary
        $TextHeading1 = $TextKey + ' - ' + $TextSummary 
        Add-WordText -WordDocument $WordDocument -Text $TextHeading1 -FontSize 14 -HeadingType Heading2 # Add to Word Doc
        Add-WordText -WordDocument $WordDocument -Text $JiraObject.Description  -FontSize 12 # Add to Word Doc

        Get-JiraIssue $JiraObject.Key -Credential $Credentials | Get-JiraIssueAttachment -Credential $Credentials -FileName "*.png" | % { Invoke-JiraMethod -Credential $Credentials -Uri $_.content -OutFile $Env:USERPROFILE\Desktop\$_ }        
        Add-WordPageBreak -WordDocument $WordDocument
    }
    Save-WordDocument $WordDocument -Supress $true
    Invoke-Item $FilePath

} else {
    Write-Host "No Issues found using specified filter!" -ForegroundColor Red
}

This is still a rough work in progress, so there are parts hard-coded which will not be in the end.

lipkau commented 5 years ago

Great to hear you could make it work.

I only have two comments about your script. specifically this line:

% { Invoke-JiraMethod -Credential $Credentials -Uri $_.content -OutFile $Env:USERPROFILE\Desktop\$_ }   
  1. $_, which you are passing to the -OutFile parameter, is an object. I believe you want to use a property of $_.
  2. you are not using the file on your desktop anywhere.
nickrobbo commented 5 years ago

Hi, yes I am in the process of adding the parts which will use the attachments, and then delete them once they have been added to the document. Sorry, I should have mentioned that.

In relation to point 1, when I used $_.filename it would save the files as"ImageFileName.Filename". So I changed it to just $_ and it seemed to resolve my issue.

lipkau commented 5 years ago

great. if you need further help, please open a new issue or join us on slack

nickrobbo commented 5 years ago

Thanks, you can now close this issue if you want.