Class: Gitlab::GithubImport::Representation::DiffNote

Inherits:
Object
  • Object
show all
Includes:
ExposeAttribute, ToHash
Defined in:
lib/gitlab/github_import/representation/diff_note.rb

Constant Summary collapse

NOTEABLE_ID_REGEX =
%r{/pull/(?<iid>\d+)}i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExposeAttribute

#[]

Methods included from ToHash

#convert_value_for_to_hash, #to_hash

Constructor Details

#initialize(attributes) ⇒ DiffNote

attributes - A Hash containing the raw note details. The keys of this

Hash must be Symbols.


65
66
67
68
69
70
71
72
73
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 65

def initialize(attributes)
  @attributes = attributes

  @note_formatter = DiffNotes::SuggestionFormatter.new(
    note: attributes[:note],
    start_line: attributes[:start_line],
    end_line: attributes[:end_line]
  )
end

Instance Attribute Details

#merge_requestObject

Returns the value of attribute merge_request.



61
62
63
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 61

def merge_request
  @merge_request
end

Class Method Details

.from_api_response(note, additional_data = {}) ⇒ Object

Builds a diff note from a GitHub API response.

note - An instance of ‘Hash` containing the note details.



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
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 20

def self.from_api_response(note, additional_data = {})
  matches = note[:html_url].match(NOTEABLE_ID_REGEX)

  unless matches
    raise(
      ArgumentError,
      "The note URL #{note[:html_url].inspect} is not supported"
    )
  end

  user = Representation::User.from_api_response(note[:user]) if note[:user]
  hash = {
    noteable_id: matches[:iid].to_i,
    file_path: note[:path],
    commit_id: note[:commit_id],
    original_commit_id: note[:original_commit_id],
    diff_hunk: note[:diff_hunk],
    author: user,
    note: note[:body],
    created_at: note[:created_at],
    updated_at: note[:updated_at],
    note_id: note[:id],
    end_line: note[:line],
    start_line: note[:start_line],
    side: note[:side],
    in_reply_to_id: note[:in_reply_to_id],
    discussion_id: DiffNotes::DiscussionId.new(note).find_or_generate,
    subject_type: note[:subject_type]
  }

  new(hash)
end

.from_json_hash(raw_hash) ⇒ Object

Builds a new note using a Hash that was built from a JSON payload.



54
55
56
57
58
59
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 54

def self.from_json_hash(raw_hash)
  hash = Representation.symbolize_hash(raw_hash)
  hash[:author] &&= Representation::User.from_json_hash(hash[:author])

  new(hash)
end

Instance Method Details

#contains_suggestion?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 79

def contains_suggestion?
  @note_formatter.contains_suggestion?
end

#diff_hashObject

Returns a Hash that can be used to populate ‘notes.st_diff`, removing the need for requesting Git data for every diff note. Used when importing with LegacyDiffNote



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 102

def diff_hash
  {
    diff: diff_hunk,
    new_path: file_path,
    old_path: file_path,

    # These fields are not displayed for LegacyDiffNote notes, so it
    # doesn't really matter what we set them to.
    a_mode: '100644',
    b_mode: '100644',
    new_file: false
  }
end

#diff_positionObject

Used when importing with DiffNote



117
118
119
120
121
122
123
124
125
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 117

def diff_position
  position_params = {
    diff_refs: merge_request.diff_refs,
    old_path: file_path,
    new_path: file_path
  }

  Gitlab::Diff::Position.new(position_params.merge(diff_line_params))
end

#github_identifiersObject



127
128
129
130
131
132
133
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 127

def github_identifiers
  {
    note_id: note_id,
    noteable_iid: noteable_id,
    noteable_type: noteable_type
  }
end

#line_codeObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 87

def line_code
  # on the GitHub side it is possible to leave a comment on a file
  # or on a line. When the comment is left on a file there is no
  # diff hunk, but LegacyDiffNote requires line_code to be always present
  # and DiffFile requires it for text files
  # so it is set as the first line for any type of file (image, binary, text)
  return Gitlab::Git.diff_line_code(file_path, 1, 1) if on_file?

  diff_line = Gitlab::Diff::Parser.new.parse(diff_hunk.lines).to_a.last
  Gitlab::Git.diff_line_code(file_path, diff_line.new_pos, diff_line.old_pos)
end

#noteObject



83
84
85
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 83

def note
  @note_formatter.formatted_note
end

#noteable_typeObject



75
76
77
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 75

def noteable_type
  DiffNotes::DiscussionId::NOTEABLE_TYPE
end