diff options
author | Dmitriy Zaporozhets <dzaporozhets@gitlab.com> | 2015-02-10 18:12:28 +0000 |
---|---|---|
committer | Dmitriy Zaporozhets <dzaporozhets@gitlab.com> | 2015-02-10 18:12:28 +0000 |
commit | 9f3f7acaf7519247e43dbeb6540bccaa3e7a66f3 (patch) | |
tree | cc58a19ce8482e054479a50309ea8aaebcc0db20 /lib/gitlab_post_receive.rb | |
parent | 6d23be22d907ad8328169d36712ea36d36107b93 (diff) | |
parent | 1da7d54ee549ffd54ce8f8df37caeee30badcce4 (diff) | |
download | gitlab-shell-9f3f7acaf7519247e43dbeb6540bccaa3e7a66f3.tar.gz |
Merge branch 'broadcast-message' into 'master'
Print broadcast message if one is available
See gitlab/gitlab-ee#241.
Uses gitlab/gitlabhq!1486.
Text is centered automatically:
```
================================================================================
Maintenance window planned from 7AM to 9AM PST.
================================================================================
```
Longer text is wrapped automatically:
```
================================================================================
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
consectetur mi ac risus dapibus, sed rutrum eros elementum. Nunc id elit
justo. Duis sagittis, orci quis fringilla tempus, odio ipsum convallis
mauris, in porta quam nisi ac ante. Class aptent taciti sociosqu ad litora
torquent per conubia nostra, per inceptos himenaeos. Pellentesque habitant
morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed
sit amet blandit diam. Quisque euismod dolor in ligula euismod aliquet in
non libero. Aenean gravida ac augue vel laoreet. Phasellus vitae dictum
tellus. In euismod dui sed odio tempus, eget lacinia ante fermentum. Nullam
lacinia bibendum sollicitudin. Aenean et consequat felis. Curabitur libero
orci, varius ac ultrices laoreet, blandit eu neque. Nullam eget semper nunc,
id porta lorem. Phasellus at bibendum lorem, vitae sodales arcu.
================================================================================
```
Existing linebreaks are respected:
```
================================================================================
Planned maintenance windows:
February 8 7AM - 9AM
February 9 7AM - 8AM
================================================================================
```
See merge request !53
Diffstat (limited to 'lib/gitlab_post_receive.rb')
-rw-r--r-- | lib/gitlab_post_receive.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/gitlab_post_receive.rb b/lib/gitlab_post_receive.rb index b4770d9..7cd5535 100644 --- a/lib/gitlab_post_receive.rb +++ b/lib/gitlab_post_receive.rb @@ -1,4 +1,5 @@ require_relative 'gitlab_init' +require_relative 'gitlab_net' require 'json' class GitlabPostReceive @@ -16,10 +17,50 @@ class GitlabPostReceive ENV['GL_ID'] = nil update_redis + + if broadcast_message = GitlabNet.new.broadcast_message + puts + print_broadcast_message(broadcast_message["message"]) + end end protected + def print_broadcast_message(message) + # A standard terminal window is (at least) 80 characters wide. + total_width = 80 + + # Git prefixes remote messages with "remote: ", so this width is subtracted + # from the width available to us. + total_width -= "remote: ".length + + # Our centered text shouldn't start or end right at the edge of the window, + # so we add some horizontal padding: 2 chars on either side. + text_width = total_width - 2 * 2 + + # 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 + + puts "=" * total_width + puts + + lines.each do |line| + line.strip! + + # Center the line by calculating the left padding measured in characters. + line_padding = [(total_width - line.length) / 2, 0].max + puts (" " * line_padding) + line + end + + puts + puts "=" * total_width + end + def update_redis queue = "#{config.redis_namespace}:queue:post_receive" msg = JSON.dump({'class' => 'PostReceive', 'args' => [@repo_path, @actor, @changes]}) |