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

Sanitize strings in table output #4466

Merged
merged 2 commits into from
Nov 26, 2024
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
14 changes: 10 additions & 4 deletions cli/output/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@
colName := strings.ToLower(col)
if alias, ok := o.fieldAlias[colName]; ok {
if fn, ok := o.fieldMapping[alias]; ok {
out = append(out, fn(obj))
out = append(out, sanitizeString(fn(obj)))
continue
}
}
if fn, ok := o.fieldMapping[colName]; ok {
out = append(out, fn(obj))
out = append(out, sanitizeString(fn(obj)))

Check warning on line 155 in cli/output/table.go

View check run for this annotation

Codecov / codecov/patch

cli/output/table.go#L155

Added line #L155 was not covered by tests
continue
}
if value, ok := dataL[strings.ReplaceAll(colName, "_", "")]; ok {
Expand All @@ -165,10 +165,10 @@
continue
}
if s, ok := value.(string); ok {
out = append(out, NA(s))
out = append(out, NA(sanitizeString(s)))
continue
}
out = append(out, fmt.Sprintf("%v", value))
out = append(out, sanitizeString(value))
}
}
_, _ = fmt.Fprintln(o.w, strings.Join(out, "\t"))
Expand Down Expand Up @@ -201,3 +201,9 @@
}
return string(out)
}

func sanitizeString(value any) string {
str := fmt.Sprintf("%v", value)
replacer := strings.NewReplacer("\n", " ", "\r", " ")
return strings.TrimSpace(replacer.Replace(str))
}
8 changes: 4 additions & 4 deletions cli/pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestPipelineOutput(t *testing.T) {
{
name: "table output with default columns",
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",
Expand All @@ -33,7 +33,7 @@ func TestPipelineOutput(t *testing.T) {
{
name: "table output with no header",
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",
Expand All @@ -53,8 +53,8 @@ func TestPipelineOutput(t *testing.T) {
Status: "success",
Event: "push",
Branch: "main",
Message: "message",
Author: "John Doe",
Message: "message\nmultiline",
Author: "John Doe\n",
},
}

Expand Down