diff options
Diffstat (limited to 'lib/gitlab/github_import/representation/issue_event.rb')
-rw-r--r-- | lib/gitlab/github_import/representation/issue_event.rb | 83 |
1 files changed, 55 insertions, 28 deletions
diff --git a/lib/gitlab/github_import/representation/issue_event.rb b/lib/gitlab/github_import/representation/issue_event.rb index 9016338db3b..67a5df73a97 100644 --- a/lib/gitlab/github_import/representation/issue_event.rb +++ b/lib/gitlab/github_import/representation/issue_event.rb @@ -10,34 +10,7 @@ module Gitlab attr_reader :attributes expose_attribute :id, :actor, :event, :commit_id, :label_title, :old_title, :new_title, - :source, :created_at - expose_attribute :issue_db_id # set in SingleEndpointIssueEventsImporter#each_associated - - # Builds a event from a GitHub API response. - # - # event - An instance of `Sawyer::Resource` containing the event details. - def self.from_api_response(event) - new( - id: event.id, - actor: event.actor && Representation::User.from_api_response(event.actor), - event: event.event, - commit_id: event.commit_id, - label_title: event.label && event.label[:name], - old_title: event.rename && event.rename[:from], - new_title: event.rename && event.rename[:to], - source: event.source, - issue_db_id: event.issue_db_id, - created_at: event.created_at - ) - end - - # Builds a event using a Hash that was built from a JSON payload. - def self.from_json_hash(raw_hash) - hash = Representation.symbolize_hash(raw_hash) - hash[:actor] &&= Representation::User.from_json_hash(hash[:actor]) - - new(hash) - end + :milestone_title, :issue, :source, :assignee, :assigner, :created_at # attributes - A Hash containing the event details. The keys of this # Hash (and any nested hashes) must be symbols. @@ -48,6 +21,60 @@ module Gitlab def github_identifiers { id: id } end + + def issuable_type + issue && issue[:pull_request].present? ? 'MergeRequest' : 'Issue' + end + + def issuable_id + issue && issue[:number] + end + + class << self + # Builds an event from a GitHub API response. + # + # event - An instance of `Sawyer::Resource` containing the event details. + def from_api_response(event, additional_data = {}) + new( + id: event.id, + actor: user_representation(event.actor), + event: event.event, + commit_id: event.commit_id, + label_title: event.label && event.label[:name], + old_title: event.rename && event.rename[:from], + new_title: event.rename && event.rename[:to], + milestone_title: event.milestone && event.milestone[:title], + issue: event.issue&.to_h&.symbolize_keys, + source: event.source, + assignee: user_representation(event.assignee), + assigner: user_representation(event.assigner), + created_at: event.created_at + ) + end + + # Builds an event using a Hash that was built from a JSON payload. + def from_json_hash(raw_hash) + hash = Representation.symbolize_hash(raw_hash) + hash[:actor] = user_representation(hash[:actor], source: :hash) + hash[:assignee] = user_representation(hash[:assignee], source: :hash) + hash[:assigner] = user_representation(hash[:assigner], source: :hash) + + new(hash) + end + + private + + def user_representation(data, source: :api_response) + return unless data + + case source + when :api_response + Representation::User.from_api_response(data) + when :hash + Representation::User.from_json_hash(data) + end + end + end end end end |