debian-mirror-gitlab/lib/gitlab/gitlab_import/importer.rb

54 lines
1.5 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
module Gitlab
module GitlabImport
class Importer
attr_reader :project, :client
def initialize(project)
@project = project
2015-09-25 12:07:36 +05:30
import_data = project.import_data.try(:data)
gitlab_session = import_data["gitlab_session"] if import_data
@client = Client.new(gitlab_session["gitlab_access_token"])
2015-04-26 12:48:37 +05:30
@formatter = Gitlab::ImportFormatter.new
end
def execute
project_identifier = CGI.escape(project.import_source, '/')
2015-04-26 12:48:37 +05:30
#Issues && Comments
issues = client.issues(project_identifier)
2015-09-25 12:07:36 +05:30
2015-04-26 12:48:37 +05:30
issues.each do |issue|
2015-09-25 12:07:36 +05:30
body = @formatter.author_line(issue["author"]["name"])
body += issue["description"]
2015-04-26 12:48:37 +05:30
comments = client.issue_comments(project_identifier, issue["id"])
2015-09-25 12:07:36 +05:30
2015-04-26 12:48:37 +05:30
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!(
2015-09-25 12:07:36 +05:30
description: body,
2015-04-26 12:48:37 +05:30
title: issue["title"],
state: issue["state"],
author_id: gl_user_id(project, issue["author"]["id"])
)
end
2015-09-25 12:07:36 +05:30
2015-04-26 12:48:37 +05:30
true
end
private
def gl_user_id(project, gitlab_id)
user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'gitlab'", gitlab_id.to_s)
(user && user.id) || project.creator_id
end
end
end
end