1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
module Gitlab
module GithubImport
class Importer
attr_reader :project, :client
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"])
@formatter = Gitlab::ImportFormatter.new
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|
if issue.pull_request.nil?
body = @formatter.author_line(issue.user.login)
body += issue.body || ""
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_author_id(project, issue.user.id)
)
end
end
end
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
)
client.issue_comments(project.import_source, pull_request.number).each do |c|
merge_request.notes.create!(
project: project,
note: format_body(c.user.login, c.body),
author_id: gl_author_id(project, c.user.id),
created_at: c.created_at,
updated_at: c.updated_at
)
end
client.pull_request_comments(project.import_source, pull_request.number).each do |c|
merge_request.notes.create!(
project: project,
note: format_body(c.user.login, c.body),
commit_id: c.commit_id,
line_code: generate_line_code(c.path, c.position),
author_id: gl_author_id(project, c.user.id),
created_at: c.created_at,
updated_at: c.updated_at
)
end
end
end
def format_body(author, body)
@formatter.author_line(author) + (body || "")
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 generate_line_code(file_path, position)
Gitlab::Diff::LineCode.generate(file_path, position, 0)
end
def gl_author_id(project, github_id)
gl_user_id(github_id) || project.creator_id
end
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
end
end
end
end
|