Skip to content

Instantly share code, notes, and snippets.

@namikingsoft
Last active May 10, 2019 04:51
Show Gist options
  • Save namikingsoft/a65c19e135057495d92cc1c5591873d4 to your computer and use it in GitHub Desktop.
Save namikingsoft/a65c19e135057495d92cc1c5591873d4 to your computer and use it in GitHub Desktop.
Notify Exceeded WIP Limit on GitHub Project via Slack (Experimental)
#!/bin/sh -eu
#
# required env:
# GITHUB_ACCESS_TOKEN
# GITHUB_PROJECT_ID
# GITHUB_PROJECT_SETTING_CARD_ID
# SLACK_INCOMING_WEBHOOK_URL
#
# required packages:
# base64
# curl
# jq
#
# usage:
# GITHUB_PROJECT_ID="1234" \
# GITHUB_PROJECT_SETTING_CARD_ID="2345" \
# SLACK_INCOMING_URL="https://hooks.slack.com/services/T***" \
# sh <(curl -L https://gist.githubusercontent.com/namikingsoft/a65c19e135057495d92cc1c5591873d4/raw/notify-wiplimit-of-github-project.sh)
#
# create setting card:
# then edit note text the following json on GitHub Project, i.e.
#
# ```json
# {
# "wipLimitedColumns": [
# {
# "name": "In Progress",
# "limit": {
# "count": 2
# }
# },
# {
# "name": "Merged",
# "limit": {
# "count": 50,
# "size": 100
# }
# }
# ]
# }
# ```
wip_limited_columns_encoded="$(
curl --silent \
-H "Accept: application/vnd.github.inertia-preview+json" \
-H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \
-X GET "https://api.github.com/projects/columns/cards/${GITHUB_PROJECT_SETTING_CARD_ID}" \
| jq -r .note \
| grep -v "^\`\`\`" \
| jq -r '.wipLimitedColumns[] | @base64'
)"
project_json="$(
curl --silent \
-H "Accept: application/vnd.github.inertia-preview+json" \
-H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \
-X GET "https://api.github.com/projects/${GITHUB_PROJECT_ID}" \
| jq .
)"
columns_json="$(
curl --silent \
-H "Accept: application/vnd.github.inertia-preview+json" \
-H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \
-X GET "https://api.github.com/projects/${GITHUB_PROJECT_ID}/columns" \
| jq .
)"
project_name="$(echo "$project_json" | jq -r .name)"
project_link="$(echo "$project_json" | jq -r .html_url)"
exceeded_text="";
for row in $wip_limited_columns_encoded; do
column_name="$(echo "$row" | base64 --decode | jq -r .name)"
limit_count="$(echo "$row" | base64 --decode | jq -r .limit.count)"
column_id="$(echo "$columns_json" | jq -r ".[] | select(.name == \"$column_name\") | .id")"
column_count="$(
curl --silent \
-H "Accept: application/vnd.github.inertia-preview+json" \
-H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \
-X GET "https://api.github.com/projects/columns/$column_id/cards" \
| jq -r 'length'
)"
# TODO: calc card size from label?
if [ "$column_count" -gt "$limit_count" ]; then
exceeded_text="${exceeded_text}\`${column_name}\` count (${column_count}) > ${limit_count}\n"
fi
done
if [ "$exceeded_text" = "" ]; then
echo no exceede wip limit
exit 0
fi
# TODO: use blocks instead of attachments
post_json="$(echo {} | jq "$(cat << EOS
{
text: "Exceeded WIP Limit on <${project_link}|${project_name}>",
attachments: [{
color: "#d00000",
fields: [{ value: "${exceeded_text}" }],
}],
}
EOS
)")"
curl -v \
-H "Content-Type: application/json" \
-X POST -d "${post_json}" "$SLACK_INCOMING_WEBHOOK_URL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment