| 1 | #!/bin/sh |
| 2 | set -u |
| 3 | |
| 4 | # Shale runs git's CGI with a cleared environment; PID 1 still holds the service's. |
| 5 | service_env() { tr '\0' '\n' <"${SHALE_SERVICE_ENVIRON:-/proc/1/environ}" | sed -n "s/^$1=//p"; } |
| 6 | : "${SHALE_WEBHOOK_ENABLED:=$(service_env SHALE_WEBHOOK_ENABLED)}" |
| 7 | : "${SHALE_WEBHOOK_URL:=$(service_env SHALE_WEBHOOK_URL)}" |
| 8 | : "${DOMAIN:=$(service_env DOMAIN)}" |
| 9 | |
| 10 | [ "${SHALE_WEBHOOK_ENABLED:-false}" = true ] || exit 0 |
| 11 | [ -n "${SHALE_WEBHOOK_URL:-}" ] || exit 0 |
| 12 | case "$SHALE_WEBHOOK_URL" in |
| 13 | */github|*/github/) webhook_url=${SHALE_WEBHOOK_URL%/}/ ;; |
| 14 | *) webhook_url=${SHALE_WEBHOOK_URL%/}/github/ ;; |
| 15 | esac |
| 16 | |
| 17 | repository=$(git config --get shale.snowglobe.repository) || exit 0 |
| 18 | origin="https://${DOMAIN}/${repository}" |
| 19 | avatar="https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png" |
| 20 | |
| 21 | json_string() { |
| 22 | awk 'BEGIN { ORS = "" } |
| 23 | { |
| 24 | if (NR > 1) printf "\\n" |
| 25 | for (i = 1; i <= length($0); i++) { |
| 26 | c = substr($0, i, 1) |
| 27 | if (c == "\\") printf "\\\\" |
| 28 | else if (c == "\"") printf "\\\"" |
| 29 | else if (c == "\t") printf "\\t" |
| 30 | else if (c == "\r") printf "\\r" |
| 31 | else if (c == sprintf("%c", 8)) printf "\\b" |
| 32 | else if (c == sprintf("%c", 12)) printf "\\f" |
| 33 | else printf "%s", c |
| 34 | } |
| 35 | }' |
| 36 | } |
| 37 | |
| 38 | quote() { |
| 39 | printf '"' |
| 40 | printf '%s' "$1" | json_string |
| 41 | printf '"' |
| 42 | } |
| 43 | |
| 44 | is_zero() { |
| 45 | case "$1" in |
| 46 | *[!0]*) return 1 ;; |
| 47 | *) return 0 ;; |
| 48 | esac |
| 49 | } |
| 50 | |
| 51 | write_ref_payload() { |
| 52 | { |
| 53 | printf '{"ref":'; quote "$short_ref" |
| 54 | printf ',"ref_type":'; quote "$ref_type" |
| 55 | printf ',"repository":{"name":'; quote "$repository" |
| 56 | printf ',"full_name":'; quote "$repository" |
| 57 | printf ',"html_url":'; quote "$origin/" |
| 58 | printf '},"sender":{"login":'; quote "$actor" |
| 59 | printf ',"avatar_url":'; quote "$avatar" |
| 60 | printf ',"html_url":'; quote "$origin/" |
| 61 | printf '}}\n' |
| 62 | } >"$payload" |
| 63 | } |
| 64 | |
| 65 | send() { |
| 66 | event=$1 |
| 67 | payload=$2 |
| 68 | delivery=$(printf '%s\n' "$repository:$event:$(date +%s):$$:$index" | git hash-object --stdin) |
| 69 | curl --fail --silent --show-error --max-time 10 \ |
| 70 | -H "Content-Type: application/json" \ |
| 71 | -H "User-Agent: Shale-Hookshot/1.0" \ |
| 72 | -H "X-GitHub-Event: $event" \ |
| 73 | -H "X-GitHub-Delivery: $delivery" \ |
| 74 | --data-binary "@$payload" "$webhook_url" >/dev/null |
| 75 | } |
| 76 | |
| 77 | temporary=$(mktemp -d) |
| 78 | trap 'rm -rf "$temporary"' EXIT HUP INT TERM |
| 79 | index=0 |
| 80 | while read -r before after ref; do |
| 81 | index=$((index + 1)) |
| 82 | payload="$temporary/$index.json" |
| 83 | short_ref=${ref#refs/heads/} |
| 84 | if [ "$short_ref" = "$ref" ]; then |
| 85 | short_ref=${ref#refs/tags/} |
| 86 | ref_type=tag |
| 87 | else |
| 88 | ref_type=branch |
| 89 | fi |
| 90 | |
| 91 | if is_zero "$after"; then |
| 92 | actor=${REMOTE_USER:-$(git show -s --format=%an "$before")} |
| 93 | write_ref_payload |
| 94 | send delete "$payload" || printf 'Shale webhook delivery failed for %s\n' "$repository" >&2 |
| 95 | continue |
| 96 | fi |
| 97 | |
| 98 | actor=${REMOTE_USER:-$(git show -s --format=%an "$after")} |
| 99 | if [ "$ref_type" = tag ]; then |
| 100 | if is_zero "$before"; then |
| 101 | write_ref_payload |
| 102 | send create "$payload" || printf 'Shale webhook delivery failed for %s\n' "$repository" >&2 |
| 103 | fi |
| 104 | continue |
| 105 | fi |
| 106 | if is_zero "$before"; then |
| 107 | created=true |
| 108 | forced=false |
| 109 | commits=$(git rev-list -n 20 --reverse "$after" --not --exclude="$short_ref" --branches --tags) |
| 110 | else |
| 111 | created=false |
| 112 | if git merge-base --is-ancestor "$before" "$after"; then forced=false; else forced=true; fi |
| 113 | commits=$(git rev-list -n 20 --reverse "$before..$after") |
| 114 | fi |
| 115 | compare="$origin/commit/$after" |
| 116 | { |
| 117 | printf '{"ref":'; quote "$ref" |
| 118 | printf ',"before":'; quote "$before" |
| 119 | printf ',"after":'; quote "$after" |
| 120 | printf ',"created":%s,"deleted":false,"forced":%s' "$created" "$forced" |
| 121 | printf ',"compare":'; quote "$compare" |
| 122 | printf ',"repository":{"name":'; quote "$repository" |
| 123 | printf ',"full_name":'; quote "$repository" |
| 124 | printf ',"html_url":'; quote "$origin/" |
| 125 | printf '},"sender":{"login":'; quote "$actor" |
| 126 | printf ',"avatar_url":'; quote "$avatar" |
| 127 | printf ',"html_url":'; quote "$origin/" |
| 128 | printf '},"commits":[' |
| 129 | separator= |
| 130 | for commit in $commits; do |
| 131 | message=$(git show -s --format=%B "$commit") |
| 132 | author=$(git show -s --format=%an "$commit") |
| 133 | timestamp=$(git show -s --format=%aI "$commit") |
| 134 | printf '%s{"id":' "$separator"; quote "$commit" |
| 135 | printf ',"message":'; quote "$message" |
| 136 | printf ',"timestamp":'; quote "$timestamp" |
| 137 | printf ',"url":'; quote "$origin/commit/$commit" |
| 138 | printf ',"author":{"name":'; quote "$author" |
| 139 | printf ',"username":'; quote "$author" |
| 140 | printf '}}' |
| 141 | separator=, |
| 142 | done |
| 143 | printf '],"head_commit":{"id":'; quote "$after" |
| 144 | printf ',"message":'; quote "$(git show -s --format=%B "$after")" |
| 145 | printf ',"timestamp":'; quote "$(git show -s --format=%aI "$after")" |
| 146 | printf ',"url":'; quote "$origin/commit/$after" |
| 147 | printf '}}\n' |
| 148 | } >"$payload" |
| 149 | send push "$payload" || printf 'Shale webhook delivery failed for %s\n' "$repository" >&2 |
| 150 | done |