From 68328a2b641382c6728749718fce4a6e981a0899 Mon Sep 17 00:00:00 2001 From: "Balasankar \"Balu\" C" Date: Fri, 13 Sep 2019 09:14:30 +0530 Subject: Make notification branch selection a concern Signed-off-by: Balasankar "Balu" C --- .../concerns/notification_branch_selection.rb | 39 ++++++++++++++++++++++ .../notification_branch_selection.rb | 36 -------------------- 2 files changed, 39 insertions(+), 36 deletions(-) create mode 100644 app/models/concerns/notification_branch_selection.rb delete mode 100644 app/models/project_services/notification_branch_selection.rb diff --git a/app/models/concerns/notification_branch_selection.rb b/app/models/concerns/notification_branch_selection.rb new file mode 100644 index 00000000000..d8e18de7551 --- /dev/null +++ b/app/models/concerns/notification_branch_selection.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +# Concern handling functionality around deciding whether to send notification +# for activities on a specified branch or not. Will be included in +# ChatNotificationService and PipelinesEmailService classes. +module NotificationBranchSelection + extend ActiveSupport::Concern + + 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) + + case branches_to_be_notified + when "all" + true + when "default" + is_default_branch + when "protected" + is_protected_branch + when "default_and_protected" + is_default_branch || is_protected_branch + else + false + end + end +end diff --git a/app/models/project_services/notification_branch_selection.rb b/app/models/project_services/notification_branch_selection.rb deleted file mode 100644 index 1fa6f3250d4..00000000000 --- a/app/models/project_services/notification_branch_selection.rb +++ /dev/null @@ -1,36 +0,0 @@ -# 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) - - case branches_to_be_notified - when "all" - true - when "default" - is_default_branch - when "protected" - is_protected_branch - when "default_and_protected" - is_default_branch || is_protected_branch - else - false - end - end -end -- cgit v1.2.1