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

52 lines
1.6 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
module Gitlab
module GithubImport
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)
github_session = import_data["github_session"] if import_data
@client = Client.new(github_session["github_access_token"])
2015-04-26 12:48:37 +05:30
@formatter = Gitlab::ImportFormatter.new
end
def execute
#Issues && Comments
2015-09-11 14:41:01 +05:30
client.list_issues(project.import_source, state: :all,
sort: :created,
direction: :asc).each do |issue|
2015-04-26 12:48:37 +05:30
if issue.pull_request.nil?
2015-09-25 12:07:36 +05:30
body = @formatter.author_line(issue.user.login)
body += issue.body
2015-04-26 12:48:37 +05:30
if issue.comments > 0
body += @formatter.comments_header
client.issue_comments(project.import_source, issue.number).each do |c|
body += @formatter.comment(c.user.login, c.created_at, c.body)
end
end
project.issues.create!(
description: body,
title: issue.title,
state: issue.state == 'closed' ? 'closed' : 'opened',
author_id: gl_user_id(project, issue.user.id)
)
end
end
end
private
def gl_user_id(project, github_id)
user = User.joins(:identities).
find_by("identities.extern_uid = ? AND identities.provider = 'github'", github_id.to_s)
(user && user.id) || project.creator_id
end
end
end
end