Skip to content
代码片段 群组 项目
未验证 提交 86765d38 编辑于 作者: Gosia Ksionek's avatar Gosia Ksionek 提交者: GitLab
浏览文件

Merge branch 'move-delete-group-internal-api' into 'master'

Add json support to destroy group controller action

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/182640



Merged-by: default avatarGosia Ksionek <mksionek@gitlab.com>
Approved-by: default avatarAbdul Wadood <awadood@gitlab.com>
Approved-by: default avatarGosia Ksionek <mksionek@gitlab.com>
Co-authored-by: default avatarPeter Hegman <phegman@gitlab.com>
No related branches found
No related tags found
2 合并请求!3031Merge per-main-jh to main-jh by luzhiyuan,!3030Merge per-main-jh to main-jh
...@@ -181,10 +181,18 @@ def edit_group_origin_location ...@@ -181,10 +181,18 @@ def edit_group_origin_location
def destroy def destroy
Groups::DestroyService.new(@group, current_user).async_execute Groups::DestroyService.new(@group, current_user).async_execute
message = format(_("Group '%{group_name}' is being deleted."), group_name: @group.full_name)
flash[:toast] = format(_("Group '%{group_name}' is being deleted."), group_name: @group.full_name) respond_to do |format|
format.html do
flash[:toast] = message
redirect_to root_path, status: :found
end
redirect_to root_path, status: :found format.json do
render json: { message: message }
end
end
end end
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
......
...@@ -49,9 +49,31 @@ def destroy ...@@ -49,9 +49,31 @@ def destroy
result = ::Groups::MarkForDeletionService.new(group, current_user).execute result = ::Groups::MarkForDeletionService.new(group, current_user).execute
if result[:status] == :success if result[:status] == :success
redirect_to group_path(group), status: :found respond_to do |format|
format.html do
redirect_to group_path(group), status: :found
end
format.json do
render json: {
message: format(
_("'%{group_name}' has been scheduled for deletion and will be deleted on %{date}."),
group_name: group.name,
date: permanent_deletion_date_formatted(group, group.marked_for_deletion_on)
)
}
end
end
else else
redirect_to edit_group_path(group), status: :found, alert: result[:message] respond_to do |format|
format.html do
redirect_to edit_group_path(group), status: :found, alert: result[:message]
end
format.json do
render json: { message: result[:message] }, status: :unprocessable_entity
end
end
end end
end end
...@@ -71,9 +93,17 @@ def restore ...@@ -71,9 +93,17 @@ def restore
def check_subscription! def check_subscription!
if group.linked_to_subscription? if group.linked_to_subscription?
redirect_to edit_group_path(group), respond_to do |format|
status: :found, format.html do
alert: _('This group is linked to a subscription') redirect_to edit_group_path(group),
status: :found,
alert: _('This group is linked to a subscription')
end
format.json do
render json: { message: _('This group is linked to a subscription') }, status: :unprocessable_entity
end
end
end end
end end
......
...@@ -246,7 +246,10 @@ ...@@ -246,7 +246,10 @@
end end
describe 'DELETE #destroy' do describe 'DELETE #destroy' do
subject { delete :destroy, params: { id: group.to_param } } let(:format) { :html }
let(:params) { {} }
subject { delete :destroy, format: format, params: { id: group.to_param, **params } }
before do before do
group.add_owner(user) group.add_owner(user)
...@@ -273,10 +276,22 @@ ...@@ -273,10 +276,22 @@
end end
end end
it 'redirects to group path' do context 'for a html request' do
subject it 'redirects to group path' do
subject
expect(response).to redirect_to(group_path(group)) expect(response).to redirect_to(group_path(group))
end
end
context 'for a json request', :freeze_time do
let(:format) { :json }
it 'returns json with message' do
subject
expect(json_response['message']).to eq("'#{group.name}' has been scheduled for deletion and will be deleted on #{permanent_deletion_date_formatted(group, group.marked_for_deletion_on)}.")
end
end end
end end
...@@ -289,11 +304,23 @@ ...@@ -289,11 +304,23 @@
expect { subject }.not_to change { group.reload.marked_for_deletion? }.from(false) expect { subject }.not_to change { group.reload.marked_for_deletion? }.from(false)
end end
it 'redirects to group edit page' do context 'for a html request' do
subject it 'redirects to group edit page' do
subject
expect(response).to redirect_to(edit_group_path(group)) expect(response).to redirect_to(edit_group_path(group))
expect(flash[:alert]).to include 'error' expect(flash[:alert]).to include 'error'
end
end
context 'for a json request' do
let(:format) { :json }
it 'returns json with message' do
subject
expect(json_response['message']).to eq("error")
end
end end
end end
...@@ -303,39 +330,108 @@ ...@@ -303,39 +330,108 @@
end end
context 'when permanently_remove param is set' do context 'when permanently_remove param is set' do
it 'deletes the group immediately' do let(:params) { { permanently_remove: true } }
expect(GroupDestroyWorker).to receive(:perform_async)
delete :destroy, params: { id: group.to_param, permanently_remove: true } context 'for a html request' do
it 'deletes the group immediately and redirects to root path' do
expect(GroupDestroyWorker).to receive(:perform_async)
expect(response).to redirect_to(root_path) subject
expect(flash[:toast]).to include "Group '#{group.name}' is being deleted."
expect(response).to redirect_to(root_path)
expect(flash[:toast]).to include "Group '#{group.name}' is being deleted."
end
end
context 'for a json request' do
let(:format) { :json }
it 'deletes the group immediately and returns json with message' do
expect(GroupDestroyWorker).to receive(:perform_async)
subject
expect(json_response['message']).to eq("Group '#{group.name}' is being deleted.")
end
end end
end end
context 'when permanently_remove param is not set' do context 'when permanently_remove param is not set' do
it 'does nothing' do context 'for a html request' do
subject it 'redirects to edit path with error' do
subject
expect(response).to redirect_to(edit_group_path(group)) expect(response).to redirect_to(edit_group_path(group))
expect(flash[:alert]).to include "Group has been already marked for deletion" expect(flash[:alert]).to include "Group has been already marked for deletion"
end
end
context 'for a json request' do
let(:format) { :json }
it 'returns json with message' do
subject
expect(json_response['message']).to eq("Group has been already marked for deletion")
end
end end
end end
end end
end end
context 'delayed deletion feature is not available' do context 'delayed deletion feature is not available', :sidekiq_inline do
before do before do
stub_licensed_features(adjourned_deletion_for_projects_and_groups: false) stub_licensed_features(adjourned_deletion_for_projects_and_groups: false)
end end
it 'immediately schedules a group destroy and redirects to root page with alert about immediate deletion' do context 'for a html request' do
Sidekiq::Testing.fake! do it 'immediately schedules a group destroy and redirects to root page with alert about immediate deletion' do
expect { subject }.to change { GroupDestroyWorker.jobs.size }.by(1) Sidekiq::Testing.fake! do
expect { subject }.to change { GroupDestroyWorker.jobs.size }.by(1)
end
expect(response).to redirect_to(root_path)
expect(flash[:toast]).to include "Group '#{group.name}' is being deleted."
end
end
context 'for a json request' do
let(:format) { :json }
it 'immediately schedules a group destroy and returns json with message' do
Sidekiq::Testing.fake! do
expect { subject }.to change { GroupDestroyWorker.jobs.size }.by(1)
end
expect(json_response['message']).to eq("Group '#{group.name}' is being deleted.")
end
end
end
context 'when group is linked to a subscription', :saas do
let_it_be(:group_with_plan) do
create(:group_with_plan, plan: :ultimate_plan, owners: user, organization: current_organization)
end
let(:params) { { id: group_with_plan.to_param } }
context 'for a html request' do
it 'redirects to edit page with alert' do
subject
expect(response).to redirect_to(edit_group_path(group_with_plan))
expect(flash[:alert]).to eq 'This group is linked to a subscription'
end end
end
context 'for a json request' do
let(:format) { :json }
expect(response).to redirect_to(root_path) it 'returns json with message' do
expect(flash[:toast]).to include "Group '#{group.name}' is being deleted." subject
expect(json_response['message']).to eq('This group is linked to a subscription')
end
end end
end end
end end
......
...@@ -1639,6 +1639,9 @@ msgstr "" ...@@ -1639,6 +1639,9 @@ msgstr ""
msgid "%{wildcards_link_start}Wildcards%{wildcards_link_end} such as %{code_tag_start}v*%{code_tag_end} or %{code_tag_start}*-release%{code_tag_end} are supported." msgid "%{wildcards_link_start}Wildcards%{wildcards_link_end} such as %{code_tag_start}v*%{code_tag_end} or %{code_tag_start}*-release%{code_tag_end} are supported."
msgstr "" msgstr ""
   
