Commit 152dcfbd authored by Sean Arnold's avatar Sean Arnold

Add label system note (resource event)

Add specs for services
Fix cop error
parent fd262a4f
......@@ -13,15 +13,24 @@ module IncidentManagement
return if incident.label_ids.include?(label.id)
incident.labels << label
add_resource_event
label
end
private
attr_reader :incident, :label
def add_resource_event
ResourceEvents::ChangeLabelsService
.new(incident, User.alert_bot)
.execute(added_labels: [label])
end
def incident_exceeded_label
::IncidentManagement::CreateIncidentSlaExceededLabelService
.new(project, current_user)
.new(project)
.execute
.payload[:label]
end
......
......@@ -3,10 +3,10 @@
module IncidentManagement
class CreateIncidentSlaExceededLabelService < BaseService
LABEL_PROPERTIES = {
title: 'SLA exceeded',
color: '#7E6AB0',
title: 'missed::SLA',
color: '#D9534F',
description: <<~DESCRIPTION.chomp
This incident was not closed before the SLA (Service Level Agreement) time exceeded
Incidents that have missed the targeted SLA (Service Level Agreement).
DESCRIPTION
}.freeze
......
# frozen_string_literal: true
module IncidentManagement
class IncidentSlaExceededCheckWorker
class IncidentSlaExceededCheckWorker # rubocop:disable Scalability/IdempotentWorker
include ApplicationWorker
include CronjobQueue # rubocop:disable Scalability/CronWorkerContext
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe IncidentManagement::ApplyIncidentSlaExceededLabelService do
let_it_be_with_refind(:incident) { create(:incident) }
let_it_be(:project) { incident.project }
let_it_be(:label) do
::IncidentManagement::CreateIncidentSlaExceededLabelService
.new(project)
.execute
.payload[:label]
end
subject { described_class.new(incident).execute }
context 'label exists already' do
before do
incident.labels << label
end
it 'does not add a label' do
expect { subject }.not_to change{ incident.labels.reload.count }
end
end
it 'adds a label to the incident' do
expect { subject }.to change{ incident.labels.reload.count }
end
it 'adds a note that the label was added' do
expect { subject }.to change{ incident.resource_label_events.reload.count }
event = incident.resource_label_events.first
expect(event.action).to eq('add')
expect(event.label).to eq(label)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe IncidentManagement::CreateIncidentLabelService do
it_behaves_like 'incident management label service'
end
......@@ -3,65 +3,5 @@
require 'spec_helper'
RSpec.describe IncidentManagement::CreateIncidentLabelService do
let_it_be(:project) { create(:project, :private) }
let_it_be(:user) { User.alert_bot }
let(:service) { described_class.new(project, user) }
subject(:execute) { service.execute }
describe 'execute' do
let(:incident_label_attributes) { attributes_for(:label, :incident) }
let(:title) { incident_label_attributes[:title] }
let(:color) { incident_label_attributes[:color] }
let(:description) { incident_label_attributes[:description] }
shared_examples 'existing label' do
it 'returns the existing label' do
expect { execute }.not_to change(Label, :count)
expect(execute).to be_success
expect(execute.payload).to eq(label: label)
end
end
shared_examples 'new label' do
it 'creates a new label' do
expect { execute }.to change(Label, :count).by(1)
label = project.reload.labels.last
expect(execute).to be_success
expect(execute.payload).to eq(label: label)
expect(label.title).to eq(title)
expect(label.color).to eq(color)
expect(label.description).to eq(description)
end
end
context 'with predefined project label' do
it_behaves_like 'existing label' do
let!(:label) { create(:label, project: project, title: title) }
end
end
context 'with predefined group label' do
let(:project) { create(:project, group: group) }
let(:group) { create(:group) }
it_behaves_like 'existing label' do
let!(:label) { create(:group_label, group: group, title: title) }
end
end
context 'without label' do
context 'when user has permissions to create labels' do
it_behaves_like 'new label'
end
context 'when user has no permissions to create labels' do
let_it_be(:user) { create(:user) }
it_behaves_like 'new label'
end
end
end
it_behaves_like 'incident management label service'
end
......@@ -45,3 +45,74 @@ RSpec.shared_examples 'not an incident issue' do
expect(issue.labels).not_to include(have_attributes(label_properties))
end
end
# This shared example is to test the execution of incident management label services
# For example:
# - IncidentManagement::CreateIncidentSlaExceededLabelService
# - IncidentManagement::CreateIncidentLabelService
# It doesn't require any defined variables
RSpec.shared_examples 'incident management label service' do
let_it_be(:project) { create(:project, :private) }
let_it_be(:user) { User.alert_bot }
let(:service) { described_class.new(project, user) }
subject(:execute) { service.execute }
describe 'execute' do
let(:incident_label_attributes) { described_class::LABEL_PROPERTIES }
let(:title) { incident_label_attributes[:title] }
let(:color) { incident_label_attributes[:color] }
let(:description) { incident_label_attributes[:description] }
shared_examples 'existing label' do
it 'returns the existing label' do
expect { execute }.not_to change(Label, :count)
expect(execute).to be_success
expect(execute.payload).to eq(label: label)
end
end
shared_examples 'new label' do
it 'creates a new label' do
expect { execute }.to change(Label, :count).by(1)
label = project.reload.labels.last
expect(execute).to be_success
expect(execute.payload).to eq(label: label)
expect(label.title).to eq(title)
expect(label.color).to eq(color)
expect(label.description).to eq(description)
end
end
context 'with predefined project label' do
it_behaves_like 'existing label' do
let!(:label) { create(:label, project: project, title: title) }
end
end
context 'with predefined group label' do
let(:project) { create(:project, group: group) }
let(:group) { create(:group) }
it_behaves_like 'existing label' do
let!(:label) { create(:group_label, group: group, title: title) }
end
end
context 'without label' do
context 'when user has permissions to create labels' do
it_behaves_like 'new label'
end
context 'when user has no permissions to create labels' do
let_it_be(:user) { create(:user) }
it_behaves_like 'new label'
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