2017-05-31 01:50:53 -04:00
|
|
|
|
module QuickActions
|
2016-06-30 11:34:19 -04:00
|
|
|
|
class InterpretService < BaseService
|
2017-05-31 01:50:53 -04:00
|
|
|
|
include Gitlab::QuickActions::Dsl
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
|
attr_reader :issuable
|
2016-08-09 13:26:45 -04:00
|
|
|
|
|
2017-07-24 23:11:22 -04:00
|
|
|
|
SHRUG = '¯\\_(ツ)_/¯'.freeze
|
|
|
|
|
TABLEFLIP = '(╯°□°)╯︵ ┻━┻'.freeze
|
|
|
|
|
|
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)
|
2017-05-31 01:50:53 -04:00
|
|
|
|
return [content, {}] unless current_user.can?(:use_quick_actions)
|
2017-04-06 14:19:45 -04:00
|
|
|
|
|
2016-08-13 12:58:51 -04:00
|
|
|
|
@issuable = issuable
|
2016-06-30 11:34:19 -04:00
|
|
|
|
@updates = {}
|
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
|
content, commands = extractor.extract_commands(content, context)
|
|
|
|
|
extract_updates(commands, context)
|
2017-07-24 23:11:22 -04:00
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
|
[content, @updates]
|
|
|
|
|
end
|
2016-08-12 21:17:18 -04:00
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
|
# Takes a text and interprets the commands that are extracted from it.
|
|
|
|
|
# Returns the content without commands, and array of changes explained.
|
|
|
|
|
def explain(content, issuable)
|
2017-05-31 01:50:53 -04:00
|
|
|
|
return [content, []] unless current_user.can?(:use_quick_actions)
|
2016-08-12 21:17:18 -04:00
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
|
@issuable = issuable
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
|
content, commands = extractor.extract_commands(content, context)
|
|
|
|
|
commands = explain_commands(commands, context)
|
|
|
|
|
[content, commands]
|
2016-06-30 11:34:19 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2016-08-12 21:17:18 -04:00
|
|
|
|
def extractor
|
2017-05-31 01:50:53 -04:00
|
|
|
|
Gitlab::QuickActions::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
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation do
|
|
|
|
|
"Closes this #{issuable.to_ability_name.humanize(capitalize: false)}."
|
|
|
|
|
end
|
2016-08-11 12:51:37 -04:00
|
|
|
|
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
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation do
|
|
|
|
|
"Reopens this #{issuable.to_ability_name.humanize(capitalize: false)}."
|
|
|
|
|
end
|
2016-08-11 12:51:37 -04:00
|
|
|
|
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
|
|
|
|
|
|
2017-02-17 08:56:13 -05:00
|
|
|
|
desc 'Merge (when the pipeline succeeds)'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation 'Merges this merge request when the pipeline succeeds.'
|
2016-11-24 09:05:15 -05:00
|
|
|
|
condition do
|
2016-12-30 14:49:59 -05:00
|
|
|
|
last_diff_sha = params && params[:merge_request_diff_head_sha]
|
2016-11-24 09:05:15 -05:00
|
|
|
|
issuable.is_a?(MergeRequest) &&
|
2016-12-30 14:49:59 -05:00
|
|
|
|
issuable.persisted? &&
|
2017-05-31 01:50:53 -04:00
|
|
|
|
issuable.mergeable_with_quick_action?(current_user, autocomplete_precheck: !last_diff_sha, last_diff_sha: last_diff_sha)
|
2016-11-24 09:05:15 -05:00
|
|
|
|
end
|
|
|
|
|
command :merge do
|
|
|
|
|
@updates[:merge] = params[:merge_request_diff_head_sha]
|
|
|
|
|
end
|
|
|
|
|
|
2016-08-09 14:54:18 -04:00
|
|
|
|
desc 'Change title'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation do |title_param|
|
|
|
|
|
"Changes the title to \"#{title_param}\"."
|
|
|
|
|
end
|
2016-08-09 14:54:18 -04:00
|
|
|
|
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'
|
2017-05-05 11:22:54 -04:00
|
|
|
|
explanation do |users|
|
2017-06-14 16:08:24 -04:00
|
|
|
|
users = issuable.allows_multiple_assignees? ? users : users.take(1)
|
|
|
|
|
"Assigns #{users.map(&:to_reference).to_sentence}."
|
|
|
|
|
end
|
|
|
|
|
params do
|
|
|
|
|
issuable.allows_multiple_assignees? ? '@user1 @user2' : '@user'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
end
|
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
|
2017-05-05 11:22:54 -04:00
|
|
|
|
parse_params do |assignee_param|
|
2017-06-06 04:49:36 -04:00
|
|
|
|
extract_users(assignee_param)
|
2017-05-05 11:22:54 -04:00
|
|
|
|
end
|
|
|
|
|
command :assign do |users|
|
|
|
|
|
next if users.empty?
|
2017-05-04 08:11:15 -04:00
|
|
|
|
|
2017-06-20 15:32:49 -04:00
|
|
|
|
@updates[:assignee_ids] =
|
|
|
|
|
if issuable.allows_multiple_assignees?
|
|
|
|
|
issuable.assignees.pluck(:id) + users.map(&:id)
|
|
|
|
|
else
|
2017-08-15 06:27:37 -04:00
|
|
|
|
[users.first.id]
|
2017-06-20 15:32:49 -04:00
|
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-06-14 16:08:24 -04:00
|
|
|
|
desc do
|
|
|
|
|
if issuable.allows_multiple_assignees?
|
|
|
|
|
'Remove all or specific assignee(s)'
|
|
|
|
|
else
|
|
|
|
|
'Remove assignee'
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation do
|
2017-06-14 16:08:24 -04:00
|
|
|
|
"Removes #{'assignee'.pluralize(issuable.assignees.size)} #{issuable.assignees.map(&:to_reference).to_sentence}."
|
|
|
|
|
end
|
|
|
|
|
params do
|
|
|
|
|
issuable.allows_multiple_assignees? ? '@user1 @user2' : ''
|
2016-11-16 06:09:09 -05:00
|
|
|
|
end
|
2016-08-11 12:51:37 -04:00
|
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
|
issuable.persisted? &&
|
2017-05-04 08:11:15 -04:00
|
|
|
|
issuable.assignees.any? &&
|
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
|
2017-06-20 16:02:41 -04:00
|
|
|
|
parse_params do |unassign_param|
|
2017-06-14 16:08:24 -04:00
|
|
|
|
# When multiple users are assigned, all will be unassigned if multiple assignees are no longer allowed
|
2017-06-20 16:02:41 -04:00
|
|
|
|
extract_users(unassign_param) if issuable.allows_multiple_assignees?
|
|
|
|
|
end
|
|
|
|
|
command :unassign do |users = nil|
|
2017-06-20 15:32:49 -04:00
|
|
|
|
@updates[:assignee_ids] =
|
|
|
|
|
if users&.any?
|
|
|
|
|
issuable.assignees.pluck(:id) - users.map(&:id)
|
|
|
|
|
else
|
|
|
|
|
[]
|
|
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
|
end
|
|
|
|
|
|
2016-08-09 16:47:29 -04:00
|
|
|
|
desc 'Set milestone'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation do |milestone|
|
|
|
|
|
"Sets the milestone to #{milestone.to_reference}." if milestone
|
|
|
|
|
end
|
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-11-16 06:09:09 -05:00
|
|
|
|
parse_params do |milestone_param|
|
|
|
|
|
extract_references(milestone_param, :milestone).first ||
|
|
|
|
|
project.milestones.find_by(title: milestone_param.strip)
|
|
|
|
|
end
|
|
|
|
|
command :milestone do |milestone|
|
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-11-16 06:09:09 -05:00
|
|
|
|
explanation do
|
|
|
|
|
"Removes #{issuable.milestone.to_reference(format: :name)} milestone."
|
|
|
|
|
end
|
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)'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation do |labels_param|
|
|
|
|
|
labels = find_label_references(labels_param)
|
|
|
|
|
|
|
|
|
|
"Adds #{labels.join(' ')} #{'label'.pluralize(labels.count)}." if labels.any?
|
|
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
|
params '~label1 ~"label 2"'
|
2016-08-11 12:51:37 -04:00
|
|
|
|
condition do
|
2016-10-18 02:30:43 -04:00
|
|
|
|
available_labels = LabelsFinder.new(current_user, project_id: project.id).execute
|
|
|
|
|
|
2016-08-13 12:58:51 -04:00
|
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project) &&
|
2016-10-18 02:30:43 -04:00
|
|
|
|
available_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-10-10 11:04:52 -04:00
|
|
|
|
if label_ids.any?
|
|
|
|
|
@updates[:add_label_ids] ||= []
|
|
|
|
|
@updates[:add_label_ids] += label_ids
|
|
|
|
|
|
|
|
|
|
@updates[:add_label_ids].uniq!
|
|
|
|
|
end
|
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-11-16 06:09:09 -05:00
|
|
|
|
explanation do |labels_param = nil|
|
|
|
|
|
if labels_param.present?
|
|
|
|
|
labels = find_label_references(labels_param)
|
|
|
|
|
"Removes #{labels.join(' ')} #{'label'.pluralize(labels.count)}." if labels.any?
|
|
|
|
|
else
|
|
|
|
|
'Removes all labels.'
|
|
|
|
|
end
|
|
|
|
|
end
|
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-10-10 11:04:52 -04:00
|
|
|
|
if label_ids.any?
|
|
|
|
|
@updates[:remove_label_ids] ||= []
|
|
|
|
|
@updates[:remove_label_ids] += label_ids
|
|
|
|
|
|
|
|
|
|
@updates[:remove_label_ids].uniq!
|
|
|
|
|
end
|
2016-08-17 19:58:52 -04:00
|
|
|
|
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)'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation do |labels_param|
|
|
|
|
|
labels = find_label_references(labels_param)
|
|
|
|
|
"Replaces all labels with #{labels.join(' ')} #{'label'.pluralize(labels.count)}." if labels.any?
|
|
|
|
|
end
|
2016-08-17 19:58:52 -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 :relabel do |labels_param|
|
|
|
|
|
label_ids = find_label_ids(labels_param)
|
|
|
|
|
|
2016-10-10 11:04:52 -04:00
|
|
|
|
if label_ids.any?
|
|
|
|
|
@updates[:label_ids] ||= []
|
|
|
|
|
@updates[:label_ids] += label_ids
|
|
|
|
|
|
|
|
|
|
@updates[:label_ids].uniq!
|
|
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
desc 'Add a todo'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation 'Adds 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-11-16 06:09:09 -05:00
|
|
|
|
explanation 'Marks 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-11-16 06:09:09 -05:00
|
|
|
|
explanation do
|
|
|
|
|
"Subscribes to this #{issuable.to_ability_name.humanize(capitalize: false)}."
|
|
|
|
|
end
|
2016-08-11 12:51:37 -04:00
|
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
|
issuable.persisted? &&
|
2016-11-04 14:19:08 -04:00
|
|
|
|
!issuable.subscribed?(current_user, project)
|
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-11-16 06:09:09 -05:00
|
|
|
|
explanation do
|
|
|
|
|
"Unsubscribes from this #{issuable.to_ability_name.humanize(capitalize: false)}."
|
|
|
|
|
end
|
2016-08-11 12:51:37 -04:00
|
|
|
|
condition do
|
2016-08-13 12:58:51 -04:00
|
|
|
|
issuable.persisted? &&
|
2016-11-04 14:19:08 -04:00
|
|
|
|
issuable.subscribed?(current_user, project)
|
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-11-16 06:09:09 -05:00
|
|
|
|
explanation do |due_date|
|
|
|
|
|
"Sets the due date to #{due_date.to_s(:medium)}." if due_date
|
|
|
|
|
end
|
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) &&
|
2016-09-27 05:29:06 -04:00
|
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
|
2016-08-10 11:51:01 -04:00
|
|
|
|
end
|
2016-11-16 06:09:09 -05:00
|
|
|
|
parse_params do |due_date_param|
|
|
|
|
|
Chronic.parse(due_date_param).try(:to_date)
|
|
|
|
|
end
|
|
|
|
|
command :due do |due_date|
|
2016-06-30 11:34:19 -04:00
|
|
|
|
@updates[:due_date] = due_date if due_date
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
desc 'Remove due date'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation 'Removes the 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? &&
|
2016-09-27 05:29:06 -04:00
|
|
|
|
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_due_date do
|
2016-06-30 11:34:19 -04:00
|
|
|
|
@updates[:due_date] = nil
|
|
|
|
|
end
|
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
|
desc 'Toggle the Work In Progress status'
|
|
|
|
|
explanation do
|
|
|
|
|
verb = issuable.work_in_progress? ? 'Unmarks' : 'Marks'
|
|
|
|
|
noun = issuable.to_ability_name.humanize(capitalize: false)
|
|
|
|
|
"#{verb} this #{noun} as Work In Progress."
|
2016-09-08 05:18:41 -04:00
|
|
|
|
end
|
|
|
|
|
condition do
|
|
|
|
|
issuable.persisted? &&
|
|
|
|
|
issuable.respond_to?(:work_in_progress?) &&
|
|
|
|
|
current_user.can?(:"update_#{issuable.to_ability_name}", issuable)
|
|
|
|
|
end
|
|
|
|
|
command :wip do
|
|
|
|
|
@updates[:wip_event] = issuable.work_in_progress? ? 'unwip' : 'wip'
|
|
|
|
|
end
|
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
|
desc 'Toggle emoji award'
|
|
|
|
|
explanation do |name|
|
|
|
|
|
"Toggles :#{name}: emoji award." if name
|
|
|
|
|
end
|
2017-02-28 15:38:19 -05:00
|
|
|
|
params ':emoji:'
|
|
|
|
|
condition do
|
|
|
|
|
issuable.persisted?
|
|
|
|
|
end
|
2016-11-16 06:09:09 -05:00
|
|
|
|
parse_params do |emoji_param|
|
|
|
|
|
match = emoji_param.match(Banzai::Filter::EmojiFilter.emoji_pattern)
|
|
|
|
|
match[1] if match
|
|
|
|
|
end
|
|
|
|
|
command :award do |name|
|
2017-02-28 15:38:19 -05:00
|
|
|
|
if name && issuable.user_can_award?(current_user, name)
|
|
|
|
|
@updates[:emoji_award] = name
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-12-23 00:44:02 -05:00
|
|
|
|
desc 'Set time estimate'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation do |time_estimate|
|
|
|
|
|
time_estimate = Gitlab::TimeTrackingFormatter.output(time_estimate)
|
|
|
|
|
|
|
|
|
|
"Sets time estimate to #{time_estimate}." if time_estimate
|
|
|
|
|
end
|
2016-12-23 00:44:02 -05:00
|
|
|
|
params '<1w 3d 2h 14m>'
|
|
|
|
|
condition do
|
|
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
|
|
|
|
|
end
|
2016-11-16 06:09:09 -05:00
|
|
|
|
parse_params do |raw_duration|
|
|
|
|
|
Gitlab::TimeTrackingFormatter.parse(raw_duration)
|
|
|
|
|
end
|
|
|
|
|
command :estimate do |time_estimate|
|
2016-12-23 00:44:02 -05:00
|
|
|
|
if time_estimate
|
|
|
|
|
@updates[:time_estimate] = time_estimate
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
desc 'Add or substract spent time'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation do |time_spent|
|
|
|
|
|
if time_spent
|
|
|
|
|
if time_spent > 0
|
|
|
|
|
verb = 'Adds'
|
|
|
|
|
value = time_spent
|
|
|
|
|
else
|
|
|
|
|
verb = 'Substracts'
|
|
|
|
|
value = -time_spent
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
"#{verb} #{Gitlab::TimeTrackingFormatter.output(value)} spent time."
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-12-23 00:44:02 -05:00
|
|
|
|
params '<1h 30m | -1h 30m>'
|
|
|
|
|
condition do
|
|
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", issuable)
|
|
|
|
|
end
|
2016-11-16 06:09:09 -05:00
|
|
|
|
parse_params do |raw_duration|
|
|
|
|
|
Gitlab::TimeTrackingFormatter.parse(raw_duration)
|
|
|
|
|
end
|
|
|
|
|
command :spend do |time_spent|
|
2016-12-23 00:44:02 -05:00
|
|
|
|
if time_spent
|
2017-01-18 11:48:16 -05:00
|
|
|
|
@updates[:spend_time] = { duration: time_spent, user: current_user }
|
2016-12-23 00:44:02 -05:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
desc 'Remove time estimate'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation 'Removes time estimate.'
|
2016-12-23 00:44:02 -05:00
|
|
|
|
condition do
|
|
|
|
|
issuable.persisted? &&
|
|
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
|
|
|
|
|
end
|
|
|
|
|
command :remove_estimate do
|
|
|
|
|
@updates[:time_estimate] = 0
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
desc 'Remove spent time'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation 'Removes spent time.'
|
2016-12-23 00:44:02 -05:00
|
|
|
|
condition do
|
|
|
|
|
issuable.persisted? &&
|
|
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
|
|
|
|
|
end
|
|
|
|
|
command :remove_time_spent do
|
2017-01-18 11:48:16 -05:00
|
|
|
|
@updates[:spend_time] = { duration: :reset, user: current_user }
|
2016-12-23 00:44:02 -05:00
|
|
|
|
end
|
|
|
|
|
|
2017-07-24 23:11:22 -04:00
|
|
|
|
desc "Append the comment with #{SHRUG}"
|
|
|
|
|
params '<Comment>'
|
|
|
|
|
substitution :shrug do |comment|
|
|
|
|
|
"#{comment} #{SHRUG}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
desc "Append the comment with #{TABLEFLIP}"
|
|
|
|
|
params '<Comment>'
|
|
|
|
|
substitution :tableflip do |comment|
|
|
|
|
|
"#{comment} #{TABLEFLIP}"
|
|
|
|
|
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
|
|
|
|
|
2017-06-14 17:52:50 -04:00
|
|
|
|
desc 'Set target branch'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation do |branch_name|
|
|
|
|
|
"Sets target branch to #{branch_name}."
|
|
|
|
|
end
|
2017-01-31 08:27:29 -05:00
|
|
|
|
params '<Local branch name>'
|
|
|
|
|
condition do
|
|
|
|
|
issuable.respond_to?(:target_branch) &&
|
|
|
|
|
(current_user.can?(:"update_#{issuable.to_ability_name}", issuable) ||
|
|
|
|
|
issuable.new_record?)
|
|
|
|
|
end
|
2016-11-16 06:09:09 -05:00
|
|
|
|
parse_params do |target_branch_param|
|
|
|
|
|
target_branch_param.strip
|
|
|
|
|
end
|
|
|
|
|
command :target_branch do |branch_name|
|
2017-01-31 08:27:29 -05:00
|
|
|
|
@updates[:target_branch] = branch_name if project.repository.branch_names.include?(branch_name)
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-28 08:11:53 -04:00
|
|
|
|
desc 'Move issue from one column of the board to another'
|
2016-11-16 06:09:09 -05:00
|
|
|
|
explanation do |target_list_name|
|
|
|
|
|
label = find_label_references(target_list_name).first
|
|
|
|
|
"Moves issue to #{label} column in the board." if label
|
|
|
|
|
end
|
2017-04-28 08:11:53 -04:00
|
|
|
|
params '~"Target column"'
|
|
|
|
|
condition do
|
|
|
|
|
issuable.is_a?(Issue) &&
|
|
|
|
|
current_user.can?(:"update_#{issuable.to_ability_name}", issuable) &&
|
|
|
|
|
issuable.project.boards.count == 1
|
|
|
|
|
end
|
|
|
|
|
command :board_move do |target_list_name|
|
|
|
|
|
label_ids = find_label_ids(target_list_name)
|
|
|
|
|
|
|
|
|
|
if label_ids.size == 1
|
|
|
|
|
label_id = label_ids.first
|
|
|
|
|
|
|
|
|
|
# Ensure this label corresponds to a list on the board
|
|
|
|
|
next unless Label.on_project_boards(issuable.project_id).where(id: label_id).exists?
|
|
|
|
|
|
|
|
|
|
@updates[:remove_label_ids] =
|
|
|
|
|
issuable.labels.on_project_boards(issuable.project_id).where.not(id: label_id).pluck(:id)
|
|
|
|
|
@updates[:add_label_ids] = [label_id]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-03-29 21:39:06 -04:00
|
|
|
|
desc 'Mark this issue as a duplicate of another issue'
|
2017-07-20 10:42:33 -04:00
|
|
|
|
explanation do |duplicate_reference|
|
|
|
|
|
"Marks this issue as a duplicate of #{duplicate_reference}."
|
|
|
|
|
end
|
2017-03-29 21:39:06 -04:00
|
|
|
|
params '#issue'
|
|
|
|
|
condition do
|
|
|
|
|
issuable.is_a?(Issue) &&
|
|
|
|
|
issuable.persisted? &&
|
|
|
|
|
current_user.can?(:"update_#{issuable.to_ability_name}", issuable)
|
|
|
|
|
end
|
|
|
|
|
command :duplicate do |duplicate_param|
|
2017-07-20 10:42:33 -04:00
|
|
|
|
canonical_issue = extract_references(duplicate_param, :issue).first
|
|
|
|
|
|
|
|
|
|
if canonical_issue.present?
|
|
|
|
|
@updates[:canonical_issue_id] = canonical_issue.id
|
2017-03-29 21:39:06 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-08-09 11:37:06 -04:00
|
|
|
|
desc 'Move this issue to another project.'
|
|
|
|
|
explanation do |path_to_project|
|
|
|
|
|
"Moves this issue to #{path_to_project}."
|
|
|
|
|
end
|
|
|
|
|
params 'path/to/project'
|
|
|
|
|
condition do
|
|
|
|
|
issuable.is_a?(Issue) &&
|
|
|
|
|
issuable.persisted? &&
|
|
|
|
|
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
|
|
|
|
|
end
|
|
|
|
|
command :move do |target_project_path|
|
|
|
|
|
target_project = Project.find_by_full_path(target_project_path)
|
|
|
|
|
|
|
|
|
|
if target_project.present?
|
|
|
|
|
@updates[:target_project] = target_project
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-06 04:49:36 -04:00
|
|
|
|
def extract_users(params)
|
|
|
|
|
return [] if params.nil?
|
|
|
|
|
|
|
|
|
|
users = extract_references(params, :user)
|
|
|
|
|
|
|
|
|
|
if users.empty?
|
2017-08-06 18:12:13 -04:00
|
|
|
|
users =
|
|
|
|
|
if params == 'me'
|
|
|
|
|
[current_user]
|
|
|
|
|
else
|
|
|
|
|
User.where(username: params.split(' ').map(&:strip))
|
|
|
|
|
end
|
2017-06-06 04:49:36 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
users
|
|
|
|
|
end
|
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
|
def find_labels(labels_param)
|
|
|
|
|
extract_references(labels_param, :label) |
|
|
|
|
|
LabelsFinder.new(current_user, project_id: project.id, name: labels_param.split).execute
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def find_label_references(labels_param)
|
|
|
|
|
find_labels(labels_param).map(&:to_reference)
|
|
|
|
|
end
|
|
|
|
|
|
2016-06-30 11:34:19 -04:00
|
|
|
|
def find_label_ids(labels_param)
|
2016-11-16 06:09:09 -05:00
|
|
|
|
find_labels(labels_param).map(&:id)
|
|
|
|
|
end
|
2016-08-12 21:23:33 -04:00
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
|
def explain_commands(commands, opts)
|
|
|
|
|
commands.map do |name, arg|
|
|
|
|
|
definition = self.class.definition_by_name(name)
|
|
|
|
|
next unless definition
|
|
|
|
|
|
|
|
|
|
definition.explain(self, opts, arg)
|
|
|
|
|
end.compact
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def extract_updates(commands, opts)
|
|
|
|
|
commands.each do |name, arg|
|
|
|
|
|
definition = self.class.definition_by_name(name)
|
|
|
|
|
next unless definition
|
|
|
|
|
|
|
|
|
|
definition.execute(self, opts, arg)
|
|
|
|
|
end
|
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
|
2017-02-28 15:38:19 -05:00
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
|
def context
|
|
|
|
|
{
|
|
|
|
|
issuable: issuable,
|
|
|
|
|
current_user: current_user,
|
|
|
|
|
project: project,
|
|
|
|
|
params: params
|
|
|
|
|
}
|
2017-02-28 15:38:19 -05:00
|
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
|
end
|
|
|
|
|
end
|