Skip to content

Instantly share code, notes, and snippets.

@DerBroader71
Last active June 18, 2022 19:58
Show Gist options
  • Save DerBroader71/968b5b07e20e0c554b9c08af5a389765 to your computer and use it in GitHub Desktop.
Save DerBroader71/968b5b07e20e0c554b9c08af5a389765 to your computer and use it in GitHub Desktop.
Bash script for logging time to a Jira task
#!/bin/bash
####based on https://gist.github.com/patrick-blom/593dd02338490941f4ba09f714e821c3 with API support instead of basic auth####
####Jira cloud doesn't support basic auth####
####add a worklog to a specific jira issue####
####get API token from https://id.atlassian.com/manage/api-tokens####
#arguments:
#-i: issue or ticket number
#-t: time in minutes
#-c: comment for the worklog
#optional:
#-u: jira user, if not defined in script
####example
#worklog -i SCRUM-42 -t 15 -c "Daily Scrum"
#####configuration
jiraUser="[USERNAME]"
jiraDomain="[JIRA_URL]"
jiraAPIToken="[APITOKEN]"
#####configuration
for arg in "$@"
do
if [ "$arg" == "--help" ] || [ "$arg" == "-h" ]
then
echo -e "####add a worklog to a specific jira issue####"
echo -e ""
echo -e "arguments:"
echo -e "-i: issue or ticket number"
echo -e "-t: time in minutes"
echo -e "-c: comment for the worklog"
echo -e ""
echo -e "optional:"
echo -e "-u: jira user, if not defined in script"
echo -e ""
echo -e "####example"
echo -e "worklog -i SCRUM-42 -t 15 -c \"Daily Scrum\" "
exit 0
fi
done
while getopts u:i:t:c: option
do
case "${option}"
in
u) jiraUser=${OPTARG};;
i) issue=${OPTARG};;
t) timeInSeconds=$((${OPTARG}* 60));;
c) comment="${OPTARG}";;
esac
done
currentDateTime=$(date -u +"%Y-%m-%dT%H:%M:%S.000+0000")
if [[ -z "$issue" || -z "$timeInSeconds" || -z "$comment" ]]; then
echo -e "the paramters -i, -t and -c are requiered!"
echo -e "use --help or -h for help"
exit 1
fi
if [ -z "$jiraUser" ]; then
read -p "Enter Username: " jiraUser
fi
echo -e "";
dataString=$( jq -n \
--arg c "$comment" \
--arg cdt "$currentDateTime" \
--arg t "$timeInSeconds" \
'{comment:$c,started:$cdt,timeSpentSeconds:$t}')
curl -u $jiraUser:$jiraAPIToken -H "Content-Type: application/json" -X POST "$jiraDomain/rest/api/2/issue/$issue/worklog" -d "$dataString"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment