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

fix: Prevent error when using quotation marks in BuildMessage #75

Merged
merged 3 commits into from
Jul 17, 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
10 changes: 9 additions & 1 deletion cmd/vela-slack/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (p *Plugin) Exec() error {
// clean up newlines that could invalidate JSON
// BuildMessage is the only field that can have newlines;
// typically when the commit contains a title and body message
p.Env.BuildMessage = strings.Replace(p.Env.BuildMessage, "\n", "\\n", -1)
p.Env.BuildMessage = cleanBuildMessage(p.Env.BuildMessage)

// create message struct file Slack
msg := slack.WebhookMessage{
Expand Down Expand Up @@ -187,6 +187,14 @@ func (p *Plugin) Exec() error {
return nil
}

func cleanBuildMessage(buildMessage string) string {
subject := buildMessage
subject = strings.ReplaceAll(subject, "\n", "\\n")
subject = strings.ReplaceAll(subject, "\"", "\\\"")

return subject
}

// Validate function to validate plugin configuration.
func (p *Plugin) Validate() error {
logrus.Debug("validating plugin configuration")
Expand Down
23 changes: 23 additions & 0 deletions cmd/vela-slack/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,29 @@ Newlines`,
}
}

func TestSlack_Plugin_Exec_Quote(t *testing.T) {
// setup types
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "ok")
}))
defer ts.Close()

p := &Plugin{
Webhook: ts.URL,
Env: &Env{
BuildMessage: `This message has "quotes"`,
},
Path: "testdata/slack_attachment.json",
WebhookMsg: &slack.WebhookMessage{},
Remote: false,
}

err := p.Exec()
if err != nil {
t.Errorf("Exec returned err: %v", err)
}
}

func TestSlack_Plugin_Exec_Newline_Embedded(t *testing.T) {
// setup types
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down