Commit 1b231be1 authored by Stan Hu's avatar Stan Hu

Merge branch '332433-add-limit-on-one-escalation-policy-per-project' into 'master'

Enforce one escalation policy per project

See merge request gitlab-org/gitlab!63603
parents 246e8194 12b8516d
......@@ -7,6 +7,7 @@ module IncidentManagement
belongs_to :project
has_many :rules, class_name: 'EscalationRule', inverse_of: :policy, foreign_key: 'policy_id', index_errors: true
validates :project_id, uniqueness: { message: _('can only have one escalation policy') }, on: :create
validates :name, presence: true, uniqueness: { scope: [:project_id] }, length: { maximum: 72 }
validates :description, length: { maximum: 160 }
validates :rules, presence: true
......
......@@ -6,7 +6,6 @@ RSpec.describe IncidentManagement::EscalationPoliciesFinder do
let_it_be(:current_user) { create(:user) }
let_it_be_with_refind(:project) { create(:project) }
let_it_be(:escalation_policy) { create(:incident_management_escalation_policy, project: project) }
let_it_be(:escalation_policy_2) { create(:incident_management_escalation_policy, project: project) }
let_it_be(:escalation_policy_from_another_project) { create(:incident_management_escalation_policy) }
let(:params) { {} }
......@@ -26,19 +25,13 @@ RSpec.describe IncidentManagement::EscalationPoliciesFinder do
end
it 'returns project escalation policies' do
is_expected.to contain_exactly(escalation_policy, escalation_policy_2)
is_expected.to contain_exactly(escalation_policy)
end
context 'when id given' do
let(:params) { { id: escalation_policy.id } }
it { is_expected.to contain_exactly(escalation_policy) }
context 'an array of ids given' do
let(:params) { { id: [escalation_policy.id, escalation_policy_2.id] } }
it { is_expected.to contain_exactly(escalation_policy, escalation_policy_2) }
end
end
end
......
......@@ -17,6 +17,7 @@ RSpec.describe IncidentManagement::EscalationPolicy do
describe 'validations' do
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:rules) }
it { is_expected.to validate_uniqueness_of(:project_id).with_message(/can only have one escalation policy/).on(:create) }
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) }
it { is_expected.to validate_length_of(:name).is_at_most(72) }
it { is_expected.to validate_length_of(:description).is_at_most(160) }
......
......@@ -72,7 +72,6 @@ RSpec.describe 'getting Incident Management escalation policies' do
context 'with escalation policies' do
let_it_be(:policy) { create(:incident_management_escalation_policy, project: project) }
let_it_be(:second_policy) { create(:incident_management_escalation_policy, project: project) }
let(:last_policy) { escalation_policies.last }
......@@ -83,7 +82,6 @@ RSpec.describe 'getting Incident Management escalation policies' do
it_behaves_like 'a working graphql query'
it 'returns the correct properties of the escalation policy' do
expect(escalation_policies.size).to eq(2)
expect(last_policy).to include(
'id' => policy.to_global_id.to_s,
'name' => policy.name,
......
......@@ -78,6 +78,14 @@ RSpec.describe IncidentManagement::EscalationPolicies::CreateService do
it_behaves_like 'error response', "Rules[0] oncall schedule can't be blank"
end
context 'project has an existing escalation policy' do
before do
create(:incident_management_escalation_policy, project: project)
end
it_behaves_like 'error response', "Project can only have one escalation policy"
end
end
context 'valid params' do
......
......@@ -38039,6 +38039,9 @@ msgstr ""
msgid "can only be changed by a group admin."
msgstr ""
msgid "can only have one escalation policy"
msgstr ""
msgid "can't be enabled because signed commits are required for this project"
msgstr ""
......
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