Commit ed7f6f0d authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '7723-epic-commands-autocomplete' into 'master'

Support autocomplete for commands in group entities

Closes #7723

See merge request gitlab-org/gitlab-ee!7588
parents a97b697d a8e8275f
...@@ -211,9 +211,14 @@ module QuickActions ...@@ -211,9 +211,14 @@ module QuickActions
end end
params '~label1 ~"label 2"' params '~label1 ~"label 2"'
condition do condition do
available_labels = LabelsFinder.new(current_user, project_id: project.id, include_ancestor_groups: true).execute if project
available_labels = LabelsFinder
.new(current_user, project_id: project.id, include_ancestor_groups: true)
.execute
end
current_user.can?(:"admin_#{issuable.to_ability_name}", project) && project &&
current_user.can?(:"admin_#{issuable.to_ability_name}", project) &&
available_labels.any? available_labels.any?
end end
command :label do |labels_param| command :label do |labels_param|
...@@ -287,7 +292,8 @@ module QuickActions ...@@ -287,7 +292,8 @@ module QuickActions
end end
params '#issue | !merge_request' params '#issue | !merge_request'
condition do condition do
current_user.can?(:"update_#{issuable.to_ability_name}", issuable) [MergeRequest, Issue].include?(issuable.class) &&
current_user.can?(:"update_#{issuable.to_ability_name}", issuable)
end end
parse_params do |issuable_param| parse_params do |issuable_param|
extract_references(issuable_param, :issue).first || extract_references(issuable_param, :issue).first ||
...@@ -444,7 +450,8 @@ module QuickActions ...@@ -444,7 +450,8 @@ module QuickActions
end end
params '<time(1h30m | -1h30m)> <date(YYYY-MM-DD)>' params '<time(1h30m | -1h30m)> <date(YYYY-MM-DD)>'
condition do condition do
current_user.can?(:"admin_#{issuable.to_ability_name}", issuable) issuable.is_a?(TimeTrackable) &&
current_user.can?(:"admin_#{issuable.to_ability_name}", issuable)
end end
parse_params do |raw_time_date| parse_params do |raw_time_date|
Gitlab::QuickActions::SpendTimeAndDateSeparator.new(raw_time_date).execute Gitlab::QuickActions::SpendTimeAndDateSeparator.new(raw_time_date).execute
...@@ -494,7 +501,7 @@ module QuickActions ...@@ -494,7 +501,7 @@ module QuickActions
desc "Lock the discussion" desc "Lock the discussion"
explanation "Locks the discussion" explanation "Locks the discussion"
condition do condition do
issuable.is_a?(Issuable) && [MergeRequest, Issue].include?(issuable.class) &&
issuable.persisted? && issuable.persisted? &&
!issuable.discussion_locked? && !issuable.discussion_locked? &&
current_user.can?(:"admin_#{issuable.to_ability_name}", issuable) current_user.can?(:"admin_#{issuable.to_ability_name}", issuable)
...@@ -506,7 +513,7 @@ module QuickActions ...@@ -506,7 +513,7 @@ module QuickActions
desc "Unlock the discussion" desc "Unlock the discussion"
explanation "Unlocks the discussion" explanation "Unlocks the discussion"
condition do condition do
issuable.is_a?(Issuable) && [MergeRequest, Issue].include?(issuable.class) &&
issuable.persisted? && issuable.persisted? &&
issuable.discussion_locked? && issuable.discussion_locked? &&
current_user.can?(:"admin_#{issuable.to_ability_name}", issuable) current_user.can?(:"admin_#{issuable.to_ability_name}", issuable)
......
...@@ -102,6 +102,7 @@ constraints(::Constraints::GroupUrlConstrainer.new) do ...@@ -102,6 +102,7 @@ constraints(::Constraints::GroupUrlConstrainer.new) do
get 'members' get 'members'
get 'labels' get 'labels'
get 'epics' get 'epics'
get 'commands'
end end
end end
......
...@@ -13,6 +13,10 @@ class Groups::AutocompleteSourcesController < Groups::ApplicationController ...@@ -13,6 +13,10 @@ class Groups::AutocompleteSourcesController < Groups::ApplicationController
render json: @autocomplete_service.epics render json: @autocomplete_service.epics
end end
def commands
render json: @autocomplete_service.commands(target)
end
private private
def load_autocomplete_service def load_autocomplete_service
......
...@@ -70,7 +70,8 @@ module EE ...@@ -70,7 +70,8 @@ module EE
{ {
members: members_group_autocomplete_sources_path(object, type: noteable_type, type_id: params[:id]), members: members_group_autocomplete_sources_path(object, type: noteable_type, type_id: params[:id]),
labels: labels_group_autocomplete_sources_path(object), labels: labels_group_autocomplete_sources_path(object),
epics: epics_group_autocomplete_sources_path(object) epics: epics_group_autocomplete_sources_path(object),
commands: commands_group_autocomplete_sources_path(object, type: noteable_type, type_id: params[:id])
} }
end end
......
...@@ -16,5 +16,11 @@ module Groups ...@@ -16,5 +16,11 @@ module Groups
def labels_as_hash(target) def labels_as_hash(target)
super(target, group_id: group.id, only_group_labels: true) super(target, group_id: group.id, only_group_labels: true)
end end
def commands(noteable)
return [] unless noteable
QuickActions::InterpretService.new(nil, current_user).available_commands(noteable)
end
end end
end end
---
title: Support autocomplete for commands in epics
merge_request: 7588
author:
type: added
# frozen_string_literal: tru
require 'spec_helper'
describe Groups::AutocompleteSourcesController do
let(:user) { create(:user) }
let(:group) { create(:group) }
let!(:epic) { create(:epic, group: group) }
before do
group.add_developer(user)
stub_licensed_features(epics: true)
sign_in(user)
end
context '#epics' do
it 'returns 200 status' do
get :epics, group_id: group
expect(response).to have_gitlab_http_status(200)
end
it 'returns the correct response' do
get :epics, group_id: group
expect(json_response).to be_an(Array)
expect(json_response.first).to include(
'id' => epic.id, 'iid' => epic.iid, 'title' => epic.title
)
end
end
context '#commands' do
it 'returns 200 status' do
get :commands, group_id: group, type: 'Epic', type_id: epic.iid
expect(response).to have_gitlab_http_status(200)
end
it 'returns the correct response' do
get :commands, group_id: group, type: 'Epic', type_id: epic.iid
expect(json_response).to be_an(Array)
expect(json_response).to include(
{ 'name' => 'close', 'aliases' => [], 'description' => 'Close this epic', 'params' => [] }
)
end
end
end
...@@ -15,7 +15,7 @@ describe ApplicationHelper do ...@@ -15,7 +15,7 @@ describe ApplicationHelper do
let(:noteable_type) { Epic } let(:noteable_type) { Epic }
it 'returns paths for autocomplete_sources_controller' do it 'returns paths for autocomplete_sources_controller' do
expect_autocomplete_data_sources(object, noteable_type, [:members, :labels, :epics]) expect_autocomplete_data_sources(object, noteable_type, [:members, :labels, :epics, :commands])
end end
end end
......
...@@ -6,8 +6,10 @@ describe Groups::AutocompleteService do ...@@ -6,8 +6,10 @@ describe Groups::AutocompleteService do
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:epic) { create(:epic, group: group, author: user) } let!(:epic) { create(:epic, group: group, author: user) }
subject { described_class.new(group, user) }
before do before do
create(:group_member, group: group, user: user) group.add_developer(user)
end end
def expect_labels_to_equal(labels, expected_labels) def expect_labels_to_equal(labels, expected_labels)
...@@ -22,8 +24,8 @@ describe Groups::AutocompleteService do ...@@ -22,8 +24,8 @@ describe Groups::AutocompleteService do
let!(:parent_group_label) { create(:group_label, group: group.parent, group_id: group.id) } let!(:parent_group_label) { create(:group_label, group: group.parent, group_id: group.id) }
it 'returns labels from own group and ancestor groups' do it 'returns labels from own group and ancestor groups' do
service = described_class.new(group, user) results = subject.labels_as_hash(nil)
results = service.labels_as_hash(nil)
expected_labels = [label1, label2, parent_group_label] expected_labels = [label1, label2, parent_group_label]
expect_labels_to_equal(results, expected_labels) expect_labels_to_equal(results, expected_labels)
...@@ -35,8 +37,7 @@ describe Groups::AutocompleteService do ...@@ -35,8 +37,7 @@ describe Groups::AutocompleteService do
end end
it 'marks already assigned as set' do it 'marks already assigned as set' do
service = described_class.new(group, user) results = subject.labels_as_hash(epic)
results = service.labels_as_hash(epic)
expected_labels = [label1, label2, parent_group_label] expected_labels = [label1, label2, parent_group_label]
expect_labels_to_equal(results, expected_labels) expect_labels_to_equal(results, expected_labels)
...@@ -56,16 +57,29 @@ describe Groups::AutocompleteService do ...@@ -56,16 +57,29 @@ describe Groups::AutocompleteService do
describe '#epics' do describe '#epics' do
it 'returns nothing if not allowed' do it 'returns nothing if not allowed' do
allow(Ability).to receive(:allowed?).with(user, :read_epic, group).and_return(false) allow(Ability).to receive(:allowed?).with(user, :read_epic, group).and_return(false)
service = described_class.new(group, user)
expect(service.epics).to eq([]) expect(subject.epics).to eq([])
end end
it 'returns epics from group' do it 'returns epics from group' do
allow(Ability).to receive(:allowed?).with(user, :read_epic, group).and_return(true) allow(Ability).to receive(:allowed?).with(user, :read_epic, group).and_return(true)
service = described_class.new(group, user)
expect(service.epics).to contain_exactly(epic) expect(subject.epics).to contain_exactly(epic)
end
end
describe '#commands' do
context 'when target is an epic' do
before do
stub_licensed_features(epics: true)
end
it 'returns available commands' do
expect(subject.commands(epic).map { |c| c[:name] })
.to match_array(
[:todo, :unsubscribe, :award, :shrug, :tableflip, :cc, :title, :close]
)
end
end end
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