2015-01-27 23:37:19 +00:00
|
|
|
module Gitlab
|
|
|
|
module GitlabImport
|
|
|
|
class Importer
|
|
|
|
attr_reader :project, :client
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2016-05-26 09:25:35 +00:00
|
|
|
import_data = project.import_data
|
|
|
|
if import_data && import_data.credentials && import_data.credentials[:password]
|
|
|
|
@client = Client.new(import_data.credentials[:password])
|
2016-03-22 17:03:54 +00:00
|
|
|
@formatter = Gitlab::ImportFormatter.new
|
|
|
|
else
|
|
|
|
raise Projects::ImportService::Error, "Unable to find project import data credentials for project ID: #{@project.id}"
|
|
|
|
end
|
2015-01-27 23:37:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-07-14 15:06:48 +00:00
|
|
|
ActiveRecord::Base.no_touching do
|
|
|
|
project_identifier = CGI.escape(project.import_source)
|
|
|
|
|
|
|
|
# Issues && Comments
|
|
|
|
issues = client.issues(project_identifier)
|
|
|
|
|
|
|
|
issues.each do |issue|
|
|
|
|
body = @formatter.author_line(issue["author"]["name"])
|
|
|
|
body += issue["description"]
|
|
|
|
|
2018-05-15 13:39:33 +00:00
|
|
|
comments = client.issue_comments(project_identifier, issue["iid"])
|
2016-07-14 15:06:48 +00:00
|
|
|
|
|
|
|
if comments.any?
|
|
|
|
body += @formatter.comments_header
|
|
|
|
end
|
|
|
|
|
|
|
|
comments.each do |comment|
|
|
|
|
body += @formatter.comment(comment["author"]["name"], comment["created_at"], comment["body"])
|
|
|
|
end
|
|
|
|
|
|
|
|
project.issues.create!(
|
|
|
|
iid: issue["iid"],
|
|
|
|
description: body,
|
|
|
|
title: issue["title"],
|
|
|
|
state: issue["state"],
|
|
|
|
updated_at: issue["updated_at"],
|
2016-09-08 14:43:19 +00:00
|
|
|
author_id: gitlab_user_id(project, issue["author"]["id"]),
|
2016-09-02 14:52:11 +00:00
|
|
|
confidential: issue["confidential"]
|
2016-07-14 15:06:48 +00:00
|
|
|
)
|
2015-01-27 23:37:19 +00:00
|
|
|
end
|
|
|
|
end
|
2015-08-07 07:06:20 +00:00
|
|
|
|
2015-01-27 23:37:19 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-09-08 00:44:58 +00:00
|
|
|
def gitlab_user_id(project, gitlab_id)
|
2015-02-05 18:31:36 +00:00
|
|
|
user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'gitlab'", gitlab_id.to_s)
|
2015-01-27 23:37:19 +00:00
|
|
|
(user && user.id) || project.creator_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|