-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-commit-synchook
executable file
·143 lines (133 loc) · 5.58 KB
/
post-commit-synchook
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/sh
BRAKECODE_ENV_DIR1=~/.brakecode
BRAKECODE_ENV_DIR2=${PWD}/.brakecode
ENV_FILE_PATH=${BRAKECODE_ENV_DIR1}/.env
DEFAULT_SYNCHOOK_HOST=api.brakecode.com
CURL_OPTIONS="-k"
WGET_OPTIONS="--no-check-certificate"
if [ ! -d $BRAKECODE_ENV_DIR1 ]; then
mkdir $BRAKECODE_ENV_DIR1
echo "BRAKECODE_API_KEY=" > $ENV_FILE_PATH
fi
if [ -d $BRAKECODE_ENV_DIR2 ]; then
printf "Using environment directory path ${BRAKECODE_ENV_DIR2}\n"
ENV_FILE_PATH=${BRAKECODE_ENV_DIR2}/.env
fi
BRAKECODE_API_KEY=$(cat $ENV_FILE_PATH | grep -G "^BRAKECODE_API_KEY" | grep -v "#" | cut -f2 -d"=")
SYNCHOOK_HOST=$(cat $ENV_FILE_PATH | grep -G "^SYNCHOOK_HOST" | grep -v "#" | cut -f2 -d"=")
FILTERS=$(cat $ENV_FILE_PATH | grep -G "^FILTER=" | grep -v "#" | cut -f2 -d"=")
NDAS=$(cat $ENV_FILE_PATH | grep -G "^NDA=" | grep -v "#" | cut -f2 -d"=")
if [ -z "$SYNCHOOK_HOST" ]; then
SYNCHOOK_HOST=$DEFAULT_SYNCHOOK_HOST
CURL_OPTIONS=
WGET_OPTIONS=
fi
if [ -z "$BRAKECODE_API_KEY" -a $(env | grep -c -e "^GIT_") -gt 1 ]; then
printf "Your BrakeCODE API Key is missing.\n"
printf "Please run this $0 from a normal shell to enter your BrakeCODE API Key, or update $ENV_FILE_PATH.\n"
printf "Synchook exiting...\n\n"
exit 1
elif [ -z "$BRAKECODE_API_KEY" ]; then
printf "Please enter your BrakeCODE API Key: "
read -r BRAKECODE_API_KEY
if [ -z "$BRAKECODE_API_KEY" ]; then
printf "Go to https://brakecode.com/dashboard?panel=synchook to generate a FREE API Key.\n"
printf "Synchook exiting...\n\n"
exit 1
else
printf "Validating key...\n"
if [ $(which curl) ]; then
response=$(curl -s ${CURL_OPTIONS} -L -w ' %{response_code}' https://$SYNCHOOK_HOST/api/v1/synchook \
-H 'Content-Type: application/json' \
-H "x-environment: \"canary\"" \
-H "x-api-key: { \"apikey\": \"${BRAKECODE_API_KEY}\" }")
else
response=$(wget -q -O - $WGET_OPTIONS --max-redirect=20 --server-response URL-response https://$SYNCHOOK_HOST/api/v1/synchook \
--header='Content-Type: application/json' \
--header="x-environment: \"canary\"" \
--header="x-api-key: { \"apikey\": \"${BRAKECODE_API_KEY}\" }" 2>&1 | egrep "\s200|^\"")
fi
if [ -n "$(echo $response | egrep "\s200")" ]; then
# append the line if "BRAKECODE_API_KEY=" is missing
perl -pi -e "s/BRAKECODE_API_KEY=/BRAKECODE_API_KEY=${BRAKECODE_API_KEY}/g || $_ .= BRAKECODE_API_KEY=${BRAKECODE_API_KEY}>\n" $ENV_FILE_PATH
echo "Saved BRAKECODE_API_KEY to $ENV_FILE_PATH"
printf "Synchok is now enabled!\n\n"
exit 1
else
printf "server response: $response\n\n"
printf "Go to https://brakecode.com/dashboard?panel=synchook to verify and/or generate a FREE API Key.\n"
printf "Synchook exiting...\n\n"
exit 1
fi
fi
exit 0
fi
git_branch=$(git branch --show-current 2>&1 > /dev/null)
if [ $(echo $git_branch | grep -c "error: unknown option") -ne 0 ]; then
git_branch=$(git branch --list | grep "*" | cut -f2 -d" ")
fi
git_repo=$(basename "`git rev-parse --show-toplevel`")
for filter in $FILTERS; do
match=$(echo $filter | cut -f1 -d":")
replacement=$(echo $filter | cut -f2 -d":")
git_repo=$(echo $git_repo | sed s/$match/$replacement/g)
done
base64args="-w0"
if [ $(echo $OSTYPE | egrep "darwin") ]; then
base64args=""
fi
hostname=$(hostname)
git_url=$(git config remote.origin.url)
git_log=$(git log -1 HEAD | base64 $base64args)
for nda in $NDAS; do
metadata=$(echo $nda | cut -f1 -d":")
fakedata=$(echo $nda | cut -f2 -d":")
nda_safe_metadata="*** redacted ***"
if [ -n $metadata -a -n $fakedata -a $fakedata != $metadata ]; then
nda_safe_metadata=$fakedata
fi
if [ "$metadata" = "host" ]; then
hostname=$nda_safe_metadata
elif [ "$metadata" = "repo" ]; then
git_repo=$nda_safe_metadata
elif [ "$metadata" = "branch" ]; then
git_branch=$nda_safe_metadata
elif [ "$metadata" = "url" ]; then
git_url=$nda_safe_metadata
elif [ "$metadata" = "log" ]; then
git_log=$(echo "$nda_safe_metadata" | base64 $base64args)
fi
done
appConfig=$(echo "{
\"git_user_email\": \"$(cat $ENV_FILE_PATH | egrep "^GIT_USER_EMAIL=" | cut -f2 -d"=" | tr "\n" "," | sed 's/,*$//g')\",
\"exclude\": \"$(cat $ENV_FILE_PATH | egrep "^EXCLUDE=" | cut -f2 -d"=" | tr "\n" "," | sed 's/,*$//g')\"
}" | jq -cr 'del(..|select(. == ""))' | base64 $base64args)
data=$(echo "{
\"hostname\": \"$hostname\",
\"git_repo\": \"$git_repo\",
\"git_branch\": \"$git_branch\",
\"git_url\": \"$git_url\",
\"git_log\": \"$git_log\"
}" | base64 $base64args)
if [ $(which curl) ]; then
response=$(curl -s -k -L -w ' %{response_code}' https://$SYNCHOOK_HOST/api/v1/synchook \
-H 'Content-Type: text/plain' \
-H "x-api-key: { \"apikey\": \"${BRAKECODE_API_KEY}\" }" \
-H "x-environment: \"canary\"" \
-H "x-app-config: \"${appConfig}\"" \
-d ${data})
else
response=$(wget -q -O - $WGET_OPTIONS --max-redirect=20 --server-response URL-response https://$SYNCHOOK_HOST/api/v1/synchook \
--header='Content-Type: text/plain' \
--header="x-environment: \"canary\"" \
--header="x-api-key: { \"apikey\": \"${BRAKECODE_API_KEY}\" }" \
--header="x-app-config: \"${appConfig}\"" \
--post-data="${data}" 2>&1 | egrep "\s200|^\"")
fi
if [ -z "$(echo $response | egrep "\s200")" ]; then
printf "\n$response\n"
printf "Sync failed. Synchook exiting...\n\n"
exit 1
fi
printf "\nSynchook successfully synced commit!\n\n"
exit 0