Commit a4a47cfb authored by Filipa Lacerda's avatar Filipa Lacerda

Merge branch 'axios-post-importer' into 'master'

Replace $.post in importer status with axios

See merge request gitlab-org/gitlab-ce!16692
parents d997fe13 71c948d6
import { __ } from './locale';
import axios from './lib/utils/axios_utils';
import flash from './flash';
class ImporterStatus { class ImporterStatus {
constructor(jobsUrl, importUrl) { constructor(jobsUrl, importUrl) {
this.jobsUrl = jobsUrl; this.jobsUrl = jobsUrl;
...@@ -9,7 +13,20 @@ class ImporterStatus { ...@@ -9,7 +13,20 @@ class ImporterStatus {
initStatusPage() { initStatusPage() {
$('.js-add-to-import') $('.js-add-to-import')
.off('click') .off('click')
.on('click', (event) => { .on('click', this.addToImport.bind(this));
$('.js-import-all')
.off('click')
.on('click', function onClickImportAll() {
const $btn = $(this);
$btn.disable().addClass('is-loading');
return $('.js-add-to-import').each(function triggerAddImport() {
return $(this).trigger('click');
});
});
}
addToImport(event) {
const $btn = $(event.currentTarget); const $btn = $(event.currentTarget);
const $tr = $btn.closest('tr'); const $tr = $btn.closest('tr');
const $targetField = $tr.find('.import-target'); const $targetField = $tr.find('.import-target');
...@@ -24,24 +41,22 @@ class ImporterStatus { ...@@ -24,24 +41,22 @@ class ImporterStatus {
} }
$btn.disable().addClass('is-loading'); $btn.disable().addClass('is-loading');
return $.post(this.importUrl, { return axios.post(this.importUrl, {
repo_id: id, repo_id: id,
target_namespace: targetNamespace, target_namespace: targetNamespace,
new_name: newName, new_name: newName,
}, { })
dataType: 'script', .then(({ data }) => {
}); const job = $(`tr#repo_${id}`);
}); job.attr('id', `project_${data.id}`);
$('.js-import-all') job.find('.import-target').html(`<a href="${data.full_path}">${data.full_path}</a>`);
.off('click') $('table.import-jobs tbody').prepend(job);
.on('click', function onClickImportAll() {
const $btn = $(this); job.addClass('active');
$btn.disable().addClass('is-loading'); job.find('.import-actions').html('<i class="fa fa-spinner fa-spin" aria-label="importing"></i> started');
return $('.js-add-to-import').each(function triggerAddImport() { })
return $(this).trigger('click'); .catch(() => flash(__('An error occurred while importing project')));
});
});
} }
setAutoUpdate() { setAutoUpdate() {
...@@ -71,7 +86,7 @@ class ImporterStatus { ...@@ -71,7 +86,7 @@ class ImporterStatus {
} }
// eslint-disable-next-line consistent-return // eslint-disable-next-line consistent-return
export default function initImporterStatus() { function initImporterStatus() {
const importerStatus = document.querySelector('.js-importer-status'); const importerStatus = document.querySelector('.js-importer-status');
if (importerStatus) { if (importerStatus) {
...@@ -79,3 +94,8 @@ export default function initImporterStatus() { ...@@ -79,3 +94,8 @@ export default function initImporterStatus() {
return new ImporterStatus(data.jobsImportPath, data.importPath); return new ImporterStatus(data.jobsImportPath, data.importPath);
} }
} }
export {
initImporterStatus as default,
ImporterStatus,
};
...@@ -37,24 +37,30 @@ class Import::BitbucketController < Import::BaseController ...@@ -37,24 +37,30 @@ class Import::BitbucketController < Import::BaseController
def create def create
bitbucket_client = Bitbucket::Client.new(credentials) bitbucket_client = Bitbucket::Client.new(credentials)
@repo_id = params[:repo_id].to_s repo_id = params[:repo_id].to_s
name = @repo_id.gsub('___', '/') name = repo_id.gsub('___', '/')
repo = bitbucket_client.repo(name) repo = bitbucket_client.repo(name)
@project_name = params[:new_name].presence || repo.name project_name = params[:new_name].presence || repo.name
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
namespace_path = params[:new_namespace].presence || repo_owner namespace_path = params[:new_namespace].presence || repo_owner
target_namespace = find_or_create_namespace(namespace_path, current_user)
@target_namespace = find_or_create_namespace(namespace_path, current_user) if current_user.can?(:create_projects, target_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, @target_namespace, current_user, credentials).execute
project = Gitlab::BitbucketImport::ProjectCreator.new(repo, project_name, target_namespace, current_user, credentials).execute
if project.persisted?
render json: ProjectSerializer.new.represent(project)
else
render json: { errors: project.errors.full_messages }, status: :unprocessable_entity
end
else else
render 'unauthorized' render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
end end
end end
......
...@@ -58,17 +58,17 @@ class Import::FogbugzController < Import::BaseController ...@@ -58,17 +58,17 @@ class Import::FogbugzController < Import::BaseController
end end
def create def create
@repo_id = params[:repo_id] repo = client.repo(params[:repo_id])
repo = client.repo(@repo_id)
fb_session = { uri: session[:fogbugz_uri], token: session[:fogbugz_token] } fb_session = { uri: session[:fogbugz_uri], token: session[:fogbugz_token] }
@target_namespace = current_user.namespace
@project_name = repo.name
namespace = @target_namespace
umap = session[:fogbugz_user_map] || client.user_map umap = session[:fogbugz_user_map] || client.user_map
@project = Gitlab::FogbugzImport::ProjectCreator.new(repo, fb_session, namespace, current_user, umap).execute project = Gitlab::FogbugzImport::ProjectCreator.new(repo, fb_session, current_user.namespace, current_user, umap).execute
if project.persisted?
render json: ProjectSerializer.new.represent(project)
else
render json: { errors: project.errors.full_messages }, status: :unprocessable_entity
end
end end
private private
......
...@@ -36,16 +36,21 @@ class Import::GithubController < Import::BaseController ...@@ -36,16 +36,21 @@ class Import::GithubController < Import::BaseController
end end
def create def create
@repo_id = params[:repo_id].to_i repo = client.repo(params[:repo_id].to_i)
repo = client.repo(@repo_id) project_name = params[:new_name].presence || repo.name
@project_name = params[:new_name].presence || repo.name
namespace_path = params[:target_namespace].presence || current_user.namespace_path namespace_path = params[:target_namespace].presence || current_user.namespace_path
@target_namespace = find_or_create_namespace(namespace_path, current_user.namespace_path) target_namespace = find_or_create_namespace(namespace_path, current_user.namespace_path)
if can?(current_user, :create_projects, @target_namespace) if can?(current_user, :create_projects, target_namespace)
@project = Gitlab::LegacyGithubImport::ProjectCreator.new(repo, @project_name, @target_namespace, current_user, access_params, type: provider).execute project = Gitlab::LegacyGithubImport::ProjectCreator.new(repo, project_name, target_namespace, current_user, access_params, type: provider).execute
if project.persisted?
render json: ProjectSerializer.new.represent(project)
else
render json: { errors: project.errors.full_messages }, status: :unprocessable_entity
end
else else
render 'unauthorized' render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
end end
end end
......
...@@ -24,15 +24,19 @@ class Import::GitlabController < Import::BaseController ...@@ -24,15 +24,19 @@ class Import::GitlabController < Import::BaseController
end end
def create def create
@repo_id = params[:repo_id].to_i repo = client.project(params[:repo_id].to_i)
repo = client.project(@repo_id) target_namespace = find_or_create_namespace(repo['namespace']['path'], client.user['username'])
@project_name = repo['name']
@target_namespace = find_or_create_namespace(repo['namespace']['path'], client.user['username'])
if current_user.can?(:create_projects, @target_namespace) if current_user.can?(:create_projects, target_namespace)
@project = Gitlab::GitlabImport::ProjectCreator.new(repo, @target_namespace, current_user, access_params).execute project = Gitlab::GitlabImport::ProjectCreator.new(repo, target_namespace, current_user, access_params).execute
if project.persisted?
render json: ProjectSerializer.new.represent(project)
else
render json: { errors: project.errors.full_messages }, status: :unprocessable_entity
end
else else
render 'unauthorized' render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
end end
end end
......
...@@ -85,16 +85,16 @@ class Import::GoogleCodeController < Import::BaseController ...@@ -85,16 +85,16 @@ class Import::GoogleCodeController < Import::BaseController
end end
def create def create
@repo_id = params[:repo_id] repo = client.repo(params[:repo_id])
repo = client.repo(@repo_id)
@target_namespace = current_user.namespace
@project_name = repo.name
namespace = @target_namespace
user_map = session[:google_code_user_map] user_map = session[:google_code_user_map]
@project = Gitlab::GoogleCodeImport::ProjectCreator.new(repo, namespace, current_user, user_map).execute project = Gitlab::GoogleCodeImport::ProjectCreator.new(repo, current_user.namespace, current_user, user_map).execute
if project.persisted?
render json: ProjectSerializer.new.represent(project)
else
render json: { errors: project.errors.full_messages }, status: :unprocessable_entity
end
end end
private private
......
class ProjectSerializer < BaseSerializer
entity ProjectEntity
end
- if @project.persisted?
:plain
job = $("tr#repo_#{@repo_id}")
job.attr("id", "project_#{@project.id}")
target_field = job.find(".import-target")
target_field.empty()
target_field.append('#{link_to @project.full_path, project_path(@project)}')
$("table.import-jobs tbody").prepend(job)
job.addClass("active").find(".import-actions").html("<i class='fa fa-spinner fa-spin'></i> started")
- else
:plain
job = $("tr#repo_#{@repo_id}")
job.find(".import-actions").html("<i class='fa fa-exclamation-circle'></i> Error saving project: #{escape_javascript(h(@project.errors.full_messages.join(',')))}")
:plain
tr = $("tr#repo_#{@repo_id}")
target_field = tr.find(".import-target")
import_button = tr.find(".btn-import")
origin_target = target_field.text()
project_name = "#{@project_name}"
origin_namespace = "#{@target_namespace.full_path}"
target_field.empty()
target_field.append("<p class='alert alert-danger'>This namespace has already been taken! Please choose another one.</p>")
target_field.append("<input type='text' name='target_namespace' />")
target_field.append("/" + project_name)
target_field.data("project_name", project_name)
target_field.find('input').prop("value", origin_namespace)
import_button.enable().removeClass('is-loading')
...@@ -84,20 +84,42 @@ describe Import::BitbucketController do ...@@ -84,20 +84,42 @@ describe Import::BitbucketController do
double(slug: "vim", owner: bitbucket_username, name: 'vim') double(slug: "vim", owner: bitbucket_username, name: 'vim')
end end
let(:project) { create(:project) }
before do before do
allow_any_instance_of(Bitbucket::Client).to receive(:repo).and_return(bitbucket_repo) allow_any_instance_of(Bitbucket::Client).to receive(:repo).and_return(bitbucket_repo)
allow_any_instance_of(Bitbucket::Client).to receive(:user).and_return(bitbucket_user) allow_any_instance_of(Bitbucket::Client).to receive(:user).and_return(bitbucket_user)
assign_session_tokens assign_session_tokens
end end
it 'returns 200 response when the project is imported successfully' do
allow(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params)
.and_return(double(execute: project))
post :create, format: :json
expect(response).to have_gitlab_http_status(200)
end
it 'returns 422 response when the project could not be imported' do
allow(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params)
.and_return(double(execute: build(:project)))
post :create, format: :json
expect(response).to have_gitlab_http_status(422)
end
context "when the repository owner is the Bitbucket user" do context "when the repository owner is the Bitbucket user" do
context "when the Bitbucket user and GitLab user's usernames match" do context "when the Bitbucket user and GitLab user's usernames match" do
it "takes the current user's namespace" do it "takes the current user's namespace" do
expect(Gitlab::BitbucketImport::ProjectCreator) expect(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params) .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
...@@ -107,9 +129,9 @@ describe Import::BitbucketController do ...@@ -107,9 +129,9 @@ describe Import::BitbucketController do
it "takes the current user's namespace" do it "takes the current user's namespace" do
expect(Gitlab::BitbucketImport::ProjectCreator) expect(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params) .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
...@@ -120,7 +142,7 @@ describe Import::BitbucketController do ...@@ -120,7 +142,7 @@ describe Import::BitbucketController do
allow(controller).to receive(:current_user).and_return(user) allow(controller).to receive(:current_user).and_return(user)
allow(user).to receive(:can?).and_return(false) allow(user).to receive(:can?).and_return(false)
post :create, format: :js post :create, format: :json
end end
end end
end end
...@@ -143,9 +165,9 @@ describe Import::BitbucketController do ...@@ -143,9 +165,9 @@ describe Import::BitbucketController do
it "takes the existing namespace" do it "takes the existing namespace" do
expect(Gitlab::BitbucketImport::ProjectCreator) expect(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, bitbucket_repo.name, existing_namespace, user, access_params) .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, existing_namespace, user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
...@@ -154,7 +176,7 @@ describe Import::BitbucketController do ...@@ -154,7 +176,7 @@ describe Import::BitbucketController do
expect(Gitlab::BitbucketImport::ProjectCreator) expect(Gitlab::BitbucketImport::ProjectCreator)
.not_to receive(:new) .not_to receive(:new)
post :create, format: :js post :create, format: :json
end end
end end
end end
...@@ -163,17 +185,17 @@ describe Import::BitbucketController do ...@@ -163,17 +185,17 @@ describe Import::BitbucketController do
context "when current user can create namespaces" do context "when current user can create namespaces" do
it "creates the namespace" do it "creates the namespace" do
expect(Gitlab::BitbucketImport::ProjectCreator) expect(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).and_return(double(execute: true)) .to receive(:new).and_return(double(execute: project))
expect { post :create, format: :js }.to change(Namespace, :count).by(1) expect { post :create, format: :json }.to change(Namespace, :count).by(1)
end end
it "takes the new namespace" do it "takes the new namespace" do
expect(Gitlab::BitbucketImport::ProjectCreator) expect(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, bitbucket_repo.name, an_instance_of(Group), user, access_params) .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, an_instance_of(Group), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
...@@ -184,17 +206,17 @@ describe Import::BitbucketController do ...@@ -184,17 +206,17 @@ describe Import::BitbucketController do
it "doesn't create the namespace" do it "doesn't create the namespace" do
expect(Gitlab::BitbucketImport::ProjectCreator) expect(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).and_return(double(execute: true)) .to receive(:new).and_return(double(execute: project))
expect { post :create, format: :js }.not_to change(Namespace, :count) expect { post :create, format: :json }.not_to change(Namespace, :count)
end end
it "takes the current user's namespace" do it "takes the current user's namespace" do
expect(Gitlab::BitbucketImport::ProjectCreator) expect(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params) .to receive(:new).with(bitbucket_repo, bitbucket_repo.name, user.namespace, user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
end end
...@@ -212,9 +234,9 @@ describe Import::BitbucketController do ...@@ -212,9 +234,9 @@ describe Import::BitbucketController do
it 'takes the selected namespace and name' do it 'takes the selected namespace and name' do
expect(Gitlab::BitbucketImport::ProjectCreator) expect(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, test_name, nested_namespace, user, access_params) .to receive(:new).with(bitbucket_repo, test_name, nested_namespace, user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: nested_namespace.full_path, new_name: test_name, format: :js } post :create, { target_namespace: nested_namespace.full_path, new_name: test_name, format: :json }
end end
end end
...@@ -224,26 +246,26 @@ describe Import::BitbucketController do ...@@ -224,26 +246,26 @@ describe Import::BitbucketController do
it 'takes the selected namespace and name' do it 'takes the selected namespace and name' do
expect(Gitlab::BitbucketImport::ProjectCreator) expect(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params) .to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js } post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :json }
end end
it 'creates the namespaces' do it 'creates the namespaces' do
allow(Gitlab::BitbucketImport::ProjectCreator) allow(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params) .to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
expect { post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js } } expect { post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :json } }
.to change { Namespace.count }.by(2) .to change { Namespace.count }.by(2)
end end
it 'new namespace has the right parent' do it 'new namespace has the right parent' do
allow(Gitlab::BitbucketImport::ProjectCreator) allow(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params) .to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js } post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :json }
expect(Namespace.find_by_path_or_name('bar').parent.path).to eq('foo') expect(Namespace.find_by_path_or_name('bar').parent.path).to eq('foo')
end end
...@@ -256,19 +278,29 @@ describe Import::BitbucketController do ...@@ -256,19 +278,29 @@ describe Import::BitbucketController do
it 'takes the selected namespace and name' do it 'takes the selected namespace and name' do
expect(Gitlab::BitbucketImport::ProjectCreator) expect(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params) .to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :js } post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :json }
end end
it 'creates the namespaces' do it 'creates the namespaces' do
allow(Gitlab::BitbucketImport::ProjectCreator) allow(Gitlab::BitbucketImport::ProjectCreator)
.to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params) .to receive(:new).with(bitbucket_repo, test_name, kind_of(Namespace), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
expect { post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :js } } expect { post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :json } }
.to change { Namespace.count }.by(2) .to change { Namespace.count }.by(2)
end end
end end
context 'when user can not create projects in the chosen namespace' do
it 'returns 422 response' do
other_namespace = create(:group, name: 'other_namespace')
post :create, { target_namespace: other_namespace.name, format: :json }
expect(response).to have_gitlab_http_status(422)
end
end
end end
end end
...@@ -57,6 +57,7 @@ describe Import::GitlabController do ...@@ -57,6 +57,7 @@ describe Import::GitlabController do
end end
describe "POST create" do describe "POST create" do
let(:project) { create(:project) }
let(:gitlab_username) { user.username } let(:gitlab_username) { user.username }
let(:gitlab_user) do let(:gitlab_user) do
{ username: gitlab_username }.with_indifferent_access { username: gitlab_username }.with_indifferent_access
...@@ -75,14 +76,34 @@ describe Import::GitlabController do ...@@ -75,14 +76,34 @@ describe Import::GitlabController do
assign_session_token assign_session_token
end end
it 'returns 200 response when the project is imported successfully' do
allow(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, user.namespace, user, access_params)
.and_return(double(execute: project))
post :create, format: :json
expect(response).to have_gitlab_http_status(200)
end
it 'returns 422 response when the project could not be imported' do
allow(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, user.namespace, user, access_params)
.and_return(double(execute: build(:project)))
post :create, format: :json
expect(response).to have_gitlab_http_status(422)
end
context "when the repository owner is the GitLab.com user" do context "when the repository owner is the GitLab.com user" do
context "when the GitLab.com user and GitLab server user's usernames match" do context "when the GitLab.com user and GitLab server user's usernames match" do
it "takes the current user's namespace" do it "takes the current user's namespace" do
expect(Gitlab::GitlabImport::ProjectCreator) expect(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, user.namespace, user, access_params) .to receive(:new).with(gitlab_repo, user.namespace, user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
...@@ -92,9 +113,9 @@ describe Import::GitlabController do ...@@ -92,9 +113,9 @@ describe Import::GitlabController do
it "takes the current user's namespace" do it "takes the current user's namespace" do
expect(Gitlab::GitlabImport::ProjectCreator) expect(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, user.namespace, user, access_params) .to receive(:new).with(gitlab_repo, user.namespace, user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
end end
...@@ -118,9 +139,9 @@ describe Import::GitlabController do ...@@ -118,9 +139,9 @@ describe Import::GitlabController do
it "takes the existing namespace" do it "takes the existing namespace" do
expect(Gitlab::GitlabImport::ProjectCreator) expect(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, existing_namespace, user, access_params) .to receive(:new).with(gitlab_repo, existing_namespace, user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
...@@ -129,7 +150,7 @@ describe Import::GitlabController do ...@@ -129,7 +150,7 @@ describe Import::GitlabController do
expect(Gitlab::GitlabImport::ProjectCreator) expect(Gitlab::GitlabImport::ProjectCreator)
.not_to receive(:new) .not_to receive(:new)
post :create, format: :js post :create, format: :json
end end
end end
end end
...@@ -138,17 +159,17 @@ describe Import::GitlabController do ...@@ -138,17 +159,17 @@ describe Import::GitlabController do
context "when current user can create namespaces" do context "when current user can create namespaces" do
it "creates the namespace" do it "creates the namespace" do
expect(Gitlab::GitlabImport::ProjectCreator) expect(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).and_return(double(execute: true)) .to receive(:new).and_return(double(execute: project))
expect { post :create, format: :js }.to change(Namespace, :count).by(1) expect { post :create, format: :json }.to change(Namespace, :count).by(1)
end end
it "takes the new namespace" do it "takes the new namespace" do
expect(Gitlab::GitlabImport::ProjectCreator) expect(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, an_instance_of(Group), user, access_params) .to receive(:new).with(gitlab_repo, an_instance_of(Group), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
...@@ -159,17 +180,17 @@ describe Import::GitlabController do ...@@ -159,17 +180,17 @@ describe Import::GitlabController do
it "doesn't create the namespace" do it "doesn't create the namespace" do
expect(Gitlab::GitlabImport::ProjectCreator) expect(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).and_return(double(execute: true)) .to receive(:new).and_return(double(execute: project))
expect { post :create, format: :js }.not_to change(Namespace, :count) expect { post :create, format: :json }.not_to change(Namespace, :count)
end end
it "takes the current user's namespace" do it "takes the current user's namespace" do
expect(Gitlab::GitlabImport::ProjectCreator) expect(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, user.namespace, user, access_params) .to receive(:new).with(gitlab_repo, user.namespace, user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
end end
...@@ -185,9 +206,9 @@ describe Import::GitlabController do ...@@ -185,9 +206,9 @@ describe Import::GitlabController do
it 'takes the selected namespace and name' do it 'takes the selected namespace and name' do
expect(Gitlab::GitlabImport::ProjectCreator) expect(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, nested_namespace, user, access_params) .to receive(:new).with(gitlab_repo, nested_namespace, user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: nested_namespace.full_path, format: :js } post :create, { target_namespace: nested_namespace.full_path, format: :json }
end end
end end
...@@ -197,26 +218,26 @@ describe Import::GitlabController do ...@@ -197,26 +218,26 @@ describe Import::GitlabController do
it 'takes the selected namespace and name' do it 'takes the selected namespace and name' do
expect(Gitlab::GitlabImport::ProjectCreator) expect(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, kind_of(Namespace), user, access_params) .to receive(:new).with(gitlab_repo, kind_of(Namespace), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: 'foo/bar', format: :js } post :create, { target_namespace: 'foo/bar', format: :json }
end end
it 'creates the namespaces' do it 'creates the namespaces' do
allow(Gitlab::GitlabImport::ProjectCreator) allow(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, kind_of(Namespace), user, access_params) .to receive(:new).with(gitlab_repo, kind_of(Namespace), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
expect { post :create, { target_namespace: 'foo/bar', format: :js } } expect { post :create, { target_namespace: 'foo/bar', format: :json } }
.to change { Namespace.count }.by(2) .to change { Namespace.count }.by(2)
end end
it 'new namespace has the right parent' do it 'new namespace has the right parent' do
allow(Gitlab::GitlabImport::ProjectCreator) allow(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, kind_of(Namespace), user, access_params) .to receive(:new).with(gitlab_repo, kind_of(Namespace), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: 'foo/bar', format: :js } post :create, { target_namespace: 'foo/bar', format: :json }
expect(Namespace.find_by_path_or_name('bar').parent.path).to eq('foo') expect(Namespace.find_by_path_or_name('bar').parent.path).to eq('foo')
end end
...@@ -229,20 +250,30 @@ describe Import::GitlabController do ...@@ -229,20 +250,30 @@ describe Import::GitlabController do
it 'takes the selected namespace and name' do it 'takes the selected namespace and name' do
expect(Gitlab::GitlabImport::ProjectCreator) expect(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, kind_of(Namespace), user, access_params) .to receive(:new).with(gitlab_repo, kind_of(Namespace), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: 'foo/foobar/bar', format: :js } post :create, { target_namespace: 'foo/foobar/bar', format: :json }
end end
it 'creates the namespaces' do it 'creates the namespaces' do
allow(Gitlab::GitlabImport::ProjectCreator) allow(Gitlab::GitlabImport::ProjectCreator)
.to receive(:new).with(gitlab_repo, kind_of(Namespace), user, access_params) .to receive(:new).with(gitlab_repo, kind_of(Namespace), user, access_params)
.and_return(double(execute: true)) .and_return(double(execute: project))
expect { post :create, { target_namespace: 'foo/foobar/bar', format: :js } } expect { post :create, { target_namespace: 'foo/foobar/bar', format: :json } }
.to change { Namespace.count }.by(2) .to change { Namespace.count }.by(2)
end end
end end
context 'when user can not create projects in the chosen namespace' do
it 'returns 422 response' do
other_namespace = create(:group, name: 'other_namespace')
post :create, { target_namespace: other_namespace.name, format: :json }
expect(response).to have_gitlab_http_status(422)
end
end
end end
end end
end end
import { ImporterStatus } from '~/importer_status';
import axios from '~/lib/utils/axios_utils';
import MockAdapter from 'axios-mock-adapter';
describe('Importer Status', () => {
describe('addToImport', () => {
let instance;
let mock;
const importUrl = '/import_url';
beforeEach(() => {
setFixtures(`
<tr id="repo_123">
<td class="import-target"></td>
<td class="import-actions job-status">
<button name="button" type="submit" class="btn btn-import js-add-to-import">
</button>
</td>
</tr>
`);
spyOn(ImporterStatus.prototype, 'initStatusPage').and.callFake(() => {});
spyOn(ImporterStatus.prototype, 'setAutoUpdate').and.callFake(() => {});
instance = new ImporterStatus('', importUrl);
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
it('sets table row to active after post request', (done) => {
mock.onPost(importUrl).reply(200, {
id: 1,
full_path: '/full_path',
});
instance.addToImport({
currentTarget: document.querySelector('.js-add-to-import'),
})
.then(() => {
expect(document.querySelector('tr').classList.contains('active')).toEqual(true);
done();
})
.catch(done.fail);
});
});
});
...@@ -92,6 +92,7 @@ end ...@@ -92,6 +92,7 @@ end
shared_examples 'a GitHub-ish import controller: POST create' do shared_examples 'a GitHub-ish import controller: POST create' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project) }
let(:provider_username) { user.username } let(:provider_username) { user.username }
let(:provider_user) { OpenStruct.new(login: provider_username) } let(:provider_user) { OpenStruct.new(login: provider_username) }
let(:provider_repo) do let(:provider_repo) do
...@@ -107,14 +108,34 @@ shared_examples 'a GitHub-ish import controller: POST create' do ...@@ -107,14 +108,34 @@ shared_examples 'a GitHub-ish import controller: POST create' do
assign_session_token(provider) assign_session_token(provider)
end end
it 'returns 200 response when the project is imported successfully' do
allow(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider)
.and_return(double(execute: project))
post :create, format: :json
expect(response).to have_gitlab_http_status(200)
end
it 'returns 422 response when the project could not be imported' do
allow(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider)
.and_return(double(execute: build(:project)))
post :create, format: :json
expect(response).to have_gitlab_http_status(422)
end
context "when the repository owner is the provider user" do context "when the repository owner is the provider user" do
context "when the provider user and GitLab user's usernames match" do context "when the provider user and GitLab user's usernames match" do
it "takes the current user's namespace" do it "takes the current user's namespace" do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider) .to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
...@@ -124,9 +145,9 @@ shared_examples 'a GitHub-ish import controller: POST create' do ...@@ -124,9 +145,9 @@ shared_examples 'a GitHub-ish import controller: POST create' do
it "takes the current user's namespace" do it "takes the current user's namespace" do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider) .to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
end end
...@@ -151,9 +172,9 @@ shared_examples 'a GitHub-ish import controller: POST create' do ...@@ -151,9 +172,9 @@ shared_examples 'a GitHub-ish import controller: POST create' do
it "takes the existing namespace" do it "takes the existing namespace" do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, existing_namespace, user, access_params, type: provider) .to receive(:new).with(provider_repo, provider_repo.name, existing_namespace, user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
...@@ -163,9 +184,9 @@ shared_examples 'a GitHub-ish import controller: POST create' do ...@@ -163,9 +184,9 @@ shared_examples 'a GitHub-ish import controller: POST create' do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider) .to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
end end
...@@ -174,17 +195,17 @@ shared_examples 'a GitHub-ish import controller: POST create' do ...@@ -174,17 +195,17 @@ shared_examples 'a GitHub-ish import controller: POST create' do
context "when current user can create namespaces" do context "when current user can create namespaces" do
it "creates the namespace" do it "creates the namespace" do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).and_return(double(execute: true)) .to receive(:new).and_return(double(execute: project))
expect { post :create, target_namespace: provider_repo.name, format: :js }.to change(Namespace, :count).by(1) expect { post :create, target_namespace: provider_repo.name, format: :json }.to change(Namespace, :count).by(1)
end end
it "takes the new namespace" do it "takes the new namespace" do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, an_instance_of(Group), user, access_params, type: provider) .to receive(:new).with(provider_repo, provider_repo.name, an_instance_of(Group), user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, target_namespace: provider_repo.name, format: :js post :create, target_namespace: provider_repo.name, format: :json
end end
end end
...@@ -195,17 +216,17 @@ shared_examples 'a GitHub-ish import controller: POST create' do ...@@ -195,17 +216,17 @@ shared_examples 'a GitHub-ish import controller: POST create' do
it "doesn't create the namespace" do it "doesn't create the namespace" do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).and_return(double(execute: true)) .to receive(:new).and_return(double(execute: project))
expect { post :create, format: :js }.not_to change(Namespace, :count) expect { post :create, format: :json }.not_to change(Namespace, :count)
end end
it "takes the current user's namespace" do it "takes the current user's namespace" do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider) .to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, format: :js post :create, format: :json
end end
end end
end end
...@@ -221,17 +242,17 @@ shared_examples 'a GitHub-ish import controller: POST create' do ...@@ -221,17 +242,17 @@ shared_examples 'a GitHub-ish import controller: POST create' do
it 'takes the selected namespace and name' do it 'takes the selected namespace and name' do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, test_name, test_namespace, user, access_params, type: provider) .to receive(:new).with(provider_repo, test_name, test_namespace, user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: test_namespace.name, new_name: test_name, format: :js } post :create, { target_namespace: test_namespace.name, new_name: test_name, format: :json }
end end
it 'takes the selected name and default namespace' do it 'takes the selected name and default namespace' do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, test_name, user.namespace, user, access_params, type: provider) .to receive(:new).with(provider_repo, test_name, user.namespace, user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { new_name: test_name, format: :js } post :create, { new_name: test_name, format: :json }
end end
end end
...@@ -247,9 +268,9 @@ shared_examples 'a GitHub-ish import controller: POST create' do ...@@ -247,9 +268,9 @@ shared_examples 'a GitHub-ish import controller: POST create' do
it 'takes the selected namespace and name' do it 'takes the selected namespace and name' do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, test_name, nested_namespace, user, access_params, type: provider) .to receive(:new).with(provider_repo, test_name, nested_namespace, user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: nested_namespace.full_path, new_name: test_name, format: :js } post :create, { target_namespace: nested_namespace.full_path, new_name: test_name, format: :json }
end end
end end
...@@ -259,26 +280,26 @@ shared_examples 'a GitHub-ish import controller: POST create' do ...@@ -259,26 +280,26 @@ shared_examples 'a GitHub-ish import controller: POST create' do
it 'takes the selected namespace and name' do it 'takes the selected namespace and name' do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider) .to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js } post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :json }
end end
it 'creates the namespaces' do it 'creates the namespaces' do
allow(Gitlab::LegacyGithubImport::ProjectCreator) allow(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider) .to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
expect { post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js } } expect { post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :json } }
.to change { Namespace.count }.by(2) .to change { Namespace.count }.by(2)
end end
it 'new namespace has the right parent' do it 'new namespace has the right parent' do
allow(Gitlab::LegacyGithubImport::ProjectCreator) allow(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider) .to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :js } post :create, { target_namespace: 'foo/bar', new_name: test_name, format: :json }
expect(Namespace.find_by_path_or_name('bar').parent.path).to eq('foo') expect(Namespace.find_by_path_or_name('bar').parent.path).to eq('foo')
end end
...@@ -291,19 +312,29 @@ shared_examples 'a GitHub-ish import controller: POST create' do ...@@ -291,19 +312,29 @@ shared_examples 'a GitHub-ish import controller: POST create' do
it 'takes the selected namespace and name' do it 'takes the selected namespace and name' do
expect(Gitlab::LegacyGithubImport::ProjectCreator) expect(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider) .to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :js } post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :json }
end end
it 'creates the namespaces' do it 'creates the namespaces' do
allow(Gitlab::LegacyGithubImport::ProjectCreator) allow(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider) .to receive(:new).with(provider_repo, test_name, kind_of(Namespace), user, access_params, type: provider)
.and_return(double(execute: true)) .and_return(double(execute: project))
expect { post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :js } } expect { post :create, { target_namespace: 'foo/foobar/bar', new_name: test_name, format: :json } }
.to change { Namespace.count }.by(2) .to change { Namespace.count }.by(2)
end end
end end
context 'when user can not create projects in the chosen namespace' do
it 'returns 422 response' do
other_namespace = create(:group, name: 'other_namespace')
post :create, { target_namespace: other_namespace.name, format: :json }
expect(response).to have_gitlab_http_status(422)
end
end
end end
end end
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