Skip to content

Instantly share code, notes, and snippets.

@verfriemelt-dot-org
Created June 6, 2024 12:04
Show Gist options
  • Save verfriemelt-dot-org/2de5a5d400d0bebc55b1b089586d5d72 to your computer and use it in GitHub Desktop.
Save verfriemelt-dot-org/2de5a5d400d0bebc55b1b089586d5d72 to your computer and use it in GitHub Desktop.
jira api worklog poster
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
shopt -s nocasematch
supported_projects=(
"ABO" "AC" "AND" "BETO" "CORE" "EOSJIRA" "WIGGLE" "DMDV" "DPC" "DPTEMP" "EBE" "FE" "FIS" "HOST" "HR"
"INTERN" "INTRA" "IOS" "IT" "MAN" "MDA" "MS" "MSA" "MSI" "OFF" "OS" "PB" "PL"
"PMOLD" "PSX" "QS" "RE" "RF" "RMVTRANS" "RS" "RSC" "SEC" "SUM" "TEST" "TICK"
"TICKINT" "TICKM" "TICKINF" "TR" "TT" "TZ" "UX" "VPWS" "WINP" "ROCKET" "TICKP"
)
date_command="date"
stat_command="stat"
if [[ $(uname) != 'linux' ]]
then
# use gdate on macos
date_command="gdate"
stat_command="gstat"
fi
function help() {
cat << 'HELP'
usage: work <ticket> [<amount in hours> [<absolute or relative date>]]
$ work TT-1 1.5
adding 5400.0 seconds to TT-1 at 2023-07-07T10:51:22+0200
$ work TT-1 1.5 yesterday
adding 5400.0 seconds to TT-1 at 2023-07-06T10:51:25+0200
$ work TT-1 1.5 last week
adding 5400.0 seconds to TT-1 at 2023-06-30T10:51:27+0200
you can have shortcuts prepared like «ln -s work tt-1»
to directly use
$ tt-1 <amount>
if you omit the amount, it defaults to 15min
MacOS: if you are using macos, you need to install gdate from gnu coreutils
https://apple.stackexchange.com/questions/231224/how-to-have-gnus-date-in-os-x
HELP
exit 0
}
function logWork() {
tick=$(basename "$0")
regex='('
regex+=$( IFS="|" ; echo "${supported_projects[*]}" )
regex+=')'
regex+="-[0-9]+"
# check if our filename contains a valid ticket, in case of symlinks
if [[ ! ${tick} =~ $regex ]]
then
# exit out if no argument or invalid ticket
if [[ $# -eq 0 ]]
then
help
fi
if [[ ! "${1}" =~ $regex ]]
then
echo "${1} not supported, use one of those:"
echo "${supported_projects[@]}"
exit 1
fi
tick=$1
shift 1
fi
time=${1:-0.25}
date=$(${date_command} +"%Y-%m-%dT%H:%M:%S.000%z")
if [[ $# -gt 1 ]]
then
shift 1
date=$(${date_command} +"%Y-%m-%dT%H:%M:%S.000%z" -d "$*")
fi
work=$(echo "$time * 3600" | bc)
echo "adding ${work} seconds to ${tick} at ${date}"
request='{}'
request=$(echo "$request" | jq -c ".timeSpentSeconds |= ${work}");
request=$(echo "$request" | jq -c ".started |= \"${date}\"");
curl -s \
-X POST \
-H "Authorization: Basic $auth" \
-H "Content-Type: application/json"\
"https://jira.eos-uptrade.de/rest/api/2/issue/${tick}/worklog" \
-d "${request}" | jq
exit 0
}
function listWork() {
since=$(${date_command} "+%s" -d "$(${date_command} -d 'yesterday' --iso-8601=date)")
if [[ $# -gt 1 ]]
then
shift 1
since=$(${date_command} "+%s" -d "$(${date_command} -d "$*" --iso-8601=date)")
fi
payload='{ "jql": "worklogDate>=-7d AND worklogAuthor=currentUser()"}'
# in ms
since=$(( "${since}"*1000 ))
echo "your last issues tracked: "
echo ""
curl -s \
-X POST \
-H "Authorization: Basic $auth" \
-H "Content-Type: application/json"\
"https://jira.eos-uptrade.de/rest/api/2/search" \
-d "${payload}" | jq .issues[].key -r
exit 0
}
auth=$(cat "$HOME"/.jira)
if [[ ! -f "$HOME/.jira" ]]
then
echo "run «echo -n \$user:\$password | base64 > ~/.jira && chmod 600 ~/.jira» to generate token required"
exit 1
fi
if [[ $($stat_command -c '%a' "$HOME/.jira") != "600" || $($stat_command -c '%u' "$HOME/.jira") != "$UID" || $($stat_command -c '%g' "$HOME/.jira") != "$UID" ]]
then
echo "please fix your ~/.jira file permissions, run «chmod 600 ~/.jira && chown $USER:$USER ~/.jira»"
exit 1;
fi
while getopts l opt; do
case $opt in
l)
# shellcheck disable=SC2048,SC2086
listWork $*
;;
*) exit 1
;;
esac
done
# shellcheck disable=SC2048,SC2086
logWork $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment