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

54 lines
1.5 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
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-02-02 20:01:07 -05:00
@formatter = Gitlab::ImportFormatter.new
2015-01-27 18:37:19 -05:00
end
def execute
project_identifier = URI.encode(project.import_source, '/')
#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