File: notification_branch_selection.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (43 lines) | stat: -rw-r--r-- 1,198 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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
# Integrations::BaseChatNotification and PipelinesEmailService classes.
module NotificationBranchSelection
  extend ActiveSupport::Concern

  class_methods do
    def branch_choices
      [
        [_('All branches'), 'all'].freeze,
        [_('Default branch'), 'default'].freeze,
        [_('Protected branches'), 'protected'].freeze,
        [_('Default branch and protected branches'), 'default_and_protected'].freeze
      ].freeze
    end
  end

  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 = ProtectedBranch.protected?(project, 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