Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
閉じていないPRをslackに通知するシェル
#!/bin/sh
# config
slack_url="https://hooks.slack.com/services/xxxxxx/xxxxx/xxxxxxxxxxxxxx"
slack_channel="#xxxxxxxxxxxxxx"
github_url="https://github.com/api/v3/repos/xxxxxxxxx/xxxxxxxx/pulls"
icon_emoji=":jenkins:"
# 取得
prs=$(
curl "${github_url}" \
| jq '. | map({
"author_name": .user.login,
"author_link": .user.html_url,
"title_link": .html_url,
"title": .title,
"reviewers": .requested_reviewers,
"created_at": .created_at,
"updated_at": .updated_at})' \
)
pr_len=$(($(echo ${prs} | jq length) -1))
# なければ終了
if [ ${pr_len} -lt 0 ] ; then
echo "閉じられていないPRはありませんでした"
exit 0;
fi
# 整形
for i in `seq 0 ${pr_len}`
do
if echo ${prs} | jq .[${i}].title | grep 'WIP' > /dev/null
then
# color
color="#808080"
# text
text="作成:$(echo $prs | jq -r .[${i}].created_at | sed -e 's/T/ /g' | sed -e 's/Z//g')"
text="${text}、更新:$(echo $prs | jq -r .[${i}].updated_at | sed -e 's/T/ /g' | sed -e 's/Z//g')"
else
# color
color="#32CD32"
# text
text="作成:$(echo $prs | jq -r .[${i}].created_at | sed -e 's/T/ /g' | sed -e 's/Z//g')"
text="${text}、更新:$(echo $prs | jq -r .[${i}].updated_at | sed -e 's/T/ /g' | sed -e 's/Z//g')"
rev_len=$(($(echo $prs | jq .[${i}].reviewers | jq length) -1))
if [ $rev_len -gt 0 ] ; then
field_value=""
for j in `seq 0 $rev_len`
do
field_value="${field_value} <@$(echo $prs | jq -r .[${i}].reviewers[${j}].login)> "
done
prs=$(echo ${prs} | jq ".[${i}] |= .+ {\"fields\":[{\"title\": \"レビューお願い致します!\", \"value\": \"${field_value}\"}]}")
fi
fi
prs=$(echo ${prs} | jq ".[${i}] |= .+ {\"color\":\"${color}\"}")
prs=$(echo ${prs} | jq ".[${i}] |= .+ {\"text\":\"${text}\"}")
done
# 余計なものを削除
prs="$(echo ${prs} | jq 'del(.[].reviewers)')"
prs="$(echo ${prs} | jq 'del(.[].created_at)')"
prs="$(echo ${prs} | jq 'del(.[].updated_at)')"
# payload作成
payload="payload={
\"channel\": \"$slack_channel\",
\"username\": \"閉じられていないプルリク一覧\",
\"icon_emoji\": \"$icon_emoji\",
\"attachments\": $prs
}"
# 送信
curl -sSX POST -m 5 --data-urlencode "${payload}" "${slack_url}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.