Commit bc40efa19d499f603c80c77b1d1e291bd4373be6
Exists in
spb-stable
and in
3 other branches
Merge pull request #6705 from razielgn/slack-service-attachments
Improved Slack integration with message attachments.
Showing
3 changed files
with
38 additions
and
14 deletions
Show diff stats
app/models/project_services/slack_message.rb
| @@ -11,10 +11,16 @@ class SlackMessage | @@ -11,10 +11,16 @@ class SlackMessage | ||
| 11 | @username = params.fetch(:user_name) | 11 | @username = params.fetch(:user_name) |
| 12 | end | 12 | end |
| 13 | 13 | ||
| 14 | - def compose | 14 | + def pretext |
| 15 | format(message) | 15 | format(message) |
| 16 | end | 16 | end |
| 17 | 17 | ||
| 18 | + def attachments | ||
| 19 | + return [] if new_branch? || removed_branch? | ||
| 20 | + | ||
| 21 | + commit_message_attachments | ||
| 22 | + end | ||
| 23 | + | ||
| 18 | private | 24 | private |
| 19 | 25 | ||
| 20 | attr_reader :after | 26 | attr_reader :after |
| @@ -31,7 +37,7 @@ class SlackMessage | @@ -31,7 +37,7 @@ class SlackMessage | ||
| 31 | elsif removed_branch? | 37 | elsif removed_branch? |
| 32 | removed_branch_message | 38 | removed_branch_message |
| 33 | else | 39 | else |
| 34 | - push_message << commit_messages | 40 | + push_message |
| 35 | end | 41 | end |
| 36 | end | 42 | end |
| 37 | 43 | ||
| @@ -54,15 +60,20 @@ class SlackMessage | @@ -54,15 +60,20 @@ class SlackMessage | ||
| 54 | def commit_messages | 60 | def commit_messages |
| 55 | commits.each_with_object('') do |commit, str| | 61 | commits.each_with_object('') do |commit, str| |
| 56 | str << compose_commit_message(commit) | 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 | end | 68 | end |
| 59 | 69 | ||
| 60 | def compose_commit_message(commit) | 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 | message = commit.fetch(:message) | 73 | message = commit.fetch(:message) |
| 63 | url = commit.fetch(:url) | 74 | url = commit.fetch(:url) |
| 64 | 75 | ||
| 65 | - "\n - #{message} ([#{id}](#{url}))" | 76 | + "[#{id}](#{url}): #{message} - #{author}\n" |
| 66 | end | 77 | end |
| 67 | 78 | ||
| 68 | def new_branch? | 79 | def new_branch? |
| @@ -92,4 +103,8 @@ class SlackMessage | @@ -92,4 +103,8 @@ class SlackMessage | ||
| 92 | def compare_link | 103 | def compare_link |
| 93 | "[Compare changes](#{compare_url})" | 104 | "[Compare changes](#{compare_url})" |
| 94 | end | 105 | end |
| 106 | + | ||
| 107 | + def attachment_color | ||
| 108 | + '#345' | ||
| 109 | + end | ||
| 95 | end | 110 | end |
app/models/project_services/slack_service.rb
| @@ -53,7 +53,7 @@ class SlackService < Service | @@ -53,7 +53,7 @@ class SlackService < Service | ||
| 53 | notifier = Slack::Notifier.new(subdomain, token) | 53 | notifier = Slack::Notifier.new(subdomain, token) |
| 54 | notifier.channel = room | 54 | notifier.channel = room |
| 55 | notifier.username = 'GitLab' | 55 | notifier.username = 'GitLab' |
| 56 | - notifier.ping(message.compose) | 56 | + notifier.ping(message.pretext, attachments: message.attachments) |
| 57 | end | 57 | end |
| 58 | 58 | ||
| 59 | private | 59 | private |
spec/models/slack_message_spec.rb
| @@ -14,20 +14,27 @@ describe SlackMessage do | @@ -14,20 +14,27 @@ describe SlackMessage do | ||
| 14 | } | 14 | } |
| 15 | } | 15 | } |
| 16 | 16 | ||
| 17 | + let(:color) { '#345' } | ||
| 18 | + | ||
| 17 | context 'push' do | 19 | context 'push' do |
| 18 | before do | 20 | before do |
| 19 | args[:commits] = [ | 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 | end | 25 | end |
| 24 | 26 | ||
| 25 | it 'returns a message regarding pushes' do | 27 | it 'returns a message regarding pushes' do |
| 26 | - subject.compose.should == | 28 | + subject.pretext.should == |
| 27 | 'user_name pushed to branch <url/commits/master|master> of ' << | 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 | end | 38 | end |
| 32 | end | 39 | end |
| 33 | 40 | ||
| @@ -37,9 +44,10 @@ describe SlackMessage do | @@ -37,9 +44,10 @@ describe SlackMessage do | ||
| 37 | end | 44 | end |
| 38 | 45 | ||
| 39 | it 'returns a message regarding a new branch' do | 46 | it 'returns a message regarding a new branch' do |
| 40 | - subject.compose.should == | 47 | + subject.pretext.should == |
| 41 | 'user_name pushed new branch <url/commits/master|master> to ' << | 48 | 'user_name pushed new branch <url/commits/master|master> to ' << |
| 42 | '<url|project_name>' | 49 | '<url|project_name>' |
| 50 | + subject.attachments.should be_empty | ||
| 43 | end | 51 | end |
| 44 | end | 52 | end |
| 45 | 53 | ||
| @@ -49,8 +57,9 @@ describe SlackMessage do | @@ -49,8 +57,9 @@ describe SlackMessage do | ||
| 49 | end | 57 | end |
| 50 | 58 | ||
| 51 | it 'returns a message regarding a removed branch' do | 59 | it 'returns a message regarding a removed branch' do |
| 52 | - subject.compose.should == | 60 | + subject.pretext.should == |
| 53 | 'user_name removed branch master from <url|project_name>' | 61 | 'user_name removed branch master from <url|project_name>' |
| 62 | + subject.attachments.should be_empty | ||
| 54 | end | 63 | end |
| 55 | end | 64 | end |
| 56 | end | 65 | end |