2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-01-27 18:37:19 -05:00
|
|
|
module Gitlab
|
|
|
|
module GitlabImport
|
|
|
|
class Importer
|
|
|
|
attr_reader :project, :client
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2016-05-26 05:25:35 -04: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 13:03:54 -04: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 18:37:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-07-14 11:06:48 -04:00
|
|
|
ActiveRecord::Base.no_touching do
|
|
|
|
project_identifier = CGI.escape(project.import_source)
|
|
|
|
|
|
|
|
# Issues && Comments
|
|
|
|
issues = client.issues(project_identifier)
|
|
|
|
|
|
|
|
issues.each do |issue|
|
2018-11-09 13:39:43 -05:00
|
|
|
body = [@formatter.author_line(issue["author"]["name"])]
|
|
|
|
body << issue["description"]
|
2016-07-14 11:06:48 -04:00
|
|
|
|
2018-05-15 09:39:33 -04:00
|
|
|
comments = client.issue_comments(project_identifier, issue["iid"])
|
2016-07-14 11:06:48 -04:00
|
|
|
|
|
|
|
if comments.any?
|
2018-11-09 13:39:43 -05:00
|
|
|
body << @formatter.comments_header
|
2016-07-14 11:06:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
comments.each do |comment|
|
2018-11-09 13:39:43 -05:00
|
|
|
body << @formatter.comment(comment["author"]["name"], comment["created_at"], comment["body"])
|
2016-07-14 11:06:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
project.issues.create!(
|
|
|
|
iid: issue["iid"],
|
2018-11-09 13:39:43 -05:00
|
|
|
description: body.join,
|
2016-07-14 11:06:48 -04:00
|
|
|
title: issue["title"],
|
|
|
|
state: issue["state"],
|
|
|
|
updated_at: issue["updated_at"],
|
2016-09-08 10:43:19 -04:00
|
|
|
author_id: gitlab_user_id(project, issue["author"]["id"]),
|
2016-09-02 10:52:11 -04:00
|
|
|
confidential: issue["confidential"]
|
2016-07-14 11:06:48 -04:00
|
|
|
)
|
2015-01-27 18:37:19 -05:00
|
|
|
end
|
|
|
|
end
|
2015-08-07 03:06:20 -04:00
|
|
|
|
2015-01-27 18:37:19 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-09-07 20:44:58 -04:00
|
|
|
def gitlab_user_id(project, gitlab_id)
|
2015-02-05 13:31:36 -05:00
|
|
|
user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'gitlab'", gitlab_id.to_s)
|
2015-01-27 18:37:19 -05:00
|
|
|
(user && user.id) || project.creator_id
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-01-27 18:37:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|