Commit ecab5398 authored by Chantal Rollison's avatar Chantal Rollison

add system note for weight change, closes #1481

parent c89c7b6c
......@@ -18,7 +18,6 @@ module IssuableActions
def update
@issuable = update_service.execute(issuable) # rubocop:disable Gitlab/ModuleWithInstanceVariables
respond_to do |format|
format.html do
recaptcha_check_if_spammable { render :edit }
......
class SystemNoteMetadata < ActiveRecord::Base
prepend EE::SystemNoteMetadata
# These notes's action text might contain a reference that is external.
# We should always force a deep validation upon references that are found
# in this note type.
......@@ -15,13 +17,14 @@ class SystemNoteMetadata < ActiveRecord::Base
title time_tracking branch milestone discussion task moved
opened closed merged duplicate locked unlocked
outdated
approved unapproved relate unrelate
epic_issue_added issue_added_to_epic epic_issue_removed issue_removed_from_epic
epic_issue_moved issue_changed_epic
].freeze
validates :note, presence: true
validates :action, inclusion: ICON_TYPES, allow_nil: true
validates :action, inclusion: { in: :icon_types }, allow_nil: true
belongs_to :note
def icon_types
ICON_TYPES
end
end
module Issuable
class CommonSystemNotesService < ::BaseService
prepend EE::Issuable::CommonSystemNotesService
attr_reader :issuable
def execute(issuable, old_labels)
......
......@@ -3,6 +3,7 @@
# Used for creating system notes (e.g., when a user references a merge request
# from an issue, an issue's assignee changes, an issue is closed, etc.)
module SystemNoteService
prepend EE::SystemNoteService
extend self
# Called when commits are added to a Merge Request
......
module EE
module SystemNoteMetadata
extend ::Gitlab::Utils::Override
EE_ICON_TYPES = %w[
weight approved unapproved relate unrelate
epic_issue_added issue_added_to_epic epic_issue_removed issue_removed_from_epic
epic_issue_moved issue_changed_epic
].freeze
override :icon_types
def icon_types
@icon_types ||= (super + EE_ICON_TYPES).freeze
end
end
end
module EE
module Issuable
module CommonSystemNotesService
extend ::Gitlab::Utils::Override
attr_reader :issuable
override :execute
def execute(_issuable, _old_labels)
super
handle_weight_change_note
end
private
def handle_weight_change_note
if issuable.previous_changes.include?('weight')
create_weight_change_note
end
end
def create_weight_change_note
::SystemNoteService.change_weight_note(issuable, issuable.project, current_user)
end
end
end
end
# SystemNoteService
#
# Used for creating system notes (e.g., when a user references a merge request
# from an issue, an issue's assignee changes, an issue is closed, etc.
module EE
module SystemNoteService
extend self
# Called when the weight of a Noteable is changed
#
# noteable - Noteable object
# project - Project owning noteable
# author - User performing the change
#
# Example Note text:
#
# "removed the weight"
#
# "changed weight to 4"
#
# Returns the created Note object
def change_weight_note(noteable, project, author)
body = noteable.weight ? "changed weight to **#{noteable.weight}**," : 'removed the weight'
create_note(NoteSummary.new(noteable, project, author, body, action: 'weight'))
end
end
end
---
title: Add system note for weight change
merge_request: !5531
type: added
......@@ -87,6 +87,15 @@ describe API::Issues, :mailer do
expect(json_response['error']).to eq('weight does not have a valid value')
end
it 'adds a note when the weight is changed' do
expect do
put api("/projects/#{project.id}/issues/#{issue.iid}", user), weight: 9
end.to change { Note.count }.by(1)
expect(response).to have_gitlab_http_status(200)
expect(json_response['weight']).to eq(9)
end
context 'issuable weights unlicensed' do
before do
stub_licensed_features(issue_weights: false)
......
require 'spec_helper'
describe Issuable::CommonSystemNotesService do
let(:user) { create(:user) }
let(:project) { create(:project) }
let(:issuable) { create(:issue) }
describe '#execute' do
it_behaves_like 'system note creation', { weight: 5 }, 'changed weight to **5**,'
end
end
require 'spec_helper'
describe SystemNoteService do
include ProjectForksHelper
include Gitlab::Routing
include RepoHelpers
set(:group) { create(:group) }
let(:project) { create(:project, :repository, group: group) }
set(:author) { create(:user) }
let(:noteable) { create(:issue, project: project) }
let(:issue) { noteable }
let(:epic) { create(:epic) }
shared_examples_for 'a system note' do
let(:expected_noteable) { noteable }
let(:commit_count) { nil }
it 'has the correct attributes', :aggregate_failures do
expect(subject).to be_valid
expect(subject).to be_system
expect(subject.noteable).to eq expected_noteable
expect(subject.project).to eq project
expect(subject.author).to eq author
expect(subject.system_note_metadata.action).to eq(action)
expect(subject.system_note_metadata.commit_count).to eq(commit_count)
end
end
describe '.change_weight_note' do
context 'when weight changed' do
let(:noteable) { create(:issue, project: project, title: 'Lorem ipsum', weight: 4) }
subject { described_class.change_weight_note(noteable, project, author) }
it_behaves_like 'a system note' do
let(:action) { 'weight' }
end
it 'sets the note text' do
expect(subject.note).to eq "changed weight to **4**,"
end
end
context 'when weight removed' do
let(:noteable) { create(:issue, project: project, title: 'Lorem ipsum', weight: nil) }
subject { described_class.change_weight_note(noteable, project, author) }
it_behaves_like 'a system note' do
let(:action) { 'weight' }
end
it 'sets the note text' do
expect(subject.note).to eq 'removed the weight'
end
end
end
end
......@@ -24,7 +24,7 @@ describe Gitlab::IncomingEmail do
end
describe 'self.supports_wildcard?' do
context 'address contains the wildard placeholder' do
context 'address contains the wildcard placeholder' do
before do
stub_incoming_email_setting(address: 'replies+%{key}@example.com')
end
......@@ -49,7 +49,7 @@ describe Gitlab::IncomingEmail do
stub_incoming_email_setting(address: nil)
end
it 'returns that wildard is not supported' do
it 'returns that wildcard is not supported' do
expect(described_class.supports_wildcard?).to be_falsey
end
end
......
......@@ -5,34 +5,6 @@ describe Issuable::CommonSystemNotesService do
let(:project) { create(:project) }
let(:issuable) { create(:issue) }
shared_examples 'system note creation' do |update_params, note_text|
subject { described_class.new(project, user).execute(issuable, [])}
before do
issuable.assign_attributes(update_params)
issuable.save
end
it 'creates 1 system note with the correct content' do
expect { subject }.to change { Note.count }.from(0).to(1)
note = Note.last
expect(note.note).to match(note_text)
expect(note.noteable_type).to eq(issuable.class.name)
end
end
shared_examples 'WIP notes creation' do |wip_action|
subject { described_class.new(project, user).execute(issuable, []) }
it 'creates WIP toggle and title change notes' do
expect { subject }.to change { Note.count }.from(0).to(2)
expect(Note.first.note).to match("#{wip_action} as a **Work In Progress**")
expect(Note.second.note).to match('changed title')
end
end
describe '#execute' do
it_behaves_like 'system note creation', { title: 'New title' }, 'changed title'
it_behaves_like 'system note creation', { description: 'New description' }, 'changed the description'
......
shared_examples 'system note creation' do |update_params, note_text|
subject { described_class.new(project, user).execute(issuable, [])}
before do
issuable.assign_attributes(update_params)
issuable.save
end
it 'creates 1 system note with the correct content' do
expect { subject }.to change { Note.count }.from(0).to(1)
note = Note.last
expect(note.note).to match(note_text)
expect(note.noteable_type).to eq(issuable.class.name)
end
end
shared_examples 'WIP notes creation' do |wip_action|
subject { described_class.new(project, user).execute(issuable, []) }
it 'creates WIP toggle and title change notes' do
expect { subject }.to change { Note.count }.from(0).to(2)
expect(Note.first.note).to match("#{wip_action} as a **Work In Progress**")
expect(Note.second.note).to match('changed title')
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