Class: Gitlab::BitbucketServerImport::Importers::PullRequestNotes::BaseNoteDiffImporter

Inherits:
BaseImporter
  • Object
show all
Defined in:
lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb

Direct Known Subclasses

Inline, StandaloneNotes

Constant Summary collapse

PARENT_COMMENT_CONTEXT_LENGTH =
80

Constants included from Import::UsernameMentionRewriter

Import::UsernameMentionRewriter::MENTION_REGEX

Instance Method Summary collapse

Methods inherited from BaseImporter

#execute, #initialize

Methods included from Import::PlaceholderReferences::Pusher

#map_to_personal_namespace_owner?, #push_reference, #push_reference_with_composite_key, #push_references_by_ids, #user_mapping_enabled?

Methods included from Import::UsernameMentionRewriter

#update_username_mentions, #wrap_mentions_in_backticks

Methods included from Loggable

#log_debug, #log_error, #log_info, #log_warn

Constructor Details

This class inherits a constructor from Gitlab::BitbucketServerImport::Importers::PullRequestNotes::BaseImporter

Instance Method Details

#author(comment) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb', line 84

def author(comment)
  if user_mapping_enabled?(project)
    user_finder.uid(
      username: comment[:author_username],
      display_name: comment[:author_name]
    )
  else
    user_finder.uid(comment)
  end
end

#build_position(merge_request, pr_comment) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb', line 10

def build_position(merge_request, pr_comment)
  params = {
    diff_refs: merge_request.diff_refs,
    old_path: pr_comment[:file_path],
    new_path: pr_comment[:file_path],
    old_line: pr_comment[:old_pos],
    new_line: pr_comment[:new_pos]
  }

  Gitlab::Diff::Position.new(params)
end

#create_basic_fallback_note(merge_request, comment, position) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb', line 95

def create_basic_fallback_note(merge_request, comment, position)
  attributes = pull_request_comment_attributes(comment)
  note_text = "*Comment on"

  note_text += " #{position.old_path}:#{position.old_line} -->" if position.old_line
  note_text += " #{position.new_path}:#{position.new_line}" if position.new_line
  note_text += "*\n\n#{wrap_mentions_in_backticks(comment[:note])}"

  attributes[:note] = note_text

  note = merge_request.notes.create!(attributes)
  push_reference(project, note, :author_id, comment[:author_username])
  note
end

#create_diff_note(merge_request, comment, position, discussion_id = nil) ⇒ Object



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/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb', line 22

def create_diff_note(merge_request, comment, position, discussion_id = nil)
  attributes = pull_request_comment_attributes(comment)
  attributes.merge!(position: position, type: 'DiffNote')
  attributes[:discussion_id] = discussion_id if discussion_id

  note = merge_request.notes.build(attributes)

  if note.valid?
    note.save
    push_reference(project, note, :author_id, comment[:author_username])

    return note
  end

  log_info(
    import_stage: 'create_diff_note',
    message: 'creating standalone fallback for DiffNote',
    iid: merge_request.iid,
    comment_id: comment[:id]
  )

  # Bitbucket Server supports the ability to comment on any line, not just the
  # line in the diff. If we can't add the note as a DiffNote, fallback to creating
  # a regular note.
  create_basic_fallback_note(merge_request, comment, position)
rescue StandardError => e
  Gitlab::ErrorTracking.log_exception(
    e,
    import_stage: 'create_diff_note', comment_id: comment[:id], error: e.message
  )

  nil
end

#pull_request_comment_attributes(comment) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gitlab/bitbucket_server_import/importers/pull_request_notes/base_note_diff_importer.rb', line 56

def pull_request_comment_attributes(comment)
  author = author(comment)
  note = ''

  unless author
    author = project.creator_id
    note = "*By #{comment[:author_username]} (#{comment[:author_email]})*\n\n"
  end

  note +=
    # Provide some context for replying
    if comment[:parent_comment_note]
      parent_comment_note = comment[:parent_comment_note].truncate(80, omission: ' ...')

      "> #{parent_comment_note}\n\n#{comment[:note]}"
    else
      comment[:note]
    end

  {
    project: project,
    note: wrap_mentions_in_backticks(note),
    author_id: author,
    created_at: comment[:created_at],
    updated_at: comment[:updated_at]
  }
end