Commit 06011474 authored by Balasankar "Balu" C's avatar Balasankar "Balu" C

Add setting to specify branches for which notifications should be fired

Signed-off-by: default avatarBalasankar "Balu" C <balasankar@gitlab.com>
parent 01e12b05
...@@ -10,6 +10,7 @@ module ServiceParams ...@@ -10,6 +10,7 @@ module ServiceParams
:api_url, :api_url,
:api_version, :api_version,
:bamboo_url, :bamboo_url,
:branches_to_be_notified,
:build_key, :build_key,
:build_type, :build_type,
:ca_pem, :ca_pem,
...@@ -41,7 +42,6 @@ module ServiceParams ...@@ -41,7 +42,6 @@ module ServiceParams
:new_issue_url, :new_issue_url,
:notify, :notify,
:notify_only_broken_pipelines, :notify_only_broken_pipelines,
:notify_only_default_branch,
:password, :password,
:priority, :priority,
:project_key, :project_key,
......
...@@ -14,7 +14,7 @@ class ChatNotificationService < Service ...@@ -14,7 +14,7 @@ class ChatNotificationService < Service
default_value_for :category, 'chat' default_value_for :category, 'chat'
prop_accessor :webhook, :username, :channel prop_accessor :webhook, :username, :channel, :branches_to_be_notified
# Custom serialized properties initialization # Custom serialized properties initialization
prop_accessor(*SUPPORTED_EVENTS.map { |event| EVENT_CHANNEL[event] }) prop_accessor(*SUPPORTED_EVENTS.map { |event| EVENT_CHANNEL[event] })
...@@ -23,11 +23,27 @@ class ChatNotificationService < Service ...@@ -23,11 +23,27 @@ class ChatNotificationService < Service
validates :webhook, presence: true, public_url: true, if: :activated? validates :webhook, presence: true, public_url: true, if: :activated?
BRANCH_CHOICES = [
['All branches', 'all'],
['Default branch', 'default'],
['Protected branches', 'protected'],
['Default branch and protected branches', 'default_and_protected']
].freeze
def initialize_properties def initialize_properties
if properties.nil? if properties.nil?
self.properties = {} self.properties = {}
self.notify_only_broken_pipelines = true self.notify_only_broken_pipelines = true
self.notify_only_default_branch = true self.branches_to_be_notified = "default"
elsif !self.notify_only_default_branch.nil? && self.branches_to_be_notified.nil?
# In older versions, there was only a boolean property named
# `notify_only_default_branch`. Now we have a string property named
# `branches_to_be_notified`. Instead of doing a background migration, we
# opted to set a value for the new property based on the old one, if
# users hasn't specified one already. When users edit the service and
# selects a value for this new property, it will override everything.
self.branches_to_be_notified = notify_only_default_branch? ? "default" : "all"
end end
end end
...@@ -52,7 +68,7 @@ class ChatNotificationService < Service ...@@ -52,7 +68,7 @@ class ChatNotificationService < Service
{ type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}", required: true }, { type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}", required: true },
{ type: 'text', name: 'username', placeholder: 'e.g. GitLab' }, { type: 'text', name: 'username', placeholder: 'e.g. GitLab' },
{ type: 'checkbox', name: 'notify_only_broken_pipelines' }, { type: 'checkbox', name: 'notify_only_broken_pipelines' },
{ type: 'checkbox', name: 'notify_only_default_branch' } { type: 'select', name: 'branches_to_be_notified', choices: BRANCH_CHOICES }
] ]
end end
...@@ -168,7 +184,6 @@ class ChatNotificationService < Service ...@@ -168,7 +184,6 @@ class ChatNotificationService < Service
def notify_for_ref?(data) def notify_for_ref?(data)
return true if data[:object_kind] == 'tag_push' return true if data[:object_kind] == 'tag_push'
return true if data.dig(:object_attributes, :tag) return true if data.dig(:object_attributes, :tag)
return true unless notify_only_default_branch?
ref = if data[:ref] ref = if data[:ref]
Gitlab::Git.ref_name(data[:ref]) Gitlab::Git.ref_name(data[:ref])
...@@ -176,7 +191,20 @@ class ChatNotificationService < Service ...@@ -176,7 +191,20 @@ class ChatNotificationService < Service
data.dig(:object_attributes, :ref) data.dig(:object_attributes, :ref)
end end
ref == project.default_branch is_default_branch = ref == project.default_branch
is_protected_branch = project.protected_branches.exists?(name: ref)
if branches_to_be_notified == "all"
true
elsif branches_to_be_notified == "default"
is_default_branch
elsif branches_to_be_notified == "protected"
is_protected_branch
elsif branches_to_be_notified == "default_and_protected"
is_default_branch || is_protected_branch
else
false
end
end end
def notify_for_pipeline?(data) def notify_for_pipeline?(data)
......
...@@ -42,7 +42,7 @@ class DiscordService < ChatNotificationService ...@@ -42,7 +42,7 @@ class DiscordService < ChatNotificationService
[ [
{ type: "text", name: "webhook", placeholder: "e.g. https://discordapp.com/api/webhooks/…" }, { type: "text", name: "webhook", placeholder: "e.g. https://discordapp.com/api/webhooks/…" },
{ type: "checkbox", name: "notify_only_broken_pipelines" }, { type: "checkbox", name: "notify_only_broken_pipelines" },
{ type: "checkbox", name: "notify_only_default_branch" } { type: 'select', name: 'branches_to_be_notified', choices: BRANCH_CHOICES }
] ]
end end
......
...@@ -44,7 +44,7 @@ class HangoutsChatService < ChatNotificationService ...@@ -44,7 +44,7 @@ class HangoutsChatService < ChatNotificationService
[ [
{ type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}" }, { type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}" },
{ type: 'checkbox', name: 'notify_only_broken_pipelines' }, { type: 'checkbox', name: 'notify_only_broken_pipelines' },
{ type: 'checkbox', name: 'notify_only_default_branch' } { type: 'select', name: 'branches_to_be_notified', choices: BRANCH_CHOICES }
] ]
end end
......
...@@ -42,7 +42,7 @@ class MicrosoftTeamsService < ChatNotificationService ...@@ -42,7 +42,7 @@ class MicrosoftTeamsService < ChatNotificationService
[ [
{ type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}" }, { type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}" },
{ type: 'checkbox', name: 'notify_only_broken_pipelines' }, { type: 'checkbox', name: 'notify_only_broken_pipelines' },
{ type: 'checkbox', name: 'notify_only_default_branch' } { type: 'select', name: 'branches_to_be_notified', choices: BRANCH_CHOICES }
] ]
end end
......
---
title: Support chat notifications to be fired for protected branches
merge_request: 32176
author:
type: added
...@@ -27,6 +27,12 @@ module API ...@@ -27,6 +27,12 @@ module API
name: :channel, name: :channel,
type: String, type: String,
desc: 'The default chat channel' desc: 'The default chat channel'
},
{
required: false,
name: :branches_to_be_notified,
type: String,
desc: 'Branches for which notifications are to be sent'
} }
].freeze ].freeze
end end
...@@ -38,12 +44,6 @@ module API ...@@ -38,12 +44,6 @@ module API
name: :notify_only_broken_pipelines, name: :notify_only_broken_pipelines,
type: Boolean, type: Boolean,
desc: 'Send notifications for broken pipelines' desc: 'Send notifications for broken pipelines'
},
{
required: false,
name: :notify_only_default_branch,
type: Boolean,
desc: 'Send notifications only for the default branch'
} }
].freeze ].freeze
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