Commit e39bf837 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #8501 from AKoetsier/slack_channel_and_username

Allow a user to specify a channel and username for the slack-webhook
parents f84bd771 d2c85a68
......@@ -90,6 +90,7 @@ v 7.8.0
- Improve database performance for GitLab
- Add Asana service (Jeremy Benoist)
- Improve project web hooks with extra data
- Slack username and channel options
v 7.7.2
- Update GitLab Shell to version 2.4.2 that fixes a bug when developers can push to protected branch
......
......@@ -50,7 +50,7 @@ class Projects::ServicesController < Projects::ApplicationController
:room, :recipients, :project_url, :webhook,
:user_key, :device, :priority, :sound, :bamboo_url, :username, :password,
:build_key, :server, :teamcity_url, :build_type,
:description, :issues_url, :new_issue_url, :restrict_to_branch
:description, :issues_url, :new_issue_url, :restrict_to_branch, :channel
)
end
end
......@@ -14,7 +14,7 @@
#
class SlackService < Service
prop_accessor :webhook
prop_accessor :webhook, :username, :channel
validates :webhook, presence: true, if: :activated?
def title
......@@ -31,7 +31,10 @@ class SlackService < Service
def fields
[
{ type: 'text', name: 'webhook', placeholder: 'https://hooks.slack.com/services/...' }
{ type: 'text', name: 'webhook',
placeholder: 'https://hooks.slack.com/services/...' },
{ type: 'text', name: 'username', placeholder: 'username' },
{ type: 'text', name: 'channel', placeholder: '#channel' }
]
end
......@@ -43,7 +46,11 @@ class SlackService < Service
project_name: project_name
))
notifier = Slack::Notifier.new(webhook)
opt = {}
opt[:channel] = channel if channel
opt[:username] = username if username
notifier = Slack::Notifier.new(webhook, opt)
notifier.ping(message.pretext, attachments: message.attachments)
end
......
......@@ -36,6 +36,8 @@ describe SlackService do
let(:project) { create(:project) }
let(:sample_data) { Gitlab::PushDataBuilder.build_sample(project, user) }
let(:webhook_url) { 'https://hooks.slack.com/services/SVRWFV0VVAR97N/B02R25XN3/ZBqu7xMupaEEICInN685' }
let(:username) { 'slack_username' }
let(:channel) { 'slack_channel' }
before do
slack.stub(
......@@ -53,5 +55,25 @@ describe SlackService do
expect(WebMock).to have_requested(:post, webhook_url).once
end
it 'should use the username as an option for slack when configured' do
slack.stub(username: username)
expect(Slack::Notifier).to receive(:new).
with(webhook_url, username: username).
and_return(
double(:slack_service).as_null_object
)
slack.execute(sample_data)
end
it 'should use the channel as an option when it is configured' do
slack.stub(channel: channel)
expect(Slack::Notifier).to receive(:new).
with(webhook_url, channel: channel).
and_return(
double(:slack_service).as_null_object
)
slack.execute(sample_data)
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