diff options
author | Douwe Maan <douwe@gitlab.com> | 2015-02-07 17:00:21 +0100 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-02-09 23:38:42 +0100 |
commit | 1da7d54ee549ffd54ce8f8df37caeee30badcce4 (patch) | |
tree | cc58a19ce8482e054479a50309ea8aaebcc0db20 /lib | |
parent | 6d23be22d907ad8328169d36712ea36d36107b93 (diff) | |
download | gitlab-shell-1da7d54ee549ffd54ce8f8df37caeee30badcce4.tar.gz |
Print broadcast message if one is available.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab_net.rb | 9 | ||||
-rw-r--r-- | lib/gitlab_post_receive.rb | 41 |
2 files changed, 50 insertions, 0 deletions
diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb index c178927..9bfc0d5 100644 --- a/lib/gitlab_net.rb +++ b/lib/gitlab_net.rb @@ -41,6 +41,15 @@ class GitlabNet JSON.parse(resp.body) rescue nil end + def broadcast_message + resp = get("#{host}/broadcast_message") + if resp.code == '200' + JSON.parse(resp.body) rescue nil + else + nil + end + end + def check get("#{host}/check") end 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]}) |