Commit 10d5da78 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'fix-api-project-transfer' into 'master'

Fix transferring of project to another group using the API.

Addresses internal issue https://dev.gitlab.org/gitlab/gitlabhq/issues/2397.

See merge request !926
parents 64c57502 29cbb2f0
...@@ -50,6 +50,7 @@ v 7.12.2 ...@@ -50,6 +50,7 @@ v 7.12.2
- Correctly show anonymous authorized applications under Profile > Applications. - Correctly show anonymous authorized applications under Profile > Applications.
- Faster automerge check and merge itself when source and target branches are in same repository - Faster automerge check and merge itself when source and target branches are in same repository
- Audit log for user authentication - Audit log for user authentication
- Fix transferring of project to another group using the API.
v 7.12.1 v 7.12.1
- Fix error when deleting a user who has projects (Stan Hu) - Fix error when deleting a user who has projects (Stan Hu)
......
...@@ -23,7 +23,8 @@ class Admin::ProjectsController < Admin::ApplicationController ...@@ -23,7 +23,8 @@ class Admin::ProjectsController < Admin::ApplicationController
end end
def transfer def transfer
::Projects::TransferService.new(@project, current_user, params.dup).execute namespace = Namespace.find_by(id: params[:new_namespace_id])
::Projects::TransferService.new(@project, current_user, params.dup).execute(namespace)
@project.reload @project.reload
redirect_to admin_namespace_project_path(@project.namespace, @project) redirect_to admin_namespace_project_path(@project.namespace, @project)
......
...@@ -52,10 +52,11 @@ class ProjectsController < ApplicationController ...@@ -52,10 +52,11 @@ class ProjectsController < ApplicationController
end end
def transfer def transfer
transfer_params = params.permit(:new_namespace_id) namespace = Namespace.find_by(id: params[:new_namespace_id])
::Projects::TransferService.new(project, current_user, transfer_params).execute ::Projects::TransferService.new(project, current_user).execute(namespace)
if @project.errors[:namespace_id].present?
flash[:alert] = @project.errors[:namespace_id].first if @project.errors[:new_namespace].present?
flash[:alert] = @project.errors[:new_namespace].first
end end
end end
......
...@@ -11,19 +11,16 @@ module Projects ...@@ -11,19 +11,16 @@ module Projects
include Gitlab::ShellAdapter include Gitlab::ShellAdapter
class TransferError < StandardError; end class TransferError < StandardError; end
def execute def execute(new_namespace)
namespace_id = params[:new_namespace_id] if allowed_transfer?(current_user, project, new_namespace)
namespace = Namespace.find_by(id: namespace_id) transfer(project, new_namespace)
if allowed_transfer?(current_user, project, namespace)
transfer(project, namespace)
else else
project.errors.add(:namespace, 'is invalid') project.errors.add(:new_namespace, 'is invalid')
false false
end end
rescue Projects::TransferService::TransferError => ex rescue Projects::TransferService::TransferError => ex
project.reload project.reload
project.errors.add(:namespace_id, ex.message) project.errors.add(:new_namespace, ex.message)
false false
end end
......
...@@ -74,9 +74,9 @@ module API ...@@ -74,9 +74,9 @@ module API
# POST /groups/:id/projects/:project_id # POST /groups/:id/projects/:project_id
post ":id/projects/:project_id" do post ":id/projects/:project_id" do
authenticated_as_admin! authenticated_as_admin!
group = Group.find(params[:id]) group = Group.find_by(id: params[:id])
project = Project.find(params[:project_id]) project = Project.find(params[:project_id])
result = ::Projects::TransferService.new(project, current_user, namespace_id: group.id).execute result = ::Projects::TransferService.new(project, current_user).execute(group)
if result if result
present group present group
......
...@@ -8,7 +8,7 @@ describe Projects::TransferService do ...@@ -8,7 +8,7 @@ describe Projects::TransferService do
context 'namespace -> namespace' do context 'namespace -> namespace' do
before do before do
group.add_owner(user) group.add_owner(user)
@result = transfer_project(project, user, new_namespace_id: group.id) @result = transfer_project(project, user, group)
end end
it { expect(@result).to be_truthy } it { expect(@result).to be_truthy }
...@@ -17,7 +17,7 @@ describe Projects::TransferService do ...@@ -17,7 +17,7 @@ describe Projects::TransferService do
context 'namespace -> no namespace' do context 'namespace -> no namespace' do
before do before do
@result = transfer_project(project, user, new_namespace_id: nil) @result = transfer_project(project, user, nil)
end end
it { expect(@result).to eq false } it { expect(@result).to eq false }
...@@ -26,14 +26,14 @@ describe Projects::TransferService do ...@@ -26,14 +26,14 @@ describe Projects::TransferService do
context 'namespace -> not allowed namespace' do context 'namespace -> not allowed namespace' do
before do before do
@result = transfer_project(project, user, new_namespace_id: group.id) @result = transfer_project(project, user, group)
end end
it { expect(@result).to eq false } it { expect(@result).to eq false }
it { expect(project.namespace).to eq(user.namespace) } it { expect(project.namespace).to eq(user.namespace) }
end end
def transfer_project(project, user, params) def transfer_project(project, user, new_namespace)
Projects::TransferService.new(project, user, params).execute Projects::TransferService.new(project, user).execute(new_namespace)
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