2018-07-25 18:01:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-25 16:06:10 -04:00
|
|
|
class Import::BitbucketServerController < Import::BaseController
|
|
|
|
before_action :verify_bitbucket_server_import_enabled
|
|
|
|
before_action :bitbucket_auth, except: [:new, :configure]
|
2018-07-05 17:09:01 -04:00
|
|
|
before_action :validate_import_params, only: [:create]
|
|
|
|
|
|
|
|
# As a basic sanity check to prevent URL injection, restrict project
|
2018-07-25 08:19:22 -04:00
|
|
|
# repository input and repository slugs to allowed characters. For Bitbucket:
|
2018-07-05 17:09:01 -04:00
|
|
|
#
|
|
|
|
# Project keys must start with a letter and may only consist of ASCII letters, numbers and underscores (A-Z, a-z, 0-9, _).
|
|
|
|
#
|
|
|
|
# Repository names are limited to 128 characters. They must start with a
|
|
|
|
# letter or number and may contain spaces, hyphens, underscores, and periods.
|
|
|
|
# (https://community.atlassian.com/t5/Answers-Developer-Questions/stash-repository-names/qaq-p/499054)
|
2018-12-05 19:43:29 -05:00
|
|
|
#
|
|
|
|
# Bitbucket Server starts personal project names with a tilde.
|
2019-05-05 06:19:14 -04:00
|
|
|
VALID_BITBUCKET_PROJECT_CHARS = /\A~?[\w\-\.\s]+\z/.freeze
|
|
|
|
VALID_BITBUCKET_CHARS = /\A[\w\-\.\s]+\z/.freeze
|
2018-07-05 17:09:01 -04:00
|
|
|
|
2018-06-25 16:06:10 -04:00
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-07-05 17:09:01 -04:00
|
|
|
repo = bitbucket_client.repo(@project_key, @repo_slug)
|
|
|
|
|
|
|
|
unless repo
|
2019-03-27 12:52:52 -04:00
|
|
|
return render json: { errors: _("Project %{project_repo} could not be found") % { project_repo: "#{@project_key}/#{@repo_slug}" } }, status: :unprocessable_entity
|
2018-07-09 00:36:02 -04:00
|
|
|
end
|
2018-07-05 17:09:01 -04:00
|
|
|
|
2018-06-25 16:06:10 -04:00
|
|
|
project_name = params[:new_name].presence || repo.name
|
2018-08-01 13:04:17 -04:00
|
|
|
namespace_path = params[:new_namespace].presence || current_user.username
|
2018-06-25 16:06:10 -04:00
|
|
|
target_namespace = find_or_create_namespace(namespace_path, current_user)
|
|
|
|
|
|
|
|
if current_user.can?(:create_projects, target_namespace)
|
2018-07-05 17:09:01 -04:00
|
|
|
project = Gitlab::BitbucketServerImport::ProjectCreator.new(@project_key, @repo_slug, repo, project_name, target_namespace, current_user, credentials).execute
|
2018-06-25 16:06:10 -04:00
|
|
|
|
|
|
|
if project.persisted?
|
|
|
|
render json: ProjectSerializer.new.represent(project)
|
|
|
|
else
|
|
|
|
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
else
|
2019-03-27 12:52:52 -04:00
|
|
|
render json: { errors: _('This namespace has already been taken! Please choose another one.') }, status: :unprocessable_entity
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
2019-03-27 12:52:52 -04:00
|
|
|
rescue BitbucketServer::Connection::ConnectionError => error
|
|
|
|
render json: { errors: _("Unable to connect to server: %{error}") % { error: error } }, status: :unprocessable_entity
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def configure
|
|
|
|
session[personal_access_token_key] = params[:personal_access_token]
|
|
|
|
session[bitbucket_server_username_key] = params[:bitbucket_username]
|
|
|
|
session[bitbucket_server_url_key] = params[:bitbucket_server_url]
|
|
|
|
|
|
|
|
redirect_to status_import_bitbucket_server_path
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-06-25 16:06:10 -04:00
|
|
|
def status
|
2018-11-05 18:37:21 -05:00
|
|
|
@collection = bitbucket_client.repos(page_offset: page_offset, limit: limit_per_page)
|
|
|
|
@repos, @incompatible_repos = @collection.partition { |repo| repo.valid? }
|
2018-06-25 16:06:10 -04:00
|
|
|
|
2018-11-05 18:37:21 -05:00
|
|
|
# Use the import URL to filter beyond what BaseService#find_already_added_projects
|
|
|
|
@already_added_projects = filter_added_projects('bitbucket_server', @repos.map(&:browse_url))
|
2018-06-25 16:06:10 -04:00
|
|
|
already_added_projects_names = @already_added_projects.pluck(:import_source)
|
|
|
|
|
2018-11-05 18:37:21 -05:00
|
|
|
@repos.reject! { |repo| already_added_projects_names.include?(repo.browse_url) }
|
2019-03-27 12:52:52 -04:00
|
|
|
rescue BitbucketServer::Connection::ConnectionError => error
|
|
|
|
flash[:alert] = _("Unable to connect to server: %{error}") % { error: error }
|
2018-07-05 17:09:01 -04:00
|
|
|
clear_session_data
|
|
|
|
redirect_to new_import_bitbucket_server_path
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-06-25 16:06:10 -04:00
|
|
|
|
|
|
|
def jobs
|
|
|
|
render json: find_jobs('bitbucket_server')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-11-05 18:37:21 -05:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def filter_added_projects(import_type, import_sources)
|
|
|
|
current_user.created_projects.where(import_type: import_type, import_source: import_sources).includes(:import_state)
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2018-07-08 10:44:33 -04:00
|
|
|
def bitbucket_client
|
|
|
|
@bitbucket_client ||= BitbucketServer::Client.new(credentials)
|
|
|
|
end
|
|
|
|
|
2018-07-05 17:09:01 -04:00
|
|
|
def validate_import_params
|
|
|
|
@project_key = params[:project]
|
|
|
|
@repo_slug = params[:repository]
|
|
|
|
|
2018-08-01 13:04:47 -04:00
|
|
|
return render_validation_error('Missing project key') unless @project_key.present? && @repo_slug.present?
|
2018-07-27 02:10:56 -04:00
|
|
|
return render_validation_error('Missing repository slug') unless @repo_slug.present?
|
2018-12-05 19:43:29 -05:00
|
|
|
return render_validation_error('Invalid project key') unless @project_key =~ VALID_BITBUCKET_PROJECT_CHARS
|
2018-07-27 02:10:56 -04:00
|
|
|
return render_validation_error('Invalid repository slug') unless @repo_slug =~ VALID_BITBUCKET_CHARS
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_validation_error(message)
|
2018-07-27 08:14:12 -04:00
|
|
|
render json: { errors: message }, status: :unprocessable_entity
|
2018-07-05 17:09:01 -04:00
|
|
|
end
|
|
|
|
|
2018-06-25 16:06:10 -04:00
|
|
|
def bitbucket_auth
|
|
|
|
unless session[bitbucket_server_url_key].present? &&
|
2018-06-29 01:26:33 -04:00
|
|
|
session[bitbucket_server_username_key].present? &&
|
|
|
|
session[personal_access_token_key].present?
|
2018-06-25 16:06:10 -04:00
|
|
|
redirect_to new_import_bitbucket_server_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_bitbucket_server_import_enabled
|
|
|
|
render_404 unless bitbucket_server_import_enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def bitbucket_server_url_key
|
|
|
|
:bitbucket_server_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def bitbucket_server_username_key
|
|
|
|
:bitbucket_server_username
|
|
|
|
end
|
|
|
|
|
|
|
|
def personal_access_token_key
|
|
|
|
:bitbucket_server_personal_access_token
|
|
|
|
end
|
|
|
|
|
2018-07-05 17:09:01 -04:00
|
|
|
def clear_session_data
|
|
|
|
session[bitbucket_server_url_key] = nil
|
|
|
|
session[bitbucket_server_username_key] = nil
|
|
|
|
session[personal_access_token_key] = nil
|
|
|
|
end
|
|
|
|
|
2018-06-25 16:06:10 -04:00
|
|
|
def credentials
|
|
|
|
{
|
|
|
|
base_uri: session[bitbucket_server_url_key],
|
2018-07-01 08:13:15 -04:00
|
|
|
user: session[bitbucket_server_username_key],
|
2018-06-26 01:40:11 -04:00
|
|
|
password: session[personal_access_token_key]
|
2018-06-25 16:06:10 -04:00
|
|
|
}
|
|
|
|
end
|
2018-11-05 18:37:21 -05:00
|
|
|
|
|
|
|
def page_offset
|
|
|
|
[0, params[:page].to_i].max
|
|
|
|
end
|
|
|
|
|
|
|
|
def limit_per_page
|
|
|
|
BitbucketServer::Paginator::PAGE_LENGTH
|
|
|
|
end
|
2018-06-25 16:06:10 -04:00
|
|
|
end
|