msgid "'%{group_name}' has been scheduled for deletion and will be deleted on %{date}."
msgstr ""
msgid "'%{group_name}' has been scheduled for removal on %{removal_time}." msgid "'%{group_name}' has been scheduled for removal on %{removal_time}."
msgstr "" msgstr ""
   
...@@ -545,12 +545,23 @@ ...@@ -545,12 +545,23 @@
sign_in(user) sign_in(user)
end end
it 'schedules a group destroy and redirects to the root path' do context 'for a html request' do
Sidekiq::Testing.fake! do it 'schedules a group destroy and redirects to the root path' do
expect { delete :destroy, params: { id: group.to_param } }.to change(GroupDestroyWorker.jobs, :size).by(1) Sidekiq::Testing.fake! do
expect { delete :destroy, params: { id: group.to_param } }.to change(GroupDestroyWorker.jobs, :size).by(1)
end
expect(flash[:toast]).to eq(format(_("Group '%{group_name}' is being deleted."), group_name: group.full_name))
expect(response).to redirect_to(root_path)
end
end
context 'for a json request' do
it 'schedules a group destroy and returns message' do
Sidekiq::Testing.fake! do
expect { delete :destroy, format: :json, params: { id: group.to_param } }.to change(GroupDestroyWorker.jobs, :size).by(1)
end
expect(Gitlab::Json.parse(response.body)).to eq({ 'message' => "Group '#{group.full_name}' is being deleted." })
end end
expect(flash[:toast]).to eq(format(_("Group '%{group_name}' is being deleted."), group_name: group.full_name))
expect(response).to redirect_to(root_path)
end end
end end
end end
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册