2014-12-31 08:07:48 -05:00
|
|
|
module Gitlab
|
2015-02-02 17:26:29 -05:00
|
|
|
module GithubImport
|
2014-12-31 08:07:48 -05:00
|
|
|
class Importer
|
2015-02-05 19:57:27 -05:00
|
|
|
attr_reader :project, :client
|
2014-12-31 08:07:48 -05:00
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2015-08-07 03:06:20 -04:00
|
|
|
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-02 20:01:07 -05:00
|
|
|
@formatter = Gitlab::ImportFormatter.new
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2015-12-21 13:52:45 -05:00
|
|
|
import_issues
|
|
|
|
import_pull_requests
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def import_issues
|
|
|
|
# Issues && Comments
|
2015-06-26 14:11:20 -04:00
|
|
|
client.list_issues(project.import_source, state: :all,
|
|
|
|
sort: :created,
|
|
|
|
direction: :asc).each do |issue|
|
2014-12-31 08:07:48 -05:00
|
|
|
if issue.pull_request.nil?
|
2015-02-02 20:01:07 -05:00
|
|
|
|
2015-08-31 20:03:09 -04:00
|
|
|
body = @formatter.author_line(issue.user.login)
|
2015-11-17 12:47:39 -05:00
|
|
|
body += issue.body || ""
|
2015-02-02 22:30:09 -05:00
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
if issue.comments > 0
|
2015-02-02 20:01:07 -05:00
|
|
|
body += @formatter.comments_header
|
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
client.issue_comments(project.import_source, issue.number).each do |c|
|
2015-02-17 10:59:50 -05:00
|
|
|
body += @formatter.comment(c.user.login, c.created_at, c.body)
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
project.issues.create!(
|
2015-02-02 22:30:09 -05:00
|
|
|
description: body,
|
2014-12-31 08:07:48 -05:00
|
|
|
title: issue.title,
|
|
|
|
state: issue.state == 'closed' ? 'closed' : 'opened',
|
2015-12-21 13:52:45 -05:00
|
|
|
author_id: gl_author_id(project, issue.user.id)
|
2014-12-31 08:07:48 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-21 13:52:45 -05:00
|
|
|
def import_pull_requests
|
|
|
|
client.pull_requests(project.import_source, state: :all,
|
|
|
|
sort: :created,
|
|
|
|
direction: :asc).each do |pull_request|
|
|
|
|
body = @formatter.author_line(pull_request.user.login)
|
|
|
|
body += pull_request.body || ""
|
|
|
|
|
|
|
|
source_branch = pull_request.head.ref
|
|
|
|
target_branch = pull_request.base.ref
|
|
|
|
|
|
|
|
merge_request = MergeRequest.create!(
|
|
|
|
title: pull_request.title,
|
|
|
|
description: body,
|
|
|
|
source_project: project,
|
|
|
|
source_branch: source_branch,
|
|
|
|
target_project: project,
|
|
|
|
target_branch: target_branch,
|
|
|
|
state: merge_request_state(pull_request),
|
|
|
|
author_id: gl_author_id(project, pull_request.user.id),
|
|
|
|
assignee_id: gl_user_id(pull_request.assignee.try(:id)),
|
|
|
|
created_at: pull_request.created_at,
|
|
|
|
updated_at: pull_request.updated_at
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request_state(pull_request)
|
|
|
|
case true
|
|
|
|
when pull_request.state == 'closed' && pull_request.merged_at.present?
|
|
|
|
'merged'
|
|
|
|
when pull_request.state == 'closed'
|
|
|
|
'closed'
|
|
|
|
else
|
|
|
|
'opened'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def gl_author_id(project, github_id)
|
|
|
|
gl_user_id(github_id) || project.creator_id
|
|
|
|
end
|
2014-12-31 08:07:48 -05:00
|
|
|
|
2015-12-21 13:52:45 -05: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 08:07:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|