Skip to content

Commit

Permalink
feat: add windows support (#380)
Browse files Browse the repository at this point in the history
* feat: implement workaround for windows support
  • Loading branch information
EricRibeiro authored Nov 29, 2022
1 parent 0edb427 commit afb96bd
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 26 deletions.
25 changes: 18 additions & 7 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
steps:
- checkout
- slack/notify:
debug: true
step_name: "Custom template with group mention"
event: always
custom: |
Expand All @@ -43,23 +44,28 @@ jobs:
command: |
grep "pass" /tmp/SLACK_JOB_STATUS
- slack/notify:
debug: true
step_name: "Fail template with mentions"
template: basic_fail_1
mentions: "@orbs"
event: always
- slack/notify:
debug: true
step_name: "Success template with mentions"
template: basic_success_1
event: always
- slack/notify:
debug: true
step_name: "Success tagged template"
template: success_tagged_deploy_1
event: always
- slack/notify:
debug: true
step_name: "Basic on hold template"
template: basic_on_hold_1
event: always
- slack/notify:
debug: true
step_name: "Custom template with env var in the message"
event: pass
custom: >
Expand All @@ -74,25 +80,23 @@ jobs:
}
]
}
- slack/notify:
step_name: "Notify with debug set to true"
debug: true
template: basic_success_1
event: always
- run:
name: Dynamically populate the mention and export the template as an environment variable
command: |
echo 'export COMMIT_MESSAGE="This text comes from an environment variable"' >> $BASH_ENV
echo 'export SLACK_PARAM_MENTIONS="$COMMIT_MESSAGE"' >> $BASH_ENV
echo 'export MY_ORB_TEMPLATE=$(cat src/message_templates/basic_success_1.json)' >> $BASH_ENV
- slack/notify:
debug: true
step_name: "Dynamic template with environment variable"
event: always
template: MY_ORB_TEMPLATE
- slack/notify:
debug: true
step_name: "Notify without template parameter"
# Should run for every branch but master
- slack/notify:
debug: true
step_name: "Invert match set to true on 'master' branch pattern"
branch_pattern: "master"
invert_match: true
Expand All @@ -101,6 +105,7 @@ jobs:
command: |
printf '%s\n' 'export MULTILINE_STRING=$(printf "%s\\n" "Line 1." "Line 2." "Line 3.")' >> "$BASH_ENV"
- slack/notify:
debug: true
step_name: "Notify with multiline string"
event: pass
custom: >
Expand All @@ -120,6 +125,7 @@ jobs:
command: |
printf '%s\n' 'export DOUBLE_QUOTES_STRING=$(printf "%s\\n" "Hello There! My name is \"Potato\"")' >> "$BASH_ENV"
- slack/notify:
debug: true
step_name: "Notify with double-quoted string"
event: pass
custom: >
Expand All @@ -139,9 +145,9 @@ jobs:
command: |
printf '%s\n' 'export BACKSLASHES_STRING="This is how a \ looks like"' >> "$BASH_ENV"
- slack/notify:
debug: true
step_name: "Notify with backslashes string"
event: pass
debug: true
custom: >
{
"blocks": [
Expand All @@ -163,7 +169,7 @@ workflows:
filters: *filters
matrix:
parameters:
runner: [cimg, mac, alpine]
runner: [cimg, mac, alpine, windows]
- orb-tools/lint:
filters: *filters
- orb-tools/pack:
Expand Down Expand Up @@ -198,3 +204,8 @@ executors:
# This image contains both CURL and JQ
docker:
- image: dwdraju/alpine-curl-jq
windows:
machine:
image: windows-server-2019-vs2019:current
resource_class: windows.medium
shell: bash.exe
4 changes: 3 additions & 1 deletion src/commands/notify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ steps:
SLACK_PARAM_IGNORE_ERRORS: "<<parameters.ignore_errors>>"
SLACK_PARAM_DEBUG: "<<parameters.debug>>"
SLACK_PARAM_CIRCLECI_HOST: "<<parameters.circleci_host>>"
SLACK_SCRIPT_NOTIFY: "<<include(scripts/notify.sh)>>"
SLACK_SCRIPT_UTILS: "<<include(scripts/utils.sh)>>"
# import pre-built templates using the orb-pack local script include.
basic_fail_1: "<<include(message_templates/basic_fail_1.json)>>"
success_tagged_deploy_1: "<<include(message_templates/success_tagged_deploy_1.json)>>"
basic_on_hold_1: "<<include(message_templates/basic_on_hold_1.json)>>"
basic_success_1: "<<include(message_templates/basic_success_1.json)>>"
command: <<include(scripts/notify.sh)>>
command: <<include(scripts/main.sh)>>
6 changes: 6 additions & 0 deletions src/scripts/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env sh

# Workaround for Windows Support
# For details, see: https://github.com/CircleCI-Public/slack-orb/pull/380
# shellcheck source=/dev/null
eval printf '%s' "$SLACK_SCRIPT_NOTIFY"
33 changes: 16 additions & 17 deletions src/scripts/notify.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/sh
# shellcheck disable=SC2016,SC3043

# Import utils.
eval "$SLACK_SCRIPT_UTILS"
JQ_PATH=/usr/local/bin/jq

BuildMessageBody() {
Expand All @@ -9,7 +11,7 @@ BuildMessageBody() {
# if none is supplied, check for a pre-selected template value.
# If none, error.
if [ -n "${SLACK_PARAM_CUSTOM:-}" ]; then
SanitizeVars "$SLACK_PARAM_CUSTOM"
SanitizeVars "$SLACK_PARAM_CUSTOM"
ModifyCustomTemplate
# shellcheck disable=SC2016
CUSTOM_BODY_MODIFIED=$(echo "$CUSTOM_BODY_MODIFIED" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/`/\\`/g')
Expand All @@ -30,7 +32,7 @@ BuildMessageBody() {
T1="$(printf '%s' "$template_body" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/`/\\`/g')"
T2="$(eval printf '%s' \""$T1"\")"
fi

# Insert the default channel. THIS IS TEMPORARY
T2="$(printf '%s' "$T2" | jq ". + {\"channel\": \"$SLACK_DEFAULT_CHANNEL\"}")"
SLACK_MSG_BODY="$T2"
Expand Down Expand Up @@ -181,20 +183,11 @@ SetupLogs() {
fi
}

ExitIfWindows() {
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
if printf '%s\n' "$os" | grep -q 'msys*\|cygwin*' ; then
printf '%s\n' "Windows is not supported by this orb."
printf '%s\n' "For more information, see: https://github.com/CircleCI-Public/slack-orb/wiki/FAQ."
exit 1
fi
}

# $1: Template with environment variables to be sanitized.
SanitizeVars() {
[ -z "$1" ] && { printf '%s\n' "Missing argument."; return 1; }
local template="$1"

# Find all environment variables in the template with the format $VAR or ${VAR}.
# The "|| true" is to prevent bats from failing when no matches are found.
local variables
Expand All @@ -204,23 +197,30 @@ SanitizeVars() {
# Extract the variable names from the matches.
local variable_names
variable_names="$(printf '%s\n' "$variables" | grep -o -E '[a-zA-Z0-9_]+')"


# Find out what OS we're running on.
detect_os

for var in $variable_names; do
# The variable must be wrapped in double quotes before the evaluation.
# Otherwise the newlines will be removed.
local value
value="$(eval printf '%s' \"\$"$var\"")"
[ -z "$value" ] && { printf '%s\n' "$var is empty or doesn't exist. Skipping it..."; continue; }

printf '%s\n' "Sanitizing $var..."

local sanitized_value="$value"
# Escape backslashes.
sanitized_value="$(printf '%s' "$sanitized_value" | awk '{gsub(/\\/, "&\\"); print $0}')"
# Escape newlines.
sanitized_value="$(printf '%s' "$sanitized_value" | awk 'NR > 1 { printf("\\n") } { printf("%s", $0) }')"
# Escape double quotes.
sanitized_value="$(printf '%s' "$sanitized_value" | awk '{gsub(/\"/, "\\\""); print $0}')"
if [ "$PLATFORM" = "windows" ]; then
sanitized_value="$(printf '%s' "$sanitized_value" | awk '{gsub(/"/, "\\\""); print $0}')"
else
sanitized_value="$(printf '%s' "$sanitized_value" | awk '{gsub(/\"/, "\\\""); print $0}')"
fi

# Write the sanitized value back to the original variable.
# shellcheck disable=SC3045 # This is working on Alpine.
Expand All @@ -234,7 +234,6 @@ SanitizeVars() {
# This is done so this script may be tested.
ORB_TEST_ENV="bats-core"
if [ "${0#*"$ORB_TEST_ENV"}" = "$0" ]; then
ExitIfWindows
# shellcheck source=/dev/null
. "/tmp/SLACK_JOB_STATUS"
ShouldPost
Expand Down
25 changes: 25 additions & 0 deletions src/scripts/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/false
# shellcheck shell=sh
# shellcheck disable=SC2154

detect_os() {
detected_platform="$(uname -s | tr '[:upper:]' '[:lower:]')"

case "$detected_platform" in
linux*)
PLATFORM=linux
;;
darwin*)
PLATFORM=macos
;;
msys*|cygwin*)
PLATFORM=windows
;;
*)
printf '%s\n' "Unsupported OS: \"$platform\"."
exit 1
;;
esac

export readonly PLATFORM
}
3 changes: 2 additions & 1 deletion src/tests/notify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

setup() {
source ./src/scripts/notify.sh
source ./src/scripts/utils.sh
export SLACK_PARAM_BRANCHPATTERN=$(cat $BATS_TEST_DIRNAME/sampleBranchFilters.txt)
SLACK_PARAM_INVERT_MATCH="0"
}
Expand Down Expand Up @@ -151,4 +152,4 @@ setup() {
SanitizeVars "$SLACK_PARAM_CUSTOM"
printf '%s\n' "Expected: $EXPECTED" "Actual: $CIRCLE_JOB"
[ "$CIRCLE_JOB" = "$EXPECTED" ] # Backslashes should be escaped
}
}

0 comments on commit afb96bd

Please sign in to comment.