2018-03-17 18:26:18 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
module Representation
|
|
|
|
class Note
|
|
|
|
include ToHash
|
|
|
|
include ExposeAttribute
|
|
|
|
|
|
|
|
attr_reader :attributes
|
|
|
|
|
|
|
|
expose_attribute :noteable_id, :noteable_type, :author, :note,
|
2021-11-18 22:05:49 +05:30
|
|
|
:created_at, :updated_at, :note_id
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
NOTEABLE_TYPE_REGEX = %r{/(?<type>(pull|issues))/(?<iid>\d+)}i.freeze
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
# Builds a note from a GitHub API response.
|
|
|
|
#
|
2022-11-25 23:54:43 +05:30
|
|
|
# note - An instance of `Hash` containing the note details.
|
2022-08-27 11:52:29 +05:30
|
|
|
def self.from_api_response(note, additional_data = {})
|
2022-11-25 23:54:43 +05:30
|
|
|
matches = note[:html_url].match(NOTEABLE_TYPE_REGEX)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
if !matches || !matches[:type]
|
|
|
|
raise(
|
|
|
|
ArgumentError,
|
2022-11-25 23:54:43 +05:30
|
|
|
"The note URL #{note[:html_url].inspect} is not supported"
|
2018-03-17 18:26:18 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
noteable_type =
|
|
|
|
if matches[:type] == 'pull'
|
|
|
|
'MergeRequest'
|
|
|
|
else
|
|
|
|
'Issue'
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
user = Representation::User.from_api_response(note[:user]) if note[:user]
|
2018-03-17 18:26:18 +05:30
|
|
|
hash = {
|
|
|
|
noteable_type: noteable_type,
|
|
|
|
noteable_id: matches[:iid].to_i,
|
|
|
|
author: user,
|
2022-11-25 23:54:43 +05:30
|
|
|
note: note[:body],
|
|
|
|
created_at: note[:created_at],
|
|
|
|
updated_at: note[:updated_at],
|
|
|
|
note_id: note[:id]
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
new(hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Builds a new note using a Hash that was built from a JSON payload.
|
|
|
|
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
|
|
|
|
|
|
|
|
# attributes - A Hash containing the raw note details. The keys of this
|
|
|
|
# Hash must be Symbols.
|
|
|
|
def initialize(attributes)
|
|
|
|
@attributes = attributes
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def discussion_id
|
|
|
|
Discussion.discussion_id(
|
|
|
|
Struct
|
|
|
|
.new(:noteable_id, :noteable_type)
|
|
|
|
.new(noteable_id, noteable_type)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
alias_method :issuable_type, :noteable_type
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
def github_identifiers
|
|
|
|
{
|
|
|
|
note_id: note_id,
|
|
|
|
noteable_id: noteable_id,
|
|
|
|
noteable_type: noteable_type
|
|
|
|
}
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|