Skip to content

Instantly share code, notes, and snippets.

@darryllee
Created September 12, 2020 05:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save darryllee/22ea98ccf639c303813a3ad4a2ad3480 to your computer and use it in GitHub Desktop.
Save darryllee/22ea98ccf639c303813a3ad4a2ad3480 to your computer and use it in GitHub Desktop.
Download Jira Cloud Attachments into directories named after issue keys
#!/bin/bash
# Minor update to https://bitbucket.org/snippets/atlassiansupportprojects/nedyBb/download-files-attached-to-the-issues
# * Gets issue key in addition to download URL
# * Downloads attachments into directories created for each issue key
#
# Usage:
# chmod u+x ./downloadbykey.sh
# bash ./downloadbykey.sh
# 1. Issue your API Token at https://id.atlassian.com/manage/api-tokens
# 2. Install "jq" command and add it to your bash's PATH https://stedolan.github.io/jq/
# 3. Modify the JQL below
# 4. Hardcode your email@example.com:API_TOKEN into the xargs down at the bottom because I couldn't figure out the shell quoting
HOST="example.atlassian.net"
USER="email@example.com"
API_TOKEN="__YOURS_HERE__"
USERARG="$USER:$API_TOKEN"
# https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-search-post
curl -sL --request POST \
--user "${USER}:${API_TOKEN}" \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--url "https://${HOST}/rest/api/2/search" \
--data '{
"jql": "project = SK AND NOT attachments is EMPTY",
"startAt": 0,
"maxResults": 100,
"fields": ["attachment"],
"fieldsByKeys": false
}' \
| jq -r '.issues[] | {key,content:.fields.attachment[].content} | [.key, .content] | join(" ") ' \
| xargs -n2 sh -c 'curl -L --create-dirs --user email@example.com:API_TOKEN --url $2 --output $1/$(basename $2)' sh
@omegadrh
Copy link

omegadrh commented Feb 1, 2023

Thanks for this! I forked it with the main purpose of saving the attachments with their original filenames. Additionally, I didn't find hard-coding the user and API token in the xargs invocation to be necessary in my usage, just passed in those variable names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment