Class: Label

Direct Known Subclasses

GroupLabel, ProjectLabel

Constant Summary collapse

DESCRIPTION_LENGTH_MAX =
512.kilobytes

Constants included from BaseLabel

BaseLabel::DEFAULT_COLOR

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BatchNullifyDependentAssociations

#nullify_dependent_associations_in_batches

Methods included from Subscribable

#lazy_subscription, #set_subscription, #subscribe, #subscribed?, #subscribed_without_subscriptions?, #subscribers, #toggle_subscription, #unsubscribe

Methods included from Referable

#referable_inspect, #reference_link_text, #to_reference_base

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.ids_on_board(board_id) ⇒ Object



181
182
183
# File 'app/models/label.rb', line 181

def self.ids_on_board(board_id)
  on_board(board_id).pluck(:label_id)
end

.left_join_prioritiesObject



125
126
127
128
129
130
131
132
133
134
# File 'app/models/label.rb', line 125

def self.left_join_priorities
  labels = Label.arel_table
  priorities = LabelPriority.arel_table

  label_priorities = labels.join(priorities, Arel::Nodes::OuterJoin)
                            .on(labels[:id].eq(priorities[:label_id]))
                            .join_sources

  joins(label_priorities)
end


177
178
179
# File 'app/models/label.rb', line 177

def self.link_reference_pattern
  nil
end

.on_project_board?(project_id, label_id) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
188
189
# File 'app/models/label.rb', line 185

def self.on_project_board?(project_id, label_id)
  return false if label_id.blank?

  on_project_boards(project_id).where(id: label_id).exists?
end

.optionally_subscribed_by(user_id) ⇒ Object



136
137
138
139
140
141
142
# File 'app/models/label.rb', line 136

def self.optionally_subscribed_by(user_id)
  if user_id
    subscribed_by(user_id)
  else
    all
  end
end

.pluck_titlesObject



104
105
106
# File 'app/models/label.rb', line 104

def self.pluck_titles
  pluck(:title)
end

.prioritized(project) ⇒ Object



108
109
110
111
112
# File 'app/models/label.rb', line 108

def self.prioritized(project)
  joins(:priorities)
    .where(label_priorities: { project_id: project })
    .reorder('label_priorities.priority ASC, labels.title ASC')
end

.reference_patternObject

Pattern used to extract label references from text

This pattern supports cross-project references.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/models/label.rb', line 155

