diff --git a/app/controllers/admin/services_controller.rb b/app/controllers/admin/services_controller.rb index 40938986a92..9d6287f3b61 100644 --- a/app/controllers/admin/services_controller.rb +++ b/app/controllers/admin/services_controller.rb @@ -1,4 +1,6 @@ class Admin::ServicesController < Admin::ApplicationController + include ServiceParams + before_action :service, only: [:edit, :update] def index @@ -37,18 +39,4 @@ class Admin::ServicesController < Admin::ApplicationController def service @service ||= Service.where(id: params[:id], template: true).first end - - def application_services_params - dynamic_params = [] - dynamic_params.concat(@service.event_channel_names) if @service.is_a?(SlackService) - - application_services_params = params.permit(:id, - service: Projects::ServicesController::ALLOWED_PARAMS + dynamic_params) - if application_services_params[:service].is_a?(Hash) - Projects::ServicesController::FILTER_BLANK_PARAMS.each do |param| - application_services_params[:service].delete(param) if application_services_params[:service][param].blank? - end - end - application_services_params - end end diff --git a/app/controllers/concerns/service_params.rb b/app/controllers/concerns/service_params.rb new file mode 100644 index 00000000000..a1c5cd28a27 --- /dev/null +++ b/app/controllers/concerns/service_params.rb @@ -0,0 +1,35 @@ +module ServiceParams + extend ActiveSupport::Concern + + ALLOWED_PARAMS = [:title, :token, :type, :active, :api_key, :api_url, :api_version, :subdomain, + :room, :recipients, :project_url, :webhook, + :user_key, :device, :priority, :sound, :bamboo_url, :username, :password, + :build_key, :server, :teamcity_url, :drone_url, :build_type, + :description, :issues_url, :new_issue_url, :restrict_to_branch, :channel, + :colorize_messages, :channels, + :push_events, :issues_events, :merge_requests_events, :tag_push_events, + :note_events, :build_events, :wiki_page_events, + :notify_only_broken_builds, :add_pusher, + :send_from_committer_email, :disable_diffs, :external_wiki_url, + :notify, :color, + :server_host, :server_port, :default_irc_uri, :enable_ssl_verification, + :jira_issue_transition_id] + + # Parameters to ignore if no value is specified + FILTER_BLANK_PARAMS = [:password] + + def application_services_params + dynamic_params = [] + dynamic_params.concat(@service.event_channel_names) + + application_services_params = params.permit(:id, service: ALLOWED_PARAMS + dynamic_params) + + if application_services_params[:service].is_a?(Hash) + FILTER_BLANK_PARAMS.each do |param| + application_services_params[:service].delete(param) if application_services_params[:service][param].blank? + end + end + + application_services_params + end +end diff --git a/app/controllers/projects/services_controller.rb b/app/controllers/projects/services_controller.rb index 80553e035f0..b0b66a9f599 100644 --- a/app/controllers/projects/services_controller.rb +++ b/app/controllers/projects/services_controller.rb @@ -1,20 +1,5 @@ class Projects::ServicesController < Projects::ApplicationController - ALLOWED_PARAMS = [:title, :token, :type, :active, :api_key, :api_url, :api_version, :subdomain, - :room, :recipients, :project_url, :webhook, - :user_key, :device, :priority, :sound, :bamboo_url, :username, :password, - :build_key, :server, :teamcity_url, :drone_url, :build_type, - :description, :issues_url, :new_issue_url, :restrict_to_branch, :channel, - :colorize_messages, :channels, - :push_events, :issues_events, :merge_requests_events, :tag_push_events, - :note_events, :build_events, :wiki_page_events, - :notify_only_broken_builds, :add_pusher, - :send_from_committer_email, :disable_diffs, :external_wiki_url, - :notify, :color, - :server_host, :server_port, :default_irc_uri, :enable_ssl_verification, - :jira_issue_transition_id] - - # Parameters to ignore if no value is specified - FILTER_BLANK_PARAMS = [:password] + include ServiceParams # Authorize before_action :authorize_admin_project! diff --git a/app/helpers/services_helper.rb b/app/helpers/services_helper.rb index 98753ab2c93..5d0f6e67c0c 100644 --- a/app/helpers/services_helper.rb +++ b/app/helpers/services_helper.rb @@ -2,7 +2,7 @@ module ServicesHelper def service_event_description(event) case event when "push" - "Webhook will triggered by a push to the repository" + "Webhook will be triggered by a push to the repository" when "tag_push" "Webhook will be triggered when a new tag is pushed to the repository" when "note" @@ -19,7 +19,7 @@ module ServicesHelper end def service_event_field_name(event) - event = event.pluralize if %w(merge_request issue).include?(event) + event = event.pluralize if %w[merge_request issue].include?(event) "#{event}_events" end end diff --git a/app/models/project_services/slack_service.rb b/app/models/project_services/slack_service.rb index a1f146ac05a..647188cc2ab 100644 --- a/app/models/project_services/slack_service.rb +++ b/app/models/project_services/slack_service.rb @@ -5,7 +5,7 @@ class SlackService < Service def initialize_properties # Custom serialized properties initialization - self.supported_events.each { |event| self.class.prop_accessor event_channel_name(event) } + self.supported_events.each { |event| self.class.prop_accessor(event_channel_name(event)) } if properties.nil? self.properties = {} @@ -36,7 +36,7 @@ class SlackService < Service [ { type: 'text', name: 'webhook', placeholder: 'https://hooks.slack.com/services/...' }, { type: 'text', name: 'username', placeholder: 'username' }, - { type: 'text', name: 'channel', placeholder: "#General" }, + { type: 'text', name: 'channel', placeholder: "#general" }, { type: 'checkbox', name: 'notify_only_broken_builds' }, ] @@ -99,18 +99,13 @@ class SlackService < Service def get_channel_field(event) field_name = event_channel_name(event) - self.send(field_name) + self.public_send(field_name) end def build_event_channels - channels = [] - - supported_events.each do |event| - channel_name = event_channel_name(event) - channels << { type: 'text', name: channel_name, placeholder: "#General" } + supported_events.reduce([]) do |channels, event| + channels << { type: 'text', name: event_channel_name(event), placeholder: "#general" } end - - channels end def event_channel_name(event) diff --git a/app/models/service.rb b/app/models/service.rb index 5432f8c7ab4..5e80b5e65d7 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -80,6 +80,10 @@ class Service < ActiveRecord::Base Gitlab::PushDataBuilder.build_sample(project, user) end + def event_channel_names + [] + end + def supported_events %w(push tag_push issue merge_request wiki_page) end diff --git a/doc/integration/slack.md b/doc/integration/slack.md index f6ba80f46d5..8dc6c4a24bb 100644 --- a/doc/integration/slack.md +++ b/doc/integration/slack.md @@ -26,14 +26,13 @@ After Slack is ready we need to setup GitLab. Here are the steps to achieve this 1. Navigate to Settings -> Services -> Slack -1. Pick the triggers you want to activate +1. Pick the triggers you want to activate and respective channel(#general by default). 1. Fill in your Slack details - Webhook: Paste the Webhook URL from the step above - Username: Fill this in if you want to change the username of the bot - - Channel: Fill this in if you want to change the channel where the messages will be posted - Mark it as active - + 1. Save your settings Have fun :) diff --git a/doc/project_services/img/slack_configuration.png b/doc/project_services/img/slack_configuration.png new file mode 100644 index 00000000000..0cc672c47c9 Binary files /dev/null and b/doc/project_services/img/slack_configuration.png differ diff --git a/doc/project_services/project_services.md b/doc/project_services/project_services.md index e15d5db3253..4442b7c1742 100644 --- a/doc/project_services/project_services.md +++ b/doc/project_services/project_services.md @@ -45,7 +45,7 @@ further configuration instructions and details. Contributions are welcome. | PivotalTracker | Project Management Software (Source Commits Endpoint) | | Pushover | Pushover makes it easy to get real-time notifications on your Android device, iPhone, iPad, and Desktop | | [Redmine](redmine.md) | Redmine issue tracker | -| Slack | A team communication tool for the 21st century | +| [Slack](slack.md) | A team communication tool for the 21st century | ## Services Templates diff --git a/doc/project_services/slack.md b/doc/project_services/slack.md new file mode 100644 index 00000000000..1503d9cd983 --- /dev/null +++ b/doc/project_services/slack.md @@ -0,0 +1,26 @@ +# Slack Service + +Go to your project's **Settings > Services > Slack** and you will see a checkbox with the following events that can be triggered: + +* Push +* Issue +* Merge request +* Note +* Tag push +* Build +* Wiki page + +Bellow each of these event checkboxes you will have a input to insert which Slack channel do you want to send that event message, +#general channel is default. + + +![Slack configuration](img/slack_configuration.png) + + +| Field | Description | +| ----- | ----------- | +| `Webhook` | The incoming webhook url which you have to setup on slack. (https://my.slack.com/services/new/incoming-webhook/) | +| `Username` | Optional username which can be on messages sent to slack. | +| `notify only broken builds` | Notify only about broken builds, when build events are marked to be sent.| + +