Commit aef0d817 authored by Massimeddu Cireddu's avatar Massimeddu Cireddu Committed by Nick Thomas

Fixing #65389

Wrong format on MS Teams integration push events with multi line commit messages
parent c0e679e0
...@@ -10,6 +10,7 @@ module ChatMessage ...@@ -10,6 +10,7 @@ module ChatMessage
attr_reader :user_avatar attr_reader :user_avatar
attr_reader :project_name attr_reader :project_name
attr_reader :project_url attr_reader :project_url
attr_reader :commit_message_html
def initialize(params) def initialize(params)
@markdown = params[:markdown] || false @markdown = params[:markdown] || false
...@@ -18,6 +19,7 @@ module ChatMessage ...@@ -18,6 +19,7 @@ module ChatMessage
@user_full_name = params.dig(:user, :name) || params[:user_full_name] @user_full_name = params.dig(:user, :name) || params[:user_full_name]
@user_name = params.dig(:user, :username) || params[:user_name] @user_name = params.dig(:user, :username) || params[:user_name]
@user_avatar = params.dig(:user, :avatar_url) || params[:user_avatar] @user_avatar = params.dig(:user, :avatar_url) || params[:user_avatar]
@commit_message_html = params[:commit_message_html] || false
end end
def user_combined_name def user_combined_name
......
...@@ -52,7 +52,8 @@ module ChatMessage ...@@ -52,7 +52,8 @@ module ChatMessage
end end
def commit_messages def commit_messages
commits.map { |commit| compose_commit_message(commit) }.join("\n\n") linebreak_chars = commit_message_html ? "<br/>\n<br/>\n" : "\n\n"
commits.map { |commit| compose_commit_message(commit) }.join(linebreak_chars)
end end
def commit_message_attachments def commit_message_attachments
...@@ -63,6 +64,11 @@ module ChatMessage ...@@ -63,6 +64,11 @@ module ChatMessage
author = commit[:author][:name] author = commit[:author][:name]
id = Commit.truncate_sha(commit[:id]) id = Commit.truncate_sha(commit[:id])
message = commit[:message] message = commit[:message]
if commit_message_html
message = message.gsub(Gitlab::Regex.breakline_regex, "<br/>\n")
end
url = commit[:url] url = commit[:url]
"[#{id}](#{url}): #{message} - #{author}" "[#{id}](#{url}): #{message} - #{author}"
......
...@@ -58,6 +58,6 @@ class MicrosoftTeamsService < ChatNotificationService ...@@ -58,6 +58,6 @@ class MicrosoftTeamsService < ChatNotificationService
end end
def custom_data(data) def custom_data(data)
super(data).merge(markdown: true) super(data).merge(markdown: true, commit_message_html: true)
end end
end end
---
title: Wrong format on MS teams integration push events with multi line commit messages
merge_request: 32180
author: Massimeddu Cireddu
type: fixed
...@@ -46,7 +46,7 @@ module Gitlab ...@@ -46,7 +46,7 @@ module Gitlab
private private
def line_break_chars(line) def line_break_chars(line)
match = /\r\n|\r|\n/.match(line) match = Gitlab::Regex.breakline_regex.match(line)
match[0] if match match[0] if match
end end
end end
......
...@@ -115,5 +115,9 @@ module Gitlab ...@@ -115,5 +115,9 @@ module Gitlab
def jira_transition_id_regex def jira_transition_id_regex
@jira_transition_id_regex ||= /\d+/ @jira_transition_id_regex ||= /\d+/
end end
def breakline_regex
@breakline_regex ||= /\r\n|\r|\n/
end
end end
end end
...@@ -23,7 +23,7 @@ describe ChatMessage::PushMessage do ...@@ -23,7 +23,7 @@ describe ChatMessage::PushMessage do
before do before do
args[:commits] = [ args[:commits] = [
{ message: 'message1', url: 'http://url1.com', id: 'abcdefghijkl', author: { name: 'author1' } }, { message: 'message1', url: 'http://url1.com', id: 'abcdefghijkl', author: { name: 'author1' } },
{ message: 'message2', url: 'http://url2.com', id: '123456789012', author: { name: 'author2' } } { message: "message2\nsecondline", url: 'http://url2.com', id: '123456789012', author: { name: 'author2' } }
] ]
end end
...@@ -34,7 +34,7 @@ describe ChatMessage::PushMessage do ...@@ -34,7 +34,7 @@ describe ChatMessage::PushMessage do
'<http://url.com|project_name> (<http://url.com/compare/before...after|Compare changes>)') '<http://url.com|project_name> (<http://url.com/compare/before...after|Compare changes>)')
expect(subject.attachments).to eq([{ expect(subject.attachments).to eq([{
text: "<http://url1.com|abcdefgh>: message1 - author1\n\n"\ text: "<http://url1.com|abcdefgh>: message1 - author1\n\n"\
"<http://url2.com|12345678>: message2 - author2", "<http://url2.com|12345678>: message2\nsecondline - author2",
color: color color: color
}]) }])
end end
...@@ -49,7 +49,27 @@ describe ChatMessage::PushMessage do ...@@ -49,7 +49,27 @@ describe ChatMessage::PushMessage do
expect(subject.pretext).to eq( expect(subject.pretext).to eq(
'test.user pushed to branch [master](http://url.com/commits/master) of [project_name](http://url.com) ([Compare changes](http://url.com/compare/before...after))') 'test.user pushed to branch [master](http://url.com/commits/master) of [project_name](http://url.com) ([Compare changes](http://url.com/compare/before...after))')
expect(subject.attachments).to eq( expect(subject.attachments).to eq(
"[abcdefgh](http://url1.com): message1 - author1\n\n[12345678](http://url2.com): message2 - author2") "[abcdefgh](http://url1.com): message1 - author1\n\n[12345678](http://url2.com): message2\nsecondline - author2")
expect(subject.activity).to eq(
title: 'test.user pushed to branch [master](http://url.com/commits/master)',
subtitle: 'in [project_name](http://url.com)',
text: '[Compare changes](http://url.com/compare/before...after)',
image: 'http://someavatar.com'
)
end
end
context 'with markdown and commit message html' do
before do
args[:commit_message_html] = true
args[:markdown] = true
end
it 'returns a message regarding pushes' do
expect(subject.pretext).to eq(
'test.user pushed to branch [master](http://url.com/commits/master) of [project_name](http://url.com) ([Compare changes](http://url.com/compare/before...after))')
expect(subject.attachments).to eq(
"[abcdefgh](http://url1.com): message1 - author1<br/>\n<br/>\n[12345678](http://url2.com): message2<br/>\nsecondline - author2")
expect(subject.activity).to eq( expect(subject.activity).to eq(
title: 'test.user pushed to branch [master](http://url.com/commits/master)', title: 'test.user pushed to branch [master](http://url.com/commits/master)',
subtitle: 'in [project_name](http://url.com)', subtitle: 'in [project_name](http://url.com)',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment