Commit fed319b4 authored by Z.J. van de Weg's avatar Z.J. van de Weg

Allow chat notifications only for the default branch

Right now, it once  a chat notifacation service has been enabled, there
is no way to limit the branches which trigger a notification. Instead of
allowing the user to specify the list, I opted to let the user check the
box if they'd only want to be notified of the default branch.

Tags are uneffected by this change.
parent 3574963b
......@@ -6,7 +6,7 @@ class ChatNotificationService < Service
default_value_for :category, 'chat'
prop_accessor :webhook, :username, :channel
boolean_accessor :notify_only_broken_pipelines
boolean_accessor :notify_only_broken_pipelines, :notify_only_default_branch
validates :webhook, presence: true, url: true, if: :activated?
......@@ -17,6 +17,7 @@ class ChatNotificationService < Service
if properties.nil?
self.properties = {}
self.notify_only_broken_pipelines = true
self.notify_only_default_branch = false
end
end
......@@ -29,6 +30,19 @@ class ChatNotificationService < Service
pipeline wiki_page]
end
def fields
default_fields + build_event_channels
end
def default_fields
[
{ type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}" },
{ type: 'text', name: 'username', placeholder: 'e.g. GitLab' },
{ type: 'checkbox', name: 'notify_only_broken_pipelines' },
{ type: 'checkbox', name: 'notify_only_default_branch' },
]
end
def execute(data)
return unless supported_events.include?(data[:object_kind])
return unless webhook.present?
......@@ -123,6 +137,20 @@ class ChatNotificationService < Service
end
def should_pipeline_be_notified?(data)
notify_for_branch(data) && notify_for_pipeline(data)
end
def notify_for_branch(data)
ref_type = data[:object_attributes][:tag] ? 'tag' : 'branch'
if ref_type == 'branch' && notify_only_default_branch
data[:object_attributes][:ref] == project.default_branch
else
true
end
end
def notify_for_pipeline(data)
case data[:object_attributes][:status]
when 'success'
!notify_only_broken_pipelines?
......
......@@ -22,19 +22,11 @@ class MattermostService < ChatNotificationService
</ol>'
end
def fields
default_fields + build_event_channels
end
def default_fields
[
{ type: 'text', name: 'webhook', placeholder: 'e.g. http://mattermost_host/hooks/…' },
{ type: 'text', name: 'username', placeholder: 'e.g. GitLab' },
{ type: 'checkbox', name: 'notify_only_broken_pipelines' },
]
end
def default_channel_placeholder
"Channel handle (e.g. town-square)"
end
def webhook_placeholder
'http://mattermost.example.com/hooks/…'
end
end
......@@ -21,19 +21,11 @@ class SlackService < ChatNotificationService
</ol>'
end
def fields
default_fields + build_event_channels
end
def default_fields
[
{ type: 'text', name: 'webhook', placeholder: 'e.g. https://hooks.slack.com/services/…' },
{ type: 'text', name: 'username', placeholder: 'e.g. GitLab' },
{ type: 'checkbox', name: 'notify_only_broken_pipelines' },
]
end
def default_channel_placeholder
"Channel name (e.g. general)"
end
def webhook_placeholder
'https://hooks.slack.com/services/…'
end
end
---
title: Only send chat notifications for the default branch
merge_request:
author:
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