2018-12-23 12:14:25 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module GitlabImport
|
|
|
|
class Importer
|
|
|
|
attr_reader :project, :client
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2016-06-16 23:09:34 +05:30
|
|
|
import_data = project.import_data
|
|
|
|
if import_data && import_data.credentials && import_data.credentials[:password]
|
|
|
|
@client = Client.new(import_data.credentials[:password])
|
2016-06-02 11:05:42 +05:30
|
|
|
@formatter = Gitlab::ImportFormatter.new
|
|
|
|
else
|
|
|
|
raise Projects::ImportService::Error, "Unable to find project import data credentials for project ID: #{@project.id}"
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-08-24 12:49:21 +05:30
|
|
|
ActiveRecord::Base.no_touching do
|
|
|
|
project_identifier = CGI.escape(project.import_source)
|
|
|
|
|
|
|
|
# Issues && Comments
|
|
|
|
issues = client.issues(project_identifier)
|
|
|
|
|
|
|
|
issues.each do |issue|
|
2018-12-23 12:14:25 +05:30
|
|
|
body = [@formatter.author_line(issue["author"]["name"])]
|
|
|
|
body << issue["description"]
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
comments = client.issue_comments(project_identifier, issue["iid"])
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
if comments.any?
|
2018-12-23 12:14:25 +05:30
|
|
|
body << @formatter.comments_header
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
comments.each do |comment|
|
2018-12-23 12:14:25 +05:30
|
|
|
body << @formatter.comment(comment["author"]["name"], comment["created_at"], comment["body"])
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
project.issues.create!(
|
|
|
|
iid: issue["iid"],
|
2018-12-23 12:14:25 +05:30
|
|
|
description: body.join,
|
2016-08-24 12:49:21 +05:30
|
|
|
title: issue["title"],
|
|
|
|
state: issue["state"],
|
|
|
|
updated_at: issue["updated_at"],
|
2016-09-29 09:46:39 +05:30
|
|
|
author_id: gitlab_user_id(project, issue["author"]["id"]),
|
|
|
|
confidential: issue["confidential"]
|
2016-08-24 12:49:21 +05:30
|
|
|
)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-09-29 09:46:39 +05:30
|
|
|
def gitlab_user_id(project, gitlab_id)
|
2015-04-26 12:48:37 +05:30
|
|
|
user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'gitlab'", gitlab_id.to_s)
|
|
|
|
(user && user.id) || project.creator_id
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|