2015-02-17 10:59:50 -05:00
|
|
|
class Import::BitbucketController < Import::BaseController
|
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
|
|
|
|
2015-02-21 05:08:05 -05:00
|
|
|
rescue_from OAuth::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
|
2016-08-22 15:02:48 -04:00
|
|
|
response = client.auth_code.get_token(params[:code], redirect_uri: callback_import_bitbucket_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
|
2016-11-11 18:44:52 -05:00
|
|
|
bitbucket_client = Bitbucket::Client.new(credentials)
|
|
|
|
repos = bitbucket_client.repos
|
2016-08-22 15:02:48 -04:00
|
|
|
|
2016-11-11 18:44:52 -05:00
|
|
|
@repos, @incompatible_repos = repos.partition { |repo| repo.valid? }
|
2015-04-16 08:03:37 -04:00
|
|
|
|
2016-08-22 15:02:48 -04:00
|
|
|
@already_added_projects = current_user.created_projects.where(import_type: 'bitbucket')
|
2015-02-17 10:59:50 -05:00
|
|
|
already_added_projects_names = @already_added_projects.pluck(:import_source)
|
|
|
|
|
2016-08-22 15:02:48 -04:00
|
|
|
@repos.to_a.reject! { |repo| already_added_projects_names.include?(repo.full_name) }
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def jobs
|
2016-08-22 15:02:48 -04:00
|
|
|
render json: current_user.created_projects
|
|
|
|
.where(import_type: 'bitbucket')
|
|
|
|
.to_json(only: [:id, :import_status])
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2016-11-11 18:44:52 -05:00
|
|
|
bitbucket_client = Bitbucket::Client.new(credentials)
|
2016-08-22 15:02:48 -04:00
|
|
|
|
2016-08-29 17:17:11 -04:00
|
|
|
@repo_id = params[:repo_id].to_s
|
2016-11-11 17:44:31 -05:00
|
|
|
name = @repo_id.gsub('___', '/')
|
2016-11-11 18:44:52 -05:00
|
|
|
repo = bitbucket_client.repo(name)
|
2016-11-11 17:44:31 -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
|
2016-08-22 15:02:48 -04:00
|
|
|
@target_namespace = params[:new_namespace].presence || repo_owner
|
|
|
|
|
2016-11-11 17:00:33 -05:00
|
|
|
namespace = find_or_create_namespace(@target_namespace, repo_owner)
|
2015-02-17 10:59:50 -05:00
|
|
|
|
2016-11-11 17:00:33 -05:00
|
|
|
if current_user.can?(:create_projects, namespace)
|
2016-11-11 17:44:31 -05:00
|
|
|
@project = Gitlab::BitbucketImport::ProjectCreator.new(repo, @project_name, namespace, current_user, credentials).execute
|
2016-08-30 13:34:37 -04:00
|
|
|
else
|
|
|
|
render 'unauthorized'
|
|
|
|
end
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client
|
2016-08-22 15:02:48 -04:00
|
|
|
@client ||= OAuth2::Client.new(provider.app_id, provider.app_secret, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def provider
|
|
|
|
Gitlab.config.omniauth.providers.find { |provider| provider.name == 'bitbucket' }
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2016-08-22 15:02:48 -04:00
|
|
|
redirect_to client.auth_code.authorize_url(redirect_uri: callback_import_bitbucket_url)
|
2015-02-17 10:59:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def bitbucket_unauthorized
|
|
|
|
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
|