2018-09-23 15:44:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-17 10:59:50 -05:00
|
|
|
class Import::BitbucketController < Import::BaseController
|
2020-06-15 11:08:20 -04:00
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2019-09-26 14:06:29 -04:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
|
|
|
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :verify_bitbucket_import_enabled
|
|
|
|
before_action :bitbucket_auth, except: :callback
|
2015-02-17 10:59:50 -05:00
|
|
|
|
2016-12-09 09:59:23 -05:00
|
|
|
rescue_from OAuth2::Error, with: :bitbucket_unauthorized
|
2016-08-22 15:02:48 -04:00
|
|
|
rescue_from Bitbucket::Error::Unauthorized, with: :bitbucket_unauthorized
|
2015-02-17 10:59:50 -05:00
|
|
|
|
|
|
|
def callback
|
2020-06-15 11:08:20 -04:00
|
|
|
response = oauth_client.auth_code.get_token(params[:code], redirect_uri: users_import_bitbucket_callback_url)
|
2015-02-17 10:59:50 -05:00
|
|
|
|
2016-08-22 15:02:48 -04:00
|
|
|
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-02-17 10:59:50 -05:00
|
|
|
|
|
|
|
redirect_to status_import_bitbucket_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
2020-07-16 20:09:37 -04:00
|
|
|
super
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
|
2020-06-15 11:08:20 -04:00
|
|
|
def realtime_changes
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2015-02-17 10:59:50 -05:00
|
|
|
def create
|
2016-11-11 18:44:52 -05:00
|
|
|
bitbucket_client = Bitbucket::Client.new(credentials)
|
2016-08-22 15:02:48 -04:00
|
|
|
|
2018-02-09 06:14:48 -05:00
|
|
|
repo_id = params[:repo_id].to_s
|
|
|
|
name = repo_id.gsub('___', '/')
|
2016-11-11 18:44:52 -05:00
|
|
|
repo = bitbucket_client.repo(name)
|
2018-02-09 06:14:48 -05:00
|
|
|
project_name = params[:new_name].presence || repo.name
|
2015-02-17 10:59:50 -05:00
|
|
|
|
2016-08-22 15:02:48 -04:00
|
|
|
repo_owner = repo.owner
|
2016-11-11 18:44:52 -05:00
|
|
|
repo_owner = current_user.username if repo_owner == bitbucket_client.user.username
|
2017-03-21 01:17:46 -04:00
|
|
|
namespace_path = params[:new_namespace].presence || repo_owner
|
2018-02-09 06:14:48 -05:00
|
|
|
target_namespace = find_or_create_namespace(namespace_path, current_user)
|
2016-08-22 15:02:48 -04:00
|
|
|
|
2018-02-09 06:14:48 -05:00
|
|
|
if current_user.can?(:create_projects, target_namespace)
|
2016-12-15 07:19:28 -05:00
|
|
|
# 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-02-09 06:14:48 -05:00
|
|
|
|
|
|
|
project = Gitlab::BitbucketImport::ProjectCreator.new(repo, project_name, target_namespace, current_user, credentials).execute
|
|
|
|
|
|
|
|
if project.persisted?
|
2020-06-15 11:08:20 -04:00
|
|
|
render json: ProjectSerializer.new.represent(project, serializer: :import)
|
2018-02-09 06:14:48 -05:00
|
|
|
else
|
2018-06-06 01:34:06 -04:00
|
|
|
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
|
2018-02-09 06:14:48 -05:00
|
|
|
end
|
2016-08-30 13:34:37 -04:00
|
|
|
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
|
2016-08-30 13:34:37 -04:00
|
|
|
end
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
|
2020-06-15 11:08:20 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
override :importable_repos
|
|
|
|
def importable_repos
|
2021-08-20 11:10:24 -04:00
|
|
|
bitbucket_repos.filter { |repo| repo.valid? }
|
2020-06-15 11:08:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
override :incompatible_repos
|
|
|
|
def incompatible_repos
|
|
|
|
bitbucket_repos.reject { |repo| repo.valid? }
|
|
|
|
end
|
|
|
|
|
|
|
|
override :provider_name
|
|
|
|
def provider_name
|
|
|
|
:bitbucket
|
|
|
|
end
|
|
|
|
|
|
|
|
override :provider_url
|
|
|
|
def provider_url
|
|
|
|
provider.url
|
|
|
|
end
|
|
|
|
|
2015-02-17 10:59:50 -05:00
|
|
|
private
|
|
|
|
|
2020-06-15 11:08:20 -04:00
|
|
|
def oauth_client
|
|
|
|
@oauth_client ||= OAuth2::Client.new(provider.app_id, provider.app_secret, options)
|
2016-08-22 15:02:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider
|
2018-02-23 07:10:39 -05:00
|
|
|
Gitlab::Auth::OAuth::Provider.config_for('bitbucket')
|
2016-08-22 15:02:48 -04:00
|
|
|
end
|
|
|
|
|
2020-06-15 11:08:20 -04:00
|
|
|
def client
|
|
|
|
@client ||= Bitbucket::Client.new(credentials)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bitbucket_repos
|
|
|
|
@bitbucket_repos ||= client.repos(filter: sanitized_filter_param).to_a
|
|
|
|
end
|
|
|
|
|
2016-08-22 15:02:48 -04:00
|
|
|
def options
|
|
|
|
OmniAuth::Strategies::Bitbucket.default_options[:client_options].deep_symbolize_keys
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
|
2015-02-17 16:52:32 -05:00
|
|
|
def verify_bitbucket_import_enabled
|
2015-10-09 13:07:29 -04:00
|
|
|
render_404 unless bitbucket_import_enabled?
|
2015-02-17 16:52:32 -05:00
|
|
|
end
|
|
|
|
|
2015-02-17 10:59:50 -05:00
|
|
|
def bitbucket_auth
|
2016-08-22 15:02:48 -04:00
|
|
|
go_to_bitbucket_for_permissions if session[:bitbucket_token].blank?
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def go_to_bitbucket_for_permissions
|
2020-06-15 11:08:20 -04:00
|
|
|
redirect_to oauth_client.auth_code.authorize_url(redirect_uri: users_import_bitbucket_callback_url)
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
|
2021-11-16 16:12:05 -05:00
|
|
|
def bitbucket_unauthorized(exception)
|
|
|
|
log_exception(exception)
|
|
|
|
|
2015-02-17 10:59:50 -05:00
|
|
|
go_to_bitbucket_for_permissions
|
|
|
|
end
|
2015-08-07 03:06:20 -04:00
|
|
|
|
2016-08-22 15:02:48 -04:00
|
|
|
def credentials
|
2015-08-07 03:06:20 -04:00
|
|
|
{
|
2016-08-22 15:02:48 -04:00
|
|
|
token: session[:bitbucket_token],
|
|
|
|
expires_at: session[:bitbucket_expires_at],
|
|
|
|
expires_in: session[:bitbucket_expires_in],
|
|
|
|
refresh_token: session[:bitbucket_refresh_token]
|
2015-08-07 03:06:20 -04:00
|
|
|
}
|
|
|
|
end
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|