Commit 6a9eb4b231fc7e563b9c3551dafc5d1bfdc959b3

Authored by Federico Ravasio
1 parent 2ceef769

Improved Slack integration with message attachments.

app/models/project_services/slack_message.rb
... ... @@ -11,10 +11,16 @@ class SlackMessage
11 11 @username = params.fetch(:user_name)
12 12 end
13 13  
14   - def compose
  14 + def pretext
15 15 format(message)
16 16 end
17 17  
  18 + def attachments
  19 + return [] if new_branch? || removed_branch?
  20 +
  21 + commit_message_attachments
  22 + end
  23 +
18 24 private
19 25  
20 26 attr_reader :after
... ... @@ -31,7 +37,7 @@ class SlackMessage
31 37 elsif removed_branch?
32 38 removed_branch_message
33 39 else
34   - push_message << commit_messages
  40 + push_message
35 41 end
36 42 end
37 43  
... ... @@ -54,15 +60,20 @@ class SlackMessage
54 60 def commit_messages
55 61 commits.each_with_object('') do |commit, str|
56 62 str << compose_commit_message(commit)
57   - end
  63 + end.chomp
  64 + end
  65 +
  66 + def commit_message_attachments
  67 + [{ text: format(commit_messages), color: attachment_color }]
58 68 end
59 69  
60 70 def compose_commit_message(commit)
61   - id = commit.fetch(:id)[0..5]
  71 + author = commit.fetch(:author).fetch(:name)
  72 + id = commit.fetch(:id)[0..8]
62 73 message = commit.fetch(:message)
63 74 url = commit.fetch(:url)
64 75  
65   - "\n - #{message} ([#{id}](#{url}))"
  76 + "[#{id}](#{url}): #{message} - #{author}\n"
66 77 end
67 78  
68 79 def new_branch?
... ... @@ -92,4 +103,8 @@ class SlackMessage
92 103 def compare_link
93 104 "[Compare changes](#{compare_url})"
94 105 end
  106 +
  107 + def attachment_color
  108 + '#345'
  109 + end
95 110 end
... ...
app/models/project_services/slack_service.rb
... ... @@ -53,7 +53,7 @@ class SlackService &lt; Service
53 53 notifier = Slack::Notifier.new(subdomain, token)
54 54 notifier.channel = room
55 55 notifier.username = 'GitLab'
56   - notifier.ping(message.compose)
  56 + notifier.ping(message.pretext, attachments: message.attachments)
57 57 end
58 58  
59 59 private
... ...
spec/models/slack_message_spec.rb
... ... @@ -14,20 +14,27 @@ describe SlackMessage do
14 14 }
15 15 }
16 16  
  17 + let(:color) { '#345' }
  18 +
17 19 context 'push' do
18 20 before do
19 21 args[:commits] = [
20   - { message: 'message1', url: 'url1', id: 'abcdefghi' },
21   - { message: 'message2', url: 'url2', id: '123456789' },
  22 + { message: 'message1', url: 'url1', id: 'abcdefghijkl', author: { name: 'author1' } },
  23 + { message: 'message2', url: 'url2', id: '123456789012', author: { name: 'author2' } },
22 24 ]
23 25 end
24 26  
25 27 it 'returns a message regarding pushes' do
26   - subject.compose.should ==
  28 + subject.pretext.should ==
27 29 'user_name pushed to branch <url/commits/master|master> of ' <<
28   - '<url|project_name> (<url/compare/before...after|Compare changes>)' <<
29   - "\n - message1 (<url1|abcdef>)" <<
30   - "\n - message2 (<url2|123456>)"
  30 + '<url|project_name> (<url/compare/before...after|Compare changes>)'
  31 + subject.attachments.should == [
  32 + {
  33 + text: "<url1|abcdefghi>: message1 - author1\n" <<
  34 + "<url2|123456789>: message2 - author2",
  35 + color: color,
  36 + }
  37 + ]
31 38 end
32 39 end
33 40  
... ... @@ -37,9 +44,10 @@ describe SlackMessage do
37 44 end
38 45  
39 46 it 'returns a message regarding a new branch' do
40   - subject.compose.should ==
  47 + subject.pretext.should ==
41 48 'user_name pushed new branch <url/commits/master|master> to ' <<
42 49 '<url|project_name>'
  50 + subject.attachments.should be_empty
43 51 end
44 52 end
45 53  
... ... @@ -49,8 +57,9 @@ describe SlackMessage do
49 57 end
50 58  
51 59 it 'returns a message regarding a removed branch' do
52   - subject.compose.should ==
  60 + subject.pretext.should ==
53 61 'user_name removed branch master from <url|project_name>'
  62 + subject.attachments.should be_empty
54 63 end
55 64 end
56 65 end
... ...