mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-17 13:05:27 +00:00
Sanitize strings in table output (#4466)
This commit is contained in:
parent
7109f3c6d1
commit
7cedda7387
2 changed files with 14 additions and 8 deletions
|
@ -147,12 +147,12 @@ func (o *Table) Write(columns []string, obj any) error {
|
||||||
colName := strings.ToLower(col)
|
colName := strings.ToLower(col)
|
||||||
if alias, ok := o.fieldAlias[colName]; ok {
|
if alias, ok := o.fieldAlias[colName]; ok {
|
||||||
if fn, ok := o.fieldMapping[alias]; ok {
|
if fn, ok := o.fieldMapping[alias]; ok {
|
||||||
out = append(out, fn(obj))
|
out = append(out, sanitizeString(fn(obj)))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if fn, ok := o.fieldMapping[colName]; ok {
|
if fn, ok := o.fieldMapping[colName]; ok {
|
||||||
out = append(out, fn(obj))
|
out = append(out, sanitizeString(fn(obj)))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if value, ok := dataL[strings.ReplaceAll(colName, "_", "")]; ok {
|
if value, ok := dataL[strings.ReplaceAll(colName, "_", "")]; ok {
|
||||||
|
@ -165,10 +165,10 @@ func (o *Table) Write(columns []string, obj any) error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if s, ok := value.(string); ok {
|
if s, ok := value.(string); ok {
|
||||||
out = append(out, NA(s))
|
out = append(out, NA(sanitizeString(s)))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
out = append(out, fmt.Sprintf("%v", value))
|
out = append(out, sanitizeString(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_, _ = fmt.Fprintln(o.w, strings.Join(out, "\t"))
|
_, _ = fmt.Fprintln(o.w, strings.Join(out, "\t"))
|
||||||
|
@ -201,3 +201,9 @@ func fieldName(name string) string {
|
||||||
}
|
}
|
||||||
return string(out)
|
return string(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sanitizeString(value any) string {
|
||||||
|
str := fmt.Sprintf("%v", value)
|
||||||
|
replacer := strings.NewReplacer("\n", " ", "\r", " ")
|
||||||
|
return strings.TrimSpace(replacer.Replace(str))
|
||||||
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ func TestPipelineOutput(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "table output with default columns",
|
name: "table output with default columns",
|
||||||
args: []string{},
|
args: []string{},
|
||||||
expected: "NUMBER STATUS EVENT BRANCH MESSAGE AUTHOR\n1 success push main message John Doe\n",
|
expected: "NUMBER STATUS EVENT BRANCH MESSAGE AUTHOR\n1 success push main message multiline John Doe\n",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "table output with custom columns",
|
name: "table output with custom columns",
|
||||||
|
@ -33,7 +33,7 @@ func TestPipelineOutput(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "table output with no header",
|
name: "table output with no header",
|
||||||
args: []string{"output", "--output-no-headers"},
|
args: []string{"output", "--output-no-headers"},
|
||||||
expected: "1 success push main message John Doe\n",
|
expected: "1 success push main message multiline John Doe\n",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "go-template output",
|
name: "go-template output",
|
||||||
|
@ -53,8 +53,8 @@ func TestPipelineOutput(t *testing.T) {
|
||||||
Status: "success",
|
Status: "success",
|
||||||
Event: "push",
|
Event: "push",
|
||||||
Branch: "main",
|
Branch: "main",
|
||||||
Message: "message",
|
Message: "message\nmultiline",
|
||||||
Author: "John Doe",
|
Author: "John Doe\n",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue