Skip to content

Commit

Permalink
Lint, update tests and remove junk code.
Browse files Browse the repository at this point in the history
  • Loading branch information
tannguyen04 committed Apr 25, 2024
1 parent 50e51dd commit 96f541f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
22 changes: 22 additions & 0 deletions .scaffold/tests/bats/notify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,25 @@ load _helper.bash

popd >/dev/null || exit 1
}

@test "Notify: webhook failure" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

mock_curl=$(mock_command "curl")
mock_set_output "${mock_curl}" "400" 1

export DREVOPS_NOTIFY_CHANNELS="webhook"
export DREVOPS_NOTIFY_PROJECT="testproject"
export DREVOPS_NOTIFY_REF="develop"
export DREVOPS_NOTIFY_ENVIRONMENT_URL="https://develop.testproject.com"

export DREVOPS_NOTIFY_WEBHOOK_URL="https://example-webhook-url.com"
export DREVOPS_NOTIFY_WEBHOOK_METHOD="POST"
export DREVOPS_NOTIFY_WEBHOOK_HEADERS="Content-type: application/json|Authorization: Bearer API_KEY"
export DREVOPS_NOTIFY_WEBHOOK_PAYLOAD='{"channel": "Test channel 1", "message": "Test channel 1 message"}'

run ./scripts/drevops/notify.sh
assert_failure

popd >/dev/null || exit 1
}
17 changes: 6 additions & 11 deletions scripts/drevops/notify-webhook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@ set -eu

# Project name to notify.
DREVOPS_NOTIFY_PROJECT="${DREVOPS_NOTIFY_PROJECT:-}"
DREVOPS_NOTIFY_PROJECT="Project Name"

# Git reference to notify about.
DREVOPS_NOTIFY_REF="${DREVOPS_NOTIFY_REF:-}"
DREVOPS_NOTIFY_REF="Branch Name"

# Deployment environment URL.
DREVOPS_NOTIFY_ENVIRONMENT_URL="${DREVOPS_NOTIFY_ENVIRONMENT_URL:-}"
DREVOPS_NOTIFY_ENVIRONMENT_URL="https://example.com"

# Webhook URL.
DREVOPS_NOTIFY_WEBHOOK_URL="${DREVOPS_NOTIFY_WEBHOOK_URL:-}"
DREVOPS_NOTIFY_WEBHOOK_URL="https://typedwebhook.tools/webhook/16627646-ca0e-4d9f-8bf9-d6f7a69476bc"

# Webhook method like POST, GET, PUT.
DREVOPS_NOTIFY_WEBHOOK_METHOD="${DREVOPS_NOTIFY_WEBHOOK_METHOD:-POST}"
Expand All @@ -36,8 +32,7 @@ DREVOPS_NOTIFY_WEBHOOK_HEADERS="${DREVOPS_NOTIFY_WEBHOOK_HEADERS:-Content-type:
# This is data sent to webhook.
# Some built-in variables like: %message%, %environment_url%, %project%, %ref%, %timestamp%.
# Ex: {"channel": "Channel 1", "message": "%message%"}.
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD="${DREVOPS_NOTIFY_WEBHOOK_PAYLOAD:-}"
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD='{"channel": "Channel 1", "message": "%message%", "project": "%project%", "ref": "%ref%", "timestamp": "%timestamp%", "environment_url": "%environment_url%"}'
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD="${DREVOPS_NOTIFY_WEBHOOK_PAYLOAD:-{\"channel\": \"Channel 1\", \"message\": \"%message%\", \"project\": \"%project%\", \"ref\": \"%ref%\", \"timestamp\": \"%timestamp%\", \"environment_url\": \"%environment_url%\"}}"

# The pattern of response code return by curl.
# Default is match 200.
Expand Down Expand Up @@ -69,11 +64,11 @@ for cmd in php curl; do command -v ${cmd} >/dev/null || {
timestamp=$(date '+%d/%m/%Y %H:%M:%S %Z')
message='## This is an automated message ##\nSite %project% \"%ref%\" branch has been deployed at %timestamp% and is available at %environment_url%.\nLogin at: %environment_url%/user/login'

DREVOPS_NOTIFY_WEBHOOK_PAYLOAD=$(php -r "echo str_replace('%message%', '$message', '$DREVOPS_NOTIFY_WEBHOOK_PAYLOAD');")
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD=$(php -r "echo str_replace('%timestamp%', '$timestamp', '$DREVOPS_NOTIFY_WEBHOOK_PAYLOAD');")
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD=$(php -r "echo str_replace('%ref%', '$DREVOPS_NOTIFY_REF', '$DREVOPS_NOTIFY_WEBHOOK_PAYLOAD');")
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD=$(php -r "echo str_replace('%project%', '$DREVOPS_NOTIFY_PROJECT', '$DREVOPS_NOTIFY_WEBHOOK_PAYLOAD');")
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD=$(php -r "echo str_replace('%environment_url%', '$DREVOPS_NOTIFY_ENVIRONMENT_URL', '$DREVOPS_NOTIFY_WEBHOOK_PAYLOAD');")
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD=$(php -r "echo str_replace('%message%', '${message}', '${DREVOPS_NOTIFY_WEBHOOK_PAYLOAD}');")
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD=$(php -r "echo str_replace('%timestamp%', '${timestamp}', '${DREVOPS_NOTIFY_WEBHOOK_PAYLOAD}');")
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD=$(php -r "echo str_replace('%ref%', '${DREVOPS_NOTIFY_REF}', '${DREVOPS_NOTIFY_WEBHOOK_PAYLOAD}');")
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD=$(php -r "echo str_replace('%project%', '${DREVOPS_NOTIFY_PROJECT}', '${DREVOPS_NOTIFY_WEBHOOK_PAYLOAD}');")
DREVOPS_NOTIFY_WEBHOOK_PAYLOAD=$(php -r "echo str_replace('%environment_url%', '${DREVOPS_NOTIFY_ENVIRONMENT_URL}', '${DREVOPS_NOTIFY_WEBHOOK_PAYLOAD}');")

info "Started Webhook notification."

Expand Down

1 comment on commit 96f541f

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