Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of escape sequences in messages #2

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ SLACK_MESSAGE | Generated from git commit message. | The
SLACK_TITLE | Message | Title to use before main Slack message.
SLACK_FOOTER | Powered By rtCamp's GitHub Actions Library | Slack message footer.
MSG_MINIMAL | - | If set to `true`, removes: `Ref`, `Event`, `Actions URL` and `Commit` from the message. You can optionally whitelist any of these 4 removed values by passing it comma separated to the variable instead of `true`. (ex: `MSG_MINIMAL: event` or `MSG_MINIMAL: ref,actions url`, etc.)
ENABLE_ESCAPES | - | If set to `true`, enables backslash escapes in the message such as '\n', '\t', etc.

You can see the action block with all variables as below:

Expand Down
36 changes: 23 additions & 13 deletions main.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/usr/bin/env bash

export GITHUB_BRANCH=${GITHUB_REF##*heads/}
export SLACK_ICON=${SLACK_ICON:-"https://avatars0.githubusercontent.com/u/43742164"}
export SLACK_USERNAME=${SLACK_USERNAME:-"rtBot"}
export GITHUB_BRANCH="${GITHUB_REF##*heads/}"
export SLACK_ICON="${SLACK_ICON:-"https://avatars0.githubusercontent.com/u/43742164"}"
export SLACK_USERNAME="${SLACK_USERNAME:-"rtBot"}"
export CI_SCRIPT_OPTIONS="ci_script_options"
export SLACK_TITLE=${SLACK_TITLE:-"Message"}
export COMMIT_MESSAGE=$(cat "$GITHUB_EVENT_PATH" | jq -r '.commits[-1].message')
export GITHUB_ACTOR=${SLACK_MSG_AUTHOR:-"$GITHUB_ACTOR"}
export SLACK_TITLE="${SLACK_TITLE:-"Message"}"
export GITHUB_ACTOR="${SLACK_MSG_AUTHOR:-"$GITHUB_ACTOR"}"

COMMIT_MESSAGE="$(cat "$GITHUB_EVENT_PATH" | jq -r '.commits[-1].message')"
export COMMIT_MESSAGE

hosts_file="$GITHUB_WORKSPACE/.github/hosts.yml"

Expand All @@ -30,31 +32,39 @@ if [[ -z "$SLACK_WEBHOOK" ]]; then
fi

if [[ -n "$VAULT_GITHUB_TOKEN" ]] || [[ -n "$VAULT_TOKEN" ]]; then
export SLACK_WEBHOOK=$(vault read -field=webhook secret/slack)
SLACK_WEBHOOK="$(vault read -field=webhook secret/slack)"
export SLACK_WEBHOOK
fi
fi

if [[ -f "$hosts_file" ]]; then
hostname=$(cat "$hosts_file" | shyaml get-value "$GITHUB_BRANCH.hostname")
user=$(cat "$hosts_file" | shyaml get-value "$GITHUB_BRANCH.user")
export HOST_NAME="\`$user@$hostname\`"
export DEPLOY_PATH=$(cat "$hosts_file" | shyaml get-value "$GITHUB_BRANCH.deploy_path")
DEPLOY_PATH="$(cat "$hosts_file" | shyaml get-value "$GITHUB_BRANCH.deploy_path")"
export DEPLOY_PATH

temp_url=${DEPLOY_PATH%%/app*}
temp_url="${DEPLOY_PATH%%/app*}"
export SITE_NAME="${temp_url##*sites/}"
export HOST_TITLE="SSH Host"
fi

PR_SHA=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.head.sha)
[[ 'null' != $PR_SHA ]] && export GITHUB_SHA="$PR_SHA"
PR_SHA="$(cat "$GITHUB_EVENT_PATH" | jq -r .pull_request.head.sha)"
[[ 'null' != "$PR_SHA" ]] && export GITHUB_SHA="$PR_SHA"

if [[ -n "$SITE_NAME" ]]; then
export SITE_TITLE="Site"
fi


if [[ -z "$SLACK_MESSAGE" ]]; then
export SLACK_MESSAGE="$COMMIT_MESSAGE"
SLACK_MESSAGE="$COMMIT_MESSAGE"
fi

if [[ "true" == "$ENABLE_ESCAPES" ]]; then
SLACK_MESSAGE="$(echo -e "$SLACK_MESSAGE")"
fi

slack-notify "$@"
export SLACK_MESSAGE

./slack-notify "$@"