summaryrefslogtreecommitdiff
path: root/lib/gitlab/github_import/importer/note_importer.rb
blob: c890f2df360559f15de0da699ba9209a450d7e8f (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

module Gitlab
  module GithubImport
    module Importer
      class NoteImporter
        attr_reader :note, :project, :client, :user_finder

        # note - An instance of `Gitlab::GithubImport::Representation::Note`.
        # project - An instance of `Project`.
        # client - An instance of `Gitlab::GithubImport::Client`.
        def initialize(note, project, client)
          @note = note
          @project = project
          @client = client
          @user_finder = UserFinder.new(project, client)
        end

        def execute
          return unless (noteable_id = find_noteable_id)

          author_id, author_found = user_finder.author_id_for(note)

          note_body =
            MarkdownText.format(note.note, note.author, author_found)

          attributes = {
            noteable_type: note.noteable_type,
            noteable_id: noteable_id,
            project_id: project.id,
            author_id: author_id,
            note: note_body,
            system: false,
            created_at: note.created_at,
            updated_at: note.updated_at
          }

          # We're using bulk_insert here so we can bypass any validations and
          # callbacks. Running these would result in a lot of unnecessary SQL
          # queries being executed when importing large projects.
          Gitlab::Database.bulk_insert(Note.table_name, [attributes])
        rescue ActiveRecord::InvalidForeignKey
          # It's possible the project and the issue have been deleted since
          # scheduling this job. In this case we'll just skip creating the note.
        end

        # Returns the ID of the issue or merge request to create the note for.
        def find_noteable_id
          GithubImport::IssuableFinder.new(project, note).database_id
        end
      end
    end
  end
end