Skip to content

Commit

Permalink
Update escapes to match newest action
Browse files Browse the repository at this point in the history
There is a fix in actions toolkit: actions/toolkit#302, apply it here as well
  • Loading branch information
tjamet committed Jan 17, 2020
1 parent 1984594 commit 5ece5c4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
37 changes: 20 additions & 17 deletions core/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ const (
var (
stdout io.Writer = os.Stdout
stdoutSetter sync.Mutex
dataEscapes = map[string]string{
"\r": "%0D",
"\n": "%0A",
}
escapes = map[string]string{
":": "%3A",
",": "%2C",
}
)

func SetStdout(w io.Writer) {
Expand Down Expand Up @@ -53,25 +61,20 @@ func (c *command) String() string {
return s + cmdString + escape(c.message)
}

func escapePatterns(v string, replacementsArg ...map[string]string) string {
v = strings.Replace(v, "%", "%25", -1)
for _, replacements := range replacementsArg {
for pattern, replacement := range replacements {
v = strings.Replace(v, pattern, replacement, -1)
}
}
return v
}

func escapeData(v string) string {
return strings.Replace(
strings.Replace(
v,
"\r", "%0D", -1,
),
"\n", "%0A", -1,
)
return escapePatterns(v, dataEscapes)
}

func escape(v string) string {
return strings.Replace(
strings.Replace(
strings.Replace(
escapeData(v),
"]", "%5D", -1,
),
";", "%3B", -1,
),
",", "%2C",-1,
)
return escapePatterns(v, escapes, dataEscapes)
}
13 changes: 10 additions & 3 deletions core/command_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package core

import (
"bytes"
"regexp"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -21,8 +23,13 @@ func TestIssueCommand(t *testing.T) {
b := bytes.NewBuffer(nil)
stdout = b
IssueCommand("hello", map[string]string{
"some": "a\n\rvalue,];",
"some": "a\n\rvalue,:%",
"other": "value",
}, "some\r\nmessage")
assert.Equal(t, "::hello some=a%0A%0Dvalue%2C%5D%3B,other=value::some%0D%0Amessage\n", b.String())
}, "some\r\n%message")
assert.Contains(t, b.String(), "some=a%0A%0Dvalue%2C%3A%25")
assert.Contains(t, b.String(), "other=value")
assert.Regexp(t, regexp.MustCompile("::some%0D%0A%25message\n$"), b.String())
assert.Regexp(t, regexp.MustCompile("^::hello "), b.String())
assert.Len(t, strings.Split(b.String(), ","), 2)

}

0 comments on commit 5ece5c4

Please sign in to comment.