Commit 41914c35 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'issue_212371' into 'master'

Create system note when health status is updated

See merge request gitlab-org/gitlab!28232
parents 733e1012 12cdc722
......@@ -16,8 +16,8 @@ class SystemNoteMetadata < ApplicationRecord
ICON_TYPES = %w[
commit description merge confidential visible label assignee cross_reference
title time_tracking branch milestone discussion task moved
opened closed merged duplicate locked unlocked
outdated tag due_date pinned_embed cherry_pick
opened closed merged duplicate locked unlocked outdated
tag due_date pinned_embed cherry_pick health_status
].freeze
validates :note, presence: true
......
......@@ -12,7 +12,11 @@ module EE
ActiveRecord::Base.no_touching do
handle_weight_change(is_update)
handle_date_change_note if is_update
if is_update
handle_date_change_note
handle_health_status_change
end
end
end
......@@ -40,6 +44,12 @@ module EE
end
end
def handle_health_status_change
return unless issuable.previous_changes.include?('health_status')
::SystemNoteService.change_health_status_note(issuable, issuable.project, current_user)
end
def weight_changes_tracking_enabled?
!issuable.is_a?(Epic) && ::Feature.enabled?(:track_issue_weight_change_events, issuable.project)
end
......
......@@ -110,6 +110,22 @@ module EE
::SystemNotes::IssuablesService.new(noteable: noteable, project: project, author: author).change_weight_note
end
# Called when the health_stauts of an Issue is changed
#
# noteable - Noteable object
# project - Project owning noteable
# author - User performing the change
#
# Example Note text:
#
# "removed the health status"
# "changed health status to 'at risk'"
#
# Returns the created Note object
def change_health_status_note(noteable, project, author)
::SystemNotes::IssuablesService.new(noteable: noteable, project: project, author: author).change_health_status_note
end
# Called when the start or end date of an Issuable is changed
#
# noteable - Noteable object
......
......@@ -44,6 +44,22 @@ module EE
create_note(NoteSummary.new(noteable, project, author, body, action: 'weight'))
end
# Called when the health_status of an Issue is changed
#
# Example Note text:
#
# "removed the health status"
#
# "changed health status to at risk"
#
# Returns the created Note object
def change_health_status_note
health_status = noteable.health_status&.humanize(capitalize: false)
body = health_status ? "changed health status to **#{health_status}**" : 'removed the health status'
create_note(NoteSummary.new(noteable, project, author, body, action: 'health_status'))
end
end
end
end
---
title: Create system note when health status is updated
merge_request: 28232
author:
type: added
......@@ -23,6 +23,34 @@ describe Issuable::CommonSystemNotesService do
end
end
context 'when health status is updated' do
before do
issuable.update!(health_status: 2)
end
context 'when setting a health_status' do
it 'creates system note' do
expect do
described_class.new(project, user).execute(issuable, old_labels: [])
end.to change { Note.count }.from(0).to(1)
expect(Note.last.note).to eq('changed health status to **needs attention**')
end
end
context 'when health status is removed' do
it 'creates system note' do
issuable.update!(health_status: nil)
expect do
described_class.new(project, user).execute(issuable, old_labels: [])
end.to change { Note.count }.from(0).to(1)
expect(Note.last.note).to eq('removed the health status')
end
end
end
context 'when issuable is an epic' do
let(:timestamp) { Time.now }
let(:issuable) { create(:epic, end_date: timestamp) }
......@@ -49,11 +77,10 @@ describe Issuable::CommonSystemNotesService do
subject { described_class.new(project, user).execute(issuable, old_labels: [], is_update: false) }
before do
issuable.weight = 5
issuable.save
issuable.update(weight: 5, health_status: 'at_risk')
end
it 'does not create a common system note for weight' do
it 'does not create common system notes' do
expect { subject }.not_to change { issuable.notes.count }
end
......
......@@ -74,4 +74,34 @@ describe ::SystemNotes::IssuablesService do
end
end
end
describe '#change_health_status_note' do
context 'when health_status changed' do
let(:noteable) { create(:issue, project: project, title: 'Lorem ipsum', health_status: 'at_risk') }
subject { service.change_health_status_note }
it_behaves_like 'a system note' do
let(:action) { 'health_status' }
end
it 'sets the note text' do
expect(subject.note).to eq "changed health status to **at risk**"
end
end
context 'when health_status removed' do
let(:noteable) { create(:issue, project: project, title: 'Lorem ipsum', health_status: nil) }
subject { service.change_health_status_note }
it_behaves_like 'a system note' do
let(:action) { 'health_status' }
end
it 'sets the note text' do
expect(subject.note).to eq 'removed the health status'
end
end
end
end
......@@ -103,6 +103,16 @@ describe SystemNoteService do
end
end
describe '.change_health_status_note' do
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:change_health_status_note)
end
described_class.change_health_status_note(noteable, project, author)
end
end
describe '.change_epic_date_note' do
let(:date_type) { double }
let(:date) { double }
......
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