Commit 79b0cc0d authored by Balasankar "Balu" C's avatar Balasankar "Balu" C

Add branch specification to email service also

Signed-off-by: default avatarBalasankar "Balu" C <balasankar@gitlab.com>
parent 06011474
# frozen_string_literal: true # frozen_string_literal: true
class PipelinesEmailService < Service class PipelinesEmailService < Service
prop_accessor :recipients prop_accessor :recipients, :branches_to_be_notified
boolean_accessor :notify_only_broken_pipelines, :notify_only_default_branch boolean_accessor :notify_only_broken_pipelines, :notify_only_default_branch
validates :recipients, presence: true, if: :valid_recipients? validates :recipients, presence: true, if: :valid_recipients?
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
self.properties ||= { notify_only_broken_pipelines: true, notify_only_default_branch: false } if properties.nil?
self.properties = {}
self.notify_only_broken_pipelines = 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
def title def title
...@@ -55,8 +75,9 @@ class PipelinesEmailService < Service ...@@ -55,8 +75,9 @@ class PipelinesEmailService < Service
required: true }, required: true },
{ type: 'checkbox', { type: 'checkbox',
name: 'notify_only_broken_pipelines' }, name: 'notify_only_broken_pipelines' },
{ type: 'checkbox', { type: 'select',
name: 'notify_only_default_branch' } name: 'branches_to_be_notified',
choices: BRANCH_CHOICES }
] ]
end end
...@@ -73,9 +94,21 @@ class PipelinesEmailService < Service ...@@ -73,9 +94,21 @@ class PipelinesEmailService < Service
end end
def notify_for_pipeline_branch?(data) def notify_for_pipeline_branch?(data)
return true unless notify_only_default_branch? ref = if data[:ref]
Gitlab::Git.ref_name(data[:ref])
else
data.dig(:object_attributes, :ref)
end
data[:object_attributes][:ref] == data[:project][:default_branch] if branches_to_be_notified == "all"
true
elsif %w[default default_and_protected].include?(branches_to_be_notified)
ref == project.default_branch
elsif %w[protected default_and_protected].include?(branches_to_be_notified)
project.protected_branches.exists?(name: ref)
else
false
end
end end
def notify_for_pipeline?(data) def notify_for_pipeline?(data)
......
...@@ -540,9 +540,9 @@ module API ...@@ -540,9 +540,9 @@ module API
}, },
{ {
required: false, required: false,
name: :notify_only_default_branch, name: :branches_to_be_notified,
type: Boolean, type: String,
desc: 'Send notifications only for the default branch' desc: 'Branches for which notifications are to be sent'
} }
], ],
'pivotaltracker' => [ 'pivotaltracker' => [
......
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