checks whether the defined JIRA user resolved some issues after previous check and sends a notification to Slack if he did not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# === jira_issues_check === | |
# checks whether the defined JIRA user resolved some issues after previous check and sends a notification to Slack if he did not | |
# depends on jq and curl | |
# written by deseven, 2017 | |
# http://deseven.info | |
# notifications language (ru/en) | |
notificationLang=ru | |
# temporary dir to save issues data to | |
dataDir=/tmp/scripts_data | |
# jira base URL | |
jiraURL="http://jira.somehost.com" | |
# jira authentication (base64 of user:password) | |
jiraAuth="dXNlcjpwYXNzd29yZA" | |
# slack hook URL | |
slackHook="https://hooks.slack.com/services/XXX/YYY/ZZZ" | |
if [ -z "$1" ]; then echo "usage: $0 jira-username [slack-username]"; exit 1; fi | |
userName=$1 | |
if [ -z "$2" ]; then | |
userNameSlack=$1 | |
else | |
userNameSlack=$2 | |
fi | |
function toLog() { | |
#echo "$@" | |
echo -e `date +'[%d.%m.%Y %H:%M:%S]'` "$@" >> "$dataDir/jira_issues_check_${userName}.log" | |
} | |
if [ "$notificationLang" == "ru" ]; then | |
alertName="Автоматический Наказыватель" | |
alertText="Ай-ай-ай, <@${userNameSlack}> не решил ни одной задачи с начала прошлого рабочего дня!" | |
motivationalQuoteDescr="Вот тебе вдохновляющая цитата:" | |
motivationalQuoteAuthorUnknown="Автор неизвестен" | |
else | |
alertName="Automatic Punisher" | |
alertText="Whoa, <@${userNameSlack}> didn't resolve any issues since the beginning of the previous work day!" | |
motivationalQuoteDescr="Here is the motivational quote for you:" | |
motivationalQuoteAuthorUnknown="Unknown author" | |
fi | |
checkFrom=$(cat $dataDir/jira_issues_check_$userName 2>/dev/null) | |
if [ -z "$checkFrom" ]; then | |
toLog "warning - no previous time recorded, writing current time and exiting" | |
checkFrom=$(date +'%Y-%m-%d%%20%H:%M') | |
echo -n "$checkFrom" > $dataDir/jira_issues_check_$userName | |
exit | |
fi | |
toLog "checking completed issues from ${checkFrom}" | |
toLog "$jiraURL/rest/api/2/search?jql=assignee=${userName}%20and%20resolution=done%20and%20updated%3E=%22${checkFrom}%22&fields=key&maxResults=1000" | |
data=$(curl -s -H "Authorization: Basic ${jiraAuth}" "$jiraURL/rest/api/2/search?jql=assignee=${userName}%20and%20resolution=done%20and%20updated%3E=%22${checkFrom}%22&fields=key&maxResults=1000") | |
resolvedIssues=$(echo "$data" | jq -M -c -r '.issues[].key' 2>/dev/null | wc -l | xargs) | |
if [ -z "$resolvedIssues" ] || [ $resolvedIssues == "0" ]; then | |
toLog "no issues are resolved, sending notification" | |
motivationalQuote=$(curl -s "http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=${notificationLang}") | |
motivationalQuoteText=$(echo "$motivationalQuote" | jq -M -c -r '.quoteText' 2>/dev/null) | |
motivationalQuoteAuthor=$(echo "$motivationalQuote" | jq -M -c -r '.quoteAuthor' 2>/dev/null) | |
additional="" | |
if [ ! -z "$motivationalQuoteText" ]; then | |
if [ -z "$motivationalQuoteAuthor" ]; then motivationalQuoteAuthor="${motivationalQuoteAuthorUnknown}"; fi | |
additional="\n\n${motivationalQuoteDescr}\n>${motivationalQuoteText}\n_${motivationalQuoteAuthor}_" | |
additional=${additional//\\\"/\"} | |
additional=${additional//\"/\\\"} | |
fi | |
curl -s -X POST --data-urlencode "payload={\"username\": \"${alertName}\", \"text\": \"${alertText}${additional}\", \"icon_emoji\": \":exclamation:\"}" $slackHook > /dev/null | |
else | |
toLog "$resolvedIssues issues are resolved" | |
fi | |
checkFrom=$(date +'%Y-%m-%d%%20%H:%M') | |
echo -n "$checkFrom" > $dataDir/jira_issues_check_$userName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment