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

OPSEXP-1806 Fix multiline/markdown support in slack notification #162

Merged
merged 5 commits into from
Oct 26, 2022
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
18 changes: 4 additions & 14 deletions .github/actions/send-slack-notification/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,11 @@ runs:
env:
EVENT_NAME: ${{ github.event_name }}
BLOCK_MESSAGE: ${{ inputs.message }}
PR_TITLE: ${{ github.event.pull_request.title }}
ISSUE_BODY: ${{ github.event.issue.body }}
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
run: |
case $EVENT_NAME in
pull_request)
BLOCK_MESSAGE="${BLOCK_MESSAGE:-${{ github.event.pull_request.title }}}"
;;
issues)
BLOCK_MESSAGE="${BLOCK_MESSAGE:-${{ github.event.issue.body }}}"
;;
*)
BLOCK_MESSAGE="${BLOCK_MESSAGE:-${{ github.event.head_commit.message }}}"
;;
esac
echo 'result<<EOF' >> $GITHUB_OUTPUT
echo $BLOCK_MESSAGE >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
${{ github.action_path }}/compute-message.sh >> $GITHUB_OUTPUT

- name: Send slack notification
id: slack
Expand Down
22 changes: 22 additions & 0 deletions .github/actions/send-slack-notification/compute-message.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash -e

echo 'result<<EOF'

if [ -n "$BLOCK_MESSAGE" ]; then
echo -n "${BLOCK_MESSAGE}" | sed -z 's/\n/\\n/g'
else
case $EVENT_NAME in
pull_request)
echo -n "${PR_TITLE}" | sed -z 's/\n/\\n/g'
;;
issues)
echo -n "${ISSUE_BODY}" | sed -z 's/\n/\\n/g'
;;
*)
echo -n "${COMMIT_MESSAGE}" | sed -z 's/\n/\\n/g'
;;
esac
fi

echo ''
Copy link
Contributor

Choose a reason for hiding this comment

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

what about removing the -n switch from echo commands and removing that empty echo ?

Copy link
Member Author

@gionn gionn Oct 26, 2022

Choose a reason for hiding this comment

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

it is to avoid having that last newline converted to \n by sed, while I really want a NL before the closing heredoc tag

echo "\nEOF" would have worked as well but for some reason looked less clear to me :D

echo 'EOF'
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
setup() {
# Runs everywhere
DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
PATH="$DIR/..:$PATH"

# Mock send-slack-notification defaults
export PR_TITLE="fix(jx-updatebot-pr): update release binary version to 0.3.13 #158"
export ISSUE_BODY=$(cat << 'body'
This PR adds support for retrying `jx-updatebot-pr` action in case there is a race condition on the target pr branch due to a concurrent commit from another project, i.e.

```
- uses: Alfresco/alfresco-build-tools/.github/actions/jx-updatebot-pr@ref
with:
retries: '3'
retries-wait: '10'

```
body
)
export COMMIT_MESSAGE=$(cat << 'commit'
fix: update retry inputs to use string type

* use strings
* add tests
commit
)
}

@test "basic slack message" {
export BLOCK_MESSAGE="my custom message"

run compute-message.sh

[ "$status" -eq 0 ]

expected_output=$(cat << BATS
result<<EOF
my custom message
EOF
BATS
)

[ "$output" = "$expected_output" ]
}

@test "pull_request slack message" {
export EVENT_NAME=pull_request

run compute-message.sh

[ "$status" -eq 0 ]

expected_output=$(cat << BATS
result<<EOF
fix(jx-updatebot-pr): update release binary version to 0.3.13 #158
EOF
BATS
)

[ "$output" = "$expected_output" ]
}

@test "multiline markdown for issues slack message" {
export EVENT_NAME=issues

run compute-message.sh

[ "$status" -eq 0 ]

expected_output=$(cat << 'BATS'
result<<EOF
This PR adds support for retrying `jx-updatebot-pr` action in case there is a race condition on the target pr branch due to a concurrent commit from another project, i.e.\n\n```\n- uses: Alfresco/alfresco-build-tools/.github/actions/jx-updatebot-pr@ref\n with:\n retries: '3'\n retries-wait: '10'\n\n```
EOF
BATS
)

[ "$output" = "$expected_output" ]
}

@test "multiline commit message for default slack message" {
export EVENT_NAME=whatever

run compute-message.sh

[ "$status" -eq 0 ]

expected_output=$(cat << 'BATS'
result<<EOF
fix: update retry inputs to use string type\n\n* use strings\n* add tests
EOF
BATS
)

[ "$output" = "$expected_output" ]
}
11 changes: 11 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/pre-commit
# - name: Manual test for send slack notification
# uses: ./.github/actions/send-slack-notification
# with:
# channel-id: '' # grab your slack id from your profile -> three dots -> Copy member ID
# token: ${{ secrets.SLACK_BOT_TOKEN }}
# notification-color: '#A30200'
# message: |
# This is a multiline message with `code`
#
# * another line
# * one more
- uses: ./.github/actions/setup-helm-docs
- uses: ./.github/actions/setup-jx-release-version
- uses: ./.github/actions/setup-kubepug
Expand Down