Commit 9f3f7aca authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

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
parents 6d23be22 1da7d54e
v2.4.3
- Print broadcast message if one is available
v2.4.2
- Pass git changes list as string instead of array
......
......@@ -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
......
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]})
......
......@@ -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
......
......@@ -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
---
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
---
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment