Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@omegadrh
Forked from darryllee/downloadbykey.sh
Last active February 9, 2023 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omegadrh/f4b3e43049b819ec18597c21f67578d4 to your computer and use it in GitHub Desktop.
Save omegadrh/f4b3e43049b819ec18597c21f67578d4 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://gist.github.com/darryllee/22ea98ccf639c303813a3ad4a2ad3480 ,
# which is in turn a 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
# * Maintains original filenames
#
# Usage:
# chmod u+x ./downloadbykey.sh
# ./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 (at minimum change 'SK' to your actual project key)
# 4. Update maxResults and/or startAt to suit your needs; it may be necessary to break into smaller chunks for large projects.
# My results were truncated at 100, so I had to re-run with startAt: 100, startAt: 200, etc.
# "The maximum allowable [maxResults] value is dictated by the JIRA property 'jira.search.views.default.max'. If you specify a value that is higher than this number, your search results will be truncated."
# However, that property doesn't appear to be readable by my user account in Jira cloud.
#set -x
HOST="example.atlassian.net"
USER="email@example.com"
API_TOKEN="__YOURS_HERE__"
export 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,attachment:.fields.attachment[]} | [.key, .attachment.content, .attachment.filename] | "\"" + join("\" \"") + "\""' \
| xargs -n3 sh -c 'curl -L --create-dirs --user $USERARG --url $2 --output "$1/$3"' sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment