hubot/add-github-webhooks.sh: Add an script for creating GitHub webhooks
authorW. Trevor King <wking@tremily.us>
Wed, 21 May 2014 20:10:13 +0000 (13:10 -0700)
committerW. Trevor King <wking@tremily.us>
Wed, 21 May 2014 20:10:13 +0000 (13:10 -0700)
For notifying the github-commits and github-pull-request-notifier
scripts.  Adding these hooks to a number of repositories by hand is
tedious, and it's so easy to automate ;).

hubot/add-github-webhooks.sh [new file with mode: 0755]

diff --git a/hubot/add-github-webhooks.sh b/hubot/add-github-webhooks.sh
new file mode 100755 (executable)
index 0000000..27bb24b
--- /dev/null
@@ -0,0 +1,61 @@
+#!/bin/sh
+#
+# usage: add-github-webhooks.sh OWNER REPO URL CHANNEL
+# for example:
+#   add-github-webhooks.sh wking dockerfile http://irc.example.net:80 '#dockerfile'
+
+# Create a token with https://github.com/settings/tokens/new
+#
+# The token needs write:repo_hook [1].
+#
+# [1]: https://developer.github.com/v3/oauth/
+TOKEN="FIXME"
+
+if test -z "${TOKEN}" || test 'xFIXME' = "x${TOKEN}"
+then
+       echo "edit $0 and set TOKEN to a GitHub authentication token" >&2
+       exit 1
+fi
+
+OWNER="$1"
+REPO="$2"
+URL="$3"
+CHANNEL="$4"
+
+ENDPOINT="https://api.github.com"
+
+
+# https://developer.github.com/v3/repos/hooks/#create-a-hook
+curl --request POST \
+       --header "Authorization: token ${TOKEN}" \
+       --header "Content-Type: application/json" \
+       --data @- \
+       "${ENDPOINT}/repos/${OWNER}/${REPO}/hooks" <<EOF
+{
+       "name": "web",
+       "config": {
+               "url": "${URL}/hubot/gh-commits?room=${CHANNEL}",
+               "content_type": "json",
+               "insecure_ssl": false
+               },
+       "events": ["push"],
+       "active": true
+}
+EOF
+
+curl --request POST \
+       --header "Authorization: token ${TOKEN}" \
+       --header "Content-Type: application/json" \
+       --data @- \
+       "${ENDPOINT}/repos/${OWNER}/${REPO}/hooks" <<EOF
+{
+       "name": "web",
+       "config": {
+               "url": "${URL}/hubot/gh-pull-requests?room=${CHANNEL}",
+               "content_type": "json",
+               "insecure_ssl": false
+               },
+       "events": ["pull_request"],
+       "active": true
+}
+EOF