def self.reference_pattern
  # 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.
  @reference_pattern ||= %r{
    (#{Project.reference_pattern})?
    #{Regexp.escape(reference_prefix)}
    (?:
        (?<label_id>\d+(?!\S\w)\b)
      | # Integer-based label ID, or
        (?<label_name>
            # String-based single-word label title, or
            #{Gitlab::Regex.sep_by_1(/:{1,2}/, /[A-Za-z0-9_\-\?\.&]+/)}
            (?<!\.|\?)
          |
            # String-based multi-word label surrounded in quotes
            ".+?"
        )
    )
  }x
end

.reference_prefixObject



146
147
148
# File 'app/models/label.rb', line 146

def self.reference_prefix
  '~'
end

.unprioritized(project) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'app/models/label.rb', line 114

def self.unprioritized(project)
  labels = Label.arel_table
  priorities = LabelPriority.arel_table

  label_priorities = labels.join(priorities, Arel::Nodes::OuterJoin)
                            .on(labels[:id].eq(priorities[:label_id]).and(priorities[:project_id].eq(project.id)))
                            .join_sources

  joins(label_priorities).where(priorities[:priority].eq(nil))
end

Instance Method Details

#as_json(options = {}) ⇒ Object



263
264
265
266
267
268
269
# File 'app/models/label.rb', line 263

def as_json(options = {})
  super(options).tap do |json|
    json[:type] = self.try(:type)
    json[:priority] = priority(options[:project]) if options.key?(:project)
    json[:textColor] = text_color
  end
end

#closed_issues_count(user = nil) ⇒ Object



195
196
197
# File 'app/models/label.rb', line 195

def closed_issues_count(user = nil)
  issues_count(user, state: 'closed')
end

#hook_attrsObject



271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'app/models/label.rb', line 271

def hook_attrs
  {
    id: id,
    title: title,
    color: color,
    project_id: project_id,
    created_at: created_at,
    updated_at: updated_at,
    template: template,
    description: description,
    type: type,
    group_id: group_id
  }
end

#open_issues_count(user = nil) ⇒ Object



191
192
193
# File 'app/models/label.rb', line 191

def open_issues_count(user = nil)
  issues_count(user, state: 'opened')
end

#open_merge_requests_count(user = nil) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'app/models/label.rb', line 199

def open_merge_requests_count(user = nil)
  params = {
    subject_foreign_key => subject.id,
    label_name: title,
    scope: 'all',
    state: 'opened'
  }

  MergeRequestsFinder.new(user, params.with_indifferent_access).execute.count
end

#present(attributes = {}) ⇒ Object



286
287
288
# File 'app/models/label.rb', line 286

def present(attributes = {})
  super(**attributes.merge(presenter_class: ::LabelPresenter))
end

#prioritize!(project, value) ⇒ Object



210
211
212
213
214
215
216
# File 'app/models/label.rb', line 210

def prioritize!(project, value)
  return if archived

  label_priority = priorities.find_or_initialize_by(project_id: project.id)
  label_priority.priority = value
  label_priority.save!
end

#priority(project) ⇒ Object



228
229
230
231
232
233
234
235
236
# File 'app/models/label.rb', line 228

def priority(project)
  priority = if priorities.loaded?
               priorities.first { |p| p.project == project }
             else
               priorities.find_by(project: project)
             end

  priority.try(:priority)
end

#refresh_markdown_cacheObject

TODO: Remove when sharding key NOT NULL constraint is validated This should not happen often, and will only happen once for any record gitlab.com/gitlab-org/gitlab/-/issues/558353



293
294
295
296
297
298
299
300
301
302
303
# File 'app/models/label.rb', line 293

def refresh_markdown_cache
  return super if sharding_key_columns.count { |s_column| self[s_column].present? } == 1

  # Using model callbacks to get sharding key values
  ensure_single_parent_for_given_type

  sharding_key_changes = changes.select { |k, _| sharding_key_columns.include?(k.to_sym) }
  sharding_key_updates = sharding_key_changes.transform_values(&:last)

  super.merge(sharding_key_updates)
end

#to_reference(from = nil, target_container: nil, format: :id, full: false) ⇒ Object

Returns the String necessary to reference this Label in Markdown

format - Symbol format to use (default: :id, optional: :name)

Examples:

Label.first.to_reference                                     # => "~1"
Label.first.to_reference(format: :name)                      # => "~\"bug\""
Label.first.to_reference(project, target_container: same_namespace_project)    # => "gitlab-foss~1"
Label.first.to_reference(project, target_container: another_namespace_project) # => "gitlab-org/gitlab-foss~1"

Returns a String



252
253
254
255
256
257
258
259
260
261
# File 'app/models/label.rb', line 252

def to_reference(from = nil, target_container: nil, format: :id, full: false)
  format_reference = label_format_reference(format)
  reference = "#{self.class.reference_prefix}#{format_reference}"

  if from
    "#{from.to_reference_base(target_container, full: full)}#{reference}"
  else
    reference
  end
end

#unprioritize!(project) ⇒ Object



218
219
220
# File 'app/models/label.rb', line 218

def unprioritize!(project)
  priorities.where(project: project).delete_all
end

#unprioritize_all!Object



222
223
224
225
226
# File 'app/models/label.rb', line 222

def unprioritize_all!
  return if id.blank?

  LabelPriority.where(label_id: id).delete_all
end