Commit bc40efa1 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #6705 from razielgn/slack-service-attachments

Improved Slack integration with message attachments.
parents 3740597f 6a9eb4b2
...@@ -11,10 +11,16 @@ class SlackMessage ...@@ -11,10 +11,16 @@ class SlackMessage
@username = params.fetch(:user_name) @username = params.fetch(:user_name)
end end
def compose def pretext
format(message) format(message)
end end
def attachments
return [] if new_branch? || removed_branch?
commit_message_attachments
end
private private
attr_reader :after attr_reader :after
...@@ -31,7 +37,7 @@ class SlackMessage ...@@ -31,7 +37,7 @@ class SlackMessage
elsif removed_branch? elsif removed_branch?
removed_branch_message removed_branch_message
else else
push_message << commit_messages push_message
end end
end end
...@@ -54,15 +60,20 @@ class SlackMessage ...@@ -54,15 +60,20 @@ class SlackMessage
def commit_messages def commit_messages
commits.each_with_object('') do |commit, str| commits.each_with_object('') do |commit, str|
str << compose_commit_message(commit) str << compose_commit_message(commit)
end.chomp
end end
def commit_message_attachments
[{ text: format(commit_messages), color: attachment_color }]
end end
def compose_commit_message(commit) def compose_commit_message(commit)
id = commit.fetch(:id)[0..5] author = commit.fetch(:author).fetch(:name)
id = commit.fetch(:id)[0..8]
message = commit.fetch(:message) message = commit.fetch(:message)
url = commit.fetch(:url) url = commit.fetch(:url)
"\n - #{message} ([#{id}](#{url}))" "[#{id}](#{url}): #{message} - #{author}\n"
end end
def new_branch? def new_branch?
...@@ -92,4 +103,8 @@ class SlackMessage ...@@ -92,4 +103,8 @@ class SlackMessage
def compare_link def compare_link
"[Compare changes](#{compare_url})" "[Compare changes](#{compare_url})"
end end
def attachment_color
'#345'
end
end end
...@@ -53,7 +53,7 @@ class SlackService < Service ...@@ -53,7 +53,7 @@ class SlackService < Service
notifier = Slack::Notifier.new(subdomain, token) notifier = Slack::Notifier.new(subdomain, token)
notifier.channel = room notifier.channel = room
notifier.username = 'GitLab' notifier.username = 'GitLab'
notifier.ping(message.compose) notifier.ping(message.pretext, attachments: message.attachments)
end end
private private
......
...@@ -14,20 +14,27 @@ describe SlackMessage do ...@@ -14,20 +14,27 @@ describe SlackMessage do
} }
} }
let(:color) { '#345' }
context 'push' do context 'push' do
before do before do
args[:commits] = [ args[:commits] = [
{ message: 'message1', url: 'url1', id: 'abcdefghi' }, { message: 'message1', url: 'url1', id: 'abcdefghijkl', author: { name: 'author1' } },
{ message: 'message2', url: 'url2', id: '123456789' }, { message: 'message2', url: 'url2', id: '123456789012', author: { name: 'author2' } },
] ]
end end
it 'returns a message regarding pushes' do it 'returns a message regarding pushes' do
subject.compose.should == subject.pretext.should ==
'user_name pushed to branch <url/commits/master|master> of ' << 'user_name pushed to branch <url/commits/master|master> of ' <<
'<url|project_name> (<url/compare/before...after|Compare changes>)' << '<url|project_name> (<url/compare/before...after|Compare changes>)'
"\n - message1 (<url1|abcdef>)" << subject.attachments.should == [
"\n - message2 (<url2|123456>)" {
text: "<url1|abcdefghi>: message1 - author1\n" <<
"<url2|123456789>: message2 - author2",
color: color,
}
]
end end
end end
...@@ -37,9 +44,10 @@ describe SlackMessage do ...@@ -37,9 +44,10 @@ describe SlackMessage do
end end
it 'returns a message regarding a new branch' do it 'returns a message regarding a new branch' do
subject.compose.should == subject.pretext.should ==
'user_name pushed new branch <url/commits/master|master> to ' << 'user_name pushed new branch <url/commits/master|master> to ' <<
'<url|project_name>' '<url|project_name>'
subject.attachments.should be_empty
end end
end end
...@@ -49,8 +57,9 @@ describe SlackMessage do ...@@ -49,8 +57,9 @@ describe SlackMessage do
end end
it 'returns a message regarding a removed branch' do it 'returns a message regarding a removed branch' do
subject.compose.should == subject.pretext.should ==
'user_name removed branch master from <url|project_name>' 'user_name removed branch master from <url|project_name>'
subject.attachments.should be_empty
end end
end end
end end
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