Skip to content

Commit 0932900

Browse files
authored
Merge pull request #13 from distributhor/url-encode-params
Url encode params
2 parents 6c4ff14 + a785bc7 commit 0932900

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

entrypoint.sh

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
#!/bin/bash
22

3+
urlencode() {
4+
local length="${#1}"
5+
for (( i = 0; i < length; i++ )); do
6+
local c="${1:i:1}"
7+
case $c in
8+
[a-zA-Z0-9.~_-]) printf "$c" ;;
9+
*) printf '%s' "$c" | xxd -p -c1 |
10+
while read c; do printf '%%%s' "$c"; done ;;
11+
esac
12+
done
13+
}
14+
15+
urldecode() {
16+
local url_encoded="${1//+/ }"
17+
printf '%b' "${url_encoded//%/\\x}"
18+
}
19+
320
set -e
421

522
if [ -z "$webhook_url" ]; then
@@ -12,32 +29,44 @@ if [ -z "$webhook_secret" ]; then
1229
exit 1
1330
fi
1431

15-
WEBHOOK_ENDPOINT=$webhook_url
16-
17-
if [ -n "$webhook_auth" ]; then
18-
WEBHOOK_ENDPOINT="-u $webhook_auth $webhook_url"
19-
fi
20-
2132
if [ -n "$webhook_type" ] && [ "$webhook_type" == "form-urlencoded" ]; then
33+
34+
event=`urlencode "$GITHUB_EVENT_NAME"`
35+
repository=`urlencode "$GITHUB_REPOSITORY"`
36+
commit=`urlencode "$GITHUB_SHA"`
37+
ref=`urlencode "$GITHUB_REF"`
38+
head=`urlencode "$GITHUB_HEAD_REF"`
39+
workflow=`urlencode "$GITHUB_WORKFLOW"`
40+
2241
CONTENT_TYPE="application/x-www-form-urlencoded"
23-
FORM_DATA="event=$GITHUB_EVENT_NAME&repository=$GITHUB_REPOSITORY&commit=$GITHUB_SHA&ref=$GITHUB_REF&head=$GITHUB_HEAD_REF&workflow=$GITHUB_WORKFLOW"
42+
FORM_DATA="event=$event&repository=$repository&commit=$commit&ref=$ref&head=$head&workflow=$workflow"
43+
2444
if [ -n "$data" ]; then
2545
WEBHOOK_DATA="$FORM_DATA&$data"
2646
else
2747
WEBHOOK_DATA="$FORM_DATA"
2848
fi
49+
2950
else
51+
3052
CONTENT_TYPE="application/json"
3153
JSON_DATA="\"event\":\"$GITHUB_EVENT_NAME\",\"repository\":\"$GITHUB_REPOSITORY\",\"commit\":\"$GITHUB_SHA\",\"ref\":\"$GITHUB_REF\",\"head\":\"$GITHUB_HEAD_REF\",\"workflow\":\"$GITHUB_WORKFLOW\""
54+
3255
if [ -n "$data" ]; then
3356
COMPACT_JSON=$(echo -n "$data" | jq -c '')
3457
WEBHOOK_DATA="{$JSON_DATA,\"data\":$COMPACT_JSON}"
3558
else
3659
WEBHOOK_DATA="{$JSON_DATA}"
3760
fi
61+
3862
fi
3963

4064
WEBHOOK_SIGNATURE=$(echo -n "$WEBHOOK_DATA" | openssl sha1 -hmac "$webhook_secret" -binary | xxd -p)
65+
WEBHOOK_ENDPOINT=$webhook_url
66+
67+
if [ -n "$webhook_auth" ]; then
68+
WEBHOOK_ENDPOINT="-u $webhook_auth $webhook_url"
69+
fi
4170

4271
echo "Content Type: $CONTENT_TYPE"
4372

@@ -48,7 +77,6 @@ curl -k -v \
4877
-H "X-GitHub-Delivery: $GITHUB_RUN_NUMBER" \
4978
-H "X-GitHub-Event: $GITHUB_EVENT_NAME" \
5079
--data "$WEBHOOK_DATA" $WEBHOOK_ENDPOINT
51-
# -D - $WEBHOOK_ENDPOINT --data-urlencode @"$GITHUB_EVENT_PATH"
5280

5381
# wget -q --server-response --timeout=2000 -O - \
5482
# --header="Content-Type: application/json" \

0 commit comments

Comments
 (0)