label_formatter.rb 832 Bytes
Newer Older
1 2
# frozen_string_literal: true

3
module Gitlab
4
  module LegacyGithubImport
5 6 7 8 9 10 11 12 13
    class LabelFormatter < BaseFormatter
      def attributes
        {
          project: project,
          title: title,
          color: color
        }
      end

14 15
      def project_association
        :labels
16 17
      end

18
      # rubocop: disable CodeReuse/ActiveRecord
19
      def create!
20
        params  = attributes.except(:project)
21 22
        service = ::Labels::FindOrCreateService.new(nil, project, params)
        label   = service.execute(skip_authorization: true)
23

24 25 26
        raise ActiveRecord::RecordInvalid.new(label) unless label.persisted?

        label
27
      end
28
      # rubocop: enable CodeReuse/ActiveRecord
29

30 31 32 33 34 35 36 37 38 39 40 41
      private

      def color
        "##{raw_data.color}"
      end

      def title
        raw_data.name
      end
    end
  end
end