2015-04-26 12:48:37 +05:30
|
|
|
class Import::BitbucketController < Import::BaseController
|
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
|
|
|
|
|
|
|
rescue_from OAuth::Error, with: :bitbucket_unauthorized
|
2015-09-11 14:41:01 +05:30
|
|
|
rescue_from Gitlab::BitbucketImport::Client::Unauthorized, with: :bitbucket_unauthorized
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
def callback
|
2015-09-11 14:41:01 +05:30
|
|
|
request_token = session.delete(:oauth_request_token)
|
2015-04-26 12:48:37 +05:30
|
|
|
raise "Session expired!" if request_token.nil?
|
|
|
|
|
|
|
|
request_token.symbolize_keys!
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
access_token = client.get_token(request_token, params[:oauth_verifier], callback_import_bitbucket_url)
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
session[:bitbucket_access_token] = access_token.token
|
|
|
|
session[:bitbucket_access_token_secret] = access_token.secret
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
redirect_to status_import_bitbucket_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
|
|
|
@repos = client.projects
|
2015-09-11 14:41:01 +05:30
|
|
|
@incompatible_repos = client.incompatible_projects
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
@already_added_projects = current_user.created_projects.where(import_type: "bitbucket")
|
|
|
|
already_added_projects_names = @already_added_projects.pluck(:import_source)
|
|
|
|
|
|
|
|
@repos.to_a.reject!{ |repo| already_added_projects_names.include? "#{repo["owner"]}/#{repo["slug"]}" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def jobs
|
|
|
|
jobs = current_user.created_projects.where(import_type: "bitbucket").to_json(only: [:id, :import_status])
|
|
|
|
render json: jobs
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2016-09-29 09:46:39 +05:30
|
|
|
@repo_id = params[:repo_id].to_s
|
|
|
|
repo = client.project(@repo_id.gsub('___', '/'))
|
|
|
|
@project_name = repo['slug']
|
|
|
|
@target_namespace = find_or_create_namespace(repo['owner'], client.user['user']['username'])
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
unless Gitlab::BitbucketImport::KeyAdder.new(repo, current_user, access_params).execute
|
2016-09-29 09:46:39 +05:30
|
|
|
render 'deploy_key' and return
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
if current_user.can?(:create_projects, @target_namespace)
|
|
|
|
@project = Gitlab::BitbucketImport::ProjectCreator.new(repo, @target_namespace, current_user, access_params).execute
|
|
|
|
else
|
|
|
|
render 'unauthorized'
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client
|
2015-09-25 12:07:36 +05:30
|
|
|
@client ||= Gitlab::BitbucketImport::Client.new(session[:bitbucket_access_token],
|
|
|
|
session[:bitbucket_access_token_secret])
|
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
|
2015-09-25 12:07:36 +05:30
|
|
|
if session[:bitbucket_access_token].blank?
|
2015-04-26 12:48:37 +05:30
|
|
|
go_to_bitbucket_for_permissions
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def go_to_bitbucket_for_permissions
|
|
|
|
request_token = client.request_token(callback_import_bitbucket_url)
|
|
|
|
session[:oauth_request_token] = request_token
|
|
|
|
|
|
|
|
redirect_to client.authorize_url(request_token, callback_import_bitbucket_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bitbucket_unauthorized
|
|
|
|
go_to_bitbucket_for_permissions
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
def access_params
|
|
|
|
{
|
|
|
|
bitbucket_access_token: session[:bitbucket_access_token],
|
|
|
|
bitbucket_access_token_secret: session[:bitbucket_access_token_secret]
|
|
|
|
}
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|