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
end
params '~label1 ~"label 2"'
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?
end
command :label do |labels_param|
......@@ -287,7 +292,8 @@ module QuickActions
end
params '#issue | !merge_request'
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
parse_params do |issuable_param|
extract_references(issuable_param, :issue).first ||
......@@ -444,7 +450,8 @@ module QuickActions
end
params '<time(1h30m | -1h30m)> <date(YYYY-MM-DD)>'
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
parse_params do |raw_time_date|
Gitlab::QuickActions::SpendTimeAndDateSeparator.new(raw_time_date).execute
......@@ -494,7 +501,7 @@ module QuickActions
desc "Lock the discussion"
explanation "Locks the discussion"
condition do
issuable.is_a?(Issuable) &&
[MergeRequest, Issue].include?(issuable.class) &&
issuable.persisted? &&
!issuable.discussion_locked? &&
current_user.can?(:"admin_#{issuable.to_ability_name}", issuable)
......@@ -506,7 +513,7 @@ module QuickActions
desc "Unlock the discussion"
explanation "Unlocks the discussion"
condition do
issuable.is_a?(Issuable) &&
[MergeRequest, Issue].include?(issuable.class) &&
issuable.persisted? &&
issuable.discussion_locked? &&
current_user.can?(:"admin_#{issuable.to_ability_name}", issuable)
......
......@@ -102,6 +102,7 @@ constraints(::Constraints::GroupUrlConstrainer.new) do
get 'members'
get 'labels'
get 'epics'
get 'commands'
end
end
......
......@@ -13,6 +13,10 @@ class Groups::AutocompleteSourcesController < Groups::ApplicationController
render json: @autocomplete_service.epics
end
def commands
render json: @autocomplete_service.commands(target)
end
private
def load_autocomplete_service
......
......@@ -70,7 +70,8 @@ module EE
{
members: members_group_autocomplete_sources_path(object, type: noteable_type, type_id: params[:id]),
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
......
......@@ -16,5 +16,11 @@ module Groups
def labels_as_hash(target)
super(target, group_id: group.id, only_group_labels: true)
end
def commands(noteable)
return [] unless noteable
QuickActions::InterpretService.new(nil, current_user).available_commands(noteable)
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
let(:noteable_type) { Epic }
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
......
......@@ -6,8 +6,10 @@ describe Groups::AutocompleteService do
let(:user) { create(:user) }
let!(:epic) { create(:epic, group: group, author: user) }
subject { described_class.new(group, user) }
before do
create(:group_member, group: group, user: user)
group.add_developer(user)
end
def expect_labels_to_equal(labels, expected_labels)
......@@ -22,8 +24,8 @@ describe Groups::AutocompleteService do
let!(:parent_group_label) { create(:group_label, group: group.parent, group_id: group.id) }
it 'returns labels from own group and ancestor groups' do
service = described_class.new(group, user)
results = service.labels_as_hash(nil)
results = subject.labels_as_hash(nil)
expected_labels = [label1, label2, parent_group_label]
expect_labels_to_equal(results, expected_labels)
......@@ -35,8 +37,7 @@ describe Groups::AutocompleteService do
end
it 'marks already assigned as set' do
service = described_class.new(group, user)
results = service.labels_as_hash(epic)
results = subject.labels_as_hash(epic)
expected_labels = [label1, label2, parent_group_label]
expect_labels_to_equal(results, expected_labels)
......@@ -56,16 +57,29 @@ describe Groups::AutocompleteService do
describe '#epics' do
it 'returns nothing if not allowed' do
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
it 'returns epics from group' do
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
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