debian-mirror-gitlab/lib/gitlab/jira_import/handle_labels_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1.1 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
module Gitlab
module JiraImport
class HandleLabelsService
def initialize(project, jira_labels)
@project = project
@jira_labels = jira_labels
end
def execute
return if jira_labels.blank?
existing_labels = LabelsFinder.new(nil, project: project, title: jira_labels)
2022-11-25 23:54:43 +05:30
.execute(skip_authorization: true).select(:id, :project_id, :group_id, :type, :name)
2020-05-24 23:13:21 +05:30
new_labels = create_missing_labels(existing_labels)
label_ids = existing_labels.map(&:id)
label_ids += new_labels if new_labels.present?
label_ids
end
private
attr_reader :project, :jira_labels
def create_missing_labels(existing_labels)
labels_to_create = jira_labels - existing_labels.map(&:name)
return if labels_to_create.empty?
new_labels_hash = labels_to_create.map do |title|
{ project_id: project.id, title: title, type: 'ProjectLabel' }
end
Label.insert_all(new_labels_hash).rows.flatten
end
end
end
end