Commit 2a86722a authored by Balasankar "Balu" C's avatar Balasankar "Balu" C

Extract notify_for_branch method to a general module

Signed-off-by: default avatarBalasankar "Balu" C <balasankar@gitlab.com>
parent 79b0cc0d
......@@ -4,6 +4,7 @@
# This class is not meant to be used directly, but only to inherit from.
class ChatNotificationService < Service
include ChatMessage
include NotificationBranchSelection
SUPPORTED_EVENTS = %w[
push issue confidential_issue merge_request note confidential_note
......@@ -23,13 +24,6 @@ class ChatNotificationService < Service
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
if properties.nil?
self.properties = {}
......@@ -185,26 +179,7 @@ class ChatNotificationService < Service
return true if data[:object_kind] == 'tag_push'
return true if data.dig(:object_attributes, :tag)
ref = if data[:ref]
Gitlab::Git.ref_name(data[:ref])
else
data.dig(:object_attributes, :ref)
end
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
notify_for_branch?(data)
end
def notify_for_pipeline?(data)
......
# frozen_string_literal: true
# This module will be included in ChatNotificationService and
# PipelinesEmailService classes. Not to be used directly.
module NotificationBranchSelection
BRANCH_CHOICES = [
['All branches', 'all'],
['Default branch', 'default'],
['Protected branches', 'protected'],
['Default branch and protected branches', 'default_and_protected']
].freeze
def notify_for_branch?(data)
ref = if data[:ref]
Gitlab::Git.ref_name(data[:ref])
else
data.dig(:object_attributes, :ref)
end
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
# frozen_string_literal: true
class PipelinesEmailService < Service
include NotificationBranchSelection
prop_accessor :recipients, :branches_to_be_notified
boolean_accessor :notify_only_broken_pipelines, :notify_only_default_branch
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
if properties.nil?
self.properties = {}
......@@ -90,25 +85,7 @@ class PipelinesEmailService < Service
end
def should_pipeline_be_notified?(data)
notify_for_pipeline_branch?(data) && notify_for_pipeline?(data)
end
def notify_for_pipeline_branch?(data)
ref = if data[:ref]
Gitlab::Git.ref_name(data[:ref])
else
data.dig(:object_attributes, :ref)
end
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
notify_for_branch?(data) && notify_for_pipeline?(data)
end
def notify_for_pipeline?(data)
......
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