diff options
author | Cameron Crockett <cameron@ccrockett.com> | 2018-05-14 23:22:28 -0500 |
---|---|---|
committer | Cameron Crockett <cameron@ccrockett.com> | 2018-06-01 16:09:41 -0500 |
commit | b0b8627d1e1ee0748f1468ce9489357dab482fbb (patch) | |
tree | 179448aaf0f7bf6f975ed7bb0cf261f5f8d89054 | |
parent | 4bc16881347b53709c0f28a3ac2ed3a96d7051b9 (diff) | |
download | gitlab-shell-b0b8627d1e1ee0748f1468ce9489357dab482fbb.tar.gz |
allow long strings to remain intact while parsing broadcast message
Added fix for msg nil edge case. fixed comment wording
code review issues, bumped version and changelog entry
Fixed rebase issues
Moved strip out of the function
Fixes for code review comments
Removed trailing whitespaces
-rw-r--r-- | CHANGELOG | 3 | ||||
-rw-r--r-- | VERSION | 2 | ||||
-rw-r--r-- | lib/gitlab_post_receive.rb | 30 | ||||
-rw-r--r-- | spec/gitlab_post_receive_spec.rb | 119 |
4 files changed, 149 insertions, 5 deletions
@@ -1,3 +1,6 @@ +v7.1.4 + - Don't truncate long strings in broadcast message (!202) + v7.1.3 - Use username instead of full name for identifying users (!204) @@ -1 +1 @@ -7.1.3 +7.1.4 diff --git a/lib/gitlab_post_receive.rb b/lib/gitlab_post_receive.rb index 6009b19..cb9931d 100644 --- a/lib/gitlab_post_receive.rb +++ b/lib/gitlab_post_receive.rb @@ -75,10 +75,14 @@ class GitlabPostReceive # Automatically wrap message at text_width (= 68) characters: # Splits the message up into the longest possible chunks matching # "<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. - # message.scan returns a nested array of capture groups, so flatten. - lines = message.scan(/(.{,#{text_width}})(?:\s|$)/)[0...-1].flatten + + msg_start_idx = 0 + lines = [] + 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 "=" * total_width @@ -95,4 +99,22 @@ class GitlabPostReceive puts puts "=" * total_width 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 diff --git a/spec/gitlab_post_receive_spec.rb b/spec/gitlab_post_receive_spec.rb index ec7e248..46e6158 100644 --- a/spec/gitlab_post_receive_spec.rb +++ b/spec/gitlab_post_receive_spec.rb @@ -59,6 +59,43 @@ describe GitlabPostReceive do expect(gitlab_post_receive.exec).to eq(true) 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 context 'when redirected message available' do @@ -147,4 +184,86 @@ describe GitlabPostReceive do def assert_project_created_message_printed(gitlab_post_receive) expect(gitlab_post_receive).to receive(:puts).with("This is a created project message") 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 |