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.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

58
59
60
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 58

def initialize(attributes)
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes


10
11
12
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 10

def attributes
  @attributes
end

Class Method Details

.from_api_response(note) ⇒ Object

Builds a diff note from a GitHub API response.

note - An instance of `Sawyer::Resource` containing the note details.


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

def self.from_api_response(note)
  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_type: 'MergeRequest',
    noteable_id: matches[:iid].to_i,
    file_path: note.path,
    commit_id: note.commit_id,
    diff_hunk: note.diff_hunk,
    author: user,
    note: note.body,
    created_at: note.created_at,
    updated_at: note.updated_at,
    github_id: note.id
  }

  new(hash)
end

.from_json_hash(raw_hash) ⇒ Object

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


49
50
51
52
53
54
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 49

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

#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.


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 71

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

#line_codeObject


62
63
64
65
66
67
# File 'lib/gitlab/github_import/representation/diff_note.rb', line 62

def line_code
  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