Skip to content

Commit cdad03e

Browse files
committed
refactor: github webhook creation
Signed-off-by: sebastien.heurtematte <[email protected]>
1 parent fb29205 commit cdad03e

File tree

1 file changed

+64
-59
lines changed

1 file changed

+64
-59
lines changed

github/create_webhook.sh

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ source "${SCRIPT_FOLDER}/../pass/pass_wrapper.sh"
2121

2222
GITHUB_PASS_DOMAIN="github.com"
2323

24-
#shellcheck disable=SC2089
25-
EVENTS='["push","pull_request"]'
24+
CI_ADMIN_ROOT="${SCRIPT_FOLDER}/.."
25+
LOCAL_TOKEN="$("${CI_ADMIN_ROOT}/utils/local_config.sh" "get_var" "access_token" "github")"
2626

2727
help() {
2828
printf "Available commands:\n"
@@ -32,12 +32,58 @@ help() {
3232
exit 0
3333
}
3434

35-
org() {
35+
create_github_hook() {
36+
local token="$1"
37+
local type="$2"
38+
local org="$3"
39+
local webhook_url="$4"
40+
local events="$5"
41+
42+
curl -sS \
43+
-w "\n%{http_code}" \
44+
-X POST \
45+
-H "Accept: application/vnd.github+json" \
46+
-H "Authorization: Bearer ${token}" \
47+
-H "X-GitHub-Api-Version: 2022-11-28" \
48+
"https://api.github.com/${type}/${org}/hooks" \
49+
-d '{"name":"web","active":true,"events":'${events}',"config":{"url":"'${webhook_url}'","content_type":"json"}}'
50+
}
51+
52+
process_response() {
53+
local response="$1"
54+
local message="$2"
55+
local http_code
56+
local content
57+
58+
http_code=$(echo "${response}" | tail -n 1)
59+
content=$(echo "${response}" | head -n -1)
60+
61+
case "${http_code}" in
62+
201)
63+
echo "INFO: Webhook created successfully with ${message}."
64+
return 0
65+
;;
66+
422)
67+
echo "INFO: Webhook already exists for ${message}."
68+
return 0
69+
;;
70+
*)
71+
echo "ERROR: Failed to create webhook with ${message}."
72+
printf " Message: %s\n" "$(echo "${content}" | jq '.message')"
73+
return 1
74+
;;
75+
esac
76+
}
77+
78+
create_hook() {
3679
local project_name="${1:-}"
3780
local org="${2:-}"
81+
local hook_type="${3:-}"
82+
3883
local short_name="${project_name##*.}"
3984
local webhook_url="https://ci.eclipse.org/${short_name}/github-webhook/"
40-
85+
local events='["push","pull_request"]'
86+
4187
# check that project name is not empty
4288
if [[ -z "${project_name}" ]]; then
4389
printf "ERROR: a project name must be given.\n"
@@ -50,68 +96,27 @@ org() {
5096
exit 1
5197
fi
5298

53-
local pw_store_path="bots/${project_name}/${GITHUB_PASS_DOMAIN}"
54-
local bot_token=$(passw cbi "${pw_store_path}/api-token")
99+
bot_token=$(passw cbi "bots/${project_name}/${GITHUB_PASS_DOMAIN}/api-token")
55100

56101
echo "Creating organization webhook..."
57102

58-
local response
59-
response="$(curl -sS\
60-
-X POST \
61-
-H "Accept: application/vnd.github+json" \
62-
-H "Authorization: Bearer ${bot_token}"\
63-
-H "X-GitHub-Api-Version: 2022-11-28" \
64-
"https://api.github.com/orgs/${org}/hooks" \
65-
-d '{"name":"web","active":true,"events":'${EVENTS}',"config":{"url":"'${webhook_url}'","content_type":"json"}}')"
66-
67-
if [[ "$(echo "${response}" | jq .errors)" != "null" ]] || [[ "$(echo "${response}" | jq .message)" != "null" ]]; then
68-
echo "ERROR:"
69-
printf " Message: %s\n" "$(echo "${response}" | jq '.message')"
70-
printf " Errors/Message: %s\n" "$(echo "${response}" | jq '.errors[].message')"
71-
exit 1
103+
response=$(create_github_hook "${bot_token}" "${hook_type}" "${org}" "${webhook_url}" "${events}")
104+
if ! process_response "${response}" "${project_name} project and bot token"; then
105+
echo "INFO: Try with CBI token"
106+
response=$(create_github_hook "${LOCAL_TOKEN}" "${hook_type}" "${org}" "${webhook_url}" "${events}")
107+
if ! process_response "${response}" "${project_name} project and cbi config token"; then
108+
echo "ERROR: Failed to create webhook for ${project_name} project."
109+
exit 1
110+
fi
72111
fi
73112
}
74113

75-
repo() {
76-
local project_name="${1:-}"
77-
local repo="${2:-}" # org/repo
78-
local short_name="${project_name##*.}"
79-
local webhook_url="https://ci.eclipse.org/${short_name}/github-webhook/"
80-
81-
82-
# check that project name is not empty
83-
if [[ -z "${project_name}" ]]; then
84-
printf "ERROR: a project name must be given.\n"
85-
exit 1
86-
fi
87-
88-
# check that repo name is not empty
89-
if [[ -z "${repo}" ]]; then
90-
printf "ERROR: a GitHub repo name (org/repo) must be given.\n"
91-
exit 1
92-
fi
93-
94-
local pw_store_path="bots/${project_name}/${GITHUB_PASS_DOMAIN}"
95-
local bot_token=$(passw cbi "${pw_store_path}/api-token")
96-
97-
echo "Creating repo webhook..."
114+
org() {
115+
create_hook "$1" "$2" "orgs"
116+
}
98117

99-
local response
100-
response="$(curl -sS\
101-
-X POST \
102-
-H "Accept: application/vnd.github+json" \
103-
-H "Authorization: Bearer ${bot_token}"\
104-
-H "X-GitHub-Api-Version: 2022-11-28" \
105-
"https://api.github.com/repos/${repo}/hooks" \
106-
-d '{"name":"web","active":true,"events":'${EVENTS}',"config":{"url":"'${webhook_url}'","content_type":"json"}}')"
107-
108-
if [[ "$(echo "${response}" | jq .errors)" != "null" ]] || [[ "$(echo "${response}" | jq .message)" != "null" ]]; then
109-
echo "ERROR:"
110-
printf " Message: %s\n" "$(echo "${response}" | jq '.message')"
111-
printf " Errors/Message: %s\n" "$(echo "${response}" | jq '.errors[].message')"
112-
else
113-
echo "Webhook created."
114-
fi
118+
repo() {
119+
create_hook "$1" "$2" "repos"
115120
}
116121

117122
"$@"

0 commit comments

Comments
 (0)