diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-03-02 15:17:56 -0800 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-03-02 15:17:56 -0800 |
commit | e39bf83770fe804112eef6e3d8651994d506a740 (patch) | |
tree | baf812929f49ee434fa35e62e2afa0ce4328253e | |
parent | f84bd771d4daa5753ad6a4b7e1a0c3cc9f51d33f (diff) | |
parent | d2c85a68bb763d2beccf8ebc0087791f6714c6de (diff) | |
download | gitlab-ce-e39bf83770fe804112eef6e3d8651994d506a740.tar.gz |
Merge pull request #8501 from AKoetsier/slack_channel_and_username
Allow a user to specify a channel and username for the slack-webhook
-rw-r--r-- | CHANGELOG | 1 | ||||
-rw-r--r-- | app/controllers/projects/services_controller.rb | 2 | ||||
-rw-r--r-- | app/models/project_services/slack_service.rb | 13 | ||||
-rw-r--r-- | spec/models/project_services/slack_service_spec.rb | 22 |
4 files changed, 34 insertions, 4 deletions
diff --git a/CHANGELOG b/CHANGELOG index 6bd93b8cd4b..c11ca604bb1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/app/controllers/projects/services_controller.rb b/app/controllers/projects/services_controller.rb index 5c29a6550f5..ce0f98bdeff 100644 --- a/app/controllers/projects/services_controller.rb +++ b/app/controllers/projects/services_controller.rb @@ -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 diff --git a/app/models/project_services/slack_service.rb b/app/models/project_services/slack_service.rb index 297d8bbb5d4..c7cbff63fe5 100644 --- a/app/models/project_services/slack_service.rb +++ b/app/models/project_services/slack_service.rb @@ -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 diff --git a/spec/models/project_services/slack_service_spec.rb b/spec/models/project_services/slack_service_spec.rb index 90b385423f1..8a75d8987ab 100644 --- a/spec/models/project_services/slack_service_spec.rb +++ b/spec/models/project_services/slack_service_spec.rb @@ -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 |