summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dzaporozhets@gitlab.com>2015-02-10 18:12:28 +0000
committerDmitriy Zaporozhets <dzaporozhets@gitlab.com>2015-02-10 18:12:28 +0000
commit9f3f7acaf7519247e43dbeb6540bccaa3e7a66f3 (patch)
treecc58a19ce8482e054479a50309ea8aaebcc0db20
parent6d23be22d907ad8328169d36712ea36d36107b93 (diff)
parent1da7d54ee549ffd54ce8f8df37caeee30badcce4 (diff)
downloadgitlab-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
-rw-r--r--CHANGELOG3
-rw-r--r--lib/gitlab_net.rb9
-rw-r--r--lib/gitlab_post_receive.rb41
-rw-r--r--spec/gitlab_net_spec.rb20
-rw-r--r--spec/gitlab_post_receive_spec.rb3
-rw-r--r--spec/vcr_cassettes/broadcast_message-none.yml38
-rw-r--r--spec/vcr_cassettes/broadcast_message-ok.yml38
7 files changed, 152 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG
index c0311f9..14d42d9 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,6 @@
+v2.4.3
+ - Print broadcast message if one is available
+
v2.4.2
- Pass git changes list as string instead of array
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]})
diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb
index d431ac7..5e6e4de 100644
--- a/spec/gitlab_net_spec.rb
+++ b/spec/gitlab_net_spec.rb
@@ -44,6 +44,26 @@ describe GitlabNet, vcr: true do
end
end
+ describe :broadcast_message do
+ context "broadcast message exists" do
+ it 'should return message' do
+ VCR.use_cassette("broadcast_message-ok") do
+ result = gitlab_net.broadcast_message
+ result["message"].should == "Message"
+ end
+ end
+ end
+
+ context "broadcast message doesn't exist" do
+ it 'should return nil' do
+ VCR.use_cassette("broadcast_message-none") do
+ result = gitlab_net.broadcast_message
+ result.should == nil
+ end
+ end
+ end
+ end
+
describe :check_access do
context 'ssh key with access to project' do
it 'should allow pull access for dev.gitlab.org' do
diff --git a/spec/gitlab_post_receive_spec.rb b/spec/gitlab_post_receive_spec.rb
index 50c6f0a..dc84e4a 100644
--- a/spec/gitlab_post_receive_spec.rb
+++ b/spec/gitlab_post_receive_spec.rb
@@ -9,10 +9,13 @@ describe GitlabPostReceive do
before do
GitlabConfig.any_instance.stub(repos_path: repository_path)
+ Kernel.stub(system: true)
+ GitlabNet.any_instance.stub(broadcast_message: { "message" => "test " * 10 + "message " * 10 })
end
describe :initialize do
it { gitlab_post_receive.repo_path.should == repo_path }
it { gitlab_post_receive.changes.should == 'wow' }
+ it { gitlab_post_receive.exec }
end
end
diff --git a/spec/vcr_cassettes/broadcast_message-none.yml b/spec/vcr_cassettes/broadcast_message-none.yml
new file mode 100644
index 0000000..f94adc4
--- /dev/null
+++ b/spec/vcr_cassettes/broadcast_message-none.yml
@@ -0,0 +1,38 @@
+---
+http_interactions:
+- request:
+ method: get
+ uri: https://dev.gitlab.org/api/v3/internal/broadcast_message
+ body:
+ encoding: US-ASCII
+ string: secret_token=a123
+ headers:
+ Accept-Encoding:
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
+ Accept:
+ - "*/*"
+ User-Agent:
+ - Ruby
+ response:
+ status:
+ code: 404
+ message: Not Found
+ headers:
+ Server:
+ - nginx/1.1.19
+ Date:
+ - Sat, 07 Feb 2015 16:45:35 GMT
+ Content-Type:
+ - application/json
+ Content-Length:
+ - '27'
+ Connection:
+ - keep-alive
+ Status:
+ - 200 OK
+ body:
+ encoding: UTF-8
+ string: '{"message":"404 Not Found"}'
+ http_version:
+ recorded_at: Sat, 07 Feb 2015 16:45:35 GMT
+recorded_with: VCR 2.4.0
diff --git a/spec/vcr_cassettes/broadcast_message-ok.yml b/spec/vcr_cassettes/broadcast_message-ok.yml
new file mode 100644
index 0000000..470d988
--- /dev/null
+++ b/spec/vcr_cassettes/broadcast_message-ok.yml
@@ -0,0 +1,38 @@
+---
+http_interactions:
+- request:
+ method: get
+ uri: https://dev.gitlab.org/api/v3/internal/broadcast_message
+ body:
+ encoding: US-ASCII
+ string: secret_token=a123
+ headers:
+ Accept-Encoding:
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
+ Accept:
+ - "*/*"
+ User-Agent:
+ - Ruby
+ response:
+ status:
+ code: 200
+ message: OK
+ headers:
+ Server:
+ - nginx/1.1.19
+ Date:
+ - Sat, 07 Feb 2015 16:44:35 GMT
+ Content-Type:
+ - application/json
+ Content-Length:
+ - '118'
+ Connection:
+ - keep-alive
+ Status:
+ - 200 OK
+ body:
+ encoding: UTF-8
+ string: '{"message":"Message","starts_at":"2015-02-07T15:35:00.000Z","ends_at":"2015-02-07T16:35:00.000Z","color":"","font":""}'
+ http_version:
+ recorded_at: Sat, 07 Feb 2015 16:44:35 GMT
+recorded_with: VCR 2.4.0