Skip to content

Commit

Permalink
Make sure that the backslash+n char sequences are converted to the `\…
Browse files Browse the repository at this point in the history
…n` newline escape char in the message texts (#17)

* Make sure that the backslash+n char sequences are converted to the `\n` newline escape char in the message texts

Related discussion: https://discuss.bitrise.io/t/slack-step-does-not-treat-n-as-a-newline-when-message-generated-by-a-bash-script-step/1776

* go deps update
  • Loading branch information
viktorbenei authored May 22, 2017
1 parent 9add234 commit 053d908
Show file tree
Hide file tree
Showing 31 changed files with 5,504 additions and 3 deletions.
20 changes: 20 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ workflows:
do-vendor-update:
steps:
- script:
title: go deps update
inputs:
- content: |
#!/bin/bash
Expand All @@ -17,6 +18,8 @@ workflows:
rm -rf ./Godeps
rm -rf ./vendor
go get -t -d ./...
go get github.com/davecgh/go-spew/spew
go get github.com/pmezard/go-difflib/difflib
godep save ./...
fail-message-test:
steps:
Expand Down Expand Up @@ -85,6 +88,13 @@ workflows:
bitrise share finish
test:
steps:
- script:
title: go test
inputs:
- content: |
#!/bin/bash
set -ex
go test ./...
- path::./:
title: On Success
is_skippable: false
Expand Down Expand Up @@ -134,3 +144,20 @@ workflows:
and _some_ *highlight*
- color: good
- image_url: https://media.giphy.com/media/6brH8dM3zeMyA/giphy.gif
- [email protected]:
title: Generate SLACK_MESSAGE_FROM_SCRIPT
inputs:
- content: |-
#!/bin/bash
set -ex
multi_line_msg="Multi\nline\n\ntext"
envman add --key SLACK_MESSAGE_FROM_SCRIPT --value "$multi_line_msg"
- path::./:
title: Should escape backslash+n as newline char
is_skippable: false
inputs:
- webhook_url: $SLACK_WEBHOOK_URL
- channel: $SLACK_CHANNEL
- from_username: step-dev-test
- message: $SLACK_MESSAGE_FROM_SCRIPT
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/url"
"os"

"strings"

"github.com/bitrise-io/go-utils/colorstring"
)

Expand Down Expand Up @@ -85,13 +87,13 @@ func (configs ConfigsModel) print() {
func (configs ConfigsModel) validate() error {
// required
if configs.WebhookURL == "" {
return errors.New("No Webhook URL parameter specified!")
return errors.New("No Webhook URL parameter specified")
}
if configs.Message == "" {
return errors.New("No Message parameter specified!")
return errors.New("No Message parameter specified")
}
if configs.Color == "" {
return errors.New("No Color parameter specified!")
return errors.New("No Color parameter specified")
}

return nil
Expand Down Expand Up @@ -119,6 +121,11 @@ type RequestParams struct {
IconURL *string `json:"icon_url"`
}

// ensureNewlineEscapeChar replaces the "\" + "n" char sequences with the "\n" newline char
func ensureNewlineEscapeChar(s string) string {
return strings.Replace(s, "\\"+"n", "\n", -1)
}

// CreatePayloadParam ...
func CreatePayloadParam(configs ConfigsModel) (string, error) {
// - required
Expand All @@ -138,6 +145,7 @@ func CreatePayloadParam(configs ConfigsModel) (string, error) {
msgText = configs.MessageOnError
}
}
msgText = ensureNewlineEscapeChar(msgText)
// - optional attachment params
msgImage := configs.ImageURL
if configs.IsBuildFailed {
Expand Down
17 changes: 17 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_ensureNewlineEscapeChar(t *testing.T) {
require.Equal(t, "", ensureNewlineEscapeChar(""))
require.Equal(t, "a", ensureNewlineEscapeChar("a"))
require.Equal(t, "\n", ensureNewlineEscapeChar("\n"))
require.Equal(t, "\n", ensureNewlineEscapeChar("\\"+"n"))
// should convert \ + n to \n too; where \n is a single char, the ASCII 10 "newline feed" char
require.Equal(t, uint8(10), ensureNewlineEscapeChar("\n")[0])
require.Equal(t, uint8(10), ensureNewlineEscapeChar("\\" + "n")[0])
}
15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

152 changes: 152 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 053d908

Please sign in to comment.