Commit 6a15f1db authored by Douwe Maan's avatar Douwe Maan

Merge branch 'issue-29006' into 'master'

allow long strings to remain intact while parsing broadcast message

See merge request gitlab-org/gitlab-shell!202
parents 4bc16881 b0b8627d
v7.1.4
- Don't truncate long strings in broadcast message (!202)
v7.1.3 v7.1.3
- Use username instead of full name for identifying users (!204) - Use username instead of full name for identifying users (!204)
......
...@@ -75,10 +75,14 @@ class GitlabPostReceive ...@@ -75,10 +75,14 @@ class GitlabPostReceive
# Automatically wrap message at text_width (= 68) characters: # Automatically wrap message at text_width (= 68) characters:
# Splits the message up into the longest possible chunks matching # Splits the message up into the longest possible chunks matching
# "<between 0 and text_width characters><space or end-of-line>". # "<between 0 and text_width characters><space or end-of-line>".
# The last result is always an empty string (0 chars and the end-of-line),
# so drop that. msg_start_idx = 0
# message.scan returns a nested array of capture groups, so flatten. lines = []
lines = message.scan(/(.{,#{text_width}})(?:\s|$)/)[0...-1].flatten while msg_start_idx < message.length
parsed_line = parse_broadcast_msg(message[msg_start_idx..-1], text_width)
msg_start_idx += parsed_line.length
lines.push(parsed_line.strip)
end
puts puts
puts "=" * total_width puts "=" * total_width
...@@ -95,4 +99,22 @@ class GitlabPostReceive ...@@ -95,4 +99,22 @@ class GitlabPostReceive
puts puts
puts "=" * total_width puts "=" * total_width
end end
private
def parse_broadcast_msg(msg, text_length)
msg ||= ""
# just return msg if shorter than or equal to text length
return msg if msg.length <= text_length
# search for word break shorter than text length
truncate_to_space = msg.match(/\A(.{,#{text_length}})(?=\s|$)(\s*)/).to_s
if truncate_to_space.empty?
# search for word break longer than text length
truncate_to_space = msg.match(/\A\S+/).to_s
end
truncate_to_space
end
end end
...@@ -59,6 +59,43 @@ describe GitlabPostReceive do ...@@ -59,6 +59,43 @@ describe GitlabPostReceive do
expect(gitlab_post_receive.exec).to eq(true) expect(gitlab_post_receive.exec).to eq(true)
end end
context 'when contains long url string at end' do
let(:broadcast_message) { "test " * 10 + "message " * 10 + "https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url" }
it 'doesnt truncate url' do
expect_any_instance_of(GitlabNet).to receive(:post_receive).and_return(response)
assert_broadcast_message_printed_keep_long_url_end(gitlab_post_receive)
assert_new_mr_printed(gitlab_post_receive)
expect(gitlab_post_receive.exec).to eq(true)
end
end
context 'when contains long url string at start' do
let(:broadcast_message) { "https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url " + "test " * 10 + "message " * 11}
it 'doesnt truncate url' do
expect_any_instance_of(GitlabNet).to receive(:post_receive).and_return(response)
assert_broadcast_message_printed_keep_long_url_start(gitlab_post_receive)
assert_new_mr_printed(gitlab_post_receive)
expect(gitlab_post_receive.exec).to eq(true)
end
end
context 'when contains long url string in middle' do
let(:broadcast_message) { "test " * 11 + "https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url " + "message " * 11}
it 'doesnt truncate url' do
expect_any_instance_of(GitlabNet).to receive(:post_receive).and_return(response)
assert_broadcast_message_printed_keep_long_url_middle(gitlab_post_receive)
assert_new_mr_printed(gitlab_post_receive)
expect(gitlab_post_receive.exec).to eq(true)
end
end
end end
context 'when redirected message available' do context 'when redirected message available' do
...@@ -147,4 +184,86 @@ describe GitlabPostReceive do ...@@ -147,4 +184,86 @@ describe GitlabPostReceive do
def assert_project_created_message_printed(gitlab_post_receive) def assert_project_created_message_printed(gitlab_post_receive)
expect(gitlab_post_receive).to receive(:puts).with("This is a created project message") expect(gitlab_post_receive).to receive(:puts).with("This is a created project message")
end end
def assert_broadcast_message_printed_keep_long_url_end(gitlab_post_receive)
expect(gitlab_post_receive).to receive(:puts).ordered
expect(gitlab_post_receive).to receive(:puts).with(
"========================================================================"
).ordered
expect(gitlab_post_receive).to receive(:puts).ordered
expect(gitlab_post_receive).to receive(:puts).with(
" test test test test test test test test test test message message"
).ordered
expect(gitlab_post_receive).to receive(:puts).with(
" message message message message message message message message"
).ordered
expect(gitlab_post_receive).to receive(:puts).with(
"https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url"
).ordered
expect(gitlab_post_receive).to receive(:puts).ordered
expect(gitlab_post_receive).to receive(:puts).with(
"========================================================================"
).ordered
end
def assert_broadcast_message_printed_keep_long_url_start(gitlab_post_receive)
expect(gitlab_post_receive).to receive(:puts).ordered
expect(gitlab_post_receive).to receive(:puts).with(
"========================================================================"
).ordered
expect(gitlab_post_receive).to receive(:puts).ordered
expect(gitlab_post_receive).to receive(:puts).with(
"https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url"
).ordered
expect(gitlab_post_receive).to receive(:puts).with(
" test test test test test test test test test test message message"
).ordered
expect(gitlab_post_receive).to receive(:puts).with(
" message message message message message message message message"
).ordered
expect(gitlab_post_receive).to receive(:puts).with(
" message"
).ordered
expect(gitlab_post_receive).to receive(:puts).ordered
expect(gitlab_post_receive).to receive(:puts).with(
"========================================================================"
).ordered
end
def assert_broadcast_message_printed_keep_long_url_middle(gitlab_post_receive)
expect(gitlab_post_receive).to receive(:puts).ordered
expect(gitlab_post_receive).to receive(:puts).with(
"========================================================================"
).ordered
expect(gitlab_post_receive).to receive(:puts).ordered
expect(gitlab_post_receive).to receive(:puts).with(
" test test test test test test test test test test test"
).ordered
expect(gitlab_post_receive).to receive(:puts).with(
"https://localhost:5000/test/a/really/long/url/that/is/in/the/broadcast/message/do-not-truncate-when-url"
).ordered
expect(gitlab_post_receive).to receive(:puts).with(
" message message message message message message message message"
).ordered
expect(gitlab_post_receive).to receive(:puts).with(
" message message message"
).ordered
expect(gitlab_post_receive).to receive(:puts).ordered
expect(gitlab_post_receive).to receive(:puts).with(
"========================================================================"
).ordered
end
end end
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