debian-mirror-gitlab/lib/gitlab/jira_import/issue_serializer.rb

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

91 lines
2.4 KiB
Ruby
Raw Normal View History

2020-04-22 19:07:51 +05:30
# frozen_string_literal: true
module Gitlab
module JiraImport
class IssueSerializer
2020-05-24 23:13:21 +05:30
attr_reader :jira_issue, :project, :import_owner_id, :params, :formatter
2020-04-22 19:07:51 +05:30
2022-08-13 15:12:31 +05:30
def initialize(project, jira_issue, import_owner_id, work_item_type_id, params = {})
2020-04-22 19:07:51 +05:30
@jira_issue = jira_issue
@project = project
2020-05-24 23:13:21 +05:30
@import_owner_id = import_owner_id
2022-08-13 15:12:31 +05:30
@work_item_type_id = work_item_type_id
2020-04-22 19:07:51 +05:30
@params = params
@formatter = Gitlab::ImportFormatter.new
end
def execute
{
iid: params[:iid],
project_id: project.id,
2022-08-13 15:12:31 +05:30
namespace_id: project.project_namespace_id,
2020-04-22 19:07:51 +05:30
description: description,
title: title,
state_id: map_status(jira_issue.status.statusCategory),
updated_at: jira_issue.updated,
created_at: jira_issue.created,
2020-05-24 23:13:21 +05:30
author_id: reporter,
assignee_ids: assignees,
2022-08-13 15:12:31 +05:30
label_ids: label_ids,
work_item_type_id: @work_item_type_id
2020-04-22 19:07:51 +05:30
}
end
private
def title
"[#{jira_issue.key}] #{jira_issue.summary}"
end
def description
body = []
body << jira_issue.description
2020-05-24 23:13:21 +05:30
body << MetadataCollector.new(jira_issue).execute
2020-04-22 19:07:51 +05:30
body.join
end
def map_status(jira_status_category)
case jira_status_category["key"].downcase
when 'done'
2022-08-13 15:12:31 +05:30
::Issuable::STATE_ID_MAP[:closed]
2020-04-22 19:07:51 +05:30
else
2022-08-13 15:12:31 +05:30
::Issuable::STATE_ID_MAP[:opened]
2020-04-22 19:07:51 +05:30
end
end
2020-05-24 23:13:21 +05:30
def map_user_id(jira_user)
2021-10-27 15:23:28 +05:30
jira_user_identifier = jira_user&.dig('accountId') || jira_user&.dig('key')
return unless jira_user_identifier
2020-07-28 23:09:34 +05:30
2021-10-27 15:23:28 +05:30
Gitlab::JiraImport.get_user_mapping(project.id, jira_user_identifier)
2020-04-22 19:07:51 +05:30
end
2020-05-24 23:13:21 +05:30
def reporter
map_user_id(jira_issue.reporter&.attrs) || import_owner_id
2020-04-22 19:07:51 +05:30
end
2020-05-24 23:13:21 +05:30
def assignees
found_user_id = map_user_id(jira_issue.assignee&.attrs)
2020-04-22 19:07:51 +05:30
2020-05-24 23:13:21 +05:30
return unless found_user_id
2020-04-22 19:07:51 +05:30
2020-05-24 23:13:21 +05:30
[found_user_id]
2020-04-22 19:07:51 +05:30
end
2020-05-24 23:13:21 +05:30
# We already create labels in Gitlab::JiraImport::LabelsImporter stage but
# there is a possibility it may fail or
# new labels were created on the Jira in the meantime
def label_ids
return if jira_issue.fields['labels'].blank?
2020-04-22 19:07:51 +05:30
2020-05-24 23:13:21 +05:30
Gitlab::JiraImport::HandleLabelsService.new(project, jira_issue.fields['labels']).execute
2020-04-22 19:07:51 +05:30
end
2020-05-24 23:13:21 +05:30
def logger
@logger ||= Gitlab::Import::Logger.build
2020-04-22 19:07:51 +05:30
end
end
end
end