summaryrefslogtreecommitdiff
path: root/lib/gitlab_post_receive.rb
diff options
context:
space:
mode:
authorCameron Crockett <cameron@ccrockett.com>2018-05-14 23:22:28 -0500
committerCameron Crockett <cameron@ccrockett.com>2018-06-01 16:09:41 -0500
commitb0b8627d1e1ee0748f1468ce9489357dab482fbb (patch)
tree179448aaf0f7bf6f975ed7bb0cf261f5f8d89054 /lib/gitlab_post_receive.rb
parent4bc16881347b53709c0f28a3ac2ed3a96d7051b9 (diff)
downloadgitlab-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
Diffstat (limited to 'lib/gitlab_post_receive.rb')
-rw-r--r--lib/gitlab_post_receive.rb30
1 files changed, 26 insertions, 4 deletions
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