Skip to content

Instantly share code, notes, and snippets.

@andy722
Created April 16, 2018 15:31
Show Gist options
  • Save andy722/80a18fe29a316875a1f25465e845eda9 to your computer and use it in GitHub Desktop.
Save andy722/80a18fe29a316875a1f25465e845eda9 to your computer and use it in GitHub Desktop.
Quick & dirty script to export YouTrack time spent reports filtered by user
#!/usr/bin/env bash
export YT_COOKIE="" # Get this from browser cookies, looks like "pp71vwl46b0t6ftkmo6bt5xt"
export REPORT_ID="" # Report ID, get this from URI of report page. Looks like 104-110
export USER="" # YouTrack username to filter by.
export YT_HOST="" # YouTrack host, like https://youtrack.mycompany.com.
####################################################################################################
C_LIGHTGRAY="\033[37m"
C_DARKGRAY="\033[1;30m"
C_DEFAULT="\033[m"
####################################################################################################
TMP="$(mktemp)"
REPORT="$(mktemp)"
function cleanup() { rm -fr "${TMP}"; rm -fr "${REPORT}"; }
trap cleanup exit
curl -vvv --cookie "YTSESSIONID=${YT_COOKIE}" \
"$YT_HOST/rest/current/reports/$REPORT_ID/export" >${REPORT} 2>/dev/null
if [ ! $? -eq 0 ]; then echo "Failed loading report"; exit 1; fi
####################################################################################################
# Initial fields in CSV:
# "description","date","duration","estimate","authorLogin","authorName","issueId","issueSummary","groupName","type"
while read line; do
line="$(echo ${line} | sed 's/\"\,\"/Ω/g;s/^\"//;s/\"$//;s/\"\"/\"/g;s/\\t/ /g')"
OLD_IFS="$IFS"
IFS=$'Ω' read -ra arr <<<"$line"
IFS=${OLD_IFS}
[[ "x${arr[4]}" != "x$USER" ]] && continue
_date="`date -r $[${arr[1]}/1000] +%Y.%m.%d`"
_duration="${arr[2]}"
_duration=`echo $(bc <<< "scale=1; $_duration/60") | awk '{printf "%1.1f", $0}'`
_duration="${_duration/%.0/}"
_est="${arr[3]}"
_est=`echo $(bc <<< "scale=1; $_est/60") | awk '{printf "%1.1f", $0}'`
_est="${_est/%.0/}"
_issue="${arr[6]} (${arr[7]})"
_desc="${arr[0]}"
_type="${arr[9]}"
echo -e "$_date\t$_duration\t${C_LIGHTGRAY}$_est${C_DEFAULT}\t$_issue ${C_LIGHTGRAY}$_desc${C_DEFAULT}"
done < "${REPORT}" | sort > ${TMP}
####################################################################################################
echo -e "${C_DARKGRAY}date \t""reported \t""est\t""issue\t${C_DEFAULT}"
# Exported fields:
# "date","duration","estimate","issue"
PREV_DATE=""
while read line; do
OLD_IFS=$IFS
IFS=$'\t' read -ra arr <<<"$line"
IFS=${OLD_IFS}
_date="${arr[0]}"
printf -v _duration %-10s "${arr[1]}"
_est="${arr[2]}"
printf -v _issue %-100s "${arr[3]}"
if [ -z "$PREV_DATE" ]; then PREV_DATE=${_date}; elif [ ${PREV_DATE} != ${_date} ]; then echo ""; fi
PREV_DATE="$_date"
echo -e "$_date\t$_duration\t$_est\t$_issue"
done < "$TMP"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment