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

Download attachment from Jira task to local filesystem #318

Closed
npatgiri opened this issue Oct 31, 2018 · 11 comments
Closed

Download attachment from Jira task to local filesystem #318

npatgiri opened this issue Oct 31, 2018 · 11 comments

Comments

@npatgiri
Copy link

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
Copy link

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
Copy link
Member

lipkau commented Nov 2, 2018

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
Copy link

nickrobbo commented Nov 3, 2018

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
Copy link
Member

lipkau commented Nov 3, 2018

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
Copy link

nickrobbo commented Nov 5, 2018

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
Copy link
Member

lipkau commented Nov 5, 2018

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: When should you write an error if you have no output? PoshCode/PowerShellPracticeAndStyle#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
Copy link

nickrobbo commented Nov 5, 2018

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
Copy link
Member

lipkau commented Nov 5, 2018

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
Copy link

nickrobbo commented Nov 5, 2018

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
Copy link
Member

lipkau commented Nov 5, 2018

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

@nickrobbo
Copy link

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

@lipkau lipkau added this to the v2.9 milestone Dec 6, 2018
@lipkau lipkau mentioned this issue Dec 6, 2018
6 tasks
@ghost ghost assigned lipkau Dec 6, 2018
@ghost ghost added the Status:In Review label Dec 6, 2018
lipkau added a commit to lipkau/JiraPS that referenced this issue Dec 8, 2018
lipkau added a commit to lipkau/JiraPS that referenced this issue Dec 12, 2018
lipkau added a commit that referenced this issue Dec 12, 2018
@lipkau lipkau closed this as completed Dec 12, 2018
@ghost ghost removed the Status:In Review label Dec 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants