Skip to content

Commit

Permalink
Lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
tannguyen04 committed Apr 19, 2024
1 parent f0b188e commit 2876c17
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
42 changes: 23 additions & 19 deletions scripts/drevops/notify-webhook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ for cmd in php curl jq; do command -v ${cmd} >/dev/null || {
}; done

# Find custom parameters and secrets in string and replace it by value.
# shellcheck disable=SC2001
replace_parameters_and_secrets_in_string() {
string="$1"
while read -r name value; do
Expand All @@ -68,37 +69,40 @@ note "Webhook url : ${DREVOPS_NOTIFY_WEBHOOK_URL}"
note "Webhook method : ${DREVOPS_NOTIFY_WEBHOOK_METHOD}"
note "Webhook custom header :"
echo "${DREVOPS_NOTIFY_WEBHOOK_CUSTOM_HEADERS}" | jq -c '.[]' | while read -r item; do
name=$(echo "${item}" | jq -r '.name')
value=$(echo "${item}" | jq -r '.value')
note " ${name}: ${value}"
name=$(echo "${item}" | jq -r '.name')
value=$(echo "${item}" | jq -r '.value')
note " ${name}: ${value}"
done
note "Webhook custom parameters and secrets :"
echo "${DREVOPS_NOTIFY_WEBHOOK_CUSTOM_PARAMETERS_AND_SECRETS}" | jq -c '.[]' | while read -r item; do
name=$(echo "${item}" | jq -r '.name')
value=$(echo "${item}" | jq -r '.value')
note " ${name}: ${value}"
name=$(echo "${item}" | jq -r '.name')
value=$(echo "${item}" | jq -r '.value')
note " ${name}: ${value}"
done

# Build header.
headers_replaced=$(replace_parameters_and_secrets_in_string "${DREVOPS_NOTIFY_WEBHOOK_CUSTOM_HEADERS}")
declare -a headers
while IFS=: read -r name value; do
# Add header to the curl_headers array
headers+=(-H "$name: $value")
done < <(echo "$(replace_parameters_and_secrets_in_string "${DREVOPS_NOTIFY_WEBHOOK_CUSTOM_HEADERS}")" | jq -r '.[] | "\(.name): \(.value)"')
while read -r item; do
name=$(echo "${item}" | jq -r '.name')
value=$(echo "${item}" | jq -r '.value')
headers+=("-H" "${name}: ${value}")
done < <(echo "${headers_replaced}" | jq -c '.[]')

# Build message body.
message_body="$(replace_parameters_and_secrets_in_string "${DREVOPS_NOTIFY_WEBHOOK_MESSAGE_BODY}")"

# Make curl request.
response_http_code="$(curl -s \
-o /dev/null \
-w '%{http_code}' \
-X "${DREVOPS_NOTIFY_WEBHOOK_METHOD}" \
"${headers[@]}" \
--data "${message_body}" \
"${DREVOPS_NOTIFY_WEBHOOK_URL}"
)"
if [[ "${response_http_code}" = "200" ]]; then
response_http_code="$(
curl -s \
-o /dev/null \
-w '%{http_code}' \
-X "${DREVOPS_NOTIFY_WEBHOOK_METHOD}" \
"${headers[@]}" \
--data "${message_body}" \
"${DREVOPS_NOTIFY_WEBHOOK_URL}"
)"
if [[ ${response_http_code} == "200" ]]; then
pass "Notified to webhook ${DREVOPS_NOTIFY_WEBHOOK_URL}."
else
fail "Unable to notify to webhook ${DREVOPS_NOTIFY_WEBHOOK_URL}."
Expand Down
4 changes: 4 additions & 0 deletions scripts/drevops/notify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ if [ -z "${DREVOPS_NOTIFY_CHANNELS##*jira*}" ]; then
./scripts/drevops/notify-jira.sh "$@"
fi

if [ -z "${DREVOPS_NOTIFY_CHANNELS##*webhook*}" ]; then
./scripts/drevops/notify-webhook.sh "$@"
fi

pass "Finished dispatching notifications."

1 comment on commit 2876c17

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.