Commit 07415e47 authored by Stan Hu's avatar Stan Hu

Fix Error 500 when Bitbucket importer does not have authorization

Closes #29739
parent 60853843
...@@ -44,15 +44,15 @@ class Import::BitbucketController < Import::BaseController ...@@ -44,15 +44,15 @@ class Import::BitbucketController < Import::BaseController
repo_owner = repo.owner repo_owner = repo.owner
repo_owner = current_user.username if repo_owner == bitbucket_client.user.username repo_owner = current_user.username if repo_owner == bitbucket_client.user.username
@target_namespace = params[:new_namespace].presence || repo_owner namespace_path = params[:new_namespace].presence || repo_owner
namespace = find_or_create_namespace(@target_namespace, current_user) @target_namespace = find_or_create_namespace(namespace_path, current_user)
if current_user.can?(:create_projects, namespace) if current_user.can?(:create_projects, @target_namespace)
# The token in a session can be expired, we need to get most recent one because # The token in a session can be expired, we need to get most recent one because
# Bitbucket::Connection class refreshes it. # Bitbucket::Connection class refreshes it.
session[:bitbucket_token] = bitbucket_client.connection.token session[:bitbucket_token] = bitbucket_client.connection.token
@project = Gitlab::BitbucketImport::ProjectCreator.new(repo, @project_name, namespace, current_user, credentials).execute @project = Gitlab::BitbucketImport::ProjectCreator.new(repo, @project_name, @target_namespace, current_user, credentials).execute
else else
render 'unauthorized' render 'unauthorized'
end end
......
...@@ -112,6 +112,17 @@ describe Import::BitbucketController do ...@@ -112,6 +112,17 @@ describe Import::BitbucketController do
post :create, format: :js post :create, format: :js
end end
end end
context 'when the Bitbucket user is unauthorized' do
render_views
it 'returns unauthorized' do
allow(controller).to receive(:current_user).and_return(user)
allow(user).to receive(:can?).and_return(false)
post :create, format: :js
end
end
end end
context "when the repository owner is not the Bitbucket user" do context "when the repository owner is not the Bitbucket user" do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment