1# frozen_string_literal: true
2
3# Concern handling functionality around deciding whether to send notification
4# for activities on a specified branch or not. Will be included in
5# Integrations::BaseChatNotification and PipelinesEmailService classes.
6module NotificationBranchSelection
7  extend ActiveSupport::Concern
8
9  def branch_choices
10    [
11      [_('All branches'), 'all'].freeze,
12      [_('Default branch'), 'default'].freeze,
13      [_('Protected branches'), 'protected'].freeze,
14      [_('Default branch and protected branches'), 'default_and_protected'].freeze
15    ].freeze
16  end
17
18  def notify_for_branch?(data)
19    ref = if data[:ref]
20            Gitlab::Git.ref_name(data[:ref])
21          else
22            data.dig(:object_attributes, :ref)
23          end
24
25    is_default_branch = ref == project.default_branch
26    is_protected_branch = ProtectedBranch.protected?(project, ref)
27
28    case branches_to_be_notified
29    when "all"
30      true
31    when  "default"
32      is_default_branch
33    when  "protected"
34      is_protected_branch
35    when  "default_and_protected"
36      is_default_branch || is_protected_branch
37    else
38      false
39    end
40  end
41end
42