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

93 lines
3.0 KiB
Ruby
Raw Normal View History

2014-12-31 13:07:48 +00:00
module Gitlab
2015-02-02 22:26:29 +00:00
module GithubImport
2014-12-31 13:07:48 +00:00
class Importer
2015-02-06 00:57:27 +00:00
attr_reader :project, :client
2014-12-31 13:07:48 +00:00
def initialize(project)
@project = project
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-02-03 01:01:07 +00:00
@formatter = Gitlab::ImportFormatter.new
2014-12-31 13:07:48 +00:00
end
def execute
import_issues
import_pull_requests
true
end
private
def import_issues
# Issues && Comments
client.list_issues(project.import_source, state: :all,
sort: :created,
direction: :asc).each do |issue|
2014-12-31 13:07:48 +00:00
if issue.pull_request.nil?
2015-02-03 01:01:07 +00:00
body = @formatter.author_line(issue.user.login)
body += issue.body || ""
2015-02-03 03:30:09 +00:00
2014-12-31 13:07:48 +00:00
if issue.comments > 0
2015-02-03 01:01:07 +00:00
body += @formatter.comments_header
2014-12-31 13:07:48 +00:00
client.issue_comments(project.import_source, issue.number).each do |c|
2015-02-17 15:59:50 +00:00
body += @formatter.comment(c.user.login, c.created_at, c.body)
2014-12-31 13:07:48 +00:00
end
end
project.issues.create!(
2015-02-03 03:30:09 +00:00
description: body,
2014-12-31 13:07:48 +00:00
title: issue.title,
state: issue.state == 'closed' ? 'closed' : 'opened',
author_id: gl_author_id(project, issue.user.id)
2014-12-31 13:07:48 +00:00
)
end
end
end
def import_pull_requests
client.pull_requests(project.import_source, state: :all,
sort: :created,
2015-12-23 17:04:46 +00:00
direction: :asc).each do |raw_data|
pull_request = PullRequest.new(project, raw_data)
2015-12-23 17:04:46 +00:00
if pull_request.valid?
merge_request = MergeRequest.create!(pull_request.attributes)
import_comments_on_pull_request(merge_request, raw_data)
import_comments_on_pull_request_diff(merge_request, raw_data)
end
end
end
def import_comments_on_pull_request(merge_request, pull_request)
2015-12-23 17:04:46 +00:00
client.issue_comments(project.import_source, pull_request.number).each do |raw_data|
comment = Comment.new(project, raw_data)
merge_request.notes.create!(comment.attributes)
end
end
def import_comments_on_pull_request_diff(merge_request, pull_request)
2015-12-23 17:04:46 +00:00
client.pull_request_comments(project.import_source, pull_request.number).each do |raw_data|
comment = Comment.new(project, raw_data)
merge_request.notes.create!(comment.attributes)
end
end
def gl_author_id(project, github_id)
gl_user_id(github_id) || project.creator_id
end
2014-12-31 13:07:48 +00:00
def gl_user_id(github_id)
if github_id
User.joins(:identities).
find_by("identities.extern_uid = ? AND identities.provider = 'github'", github_id.to_s).
try(:id)
end
2014-12-31 13:07:48 +00:00
end
end
end
end