Skip to content

Instantly share code, notes, and snippets.

@RomiC
Last active May 23, 2021 05:11
Show Gist options
  • Save RomiC/7232293 to your computer and use it in GitHub Desktop.
Save RomiC/7232293 to your computer and use it in GitHub Desktop.
Sample of using JIRA Rest API via bash, curl and sed.
#!/bin/bash
usage() {
echo "
Usage: wti [-h?] [-l LOGIN] ISSUE...
wti = \"(W)hat (T)he (I)ssue?\". Script tries to get description of the specified
ISSUE(es) from jira. For each ISSUE in list script will ouput the line in
the following format: ISSUE_ID — ISSUE_DESC
options:
-h -? Print this message and exit
-l LOGIN Jira-user login. Will be promted if not specified."
}
shortusage() {
echo "
Usage: wti [-h?] [-l LOGIN] ISSUE...
See wti -h for help"
}
error() {
if [[ -n $1 ]];
then
echo -e "\nERROR: $1"
if [[ -n $2 ]];
then
shortusage
fi
exit 1
fi
}
JIRA_URL="https://jira.atlassian.com"
JIRA_AUTH_URI="/rest/auth/latest/session"
JIRA_API_URI="/rest/api/latest/"
OPTIND=1
# Parsing flags
while getopts "h?l:" opt
do
case $opt in
h|\?)
usage
exit 0
;;
l)
JIRA_LOGIN=$OPTARG
;;
esac
done
# getting list of braches
if [[ -z ${!OPTIND} ]]; then
error "You must specified at least one issue!" 1
else
for ((ARG=$OPTIND, NUM=0; ARG>0; ARG++));
do
if [[ -z ${!ARG} ]];
then
break
fi
ISSUES[$NUM]=${!ARG}
NUM=$[NUM+1]
done
fi
# getting login fo JIRA
if [[ -z $JIRA_LOGIN ]]; then
read -p "Enter your login for JIRA: " JIRA_LOGIN
fi
# getting password for JIRA
read -sp "Enter your password for JIRA: " JIRA_PASSWORD
echo ""
# authentication in JIRA
JIRA_SESSION_ID=`curl -s -H "Content-Type: application/json" -d "{\"username\":\"${JIRA_LOGIN}\",\"password\":\"${JIRA_PASSWORD}\"}" -X POST ${JIRA_URL}${JIRA_AUTH_URI} | sed -r 's/^.+JSESSIONID","value":"([^"]+).+$/\1/ig'`
if [[ -n $(echo $JIRA_SESSION_ID | grep error) ]]
then
error "Wrong login or password!"
fi
# getting info about branches
for ((I=0; I<${#ISSUES[@]}; I++));
do
SED=`curl -s -H "Content-Type: application/json" -b JSESSIONID=${JIRA_SESSION_ID} ${JIRA_URL}${JIRA_API_URI}issue/${ISSUES[$I]}?fields=summary | sed -n -re 's@\\\["]([^\\\]+)\\\["]@«\1»@ig' -e 's/^.+key":"([^"]+)".+summary":"([^"]+).+$/\1 - \2\n/igp'`
if [[ -z $SED ]]
then
echo "Issue \"${ISSUES[$I]}\" not found or unknown error has occured!"
else
echo $SED
fi
done
@RomiC
Copy link
Author

RomiC commented May 14, 2021

@SwarnalataPanda, you don't need to touch anything except JIRA_URL -value on line 37. Let's say, your Jira hostname is https://myjira.com, then you need to change line 37 to the following:

JIRA_URL="https://myjira.com"

That's actually everything you need to change in the script. Pls, try this way.

To obtain the list of the attachments, you may use something like this one:

curl -H "Authorization: Basic $JIRA_AUTH" -H "Content-Type: application/json" https://YOUR_JIRA_HOST/rest/api/2/issue/JIRA_ITEM_KEY\?fields\=attachment | jq '.fields.attachment[].content'

Pay attention, that it requires jq utility to be installed on your machine.

@SwarnalataPanda
Copy link

SwarnalataPanda commented May 17, 2021 via email

@DineshSolanki
Copy link

Hi RomiC, But after changing the JIRA_URL in the script, I am getting same error "Issue "BOLT-13360" not found or unknown error has occured!" https://JIRA_URL/browse/BOLT-13360( here, i can able to access the issue) Regards Swarna

On Fri, May 14, 2021 at 4:23 PM Roman Charugin @.***> wrote: @RomiC commented on this gist. ------------------------------ @SwarnalataPanda https://github.com/SwarnalataPanda, you don't need to touch anything except JIRA_URL -value on line 37. Let's say, your Jira hostname is https://myjira.com, then you need to change line 37 to the following: JIRA_URL="https://myjira.com" That's actually everything you need to change in the script. Pls, try this way. To obtain the list of the attachments, you may use something like this one: curl -H "Authorization: Basic $JIRA_AUTH" -H "Content-Type: application/json" https://YOUR_JIRA_HOST/rest/api/2/issue/JIRA_ITEM_KEY?fields=attachment | jq '.fields.attachment[].content' Pay attention, that it requires jq utility to be installed on your machine. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/7232293#gistcomment-3742569, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJPVW2T523PDUTUBUMQZF3LTNT6KZANCNFSM44VG5ONA .
-- Regards, Swarna

make sure you don't have '/' at the end of JIRA_URL.

@DineshSolanki
Copy link

DineshSolanki commented May 22, 2021

@RomiC its very slow as it tries to login every time I run it, is there anyway that it can store the session id and use it so not to login again.
UPDATE - I observed that even if we use the existing session ID its still slow, it has something to parse issue description.

@RomiC
Copy link
Author

RomiC commented May 22, 2021

@SwarnalataPanda, in order to debug add you need to add the following string echo $JIRA_SESSION_ID into line 82. Than make a request and store the output session ID somewhere. After that try the following command in your console: curl -H "Content-Type: application/json" -b JSESSIONID=JIRA_SESSION_ID https://JIRA_URL/rest/api/latest/issue/BOLT-13360?fields=summary.

@DineshSolanki you may request several items per time: wti JIRA-1 JIRA-2 JIRA-3. To be honest, never had performance issues to start investigating the problem.

@DineshSolanki
Copy link

@SwarnalataPanda, in order to debug add you need to add the following string echo $JIRA_SESSION_ID into line 82. Than make a request and store the output session ID somewhere. After that try the following command in your console: curl -H "Content-Type: application/json" -b JSESSIONID=JIRA_SESSION_ID https://JIRA_URL/rest/api/latest/issue/BOLT-13360?fields=summary.

@DineshSolanki you may request several items per time: wti JIRA-1 JIRA-2 JIRA-3. To be honest, never had performance issues to start investigating the problem.

yeah might have to do something with my work vpn, but it's helping, thanks a lot,

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