2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-09 13:26:45 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Notes::QuickActionsService do
|
2016-08-09 13:26:45 -04:00
|
|
|
shared_context 'note on noteable' do
|
2020-08-20 20:10:44 -04:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let_it_be(:maintainer) { create(:user).tap { |u| project.add_maintainer(u) } }
|
|
|
|
let_it_be(:assignee) { create(:user) }
|
2016-12-14 16:39:53 -05:00
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(assignee)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2016-08-09 13:26:45 -04:00
|
|
|
end
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
shared_examples 'note on noteable that supports quick actions' do
|
2016-08-09 13:26:45 -04:00
|
|
|
include_context 'note on noteable'
|
|
|
|
|
|
|
|
before do
|
|
|
|
note.note = note_text
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:milestone) { create(:milestone, project: project) }
|
|
|
|
let!(:labels) { create_pair(:label, project: project) }
|
|
|
|
|
|
|
|
describe 'note with only command' do
|
|
|
|
describe '/close, /label, /assign & /milestone' do
|
|
|
|
let(:note_text) do
|
|
|
|
%(/close\n/label ~#{labels.first.name} ~#{labels.last.name}\n/assign @#{assignee.username}\n/milestone %"#{milestone.name}")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'closes noteable, sets labels, assigns, and sets milestone to noteable, and leave no note' do
|
2020-09-10 08:08:54 -04:00
|
|
|
content = execute(note)
|
2016-08-09 13:26:45 -04:00
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
expect(content).to be_empty
|
2016-08-09 13:26:45 -04:00
|
|
|
expect(note.noteable).to be_closed
|
|
|
|
expect(note.noteable.labels).to match_array(labels)
|
2017-05-04 08:11:15 -04:00
|
|
|
expect(note.noteable.assignees).to eq([assignee])
|
2016-08-09 13:26:45 -04:00
|
|
|
expect(note.noteable.milestone).to eq(milestone)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-20 20:10:44 -04:00
|
|
|
context '/relate' do
|
|
|
|
let_it_be(:issue) { create(:issue, project: project) }
|
|
|
|
let_it_be(:other_issue) { create(:issue, project: project) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2020-08-20 20:10:44 -04:00
|
|
|
let(:note_text) { "/relate #{other_issue.to_reference}" }
|
|
|
|
let(:note) { create(:note_on_issue, noteable: issue, project: project, note: note_text) }
|
|
|
|
|
2021-09-28 11:11:30 -04:00
|
|
|
context 'user cannot relate issues', :sidekiq_inline do
|
2020-08-20 20:10:44 -04:00
|
|
|
before do
|
|
|
|
project.team.find_member(maintainer.id).destroy!
|
|
|
|
project.update!(visibility: Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create issue relation' do
|
2020-09-10 08:08:54 -04:00
|
|
|
expect { execute(note) }.not_to change { IssueLink.count }
|
2020-08-20 20:10:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user is allowed to relate issues' do
|
|
|
|
it 'creates issue relation' do
|
2020-09-10 08:08:54 -04:00
|
|
|
expect { execute(note) }.to change { IssueLink.count }.by(1)
|
2020-08-20 20:10:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-17 19:58:52 -04:00
|
|
|
describe '/reopen' do
|
2016-08-09 13:26:45 -04:00
|
|
|
before do
|
|
|
|
note.noteable.close!
|
|
|
|
expect(note.noteable).to be_closed
|
|
|
|
end
|
2016-08-17 19:58:52 -04:00
|
|
|
let(:note_text) { '/reopen' }
|
2016-08-09 13:26:45 -04:00
|
|
|
|
|
|
|
it 'opens the noteable, and leave no note' do
|
2020-09-10 08:08:54 -04:00
|
|
|
content = execute(note)
|
2016-08-09 13:26:45 -04:00
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
expect(content).to be_empty
|
2016-08-09 13:26:45 -04:00
|
|
|
expect(note.noteable).to be_open
|
|
|
|
end
|
|
|
|
end
|
2016-12-23 00:44:02 -05:00
|
|
|
|
|
|
|
describe '/spend' do
|
2020-07-23 02:09:19 -04:00
|
|
|
context 'when note is not persisted' do
|
|
|
|
let(:note_text) { '/spend 1h' }
|
2016-12-23 00:44:02 -05:00
|
|
|
|
2020-07-23 02:09:19 -04:00
|
|
|
it 'adds time to noteable, adds timelog with nil note_id and has no content' do
|
2020-09-10 08:08:54 -04:00
|
|
|
content = execute(note)
|
2016-12-23 00:44:02 -05:00
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
expect(content).to be_empty
|
2020-07-23 02:09:19 -04:00
|
|
|
expect(note.noteable.time_spent).to eq(3600)
|
|
|
|
expect(Timelog.last.note_id).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when note is persisted' do
|
|
|
|
let(:note_text) { "a note \n/spend 1h" }
|
|
|
|
|
|
|
|
it 'updates the spent time and populates timelog with note_id' do
|
|
|
|
new_content, update_params = service.execute(note)
|
|
|
|
note.update!(note: new_content)
|
|
|
|
service.apply_updates(update_params, note)
|
|
|
|
|
|
|
|
expect(Timelog.last.note_id).to eq(note.id)
|
|
|
|
end
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
2021-05-07 08:10:27 -04:00
|
|
|
|
|
|
|
context 'adds a system note' do
|
|
|
|
context 'when not specifying a date' do
|
|
|
|
let(:note_text) { "/spend 1h" }
|
|
|
|
|
|
|
|
it 'does not include the date' do
|
|
|
|
_, update_params = service.execute(note)
|
|
|
|
service.apply_updates(update_params, note)
|
|
|
|
|
|
|
|
expect(Note.last.note).to eq('added 1h of time spent')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when specifying a date' do
|
|
|
|
let(:note_text) { "/spend 1h 2020-01-01" }
|
|
|
|
|
|
|
|
it 'does include the date' do
|
|
|
|
_, update_params = service.execute(note)
|
|
|
|
service.apply_updates(update_params, note)
|
|
|
|
|
|
|
|
expect(Note.last.note).to eq('added 1h of time spent at 2020-01-01')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
2016-08-09 13:26:45 -04:00
|
|
|
end
|
|
|
|
|
2021-06-02 05:09:46 -04:00
|
|
|
describe '/estimate' do
|
|
|
|
let(:note_text) { '/estimate 1h' }
|
|
|
|
|
|
|
|
it 'adds time estimate to noteable' do
|
|
|
|
content = execute(note)
|
|
|
|
|
|
|
|
expect(content).to be_empty
|
|
|
|
expect(note.noteable.time_estimate).to eq(3600)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-09 13:26:45 -04:00
|
|
|
describe 'note with command & text' do
|
|
|
|
describe '/close, /label, /assign & /milestone' do
|
|
|
|
let(:note_text) do
|
|
|
|
%(HELLO\n/close\n/label ~#{labels.first.name} ~#{labels.last.name}\n/assign @#{assignee.username}\n/milestone %"#{milestone.name}"\nWORLD)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'closes noteable, sets labels, assigns, and sets milestone to noteable' do
|
2020-09-10 08:08:54 -04:00
|
|
|
content = execute(note)
|
2016-08-09 13:26:45 -04:00
|
|
|
|
2016-08-13 12:58:51 -04:00
|
|
|
expect(content).to eq "HELLO\nWORLD"
|
2016-08-09 13:26:45 -04:00
|
|
|
expect(note.noteable).to be_closed
|
|
|
|
expect(note.noteable.labels).to match_array(labels)
|
2017-05-04 08:11:15 -04:00
|
|
|
expect(note.noteable.assignees).to eq([assignee])
|
2016-08-09 13:26:45 -04:00
|
|
|
expect(note.noteable.milestone).to eq(milestone)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-17 19:58:52 -04:00
|
|
|
describe '/reopen' do
|
2016-08-09 13:26:45 -04:00
|
|
|
before do
|
|
|
|
note.noteable.close
|
|
|
|
expect(note.noteable).to be_closed
|
|
|
|
end
|
2016-08-17 19:58:52 -04:00
|
|
|
let(:note_text) { "HELLO\n/reopen\nWORLD" }
|
2016-08-09 13:26:45 -04:00
|
|
|
|
|
|
|
it 'opens the noteable' do
|
2020-09-10 08:08:54 -04:00
|
|
|
content = execute(note)
|
2016-08-09 13:26:45 -04:00
|
|
|
|
2016-08-13 12:58:51 -04:00
|
|
|
expect(content).to eq "HELLO\nWORLD"
|
2016-08-09 13:26:45 -04:00
|
|
|
expect(note.noteable).to be_open
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-10 08:08:54 -04:00
|
|
|
|
|
|
|
describe '/milestone' do
|
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:note_text) { %(/milestone %"#{milestone.name}") }
|
|
|
|
let(:note) { create(:note_on_issue, noteable: issue, project: project, note: note_text) }
|
|
|
|
|
|
|
|
context 'on an incident' do
|
|
|
|
before do
|
|
|
|
issue.update!(issue_type: :incident)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves the note empty' do
|
|
|
|
expect(execute(note)).to be_empty
|
|
|
|
end
|
|
|
|
|
2021-01-15 10:10:31 -05:00
|
|
|
it 'assigns the milestone' do
|
|
|
|
expect { execute(note) }.to change { issue.reload.milestone }.from(nil).to(milestone)
|
2020-09-10 08:08:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on a merge request' do
|
|
|
|
let(:note_mr) { create(:note_on_merge_request, project: project, note: note_text) }
|
|
|
|
|
|
|
|
it 'leaves the note empty' do
|
|
|
|
expect(execute(note_mr)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns the milestone' do
|
|
|
|
expect { execute(note) }.to change { issue.reload.milestone }.from(nil).to(milestone)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '/remove_milestone' do
|
|
|
|
let(:issue) { create(:issue, project: project, milestone: milestone) }
|
|
|
|
let(:note_text) { '/remove_milestone' }
|
|
|
|
let(:note) { create(:note_on_issue, noteable: issue, project: project, note: note_text) }
|
|
|
|
|
|
|
|
context 'on an issue' do
|
|
|
|
it 'leaves the note empty' do
|
|
|
|
expect(execute(note)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the milestone' do
|
|
|
|
expect { execute(note) }.to change { issue.reload.milestone }.from(milestone).to(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on an incident' do
|
|
|
|
before do
|
|
|
|
issue.update!(issue_type: :incident)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves the note empty' do
|
|
|
|
expect(execute(note)).to be_empty
|
|
|
|
end
|
|
|
|
|
2021-01-15 10:10:31 -05:00
|
|
|
it 'removes the milestone' do
|
|
|
|
expect { execute(note) }.to change { issue.reload.milestone }.from(milestone).to(nil)
|
2020-09-10 08:08:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on a merge request' do
|
|
|
|
let(:note_mr) { create(:note_on_merge_request, project: project, note: note_text) }
|
|
|
|
|
|
|
|
it 'leaves the note empty' do
|
|
|
|
expect(execute(note_mr)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the milestone' do
|
|
|
|
expect { execute(note) }.to change { issue.reload.milestone }.from(milestone).to(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-09 13:26:45 -04:00
|
|
|
end
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
describe '.noteable_update_service_class' do
|
2016-09-20 04:23:13 -04:00
|
|
|
include_context 'note on noteable'
|
|
|
|
|
|
|
|
it 'returns Issues::UpdateService for a note on an issue' do
|
|
|
|
note = create(:note_on_issue, project: project)
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
expect(described_class.noteable_update_service_class(note)).to eq(Issues::UpdateService)
|
2016-09-20 04:23:13 -04:00
|
|
|
end
|
|
|
|
|
2018-07-21 07:37:01 -04:00
|
|
|
it 'returns MergeRequests::UpdateService for a note on a merge request' do
|
2016-09-20 04:23:13 -04:00
|
|
|
note = create(:note_on_merge_request, project: project)
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
expect(described_class.noteable_update_service_class(note)).to eq(MergeRequests::UpdateService)
|
2016-09-20 04:23:13 -04:00
|
|
|
end
|
|
|
|
|
2018-07-23 16:01:23 -04:00
|
|
|
it 'returns Commits::TagService for a note on a commit' do
|
2016-09-20 04:23:13 -04:00
|
|
|
note = create(:note_on_commit, project: project)
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
expect(described_class.noteable_update_service_class(note)).to eq(Commits::TagService)
|
2016-09-20 04:23:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.supported?' do
|
|
|
|
include_context 'note on noteable'
|
|
|
|
|
|
|
|
let(:note) { create(:note_on_issue, project: project) }
|
|
|
|
|
2018-03-02 07:03:03 -05:00
|
|
|
context 'with a note on an issue' do
|
2016-09-20 04:23:13 -04:00
|
|
|
it 'returns true' do
|
2018-03-02 07:03:03 -05:00
|
|
|
expect(described_class.supported?(note)).to be_truthy
|
2016-09-20 04:23:13 -04:00
|
|
|
end
|
2018-03-02 07:03:03 -05:00
|
|
|
end
|
2016-09-20 04:23:13 -04:00
|
|
|
|
2018-03-02 07:03:03 -05:00
|
|
|
context 'with a note on a commit' do
|
|
|
|
let(:note) { create(:note_on_commit, project: project) }
|
2016-09-20 04:23:13 -04:00
|
|
|
|
2018-03-02 07:03:03 -05:00
|
|
|
it 'returns false' do
|
2018-07-21 07:37:01 -04:00
|
|
|
expect(described_class.supported?(note)).to be_truthy
|
2016-09-20 04:23:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#supported?' do
|
|
|
|
include_context 'note on noteable'
|
|
|
|
|
|
|
|
it 'delegates to the class method' do
|
2018-07-11 10:36:08 -04:00
|
|
|
service = described_class.new(project, maintainer)
|
2016-09-20 04:23:13 -04:00
|
|
|
note = create(:note_on_issue, project: project)
|
|
|
|
|
2018-03-02 07:03:03 -05:00
|
|
|
expect(described_class).to receive(:supported?).with(note)
|
2016-09-20 04:23:13 -04:00
|
|
|
|
|
|
|
service.supported?(note)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-09 13:26:45 -04:00
|
|
|
describe '#execute' do
|
2018-07-11 10:36:08 -04:00
|
|
|
let(:service) { described_class.new(project, maintainer) }
|
2016-08-13 12:58:51 -04:00
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
it_behaves_like 'note on noteable that supports quick actions' do
|
2020-09-09 17:08:33 -04:00
|
|
|
let_it_be(:issue, reload: true) { create(:issue, project: project) }
|
|
|
|
let(:note) { build(:note_on_issue, project: project, noteable: issue) }
|
2016-08-09 13:26:45 -04:00
|
|
|
end
|
|
|
|
|
2021-06-02 05:09:46 -04:00
|
|
|
it_behaves_like 'note on noteable that supports quick actions' do
|
|
|
|
let_it_be(:incident, reload: true) { create(:incident, project: project) }
|
|
|
|
let(:note) { build(:note_on_issue, project: project, noteable: incident) }
|
|
|
|
end
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
it_behaves_like 'note on noteable that supports quick actions' do
|
2020-09-10 08:08:54 -04:00
|
|
|
let(:merge_request) { create(:merge_request, source_project: project) }
|
2020-09-09 17:08:33 -04:00
|
|
|
let(:note) { build(:note_on_merge_request, project: project, noteable: merge_request) }
|
2016-08-09 13:26:45 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-05 11:55:16 -04:00
|
|
|
|
|
|
|
context 'CE restriction for issue assignees' do
|
|
|
|
describe '/assign' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2017-05-05 11:55:16 -04:00
|
|
|
let(:assignee) { create(:user) }
|
2018-07-11 10:36:08 -04:00
|
|
|
let(:maintainer) { create(:user) }
|
|
|
|
let(:service) { described_class.new(project, maintainer) }
|
2017-05-05 11:55:16 -04:00
|
|
|
let(:note) { create(:note_on_issue, note: note_text, project: project) }
|
|
|
|
|
|
|
|
let(:note_text) do
|
2018-07-11 10:36:08 -04:00
|
|
|
%(/assign @#{assignee.username} @#{maintainer.username}\n")
|
2017-05-05 11:55:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2019-04-02 11:47:35 -04:00
|
|
|
stub_licensed_features(multiple_issue_assignees: false)
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(maintainer)
|
|
|
|
project.add_maintainer(assignee)
|
2017-05-05 11:55:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds only one assignee from the list' do
|
2020-09-10 08:08:54 -04:00
|
|
|
execute(note)
|
2017-05-05 11:55:16 -04:00
|
|
|
|
|
|
|
expect(note.noteable.assignees.count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-10 08:08:54 -04:00
|
|
|
|
|
|
|
def execute(note)
|
|
|
|
content, update_params = service.execute(note)
|
|
|
|
service.apply_updates(update_params, note)
|
|
|
|
|
|
|
|
content
|
|
|
|
end
|
2016-08-09 13:26:45 -04:00
|
|
|
end
|