debian-mirror-gitlab/lib/gitlab/bitbucket_import/importer.rb

86 lines
2.3 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
module Gitlab
module BitbucketImport
class Importer
attr_reader :project, :client
def initialize(project)
@project = project
2016-06-02 11:05:42 +05:30
@client = Client.from_project(@project)
2015-04-26 12:48:37 +05:30
@formatter = Gitlab::ImportFormatter.new
end
def execute
2016-04-02 18:10:28 +05:30
import_issues if has_issues?
2015-04-26 12:48:37 +05:30
2016-04-02 18:10:28 +05:30
true
rescue ActiveRecord::RecordInvalid => e
raise Projects::ImportService::Error.new, e.message
ensure
Gitlab::BitbucketImport::KeyDeleter.new(project).execute
end
2015-04-26 12:48:37 +05:30
2016-04-02 18:10:28 +05:30
private
2016-09-29 09:46:39 +05:30
def gitlab_user_id(project, bitbucket_id)
2016-04-02 18:10:28 +05:30
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-09-25 12:07:36 +05:30
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"]
2016-04-02 18:10:28 +05:30
comments = client.issue_comments(identifier, issue["local_id"])
2015-09-25 12:07:36 +05:30
2015-04-26 12:48:37 +05:30
if comments.any?
body += @formatter.comments_header
end
comments.each do |comment|
2015-09-25 12:07:36 +05:30
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-04-26 12:48:37 +05:30
end
project.issues.create!(
2015-09-25 12:07:36 +05:30
description: body,
2015-04-26 12:48:37 +05:30
title: issue["title"],
2016-06-02 11:05:42 +05:30
state: %w(resolved invalid duplicate wontfix closed).include?(issue["status"]) ? 'closed' : 'opened',
2016-09-29 09:46:39 +05:30
author_id: gitlab_user_id(project, reporter)
2015-04-26 12:48:37 +05:30
)
end
2016-04-02 18:10:28 +05:30
rescue ActiveRecord::RecordInvalid => e
raise Projects::ImportService::Error, e.message
2015-04-26 12:48:37 +05:30
end
end
end
end