2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
class Import::BitbucketController < Import::BaseController
|
2020-06-23 00:09:42 +05:30
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
include ActionView::Helpers::SanitizeHelper
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :verify_bitbucket_import_enabled
|
|
|
|
before_action :bitbucket_auth, except: :callback
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
rescue_from OAuth2::Error, with: :bitbucket_unauthorized
|
|
|
|
rescue_from Bitbucket::Error::Unauthorized, with: :bitbucket_unauthorized
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
def callback
|
2022-06-21 17:19:12 +05:30
|
|
|
auth_state = session[:bitbucket_auth_state]
|
|
|
|
session[:bitbucket_auth_state] = nil
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
if auth_state.blank? || !ActiveSupport::SecurityUtils.secure_compare(auth_state, params[:state])
|
|
|
|
go_to_bitbucket_for_permissions
|
|
|
|
else
|
2022-08-13 15:12:31 +05:30
|
|
|
response = oauth_client.auth_code.get_token(params[:code], redirect_uri: users_import_bitbucket_callback_url(namespace_id: params[:namespace_id]))
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
session[:bitbucket_token] = response.token
|
|
|
|
session[:bitbucket_expires_at] = response.expires_at
|
|
|
|
session[:bitbucket_expires_in] = response.expires_in
|
|
|
|
session[:bitbucket_refresh_token] = response.refresh_token
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
redirect_to status_import_bitbucket_url(namespace_id: params[:namespace_id])
|
2022-06-21 17:19:12 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
# We need to re-expose controller's internal method 'status' as action.
|
|
|
|
# rubocop:disable Lint/UselessMethodDefinition
|
2015-04-26 12:48:37 +05:30
|
|
|
def status
|
2020-07-28 23:09:34 +05:30
|
|
|
super
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
# rubocop:enable Lint/UselessMethodDefinition
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def create
|
2017-08-17 22:00:37 +05:30
|
|
|
bitbucket_client = Bitbucket::Client.new(credentials)
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
repo_id = params[:repo_id].to_s
|
|
|
|
name = repo_id.gsub('___', '/')
|
2017-08-17 22:00:37 +05:30
|
|
|
repo = bitbucket_client.repo(name)
|
2018-03-27 19:54:05 +05:30
|
|
|
project_name = params[:new_name].presence || repo.name
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
repo_owner = repo.owner
|
|
|
|
repo_owner = current_user.username if repo_owner == bitbucket_client.user.username
|
|
|
|
namespace_path = params[:new_namespace].presence || repo_owner
|
2018-03-27 19:54:05 +05:30
|
|
|
target_namespace = find_or_create_namespace(namespace_path, current_user)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
Gitlab::Tracking.event(
|
|
|
|
self.class.name,
|
|
|
|
'create',
|
|
|
|
label: 'import_access_level',
|
|
|
|
user: current_user,
|
|
|
|
extra: { user_role: user_role(current_user, target_namespace), import_type: 'bitbucket' }
|
|
|
|
)
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
if current_user.can?(:import_projects, target_namespace)
|
2017-08-17 22:00:37 +05:30
|
|
|
# The token in a session can be expired, we need to get most recent one because
|
|
|
|
# Bitbucket::Connection class refreshes it.
|
|
|
|
session[:bitbucket_token] = bitbucket_client.connection.token
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
project = Gitlab::BitbucketImport::ProjectCreator.new(repo, project_name, target_namespace, current_user, credentials).execute
|
|
|
|
|
|
|
|
if project.persisted?
|
2020-06-23 00:09:42 +05:30
|
|
|
render json: ProjectSerializer.new.represent(project, serializer: :import)
|
2018-03-27 19:54:05 +05:30
|
|
|
else
|
2018-11-08 19:23:39 +05:30
|
|
|
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
else
|
2023-05-27 22:25:52 +05:30
|
|
|
render json: { errors: _('You are not allowed to import projects in this namespace.') }, status: :unprocessable_entity
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
protected
|
|
|
|
|
|
|
|
override :importable_repos
|
|
|
|
def importable_repos
|
2023-03-17 16:20:25 +05:30
|
|
|
bitbucket_repos.filter(&:valid?)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
override :incompatible_repos
|
|
|
|
def incompatible_repos
|
2023-03-17 16:20:25 +05:30
|
|
|
bitbucket_repos.reject(&:valid?)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
def provider_url
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
override :provider_name
|
|
|
|
def provider_name
|
|
|
|
:bitbucket
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
private
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def user_role(user, namespace)
|
|
|
|
if current_user.id == namespace&.owner_id
|
|
|
|
Gitlab::Access.options_with_owner.key(Gitlab::Access::OWNER)
|
|
|
|
else
|
|
|
|
access_level = current_user&.group_members&.find_by(source_id: namespace&.id)&.access_level
|
|
|
|
|
|
|
|
case access_level
|
|
|
|
when nil
|
|
|
|
'Not a member'
|
|
|
|
else
|
|
|
|
Gitlab::Access.human_access(access_level)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def oauth_client
|
|
|
|
@oauth_client ||= OAuth2::Client.new(provider.app_id, provider.app_secret, options)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def provider
|
2018-03-27 19:54:05 +05:30
|
|
|
Gitlab::Auth::OAuth::Provider.config_for('bitbucket')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def client
|
|
|
|
@client ||= Bitbucket::Client.new(credentials)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bitbucket_repos
|
|
|
|
@bitbucket_repos ||= client.repos(filter: sanitized_filter_param).to_a
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def options
|
|
|
|
OmniAuth::Strategies::Bitbucket.default_options[:client_options].deep_symbolize_keys
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def verify_bitbucket_import_enabled
|
2015-10-24 18:46:33 +05:30
|
|
|
render_404 unless bitbucket_import_enabled?
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def bitbucket_auth
|
2017-08-17 22:00:37 +05:30
|
|
|
go_to_bitbucket_for_permissions if session[:bitbucket_token].blank?
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def go_to_bitbucket_for_permissions
|
2022-06-21 17:19:12 +05:30
|
|
|
state = SecureRandom.base64(64)
|
|
|
|
session[:bitbucket_auth_state] = state
|
2022-08-13 15:12:31 +05:30
|
|
|
redirect_to oauth_client.auth_code.authorize_url(redirect_uri: users_import_bitbucket_callback_url(namespace_id: params[:namespace_id]), state: state)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
def bitbucket_unauthorized(exception)
|
|
|
|
log_exception(exception)
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
go_to_bitbucket_for_permissions
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def credentials
|
2015-09-25 12:07:36 +05:30
|
|
|
{
|
2017-08-17 22:00:37 +05:30
|
|
|
token: session[:bitbucket_token],
|
|
|
|
expires_at: session[:bitbucket_expires_at],
|
|
|
|
expires_in: session[:bitbucket_expires_in],
|
|
|
|
refresh_token: session[:bitbucket_refresh_token]
|
2015-09-25 12:07:36 +05:30
|
|
|
}
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|