diff --git a/server/channels/app/webhook.go b/server/channels/app/webhook.go index 3f6711149e..b804b6b97e 100644 --- a/server/channels/app/webhook.go +++ b/server/channels/app/webhook.go @@ -264,7 +264,7 @@ func SplitWebhookPost(post *model.Post, maxPostSize int) ([]*model.Post, *model. func (a *App) CreateWebhookPost(c request.CTX, userID string, channel *model.Channel, text, overrideUsername, overrideIconURL, overrideIconEmoji string, props model.StringInterface, postType string, postRootId string) (*model.Post, *model.AppError) { // parse links into Markdown format - linkWithTextRegex := regexp.MustCompile(`<([^\n<\|>]+)\|([^\n>]+)>`) + linkWithTextRegex := regexp.MustCompile(`<([^\n<\|>]+)\|([^\|\n>]+)>`) text = linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})") post := &model.Post{UserId: userID, ChannelId: channel.Id, Message: text, Type: postType, RootId: postRootId} diff --git a/server/channels/app/webhook_test.go b/server/channels/app/webhook_test.go index 2ae65fe51b..0bca4d2042 100644 --- a/server/channels/app/webhook_test.go +++ b/server/channels/app/webhook_test.go @@ -382,6 +382,37 @@ Date: Thu Mar 1 19:46:48 2018 +0300 }) } +func TestCreateWebhookPostLinks(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = true }) + + hook, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, th.BasicChannel, &model.IncomingWebhook{ChannelId: th.BasicChannel.Id}) + require.Nil(t, err) + defer th.App.DeleteIncomingWebhook(hook.Id) + + for name, tc := range map[string]struct { + input string + expectedOutput string + }{ + "if statement": { + input: "`if(status_int < QUERY_UNKNOWN || status_int >= QUERY_STATUS_MAX)`", + expectedOutput: "`if(status_int < QUERY_UNKNOWN || status_int >= QUERY_STATUS_MAX)`", + }, + "angle bracket link": { + input: "", + expectedOutput: "[Mattermost](https://mattermost.com)", + }, + } { + t.Run(name, func(t *testing.T) { + post, err := th.App.CreateWebhookPost(th.Context, hook.UserId, th.BasicChannel, tc.input, "", "", "", model.StringInterface{}, "", "") + require.Nil(t, err) + require.Equal(t, tc.expectedOutput, post.Message) + }) + } +} + func TestSplitWebhookPost(t *testing.T) { type TestCase struct { Post *model.Post