Welcome to mirror list, hosted at ThFree Co, Russian Federation.

handle_labels_service.rb « jira_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 60d7f9e93d97fc49bb98664bec97edd97233a14e (plain)
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
# 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)
          .execute(skip_authorization: true).select(:id, :project_id, :group_id, :type, :name)
        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