Skip to content

Commit

Permalink
Check response code more flex.
Browse files Browse the repository at this point in the history
  • Loading branch information
tannguyen04 committed Apr 19, 2024
1 parent 99c69f3 commit 8d2e0bb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .scaffold/tests/bats/notify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ load _helper.bash
mock_curl=$(mock_command "curl")

mock_set_output "${mock_curl}" "200" 1
mock_set_output "${mock_curl}" "400" 2

export DREVOPS_NOTIFY_CHANNELS="webhook"
export DREVOPS_NOTIFY_ENVIRONMENT_URL="https://example-environment-notifcation.com"
Expand All @@ -241,5 +242,8 @@ load _helper.bash

assert_output_contains "Finished dispatching notifications."

run ./scripts/drevops/notify.sh
assert_failure

popd >/dev/null || exit 1
}
26 changes: 14 additions & 12 deletions scripts/drevops/notify-webhook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ DREVOPS_NOTIFY_WEBHOOK_CUSTOM_HEADERS="${DREVOPS_NOTIFY_WEBHOOK_CUSTOM_HEADERS:-
# Ex: {"channel": "XXX", "message": "Hello there"}.
DREVOPS_NOTIFY_WEBHOOK_MESSAGE_BODY="${DREVOPS_NOTIFY_WEBHOOK_MESSAGE_BODY:-}"

# The pattern of response code return by curl.
# Default is match 200.
DREVOPS_NOTIFY_WEBHOOK_RESPONSE_CODE_PATTERN="${DREVOPS_NOTIFY_WEBHOOK_RESPONSE_CODE_PATTERN:-^200$}"
# Custom parameters and secrets to use in custom header and message body.
# Ex: [{"name": "API_KEY", "value": "XXX"},{"name": "PASSWORD", "value": "XXX"}]
DREVOPS_NOTIFY_WEBHOOK_CUSTOM_PARAMETERS_AND_SECRETS="${DREVOPS_NOTIFY_WEBHOOK_CUSTOM_PARAMETERS_AND_SECRETS:-}"
Expand All @@ -45,6 +48,12 @@ for cmd in php curl jq; do command -v ${cmd} >/dev/null || {
exit 1
}; done

Check warning on line 49 in scripts/drevops/notify-webhook.sh

View check run for this annotation

Codecov / codecov/patch

scripts/drevops/notify-webhook.sh#L47-L49

Added lines #L47 - L49 were not covered by tests

[ -z "${DREVOPS_NOTIFY_ENVIRONMENT_URL}" ] && fail "Missing required value for DREVOPS_NOTIFY_ENVIRONMENT_URL" && exit 1
[ -z "${DREVOPS_NOTIFY_WEBHOOK_URL}" ] && fail "Missing required value for DREVOPS_NOTIFY_WEBHOOK_URL" && exit 1
[ -z "${DREVOPS_NOTIFY_WEBHOOK_METHOD}" ] && fail "Missing required value for DREVOPS_NOTIFY_WEBHOOK_METHOD" && exit 1
[ -z "${DREVOPS_NOTIFY_WEBHOOK_CUSTOM_HEADERS}" ] && fail "Missing required value for DREVOPS_NOTIFY_WEBHOOK_CUSTOM_HEADERS" && exit 1
[ -z "${DREVOPS_NOTIFY_WEBHOOK_MESSAGE_BODY}" ] && fail "Missing required value for DREVOPS_NOTIFY_WEBHOOK_MESSAGE_BODY" && exit 1

# Find custom parameters and secrets in string and replace it by value.
# shellcheck disable=SC2001
replace_parameters_and_secrets_in_string() {
Expand Down Expand Up @@ -88,18 +97,11 @@ done < <(echo "${headers_replaced}" | jq -c '.[]')
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
pass "Notified to webhook ${DREVOPS_NOTIFY_WEBHOOK_URL}."
else

if ! curl -L -s -o /dev/null -w '%{http_code}' \
-X "${DREVOPS_NOTIFY_WEBHOOK_METHOD}" \
"${headers[@]}" \
-d "${message_body}" "${DREVOPS_NOTIFY_WEBHOOK_URL}" | grep -q "${DREVOPS_NOTIFY_WEBHOOK_RESPONSE_CODE_PATTERN}"; then
fail "Unable to notify to webhook ${DREVOPS_NOTIFY_WEBHOOK_URL}."
exit 1
fi
Expand Down

1 comment on commit 8d2e0bb

@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.