Read about our upcoming Code of Conduct on this issue

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

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

12
13
  cache_markdown_field :description, pipeline: :single_line

14
  DEFAULT_COLOR = '#428BCA'
15

Douwe Maan's avatar
Douwe Maan committed
16
17
  default_value_for :color, DEFAULT_COLOR

18
  belongs_to :project
19
20

  has_many :lists, dependent: :destroy
21
  has_many :label_links, dependent: :destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
22
  has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
23
  has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
24

Robert Speicher's avatar
Robert Speicher committed
25
  validates :color, color: true, allow_blank: false
Valery Sizov's avatar
Valery Sizov committed
26
  validates :project, presence: true, unless: Proc.new { |service| service.template? }
27

28
  # Don't allow ',' for label titles
29
30
  validates :title,
            presence: true,
31
            format: { with: /\A[^,]+\z/ },
32
            uniqueness: { scope: :project_id }
33

Alfredo Sumaran's avatar
Alfredo Sumaran committed
34
  before_save :nullify_priority
Thijs Wouters's avatar
Thijs Wouters committed
35

36
  default_scope { order(title: :asc) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
37

Valery Sizov's avatar
Valery Sizov committed
38
  scope :templates, ->  { where(template: true) }
39

Alfredo Sumaran's avatar
Alfredo Sumaran committed
40
  def self.prioritized
41
    where.not(priority: nil).reorder(:priority, :title)
Alfredo Sumaran's avatar
Alfredo Sumaran committed
42
  end
43

Alfredo Sumaran's avatar
Alfredo Sumaran committed
44
  def self.unprioritized
45
    where(priority: nil)
Thijs Wouters's avatar
Thijs Wouters committed
46
47
  end

Robert Schilling's avatar
Robert Schilling committed
48
  alias_attribute :name, :title
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
49

50
51
52
53
  def self.reference_prefix
    '~'
  end

54
  ##
55
  # Pattern used to extract label references from text
56
57
58
  #
  # This pattern supports cross-project references.
  #
59
  def self.reference_pattern
60
61
62
    # NOTE: The id pattern only matches when all characters on the expression
    # are digits, so it will match ~2 but not ~2fa because that's probably a
    # label name and we want it to be matched as such.
63
    @reference_pattern ||= %r{
64
65
      (#{Project.reference_pattern})?
      #{Regexp.escape(reference_prefix)}
66
      (?:
67
        (?<label_id>\d+(?!\S\w)\b) | # Integer-based label ID, or
68
        (?<label_name>
69
          [A-Za-z0-9_\-\?\.&]+ | # String-based single-word label title, or
70
          ".+?"                  # String-based multi-word label surrounded in quotes
71
72
73
74
75
        )
      )
    }x
  end

76
77
78
79
  def self.link_reference_pattern
    nil
  end

80
  ##
81
82
83
84
85
86
  # Returns the String necessary to reference this Label in Markdown
  #
  # format - Symbol format to use (default: :id, optional: :name)
  #
  # Examples:
  #
87
88
  #   Label.first.to_reference                # => "~1"
  #   Label.first.to_reference(format: :name) # => "~\"bug\""
89
  #   Label.first.to_reference(project)       # => "gitlab-org/gitlab-ce~1"
90
91
  #
  # Returns a String
92
  #
93
  def to_reference(from_project = nil, format: :id)
94
95
    format_reference = label_format_reference(format)
    reference = "#{self.class.reference_prefix}#{format_reference}"
96
97
98

    if cross_project_reference?(from_project)
      project.to_reference + reference
99
    else
100
      reference
101
102
103
    end
  end

104
105
  def open_issues_count(user = nil)
    issues.visible_to_user(user).opened.count
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
106
  end
Valery Sizov's avatar
Valery Sizov committed
107

108
109
  def closed_issues_count(user = nil)
    issues.visible_to_user(user).closed.count
110
111
  end

112
113
114
115
  def open_merge_requests_count
    merge_requests.opened.count
  end

Valery Sizov's avatar
Valery Sizov committed
116
117
118
  def template?
    template
  end
119

Phil Hughes's avatar
Phil Hughes committed
120
121
122
123
  def text_color
    LabelsHelper::text_color_for_bg(self.color)
  end

124
  def title=(value)
125
    write_attribute(:title, sanitize_title(value)) if value.present?
126
127
  end

128
129
130
131
132
133
  private

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

    if format == :name && !name.include?('"')
134
      %("#{name}")
135
    else
136
      id
137
138
    end
  end
Thijs Wouters's avatar
Thijs Wouters committed
139

Alfredo Sumaran's avatar
Alfredo Sumaran committed
140
  def nullify_priority
141
    self.priority = nil if priority.blank?
Thijs Wouters's avatar
Thijs Wouters committed
142
  end
143
144

  def sanitize_title(value)
145
    CGI.unescapeHTML(Sanitize.clean(value.to_s))
146
  end
147
end