Skip to content

Commit

Permalink
[SWAT-595] Add support for multiline Field values (#85)
Browse files Browse the repository at this point in the history
* Support multiline fields with escaped newlines
* Added e2e test for multiline field
* Update input description
* Added unit test

Co-authored-by: Laszlo Pusok <[email protected]>
  • Loading branch information
DamienBitrise and lpusok authored Sep 20, 2022
1 parent 29c0977 commit b38da23
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
9 changes: 7 additions & 2 deletions bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,22 @@ workflows:
#!/bin/bash
set -ex
multi_line_msg="Multi\nline\n\ntext"
multi_line_msg="Multiline, with a link: https://www.bitrise.io, \n _some_ *highlight*, \n and linkify @slackbot #random"
envman add --key SLACK_MESSAGE_FROM_SCRIPT --value "$multi_line_msg"
- path::./:
title: Should escape backslash+n as newline char
title: Should escape backslash+n as newline char + custom multiline release notes field
is_skippable: false
inputs:
- webhook_url: $SLACK_WEBHOOK_URL
- api_token: $SLACK_API_TOKEN
- channel: $SLACK_CHANNEL
- from_username: step-dev-test
- message: $SLACK_MESSAGE_FROM_SCRIPT
- fields: |
Release notes|${SLACK_MESSAGE_FROM_SCRIPT}
App|${BITRISE_APP_TITLE}
Branch|${BITRISE_GIT_BRANCH}
Workflow|${BITRISE_TRIGGERED_WORKFLOW_ID}
fail-message-test:
steps:
- script:
Expand Down
2 changes: 1 addition & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (f Field) MarshalJSON() ([]byte, error) {

func parseFields(s string) (fs []Field) {
for _, p := range pairs(s) {
fs = append(fs, Field{Title: p[0], Value: p[1]})
fs = append(fs, Field{Title: p[0], Value: ensureNewlines(p[1])})
}
return
}
Expand Down
27 changes: 27 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"reflect"
"testing"
)

func Test_parseFields(t *testing.T) {
tests := []struct {
name string
s string
wantFs []Field
}{
{
name: "Newline in release notes",
s: "Release notes|line1\\nline2",
wantFs: []Field{{Title: "Release notes", Value: "line1\nline2"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotFs := parseFields(tt.s); !reflect.DeepEqual(gotFs, tt.wantFs) {
t.Errorf("parseFields() = %v, want %v", gotFs, tt.wantFs)
}
})
}
}
5 changes: 4 additions & 1 deletion step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,13 @@ inputs:
description: |
Fields separated by newlines and each field contains a `title` and a `value`.
The `title` and the `value` fields are separated by a pipe `|` character.
Empty lines and lines without a separator are omitted.
The *title* shown as a bold heading above the `value` text.
The *value* is the text value of the field.
Supports multiline text with escaped newlines. Example: `Release notes| - Line1 \n -Line2`.
Empty lines and lines without a separator are omitted.
- buttons: |
View App|${BITRISE_APP_URL}
View Build|${BITRISE_BUILD_URL}
Expand Down

0 comments on commit b38da23

Please sign in to comment.