More work towards supporting Bitbucket Server

This commit is contained in:
Stan Hu 2018-06-25 22:40:11 -07:00
parent c9deb7cef8
commit 046a5e398d
8 changed files with 33 additions and 165 deletions

View File

@ -20,7 +20,7 @@ class Import::BitbucketServerController < Import::BaseController
target_namespace = find_or_create_namespace(namespace_path, current_user)
if current_user.can?(:create_projects, target_namespace)
project = Gitlab::BitbucketImport::ProjectCreator.new(repo, project_name, target_namespace, current_user, credentials).execute
project = Gitlab::BitbucketServerImport::ProjectCreator.new(project_slug, repo_slug, repo, project_name, target_namespace, current_user, credentials).execute
if project.persisted?
render json: ProjectSerializer.new.represent(project)
@ -86,7 +86,7 @@ class Import::BitbucketServerController < Import::BaseController
{
base_uri: session[bitbucket_server_url_key],
username: session[bitbucket_server_username_key],
personal_access_token: session[personal_access_token_key]
password: session[personal_access_token_key]
}
end
end

View File

@ -16,8 +16,8 @@ module BitbucketServer
get_collection(path, :comment)
end
def pull_requests(repo)
path = "/repositories/#{repo}/pullrequests?state=ALL"
def pull_requests(project_key, repository_slug)
path = "/projects/#{project}/repos/#{repo}/pull-requests?state=ALL"
get_collection(path, :pull_request)
end

View File

@ -8,7 +8,7 @@ module BitbucketServer
@api_version = options.fetch(:api_version, DEFAULT_API_VERSION)
@base_uri = options[:base_uri]
@username = options[:username]
@token = options[:personal_access_token]
@token = options[:password]
end
def get(path, extra_query = {})

View File

@ -1,53 +0,0 @@
module Bitbucket
module Representation
class Issue < Representation::Base
CLOSED_STATUS = %w(resolved invalid duplicate wontfix closed).freeze
def iid
raw['id']
end
def kind
raw['kind']
end
def author
raw.dig('reporter', 'username')
end
def description
raw.fetch('content', {}).fetch('raw', nil)
end
def state
closed? ? 'closed' : 'opened'
end
def title
raw['title']
end
def milestone
raw['milestone']['name'] if raw['milestone'].present?
end
def created_at
raw['created_on']
end
def updated_at
raw['edited_on']
end
def to_s
iid
end
private
def closed?
CLOSED_STATUS.include?(raw['state'])
end
end
end
end

View File

@ -2,7 +2,7 @@ module BitbucketServer
module Representation
class PullRequest < Representation::Base
def author
raw.fetch('author', {}).fetch('username', nil)
raw.fetch('author', {}).fetch('user', {}).fetch('name')
end
def description
@ -24,11 +24,11 @@ module BitbucketServer
end
def created_at
raw['created_on']
raw['createdDate']
end
def updated_at
raw['updated_on']
raw['updatedDate']
end
def title
@ -36,29 +36,29 @@ module BitbucketServer
end
def source_branch_name
source_branch.fetch('branch', {}).fetch('name', nil)
source_branch['id']
end
def source_branch_sha
source_branch.fetch('commit', {}).fetch('hash', nil)
# XXX Not implemented?
end
def target_branch_name
target_branch.fetch('branch', {}).fetch('name', nil)
target_branch['id']
end
def target_branch_sha
target_branch.fetch('commit', {}).fetch('hash', nil)
# XXX Not implemented?
end
private
def source_branch
raw['source']
raw['fromRef'] || {}
end
def target_branch
raw['destination']
raw['toRef'] || {}
end
end
end

View File

@ -15,16 +15,8 @@ module BitbucketServer
raw['slug']
end
def clone_url(token = nil)
url = raw['links']['clone'].find { |link| link['name'].starts_with?('http') }.fetch('href')
if token.present?
clone_url = URI.parse(url)
clone_url.user = "x-token-auth:#{token}"
clone_url.to_s
else
url
end
def clone_url
raw['links']['clone'].find { |link| link['name'].starts_with?('http') }.fetch('href')
end
def description

View File

@ -8,10 +8,12 @@ module Gitlab
{ title: 'proposal', color: '#69D100' },
{ title: 'task', color: '#7F8C8D' }].freeze
attr_reader :project, :client, :errors, :users
attr_reader :project_key, :repository_slug, :client, :errors, :users
def initialize(project)
@project = project
@project_key = project.import_data.data['project_key']
@repository_slug = project.import_data.data['repo_slug']
@client = BitbucketServer::Client.new(project.import_data.credentials)
@formatter = Gitlab::ImportFormatter.new
@labels = {}
@ -20,7 +22,6 @@ module Gitlab
end
def execute
import_issues
import_pull_requests
handle_errors
@ -49,7 +50,7 @@ module Gitlab
users[username] = User.select(:id)
.joins(:identities)
.find_by("identities.extern_uid = ? AND identities.provider = 'bitbucket'", username)
.find_by("identities.extern_uid = ? AND identities.provider = 'bitbucket_server'", username)
.try(:id)
end
@ -57,80 +58,8 @@ module Gitlab
@repo ||= client.repo(project.import_source)
end
def import_issues
return unless repo.issues_enabled?
create_labels
client.issues(repo).each do |issue|
begin
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 }
end
end
end
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 }
end
end
end
def create_labels
LABELS.each do |label_params|
label = ::Labels::CreateService.new(label_params).execute(project: project)
if label.valid?
@labels[label_params[:title]] = label
else
raise "Failed to create label \"#{label_params[:title]}\" for project \"#{project.full_name}\""
end
end
end
def import_pull_requests
pull_requests = client.pull_requests(repo)
pull_requests = client.pull_requests(project_key, repository_slug)
pull_requests.each do |pull_request|
begin

View File

@ -1,9 +1,11 @@
module Gitlab
module BitbucketServerImport
class ProjectCreator
attr_reader :repo, :name, :namespace, :current_user, :session_data
attr_reader :project_key, :repo_slug, :repo, :name, :namespace, :current_user, :session_data
def initialize(repo, name, namespace, current_user, session_data)
def initialize(project_key, repo_slug, repo, name, namespace, current_user, session_data)
@project_key = project_key
@repo_slug = repo_slug
@repo = repo
@name = name
@namespace = namespace
@ -19,19 +21,17 @@ module Gitlab
description: repo.description,
namespace_id: namespace.id,
visibility_level: repo.visibility_level,
import_type: 'bitbucket',
import_type: 'bitbucket_server',
import_source: repo.full_name,
import_url: repo.clone_url(session_data[:token]),
import_data: { credentials: session_data },
skip_wiki: skip_wiki
import_url: repo.clone_url,
import_data: {
credentials: session_data,
data: { project_key: project_key,
repo_slug: repo_slug },
},
skip_wiki: true
).execute
end
private
def skip_wiki
repo.has_wiki?
end
end
end
end