2020-03-26 12:07:48 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module JiraImport
|
|
|
|
class IssueSerializer
|
2020-04-30 15:09:46 +00:00
|
|
|
attr_reader :jira_issue, :project, :import_owner_id, :params, :formatter
|
2020-03-27 12:07:43 +00:00
|
|
|
|
2020-04-30 15:09:46 +00:00
|
|
|
def initialize(project, jira_issue, import_owner_id, params = {})
|
2020-03-27 12:07:43 +00:00
|
|
|
@jira_issue = jira_issue
|
|
|
|
@project = project
|
2020-04-30 15:09:46 +00:00
|
|
|
@import_owner_id = import_owner_id
|
2020-03-27 12:07:43 +00:00
|
|
|
@params = params
|
|
|
|
@formatter = Gitlab::ImportFormatter.new
|
2020-03-26 12:07:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2020-03-27 12:07:43 +00:00
|
|
|
{
|
|
|
|
iid: params[:iid],
|
|
|
|
project_id: project.id,
|
|
|
|
description: description,
|
|
|
|
title: title,
|
|
|
|
state_id: map_status(jira_issue.status.statusCategory),
|
|
|
|
updated_at: jira_issue.updated,
|
|
|
|
created_at: jira_issue.created,
|
2020-04-29 15:09:58 +00:00
|
|
|
author_id: reporter,
|
|
|
|
assignee_ids: assignees,
|
2020-04-27 09:09:51 +00:00
|
|
|
label_ids: label_ids
|
2020-03-27 12:07:43 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def title
|
|
|
|
"[#{jira_issue.key}] #{jira_issue.summary}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
body = []
|
|
|
|
body << jira_issue.description
|
2020-04-23 00:09:41 +00:00
|
|
|
body << MetadataCollector.new(jira_issue).execute
|
2020-03-27 12:07:43 +00:00
|
|
|
|
|
|
|
body.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def map_status(jira_status_category)
|
|
|
|
case jira_status_category["key"].downcase
|
|
|
|
when 'done'
|
|
|
|
Issuable::STATE_ID_MAP[:closed]
|
|
|
|
else
|
|
|
|
Issuable::STATE_ID_MAP[:opened]
|
|
|
|
end
|
2020-03-26 12:07:48 +00:00
|
|
|
end
|
2020-04-27 09:09:51 +00:00
|
|
|
|
2020-04-29 15:09:58 +00:00
|
|
|
def map_user_id(email)
|
|
|
|
return unless email
|
|
|
|
|
|
|
|
# We also include emails that are not yet confirmed
|
|
|
|
users = User.by_any_email(email).to_a
|
|
|
|
|
|
|
|
# this event should never happen but we should log it in case we have invalid data
|
|
|
|
log_user_mapping_message('Multiple users found for an email address', email) if users.count > 1
|
|
|
|
|
|
|
|
user = users.first
|
|
|
|
|
|
|
|
unless project.project_member(user)
|
|
|
|
log_user_mapping_message('Jira user not found', email)
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def reporter
|
2020-04-30 15:09:46 +00:00
|
|
|
map_user_id(jira_issue&.reporter&.emailAddress) || import_owner_id
|
2020-04-29 15:09:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def assignees
|
|
|
|
found_user_id = map_user_id(jira_issue&.assignee&.emailAddress)
|
|
|
|
|
|
|
|
return unless found_user_id
|
|
|
|
|
|
|
|
[found_user_id]
|
|
|
|
end
|
|
|
|
|
2020-04-27 09:09:51 +00:00
|
|
|
# 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?
|
|
|
|
|
|
|
|
Gitlab::JiraImport::HandleLabelsService.new(project, jira_issue.fields['labels']).execute
|
|
|
|
end
|
2020-04-29 15:09:58 +00:00
|
|
|
|
|
|
|
def logger
|
|
|
|
@logger ||= Gitlab::Import::Logger.build
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_user_mapping_message(message, email)
|
|
|
|
logger.info(
|
|
|
|
project_id: project.id,
|
|
|
|
project_path: project.full_path,
|
|
|
|
user_email: email,
|
|
|
|
message: message
|
|
|
|
)
|
|
|
|
end
|
2020-03-26 12:07:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|