BigW Consortium Gitlab

label.rb 3.27 KB
Newer Older
1
class Label < ActiveRecord::Base
2
  include Referable
3 4
  include Subscribable

5 6
  # Represents a "No Label" state used for filtering Issues and Merge
  # Requests that have no label assigned.
7 8
  LabelStruct = Struct.new(:title, :name)
  None = LabelStruct.new('No Label', 'No Label')
9
  Any = LabelStruct.new('Any Label', '')
10

11
  DEFAULT_COLOR = '#428BCA'
12

Douwe Maan committed
13 14
  default_value_for :color, DEFAULT_COLOR

15 16
  belongs_to :project
  has_many :label_links, dependent: :destroy
Dmitriy Zaporozhets committed
17
  has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
18
  has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
19

20
  validates :color, color: true, allow_blank: false
Valery Sizov committed
21
  validates :project, presence: true, unless: Proc.new { |service| service.template? }
22

23
  # Don't allow '?', '&', and ',' for label titles
24 25
  validates :title,
            presence: true,
26
            format: { with: /\A[^&\?,]+\z/ },
27
            uniqueness: { scope: :project_id }
28

Alfredo Sumaran committed
29
  before_save :nullify_priority
Thijs Wouters committed
30

31
  default_scope { order(title: :asc) }
32

Valery Sizov committed
33
  scope :templates, ->  { where(template: true) }
34

35
  def self.prioritized
36
    where.not(priority: nil).reorder(:priority, :title)
37
  end
38

39
  def self.unprioritized
40
    where(priority: nil)
Thijs Wouters committed
41 42
  end

43
  alias_attribute :name, :title
Dmitriy Zaporozhets committed
44

45 46 47 48
  def self.reference_prefix
    '~'
  end

49
  ##
50
  # Pattern used to extract label references from text
51 52 53
  #
  # This pattern supports cross-project references.
  #
54
  def self.reference_pattern
55
    @reference_pattern ||= %r{
56 57
      (#{Project.reference_pattern})?
      #{Regexp.escape(reference_prefix)}
58
      (?:
59
        (?<label_id>\d+) | # Integer-based label ID, or
60
        (?<label_name>
61 62
          [A-Za-z0-9_-]+ | # String-based single-word label title, or
          "[^&\?,]+"       # String-based multi-word label surrounded in quotes
63 64 65 66 67
        )
      )
    }x
  end

68 69 70 71
  def self.link_reference_pattern
    nil
  end

72
  ##
73 74 75 76 77 78
  # Returns the String necessary to reference this Label in Markdown
  #
  # format - Symbol format to use (default: :id, optional: :name)
  #
  # Examples:
  #
79 80
  #   Label.first.to_reference                # => "~1"
  #   Label.first.to_reference(format: :name) # => "~\"bug\""
81
  #   Label.first.to_reference(project)       # => "gitlab-org/gitlab-ce~1"
82 83
  #
  # Returns a String
84
  #
85
  def to_reference(from_project = nil, format: :id)
86 87
    format_reference = label_format_reference(format)
    reference = "#{self.class.reference_prefix}#{format_reference}"
88 89 90

    if cross_project_reference?(from_project)
      project.to_reference + reference
91
    else
92
      reference
93 94 95
    end
  end

96 97
  def open_issues_count(user = nil)
    issues.visible_to_user(user).opened.count
Dmitriy Zaporozhets committed
98
  end
Valery Sizov committed
99

100 101
  def closed_issues_count(user = nil)
    issues.visible_to_user(user).closed.count
102 103
  end

104 105 106 107
  def open_merge_requests_count
    merge_requests.opened.count
  end

Valery Sizov committed
108 109 110
  def template?
    template
  end
111

112 113 114 115
  def text_color
    LabelsHelper::text_color_for_bg(self.color)
  end

116 117
  def title=(value)
    write_attribute(:title, Sanitize.clean(value.to_s)) if value.present?
118 119
  end

120 121 122 123 124 125
  private

  def label_format_reference(format = :id)
    raise StandardError, 'Unknown format' unless [:id, :name].include?(format)

    if format == :name && !name.include?('"')
126
      %("#{name}")
127
    else
128
      id
129 130
    end
  end
Thijs Wouters committed
131

Alfredo Sumaran committed
132
  def nullify_priority
133
    self.priority = nil if priority.blank?
Thijs Wouters committed
134
  end
135
end