-
Notifications
You must be signed in to change notification settings - Fork 4
/
post-commit
executable file
·67 lines (56 loc) · 2.34 KB
/
post-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
########################################################################################################################
# === git-hook script for pushing commit events to the mojotech/helios2 dashboard ===
#
# This file must be named "post-commit," be executable, and stored in '.git/hooks/'
#
# Note on keys:
# - The API uses public key encryption to authenticate so please make sure the server has a copy of your public key.
# - Keys must be PEM-format RSA key with SHA256 encryption.
# - RSA keys generated by openssl are verified as working.
# To generate a key-pair, run:
# openssl genrsa -out helios.pem && openssl rsa -in helios.pem -pubout -out helios_pub.pem
# Then make sure to point `path_to_your_private_key to the generated private key!
#
########################################################################################################################
###################################
# Point this to your private key!
path_to_your_private_key="${HOME}/.ssh/your-key"
###################################
###################################
# Point this to a running helios instance
# Do NOT include the trailing backslash
helios_url="http://localhost:4000"
###################################
if [ ! -f "${path_to_your_private_key}" ];
then
echo "[helios-commit]: Could not locate key for use in Signing!"
echo "[helios-commit]: Please create a private key at ${path_to_your_private_key}, or edit the" \
"path at the top of ${SCRIPT=$(realpath "$0")} to point to an existing key"
exit 1
else
rsa_key_path=${path_to_your_private_key}
fi
# get git user name in lowercase
username=$(git config user.name | awk '{print tolower($0)}')
# get latest commit sha
git_sha=$(git rev-parse HEAD)
# sign the JSON file (json_body | openssl sign with rsa_key_path | convert to b64)
signature=$(echo -n "{
\"event_type\": \"github_commit\",
\"author\": \"${username}\",
\"event_id\": \"${git_sha}\"
}" | openssl dgst -sha256 -sign ${rsa_key_path} | base64)
echo -ne "Sending commit event to helios... "
curl --header --request POST \
--url "${helios_url}/api/v1/events" \
--header "Content-Type: application/json" \
--header "http-auth: ${signature}" \
--data "{
\"event_type\": \"github_commit\",
\"author\": \"${username}\",
\"event_id\": \"${git_sha}\"
}" \
--silent \
--output /dev/null \
--max-time 5