gitlab-org--gitlab-foss/lib/gitlab/gitlab_import/importer.rb

57 lines
1.6 KiB
Ruby
Raw Normal View History

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-17 09:19:59 -04:00
credentials = project.import_data
if credentials && credentials[:password]
@client = Client.new(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
project_identifier = CGI.escape(project.import_source)
2015-01-27 18:37:19 -05:00
#Issues && Comments
issues = client.issues(project_identifier)
2015-01-27 18:37:19 -05:00
issues.each do |issue|
body = @formatter.author_line(issue["author"]["name"])
body += issue["description"]
2015-01-27 18:37:19 -05:00
comments = client.issue_comments(project_identifier, issue["id"])
2015-01-27 18:37:19 -05:00
if comments.any?
2015-02-02 20:01:07 -05:00
body += @formatter.comments_header
2015-01-27 18:37:19 -05:00
end
2015-02-02 20:01:07 -05:00
2015-01-27 18:37:19 -05:00
comments.each do |comment|
2015-02-17 10:59:50 -05:00
body += @formatter.comment(comment["author"]["name"], comment["created_at"], comment["body"])
2015-01-27 18:37:19 -05:00
end
project.issues.create!(
description: body,
2015-01-27 18:37:19 -05:00
title: issue["title"],
state: issue["state"],
author_id: gl_user_id(project, issue["author"]["id"])
)
end
2015-01-27 18:37:19 -05:00
true
end
private
def gl_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
end
end
end