Commit 504052e8 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '32351-delete-service' into 'master'

DeleteService for deleting the self monitoring project

See merge request gitlab-org/gitlab!21995
parents 649a9ebf d3426e2d
# frozen_string_literal: true
module Gitlab
module DatabaseImporters
module SelfMonitoring
module Project
class DeleteService < ::BaseService
include Stepable
include SelfMonitoring::Helpers
steps :validate_self_monitoring_project_exists,
:destroy_project_owner,
:delete_project_id
def initialize
super(nil)
end
def execute
execute_steps
end
private
def validate_self_monitoring_project_exists(result)
unless project_created? || self_monitoring_project_id.present?
return error(_('Self monitoring project does not exist'))
end
success(result)
end
def destroy_project_owner(result)
return success(result) unless project_created?
if self_monitoring_project.owner.destroy
success(result)
else
log_error(self_monitoring_project.errors.full_messages)
error(_('Error deleting project. Check logs for error details.'))
end
end
def delete_project_id(result)
update_result = application_settings.update(
instance_administration_project_id: nil
)
if update_result
success(result)
else
log_error("Could not delete self monitoring project ID, errors: %{errors}" % { errors: application_settings.errors.full_messages })
error(_('Could not delete project ID'))
end
end
end
end
end
end
end
......@@ -5121,6 +5121,9 @@ msgstr ""
msgid "Could not delete chat nickname %{chat_name}."
msgstr ""
msgid "Could not delete project ID"
msgstr ""
msgid "Could not fetch projects"
msgstr ""
......@@ -7122,6 +7125,9 @@ msgstr ""
msgid "Error deleting %{issuableType}"
msgstr ""
msgid "Error deleting project. Check logs for error details."
msgstr ""
msgid "Error details"
msgstr ""
......@@ -16262,6 +16268,9 @@ msgstr ""
msgid "Selecting a GitLab user will add a link to the GitLab user in the descriptions of issues and comments (e.g. \"By <a href=\"#\">@johnsmith</a>\"). It will also associate and/or assign these issues and comments with the selected user."
msgstr ""
msgid "Self monitoring project does not exist"
msgstr ""
msgid "Self-monitoring is not enabled on this GitLab server, contact your administrator."
msgstr ""
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::DatabaseImporters::SelfMonitoring::Project::DeleteService do
describe '#execute' do
let(:result) { subject.execute }
let(:application_setting) { Gitlab::CurrentSettings.current_application_settings }
before do
allow(ApplicationSetting).to receive(:current_without_cache) { application_setting }
end
context 'when project does not exist' do
it 'returns error' do
expect(result).to eq(
status: :error,
message: 'Self monitoring project does not exist',
last_step: :validate_self_monitoring_project_exists
)
end
end
context 'with project destroyed but ID still present in application settings' do
before do
application_setting.instance_administration_project_id = 1
end
it 'deletes project ID from application settings' do
subject.execute
expect(application_setting.instance_administration_project_id).to be_nil
end
end
context 'when self monitoring project exists' do
let(:group) { create(:group) }
let(:project) { create(:project, namespace: group) }
before do
application_setting.instance_administration_project = project
end
it 'destroys project' do
subject.execute
expect { project.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'deletes project ID from application settings' do
subject.execute
expect(application_setting.instance_administration_project_id).to be_nil
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