2015-02-17 10:59:50 -05:00
|
|
|
module Gitlab
|
|
|
|
module BitbucketImport
|
2016-03-28 10:35:03 -04:00
|
|
|
class Importer
|
|
|
|
attr_reader :project, :client
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
|
|
|
@client = Client.from_project(@project)
|
|
|
|
@formatter = Gitlab::ImportFormatter.new
|
|
|
|
end
|
2015-02-17 10:59:50 -05:00
|
|
|
|
|
|
|
def execute
|
2016-01-21 13:53:34 -05:00
|
|
|
import_issues if has_issues?
|
2015-02-17 10:59:50 -05:00
|
|
|
|
2016-01-21 13:53:34 -05:00
|
|
|
true
|
2016-01-22 10:00:00 -05:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
raise Projects::ImportService::Error.new, e.message
|
2016-01-21 13:53:34 -05:00
|
|
|
ensure
|
|
|
|
Gitlab::BitbucketImport::KeyDeleter.new(project).execute
|
|
|
|
end
|
2015-02-17 10:59:50 -05:00
|
|
|
|
2016-01-21 13:53:34 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def gl_user_id(project, bitbucket_id)
|
|
|
|
if bitbucket_id
|
|
|
|
user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'bitbucket'", bitbucket_id.to_s)
|
|
|
|
(user && user.id) || project.creator_id
|
|
|
|
else
|
|
|
|
project.creator_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def identifier
|
|
|
|
project.import_source
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_issues?
|
|
|
|
client.project(identifier)["has_issues"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_issues
|
|
|
|
issues = client.issues(identifier)
|
2015-08-07 03:06:20 -04:00
|
|
|
|
2015-08-31 20:03:09 -04:00
|
|
|
issues.each do |issue|
|
|
|
|
body = ''
|
|
|
|
reporter = nil
|
|
|
|
author = 'Anonymous'
|
|
|
|
|
|
|
|
if issue["reported_by"] && issue["reported_by"]["username"]
|
|
|
|
reporter = issue["reported_by"]["username"]
|
|
|
|
author = reporter
|
|
|
|
end
|
|
|
|
|
|
|
|
body = @formatter.author_line(author)
|
|
|
|
body += issue["content"]
|
2015-08-07 03:06:20 -04:00
|
|
|
|
2016-01-21 13:53:34 -05:00
|
|
|
comments = client.issue_comments(identifier, issue["local_id"])
|
2015-08-07 03:06:20 -04:00
|
|
|
|
2015-02-17 10:59:50 -05:00
|
|
|
if comments.any?
|
|
|
|
body += @formatter.comments_header
|
|
|
|
end
|
|
|
|
|
|
|
|
comments.each do |comment|
|
2015-08-31 20:03:09 -04:00
|
|
|
author = 'Anonymous'
|
|
|
|
|
|
|
|
if comment["author_info"] && comment["author_info"]["username"]
|
|
|
|
author = comment["author_info"]["username"]
|
|
|
|
end
|
|
|
|
|
|
|
|
body += @formatter.comment(author, comment["utc_created_on"], comment["content"])
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
project.issues.create!(
|
2015-08-07 03:06:20 -04:00
|
|
|
description: body,
|
2015-02-17 10:59:50 -05:00
|
|
|
title: issue["title"],
|
2016-02-22 18:59:13 -05:00
|
|
|
state: %w(resolved invalid duplicate wontfix closed).include?(issue["status"]) ? 'closed' : 'opened',
|
2015-08-31 20:03:09 -04:00
|
|
|
author_id: gl_user_id(project, reporter)
|
2015-02-17 10:59:50 -05:00
|
|
|
)
|
|
|
|
end
|
2016-01-21 13:53:34 -05:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
raise Projects::ImportService::Error, e.message
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|