debian-mirror-gitlab/lib/gitlab/github_import/representation/issue.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
2.5 KiB
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
# frozen_string_literal: true
module Gitlab
module GithubImport
module Representation
class Issue
include ToHash
include ExposeAttribute
attr_reader :attributes
expose_attribute :iid, :title, :description, :milestone_number,
:created_at, :updated_at, :state, :assignees,
2022-08-27 11:52:29 +05:30
:label_names, :author, :work_item_type_id
2018-03-17 18:26:18 +05:30
# Builds an issue from a GitHub API response.
#
2022-11-25 23:54:43 +05:30
# issue - An instance of `Hash` containing the issue
2018-03-17 18:26:18 +05:30
# details.
2022-08-27 11:52:29 +05:30
def self.from_api_response(issue, additional_data = {})
2018-03-17 18:26:18 +05:30
user =
2022-11-25 23:54:43 +05:30
if issue[:user]
Representation::User.from_api_response(issue[:user])
2018-03-17 18:26:18 +05:30
end
hash = {
2022-11-25 23:54:43 +05:30
iid: issue[:number],
title: issue[:title],
description: issue[:body],
milestone_number: issue.dig(:milestone, :number),
state: issue[:state] == 'open' ? :opened : :closed,
assignees: issue[:assignees].map do |u|
2018-03-17 18:26:18 +05:30
Representation::User.from_api_response(u)
end,
2022-11-25 23:54:43 +05:30
label_names: issue[:labels].map { _1[:name] },
2018-03-17 18:26:18 +05:30
author: user,
2022-11-25 23:54:43 +05:30
created_at: issue[:created_at],
updated_at: issue[:updated_at],
pull_request: issue[:pull_request] ? true : false,
2022-08-27 11:52:29 +05:30
work_item_type_id: additional_data[:work_item_type_id]
2018-03-17 18:26:18 +05:30
}
new(hash)
end
# Builds a new issue using a Hash that was built from a JSON payload.
def self.from_json_hash(raw_hash)
hash = Representation.symbolize_hash(raw_hash)
hash[:state] = hash[:state].to_sym
hash[:assignees].map! { |u| Representation::User.from_json_hash(u) }
hash[:author] &&= Representation::User.from_json_hash(hash[:author])
new(hash)
end
# attributes - A hash containing the raw issue details. The keys of this
# Hash (and any nested hashes) must be symbols.
def initialize(attributes)
@attributes = attributes
end
def truncated_title
title.truncate(255)
end
def labels?
label_names && label_names.any?
end
def pull_request?
attributes[:pull_request]
end
def issuable_type
pull_request? ? 'MergeRequest' : 'Issue'
end
2021-11-18 22:05:49 +05:30
def github_identifiers
{
iid: iid,
issuable_type: issuable_type
}
end
2018-03-17 18:26:18 +05:30
end
end
end
end