Class: Gitlab::GithubImport::Importer::DiffNoteImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/github_import/importer/diff_note_importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(note, project, client) ⇒ DiffNoteImporter

note - An instance of `Gitlab::GithubImport::Representation::DiffNote`. project - An instance of `Project`. client - An instance of `Gitlab::GithubImport::Client`.


12
13
14
15
16
17
# File 'lib/gitlab/github_import/importer/diff_note_importer.rb', line 12

def initialize(note, project, client)
  @note = note
  @project = project
  @client = client
  @user_finder = GithubImport::UserFinder.new(project, client)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


7
8
9
# File 'lib/gitlab/github_import/importer/diff_note_importer.rb', line 7

def client
  @client
end

#noteObject (readonly)

Returns the value of attribute note


7
8
9
# File 'lib/gitlab/github_import/importer/diff_note_importer.rb', line 7

def note
  @note
end

#projectObject (readonly)

Returns the value of attribute project


7
8
9
# File 'lib/gitlab/github_import/importer/diff_note_importer.rb', line 7

def project
  @project
end

#user_finderObject (readonly)

Returns the value of attribute user_finder


7
8
9
# File 'lib/gitlab/github_import/importer/diff_note_importer.rb', line 7

def user_finder
  @user_finder
end

Instance Method Details

#executeObject


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
# File 'lib/gitlab/github_import/importer/diff_note_importer.rb', line 19

def execute
  return unless (mr_id = find_merge_request_id)

  author_id, author_found = user_finder.author_id_for(note)

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

  attributes = {
    noteable_type: 'MergeRequest',
    noteable_id: mr_id,
    project_id: project.id,
    author_id: author_id,
    note: note_body,
    system: false,
    commit_id: note.commit_id,
    line_code: note.line_code,
    type: 'LegacyDiffNote',
    created_at: note.created_at,
    updated_at: note.updated_at,
    st_diff: note.diff_hash.to_yaml
  }

  # It's possible that during an import we'll insert tens of thousands
  # of diff notes. If we were to use the Note/LegacyDiffNote model here
  # we'd also have to run additional queries for both validations and
  # callbacks, putting a lot of pressure on the database.
  #
  # To work around this we're using bulk_insert with a single row. This
  # allows us to efficiently insert data (even if it's just 1 row)
  # without having to use all sorts of hacks to disable callbacks.
  Gitlab::Database.bulk_insert(LegacyDiffNote.table_name, [attributes]) # rubocop:disable Gitlab/BulkInsert
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

#find_merge_request_idObject

Returns the ID of the merge request this note belongs to.


57
58
59
# File 'lib/gitlab/github_import/importer/diff_note_importer.rb', line 57

def find_merge_request_id
  GithubImport::IssuableFinder.new(project, note).database_id
end