summaryrefslogtreecommitdiff
path: root/spec/services/quick_actions/interpret_service_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/services/quick_actions/interpret_service_spec.rb')
-rw-r--r--spec/services/quick_actions/interpret_service_spec.rb212
1 files changed, 210 insertions, 2 deletions
diff --git a/spec/services/quick_actions/interpret_service_spec.rb b/spec/services/quick_actions/interpret_service_spec.rb
index e6d1d0e90a7..21e294418a1 100644
--- a/spec/services/quick_actions/interpret_service_spec.rb
+++ b/spec/services/quick_actions/interpret_service_spec.rb
@@ -8,6 +8,7 @@ RSpec.describe QuickActions::InterpretService do
let_it_be(:project) { public_project }
let_it_be(:developer) { create(:user) }
let_it_be(:developer2) { create(:user) }
+ let_it_be(:developer3) { create(:user) }
let_it_be_with_reload(:issue) { create(:issue, project: project) }
let(:milestone) { create(:milestone, project: project, title: '9.10') }
let(:commit) { create(:commit, project: project) }
@@ -23,6 +24,7 @@ RSpec.describe QuickActions::InterpretService do
before do
stub_licensed_features(multiple_issue_assignees: false,
+ multiple_merge_request_reviewers: false,
multiple_merge_request_assignees: false)
end
@@ -665,6 +667,24 @@ RSpec.describe QuickActions::InterpretService do
end
end
+ shared_examples 'assign_reviewer command' do
+ it 'assigns a reviewer to a single user' do
+ _, updates, message = service.execute(content, issuable)
+
+ expect(updates).to eq(reviewer_ids: [developer.id])
+ expect(message).to eq("Assigned #{developer.to_reference} as reviewer.")
+ end
+ end
+
+ shared_examples 'unassign_reviewer command' do
+ it 'removes a single reviewer' do
+ _, updates, message = service.execute(content, issuable)
+
+ expect(updates).to eq(reviewer_ids: [])
+ expect(message).to eq("Removed reviewer #{developer.to_reference}.")
+ end
+ end
+
it_behaves_like 'reopen command' do
let(:content) { '/reopen' }
let(:issuable) { issue }
@@ -779,6 +799,11 @@ RSpec.describe QuickActions::InterpretService do
it_behaves_like 'assign command' do
let(:content) { "/assign @#{developer.username}" }
+ let(:issuable) { create(:incident, project: project) }
+ end
+
+ it_behaves_like 'assign command' do
+ let(:content) { "/assign @#{developer.username}" }
let(:issuable) { merge_request }
end
end
@@ -789,12 +814,32 @@ RSpec.describe QuickActions::InterpretService do
project.add_developer(developer2)
end
- it_behaves_like 'assign command' do
+ # There's no guarantee that the reference extractor will preserve
+ # the order of the mentioned users since this is dependent on the
+ # order in which rows are returned. We just ensure that at least
+ # one of the mentioned users is assigned.
+ shared_examples 'assigns to one of the two users' do
+ let(:content) { "/assign @#{developer.username} @#{developer2.username}" }
+
+ it 'assigns to a single user' do
+ _, updates, message = service.execute(content, issuable)
+
+ expect(updates[:assignee_ids].count).to eq(1)
+ assignee = updates[:assignee_ids].first
+ expect([developer.id, developer2.id]).to include(assignee)
+
+ user = assignee == developer.id ? developer : developer2
+
+ expect(message).to match("Assigned #{user.to_reference}.")
+ end
+ end
+
+ it_behaves_like 'assigns to one of the two users' do
let(:content) { "/assign @#{developer.username} @#{developer2.username}" }
let(:issuable) { issue }
end
- it_behaves_like 'assign command', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/27989' do
+ it_behaves_like 'assigns to one of the two users' do
let(:content) { "/assign @#{developer.username} @#{developer2.username}" }
let(:issuable) { merge_request }
end
@@ -834,6 +879,142 @@ RSpec.describe QuickActions::InterpretService do
let(:issuable) { issue }
end
+ context 'when the merge_request_reviewers flag is enabled' do
+ describe 'assign_reviewer command' do
+ let(:content) { "/assign_reviewer @#{developer.username}" }
+ let(:issuable) { merge_request }
+
+ context 'with one user' do
+ it_behaves_like 'assign_reviewer command'
+ end
+
+ context 'with an issue instead of a merge request' do
+ let(:issuable) { issue }
+
+ it_behaves_like 'empty command'
+ end
+
+ # CE does not have multiple reviewers
+ context 'assign command with multiple assignees' do
+ before do
+ project.add_developer(developer2)
+ end
+
+ # There's no guarantee that the reference extractor will preserve
+ # the order of the mentioned users since this is dependent on the
+ # order in which rows are returned. We just ensure that at least
+ # one of the mentioned users is assigned.
+ context 'assigns to one of the two users' do
+ let(:content) { "/assign_reviewer @#{developer.username} @#{developer2.username}" }
+
+ it 'assigns to a single reviewer' do
+ _, updates, message = service.execute(content, issuable)
+
+ expect(updates[:reviewer_ids].count).to eq(1)
+ reviewer = updates[:reviewer_ids].first
+ expect([developer.id, developer2.id]).to include(reviewer)
+
+ user = reviewer == developer.id ? developer : developer2
+
+ expect(message).to match("Assigned #{user.to_reference} as reviewer.")
+ end
+ end
+ end
+
+ context 'with "me" alias' do
+ let(:content) { '/assign_reviewer me' }
+
+ it_behaves_like 'assign_reviewer command'
+ end
+
+ context 'with an alias and whitespace' do
+ let(:content) { '/assign_reviewer me ' }
+
+ it_behaves_like 'assign_reviewer command'
+ end
+
+ context 'with an incorrect user' do
+ let(:content) { '/assign_reviewer @abcd1234' }
+
+ it_behaves_like 'empty command', "Failed to assign a reviewer because no user was found."
+ end
+
+ context 'with the "reviewer" alias' do
+ let(:content) { "/reviewer @#{developer.username}" }
+
+ it_behaves_like 'assign_reviewer command'
+ end
+
+ context 'with no user' do
+ let(:content) { '/assign_reviewer' }
+
+ it_behaves_like 'empty command', "Failed to assign a reviewer because no user was found."
+ end
+
+ context 'includes only the user reference with extra text' do
+ let(:content) { "/assign_reviewer @#{developer.username} do it!" }
+
+ it_behaves_like 'assign_reviewer command'
+ end
+ end
+
+ describe 'unassign_reviewer command' do
+ # CE does not have multiple reviewers, so basically anything
+ # after /unassign_reviewer (including whitespace) will remove
+ # all the current reviewers.
+ let(:issuable) { create(:merge_request, reviewers: [developer]) }
+ let(:content) { "/unassign_reviewer @#{developer.username}" }
+
+ context 'with one user' do
+ it_behaves_like 'unassign_reviewer command'
+ end
+
+ context 'with an issue instead of a merge request' do
+ let(:issuable) { issue }
+
+ it_behaves_like 'empty command'
+ end
+
+ context 'with anything after the command' do
+ let(:content) { '/unassign_reviewer supercalifragilisticexpialidocious' }
+
+ it_behaves_like 'unassign_reviewer command'
+ end
+
+ context 'with the "remove_reviewer" alias' do
+ let(:content) { "/remove_reviewer @#{developer.username}" }
+
+ it_behaves_like 'unassign_reviewer command'
+ end
+
+ context 'with no user' do
+ let(:content) { '/unassign_reviewer' }
+
+ it_behaves_like 'unassign_reviewer command'
+ end
+ end
+ end
+
+ context 'when the merge_request_reviewers flag is disabled' do
+ before do
+ stub_feature_flags(merge_request_reviewers: false)
+ end
+
+ describe 'assign_reviewer command' do
+ it_behaves_like 'empty command' do
+ let(:content) { "/assign_reviewer @#{developer.username}" }
+ let(:issuable) { merge_request }
+ end
+ end
+
+ describe 'unassign_reviewer command' do
+ it_behaves_like 'empty command' do
+ let(:content) { "/unassign_reviewer @#{developer.username}" }
+ let(:issuable) { merge_request }
+ end
+ end
+ end
+
context 'unassign command' do
let(:content) { '/unassign' }
@@ -1117,6 +1298,11 @@ RSpec.describe QuickActions::InterpretService do
let(:issuable) { issue }
end
+ it_behaves_like 'confidential command' do
+ let(:content) { '/confidential' }
+ let(:issuable) { create(:incident, project: project) }
+ end
+
it_behaves_like 'lock command' do
let(:content) { '/lock' }
let(:issuable) { issue }
@@ -1819,6 +2005,28 @@ RSpec.describe QuickActions::InterpretService do
end
end
+ describe 'unassign_reviewer command' do
+ let(:content) { '/unassign_reviewer' }
+ let(:merge_request) { create(:merge_request, source_project: project, reviewers: [developer]) }
+
+ it 'includes current assignee reference' do
+ _, explanations = service.explain(content, merge_request)
+
+ expect(explanations).to eq(["Removes reviewer @#{developer.username}."])
+ end
+ end
+
+ describe 'assign_reviewer command' do
+ let(:content) { "/assign_reviewer #{developer.to_reference}" }
+ let(:merge_request) { create(:merge_request, source_project: project, assignees: [developer]) }
+
+ it 'includes only the user reference' do
+ _, explanations = service.explain(content, merge_request)
+
+ expect(explanations).to eq(["Assigns #{developer.to_reference} as reviewer."])
+ end
+ end
+
describe 'milestone command' do
let(:content) { '/milestone %wrong-milestone' }
let!(:milestone) { create(:milestone, project: project, title: '9.10') }