2018-10-11 16:12:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-17 10:59:50 -05:00
|
|
|
module Gitlab
|
|
|
|
module BitbucketImport
|
2016-03-28 10:35:03 -04:00
|
|
|
class Importer
|
2016-12-19 13:44:57 -05:00
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
2016-12-09 10:25:42 -05:00
|
|
|
LABELS = [{ title: 'bug', color: '#FF0000' },
|
|
|
|
{ title: 'enhancement', color: '#428BCA' },
|
|
|
|
{ title: 'proposal', color: '#69D100' },
|
|
|
|
{ title: 'task', color: '#7F8C8D' }].freeze
|
2016-12-07 11:04:02 -05:00
|
|
|
|
2016-12-16 10:35:31 -05:00
|
|
|
attr_reader :project, :client, :errors, :users
|
2016-03-28 10:35:03 -04:00
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2016-08-22 15:13:56 -04:00
|
|
|
@client = Bitbucket::Client.new(project.import_data.credentials)
|
2016-03-28 10:35:03 -04:00
|
|
|
@formatter = Gitlab::ImportFormatter.new
|
2016-12-07 11:04:02 -05:00
|
|
|
@labels = {}
|
2016-12-14 08:18:30 -05:00
|
|
|
@errors = []
|
2016-12-16 10:35:31 -05:00
|
|
|
@users = {}
|
2016-03-28 10:35:03 -04:00
|
|
|
end
|
2015-02-17 10:59:50 -05:00
|
|
|
|
|
|
|
def execute
|
2016-12-19 13:44:57 -05:00
|
|
|
import_wiki
|
2016-08-22 15:13:56 -04:00
|
|
|
import_issues
|
2016-08-22 15:15:39 -04:00
|
|
|
import_pull_requests
|
2016-12-14 08:18:30 -05:00
|
|
|
handle_errors
|
2015-02-17 10:59:50 -05:00
|
|
|
|
2016-01-21 13:53:34 -05:00
|
|
|
true
|
|
|
|
end
|
2015-02-17 10:59:50 -05:00
|
|
|
|
2016-01-21 13:53:34 -05:00
|
|
|
private
|
|
|
|
|
2016-12-14 08:18:30 -05:00
|
|
|
def handle_errors
|
|
|
|
return unless errors.any?
|
2016-12-16 08:40:38 -05:00
|
|
|
|
2018-11-27 04:41:27 -05:00
|
|
|
project.import_state.update_column(:last_error, {
|
2016-12-14 08:18:30 -05:00
|
|
|
message: 'The remote data could not be fully imported.',
|
|
|
|
errors: errors
|
|
|
|
}.to_json)
|
|
|
|
end
|
|
|
|
|
2016-12-14 05:35:10 -05:00
|
|
|
def gitlab_user_id(project, username)
|
2016-12-16 10:35:31 -05:00
|
|
|
find_user_id(username) || project.creator_id
|
2016-01-21 13:53:34 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-12-16 10:35:31 -05:00
|
|
|
def find_user_id(username)
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless username
|
2016-12-13 14:28:04 -05:00
|
|
|
|
2016-12-16 10:35:31 -05:00
|
|
|
return users[username] if users.key?(username)
|
|
|
|
|
|
|
|
users[username] = User.select(:id)
|
|
|
|
.joins(:identities)
|
|
|
|
.find_by("identities.extern_uid = ? AND identities.provider = 'bitbucket'", username)
|
|
|
|
.try(:id)
|
2016-12-13 14:28:04 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-12-13 14:28:04 -05:00
|
|
|
|
2016-08-22 15:13:56 -04:00
|
|
|
def repo
|
|
|
|
@repo ||= client.repo(project.import_source)
|
2016-01-21 13:53:34 -05:00
|
|
|
end
|
|
|
|
|
2016-12-19 13:44:57 -05:00
|
|
|
def import_wiki
|
|
|
|
return if project.wiki.repository_exists?
|
|
|
|
|
2019-01-17 03:35:40 -05:00
|
|
|
wiki = WikiFormatter.new(project)
|
|
|
|
|
|
|
|
gitlab_shell.import_wiki_repository(project, wiki)
|
2016-12-19 13:44:57 -05:00
|
|
|
rescue StandardError => e
|
|
|
|
errors << { type: :wiki, errors: e.message }
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-01-21 13:53:34 -05:00
|
|
|
def import_issues
|
2016-12-07 04:33:32 -05:00
|
|
|
return unless repo.issues_enabled?
|
2016-08-22 15:13:56 -04:00
|
|
|
|
2016-12-07 11:04:02 -05:00
|
|
|
create_labels
|
|
|
|
|
2016-08-22 15:13:56 -04:00
|
|
|
client.issues(repo).each do |issue|
|
2019-03-13 09:42:43 -04:00
|
|
|
description = ''
|
|
|
|
description += @formatter.author_line(issue.author) unless find_user_id(issue.author)
|
|
|
|
description += issue.description
|
|
|
|
|
|
|
|
label_name = issue.kind
|
|
|
|
milestone = issue.milestone ? project.milestones.find_or_create_by(title: issue.milestone) : nil
|
|
|
|
|
|
|
|
gitlab_issue = project.issues.create!(
|
|
|
|
iid: issue.iid,
|
|
|
|
title: issue.title,
|
|
|
|
description: description,
|
|
|
|
state: issue.state,
|
|
|
|
author_id: gitlab_user_id(project, issue.author),
|
|
|
|
milestone: milestone,
|
|
|
|
created_at: issue.created_at,
|
|
|
|
updated_at: issue.updated_at
|
|
|
|
)
|
|
|
|
|
|
|
|
gitlab_issue.labels << @labels[label_name]
|
|
|
|
|
|
|
|
import_issue_comments(issue, gitlab_issue) if gitlab_issue.persisted?
|
|
|
|
rescue StandardError => e
|
|
|
|
errors << { type: :issue, iid: issue.iid, errors: e.message }
|
2016-12-16 14:11:04 -05:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-12-16 14:11:04 -05:00
|
|
|
|
|
|
|
def import_issue_comments(issue, gitlab_issue)
|
|
|
|
client.issue_comments(repo, issue.iid).each do |comment|
|
|
|
|
# The note can be blank for issue service messages like "Changed title: ..."
|
|
|
|
# We would like to import those comments as well but there is no any
|
|
|
|
# specific parameter that would allow to process them, it's just an empty comment.
|
|
|
|
# To prevent our importer from just crashing or from creating useless empty comments
|
|
|
|
# we do this check.
|
|
|
|
next unless comment.note.present?
|
|
|
|
|
|
|
|
note = ''
|
|
|
|
note += @formatter.author_line(comment.author) unless find_user_id(comment.author)
|
|
|
|
note += comment.note
|
|
|
|
|
|
|
|
begin
|
|
|
|
gitlab_issue.notes.create!(
|
|
|
|
project: project,
|
|
|
|
note: note,
|
|
|
|
author_id: gitlab_user_id(project, comment.author),
|
|
|
|
created_at: comment.created_at,
|
|
|
|
updated_at: comment.updated_at
|
|
|
|
)
|
|
|
|
rescue StandardError => e
|
|
|
|
errors << { type: :issue_comment, iid: issue.iid, errors: e.message }
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-22 15:15:39 -04:00
|
|
|
|
2016-12-07 11:04:02 -05:00
|
|
|
def create_labels
|
2017-03-29 07:45:15 -04:00
|
|
|
LABELS.each do |label_params|
|
2019-05-08 08:39:54 -04:00
|
|
|
label = ::Labels::FindOrCreateService.new(nil, project, label_params).execute(skip_authorization: true)
|
2017-03-29 07:45:15 -04:00
|
|
|
if label.valid?
|
|
|
|
@labels[label_params[:title]] = label
|
|
|
|
else
|
2018-03-07 09:07:09 -05:00
|
|
|
raise "Failed to create label \"#{label_params[:title]}\" for project \"#{project.full_name}\""
|
2017-03-29 07:45:15 -04:00
|
|
|
end
|
2016-12-07 11:04:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-22 15:15:39 -04:00
|
|
|
def import_pull_requests
|
|
|
|
pull_requests = client.pull_requests(repo)
|
|
|
|
|
|
|
|
pull_requests.each do |pull_request|
|
2019-03-13 09:42:43 -04:00
|
|
|
description = ''
|
|
|
|
description += @formatter.author_line(pull_request.author) unless find_user_id(pull_request.author)
|
|
|
|
description += pull_request.description
|
|
|
|
|
|
|
|
source_branch_sha = pull_request.source_branch_sha
|
|
|
|
target_branch_sha = pull_request.target_branch_sha
|
|
|
|
source_branch_sha = project.repository.commit(source_branch_sha)&.sha || source_branch_sha
|
|
|
|
target_branch_sha = project.repository.commit(target_branch_sha)&.sha || target_branch_sha
|
|
|
|
|
|
|
|
merge_request = project.merge_requests.create!(
|
|
|
|
iid: pull_request.iid,
|
|
|
|
title: pull_request.title,
|
|
|
|
description: description,
|
|
|
|
source_project: project,
|
|
|
|
source_branch: pull_request.source_branch_name,
|
|
|
|
source_branch_sha: source_branch_sha,
|
|
|
|
target_project: project,
|
|
|
|
target_branch: pull_request.target_branch_name,
|
|
|
|
target_branch_sha: target_branch_sha,
|
|
|
|
state: pull_request.state,
|
|
|
|
author_id: gitlab_user_id(project, pull_request.author),
|
|
|
|
assignee_id: nil,
|
|
|
|
created_at: pull_request.created_at,
|
|
|
|
updated_at: pull_request.updated_at
|
|
|
|
)
|
|
|
|
|
|
|
|
import_pull_request_comments(pull_request, merge_request) if merge_request.persisted?
|
|
|
|
rescue StandardError => e
|
|
|
|
errors << { type: :pull_request, iid: pull_request.iid, errors: e.message, trace: e.backtrace.join("\n"), raw_response: pull_request.raw }
|
2016-08-22 15:15:39 -04:00
|
|
|
end
|
|
|
|
end
|
2016-11-16 03:13:17 -05:00
|
|
|
|
|
|
|
def import_pull_request_comments(pull_request, merge_request)
|
|
|
|
comments = client.pull_request_comments(repo, pull_request.iid)
|
|
|
|
|
|
|
|
inline_comments, pr_comments = comments.partition(&:inline?)
|
|
|
|
|
|
|
|
import_inline_comments(inline_comments, pull_request, merge_request)
|
|
|
|
import_standalone_pr_comments(pr_comments, merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_inline_comments(inline_comments, pull_request, merge_request)
|
2018-08-07 16:53:25 -04:00
|
|
|
position_map = {}
|
|
|
|
discussion_map = {}
|
2016-11-16 03:13:17 -05:00
|
|
|
|
|
|
|
children, parents = inline_comments.partition(&:has_parent?)
|
|
|
|
|
|
|
|
# The Bitbucket API returns threaded replies as parent-child
|
|
|
|
# relationships. We assume that the child can appear in any order in
|
|
|
|
# the JSON.
|
|
|
|
parents.each do |comment|
|
2018-08-07 16:53:25 -04:00
|
|
|
position_map[comment.iid] = build_position(merge_request, comment)
|
2016-11-16 03:13:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
children.each do |comment|
|
2018-08-07 16:53:25 -04:00
|
|
|
position_map[comment.iid] = position_map.fetch(comment.parent_id, nil)
|
2016-11-16 03:13:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
inline_comments.each do |comment|
|
2019-03-13 09:42:43 -04:00
|
|
|
attributes = pull_request_comment_attributes(comment)
|
|
|
|
attributes[:discussion_id] = discussion_map[comment.parent_id] if comment.has_parent?
|
2018-08-07 16:53:25 -04:00
|
|
|
|
2019-03-13 09:42:43 -04:00
|
|
|
attributes.merge!(
|
|
|
|
position: position_map[comment.iid],
|
|
|
|
type: 'DiffNote')
|
2016-11-16 03:13:17 -05:00
|
|
|
|
2019-03-13 09:42:43 -04:00
|
|
|
note = merge_request.notes.create!(attributes)
|
2018-08-07 16:53:25 -04:00
|
|
|
|
2019-03-13 09:42:43 -04:00
|
|
|
# We can't store a discussion ID until a note is created, so if
|
|
|
|
# replies are created before the parent the discussion ID won't be
|
|
|
|
# linked properly.
|
|
|
|
discussion_map[comment.iid] = note.discussion_id
|
|
|
|
rescue StandardError => e
|
|
|
|
errors << { type: :pull_request, iid: comment.iid, errors: e.message }
|
2016-11-16 03:13:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-05 13:34:11 -05:00
|
|
|
def build_position(merge_request, pr_comment)
|
|
|
|
params = {
|
|
|
|
diff_refs: merge_request.diff_refs,
|
|
|
|
old_path: pr_comment.file_path,
|
|
|
|
new_path: pr_comment.file_path,
|
|
|
|
old_line: pr_comment.old_pos,
|
|
|
|
new_line: pr_comment.new_pos
|
|
|
|
}
|
|
|
|
|
|
|
|
Gitlab::Diff::Position.new(params)
|
|
|
|
end
|
|
|
|
|
2016-11-16 03:13:17 -05:00
|
|
|
def import_standalone_pr_comments(pr_comments, merge_request)
|
|
|
|
pr_comments.each do |comment|
|
2019-03-13 09:42:43 -04:00
|
|
|
merge_request.notes.create!(pull_request_comment_attributes(comment))
|
|
|
|
rescue StandardError => e
|
|
|
|
errors << { type: :pull_request, iid: comment.iid, errors: e.message }
|
2016-11-16 03:13:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def pull_request_comment_attributes(comment)
|
|
|
|
{
|
|
|
|
project: project,
|
|
|
|
note: comment.note,
|
|
|
|
author_id: gitlab_user_id(project, comment.author),
|
|
|
|
created_at: comment.created_at,
|
|
|
|
updated_at: comment.updated_at
|
|
|
|
}
|
|
|
|
end
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|