2016-06-30 11:34:19 -04:00
|
|
|
module SlashCommands
|
|
|
|
class InterpretService < BaseService
|
|
|
|
include Gitlab::SlashCommands::Dsl
|
|
|
|
|
2016-08-13 12:58:51 -04:00
|
|
|
attr_reader :issuable
|
2016-08-09 13:26:45 -04:00
|
|
|
|
2016-08-16 20:59:55 -04:00
|
|
|
# Takes a text and interprets the commands that are extracted from it.
|
|
|
|
# Returns the content without commands, and hash of changes to be applied to a record.
|
2016-08-13 12:58:51 -04:00
|
|
|
def execute(content, issuable)
|
|
|
|
@issuable = issuable
|
2016-06-30 11:34:19 -04:00
|
|
|
@updates = {}
|
|
|
|
|
2016-08-12 21:17:18 -04:00
|
|
|
opts = {
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable: issuable,
|
2016-08-12 21:17:18 -04:00
|
|
|
current_user: current_user,
|
|
|
|
project: project
|
|
|
|
}
|
|
|
|
|
|
|
|
content, commands = extractor.extract_commands(content, opts)
|
|
|
|
|
2016-08-18 15:21:52 -04:00
|
|
|
commands.each do |name, arg|
|
2016-08-12 21:17:18 -04:00
|
|
|
definition = self.class.command_definitions_by_name[name.to_sym]
|
|
|
|
next unless definition
|
|
|
|
|
2016-08-18 15:21:52 -04:00
|
|
|
definition.execute(self, opts, arg)
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 21:17:18 -04:00
|
|
|
[content, @updates]
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-12 21:17:18 -04:00
|
|
|
def extractor
|
|
|
|
Gitlab::SlashCommands::Extractor.new(self.class.command_definitions)
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-11 12:51:37 -04:00
|
|
|
desc do
|
2016-08-13 12:58:51 -04:00
|
|
|
"Close this #{issuable.to_ability_name.humanize(capitalize: false)}"
|
2016-08-11 12:51:37 -04:00
|
|
|
end
|
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
issuable.open? &&
|
|
|
|
current_user.can?(:"update_#{issuable.to_ability_name}", issuable)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
command :close do
|
|
|
|
@updates[:state_event] = 'close'
|
|
|
|
end
|
|
|
|
|
2016-08-11 12:51:37 -04:00
|
|
|
desc do
|
2016-08-13 12:58:51 -04:00
|
|
|
"Reopen this #{issuable.to_ability_name.humanize(capitalize: false)}"
|
2016-08-11 12:51:37 -04:00
|
|
|
end
|
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
issuable.closed? &&
|
|
|
|
current_user.can?(:"update_#{issuable.to_ability_name}", issuable)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-08-17 19:58:52 -04:00
|
|
|
command :reopen do
|
2016-06-30 11:34:19 -04:00
|
|
|
@updates[:state_event] = 'reopen'
|
|
|
|
end
|
|
|
|
|
2016-08-09 14:54:18 -04:00
|
|
|
desc 'Change title'
|
|
|
|
params '<New title>'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
current_user.can?(:"update_#{issuable.to_ability_name}", issuable)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-08-09 14:54:18 -04:00
|
|
|
command :title do |title_param|
|
|
|
|
@updates[:title] = title_param
|
|
|
|
end
|
|
|
|
|
2016-08-09 16:47:29 -04:00
|
|
|
desc 'Assign'
|
2016-06-30 11:34:19 -04:00
|
|
|
params '@user'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-08-12 21:23:49 -04:00
|
|
|
command :assign do |assignee_param|
|
2016-06-30 11:34:19 -04:00
|
|
|
user = extract_references(assignee_param, :user).first
|
2016-08-12 21:23:33 -04:00
|
|
|
user ||= User.find_by(username: assignee_param)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2016-08-12 05:19:29 -04:00
|
|
|
@updates[:assignee_id] = user.id if user
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Remove assignee'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
issuable.assignee_id? &&
|
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-08-17 19:58:52 -04:00
|
|
|
command :unassign do
|
2016-06-30 11:34:19 -04:00
|
|
|
@updates[:assignee_id] = nil
|
|
|
|
end
|
|
|
|
|
2016-08-09 16:47:29 -04:00
|
|
|
desc 'Set milestone'
|
2016-06-30 11:34:19 -04:00
|
|
|
params '%"milestone"'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project) &&
|
2016-08-12 21:23:49 -04:00
|
|
|
project.milestones.active.any?
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
command :milestone do |milestone_param|
|
|
|
|
milestone = extract_references(milestone_param, :milestone).first
|
2016-08-12 21:23:33 -04:00
|
|
|
milestone ||= project.milestones.find_by(title: milestone_param.strip)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2016-08-12 05:19:29 -04:00
|
|
|
@updates[:milestone_id] = milestone.id if milestone
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Remove milestone'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
issuable.milestone_id? &&
|
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-08-17 19:58:52 -04:00
|
|
|
command :remove_milestone do
|
2016-06-30 11:34:19 -04:00
|
|
|
@updates[:milestone_id] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Add label(s)'
|
|
|
|
params '~label1 ~"label 2"'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project) &&
|
2016-08-12 21:23:49 -04:00
|
|
|
project.labels.any?
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-08-17 19:58:52 -04:00
|
|
|
command :label do |labels_param|
|
2016-06-30 11:34:19 -04:00
|
|
|
label_ids = find_label_ids(labels_param)
|
|
|
|
|
2016-08-12 05:19:29 -04:00
|
|
|
@updates[:add_label_ids] = label_ids unless label_ids.empty?
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-17 19:58:52 -04:00
|
|
|
desc 'Remove all or specific label(s)'
|
2016-06-30 11:34:19 -04:00
|
|
|
params '~label1 ~"label 2"'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
issuable.labels.any? &&
|
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-08-17 19:58:52 -04:00
|
|
|
command :unlabel do |labels_param = nil|
|
|
|
|
if labels_param.present?
|
|
|
|
label_ids = find_label_ids(labels_param)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2016-08-17 19:58:52 -04:00
|
|
|
@updates[:remove_label_ids] = label_ids unless label_ids.empty?
|
|
|
|
else
|
|
|
|
@updates[:label_ids] = []
|
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-17 19:58:52 -04:00
|
|
|
desc 'Replace all label(s)'
|
|
|
|
params '~label1 ~"label 2"'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
issuable.labels.any? &&
|
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-08-17 19:58:52 -04:00
|
|
|
command :relabel do |labels_param|
|
|
|
|
label_ids = find_label_ids(labels_param)
|
|
|
|
|
|
|
|
@updates[:label_ids] = label_ids unless label_ids.empty?
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Add a todo'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
!TodoService.new.todo_exist?(issuable, current_user)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
command :todo do
|
2016-08-09 16:47:29 -04:00
|
|
|
@updates[:todo_event] = 'add'
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Mark todo as done'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
TodoService.new.todo_exist?(issuable, current_user)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
command :done do
|
|
|
|
@updates[:todo_event] = 'done'
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Subscribe'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
!issuable.subscribed?(current_user)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
command :subscribe do
|
|
|
|
@updates[:subscription_event] = 'subscribe'
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Unsubscribe'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
issuable.subscribed?(current_user)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
command :unsubscribe do
|
|
|
|
@updates[:subscription_event] = 'unsubscribe'
|
|
|
|
end
|
|
|
|
|
2016-08-10 11:51:01 -04:00
|
|
|
desc 'Set due date'
|
2016-08-17 19:58:52 -04:00
|
|
|
params '<in 2 days | this Friday | December 31st>'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.respond_to?(:due_date) &&
|
|
|
|
current_user.can?(:"update_#{issuable.to_ability_name}", issuable)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-08-17 19:58:52 -04:00
|
|
|
command :due do |due_date_param|
|
2016-08-10 11:51:01 -04:00
|
|
|
due_date = Chronic.parse(due_date_param).try(:to_date)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
@updates[:due_date] = due_date if due_date
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Remove due date'
|
2016-08-11 12:51:37 -04:00
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
issuable.persisted? &&
|
|
|
|
issuable.respond_to?(:due_date) &&
|
|
|
|
issuable.due_date? &&
|
|
|
|
current_user.can?(:"update_#{issuable.to_ability_name}", issuable)
|
2016-08-10 11:51:01 -04:00
|
|
|
end
|
2016-08-17 19:58:52 -04:00
|
|
|
command :remove_due_date do
|
2016-06-30 11:34:19 -04:00
|
|
|
@updates[:due_date] = nil
|
|
|
|
end
|
|
|
|
|
2016-08-10 08:12:09 -04:00
|
|
|
# This is a dummy command, so that it appears in the autocomplete commands
|
|
|
|
desc 'CC'
|
|
|
|
params '@user'
|
2016-08-12 21:23:49 -04:00
|
|
|
command :cc
|
2016-08-10 08:12:09 -04:00
|
|
|
|
2016-06-30 11:34:19 -04:00
|
|
|
def find_label_ids(labels_param)
|
2016-08-12 21:23:33 -04:00
|
|
|
label_ids_by_reference = extract_references(labels_param, :label).map(&:id)
|
|
|
|
labels_ids_by_name = @project.labels.where(name: labels_param.split).select(:id)
|
|
|
|
|
|
|
|
label_ids_by_reference | labels_ids_by_name
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 21:23:33 -04:00
|
|
|
def extract_references(arg, type)
|
2016-06-30 11:34:19 -04:00
|
|
|
ext = Gitlab::ReferenceExtractor.new(project, current_user)
|
2016-08-12 21:23:33 -04:00
|
|
|
ext.analyze(arg, author: current_user)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
ext.references(type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|