2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
module Gitlab
module QuickActions
module IssueAndMergeRequestActions
extend ActiveSupport :: Concern
include Gitlab :: QuickActions :: Dsl
included do
# Issue, MergeRequest: quick actions definitions
2019-07-31 22:56:46 +05:30
desc _ ( 'Assign' )
2019-07-07 11:18:12 +05:30
explanation do | users |
2019-10-12 21:52:04 +05:30
_ ( 'Assigns %{assignee_users_sentence}.' ) % { assignee_users_sentence : assignee_users_sentence ( users ) }
2019-07-07 11:18:12 +05:30
end
params do
quick_action_target . allows_multiple_assignees? ? '@user1 @user2' : '@user'
end
types Issue , MergeRequest
condition do
current_user . can? ( :" admin_ #{ quick_action_target . to_ability_name } " , project )
end
parse_params do | assignee_param |
extract_users ( assignee_param )
end
command :assign do | users |
2019-10-12 21:52:04 +05:30
if users . empty?
@execution_message [ :assign ] = _ ( " Failed to assign a user because no user was found. " )
next
end
2019-07-07 11:18:12 +05:30
if quick_action_target . allows_multiple_assignees?
@updates [ :assignee_ids ] || = quick_action_target . assignees . map ( & :id )
@updates [ :assignee_ids ] += users . map ( & :id )
else
@updates [ :assignee_ids ] = [ users . first . id ]
end
2019-10-12 21:52:04 +05:30
@execution_message [ :assign ] = _ ( 'Assigned %{assignee_users_sentence}.' ) % { assignee_users_sentence : assignee_users_sentence ( users ) }
2019-07-07 11:18:12 +05:30
end
desc do
if quick_action_target . allows_multiple_assignees?
2019-07-31 22:56:46 +05:30
_ ( 'Remove all or specific assignee(s)' )
2019-07-07 11:18:12 +05:30
else
2019-07-31 22:56:46 +05:30
_ ( 'Remove assignee' )
2019-07-07 11:18:12 +05:30
end
end
explanation do | users = nil |
2019-10-12 21:52:04 +05:30
assignees = assignees_for_removal ( users )
_ ( " Removes %{assignee_text} %{assignee_references}. " ) %
{ assignee_text : 'assignee' . pluralize ( assignees . size ) , assignee_references : assignees . map ( & :to_reference ) . to_sentence }
end
execution_message do | users = nil |
assignees = assignees_for_removal ( users )
_ ( " Removed %{assignee_text} %{assignee_references}. " ) %
{ assignee_text : 'assignee' . pluralize ( assignees . size ) , assignee_references : assignees . map ( & :to_reference ) . to_sentence }
2019-07-07 11:18:12 +05:30
end
params do
quick_action_target . allows_multiple_assignees? ? '@user1 @user2' : ''
end
types Issue , MergeRequest
condition do
quick_action_target . persisted? &&
quick_action_target . assignees . any? &&
current_user . can? ( :" admin_ #{ quick_action_target . to_ability_name } " , project )
end
parse_params do | unassign_param |
# When multiple users are assigned, all will be unassigned if multiple assignees are no longer allowed
extract_users ( unassign_param ) if quick_action_target . allows_multiple_assignees?
end
command :unassign do | users = nil |
if quick_action_target . allows_multiple_assignees? && users & . any?
@updates [ :assignee_ids ] || = quick_action_target . assignees . map ( & :id )
@updates [ :assignee_ids ] -= users . map ( & :id )
else
@updates [ :assignee_ids ] = [ ]
end
end
2019-07-31 22:56:46 +05:30
desc _ ( 'Set milestone' )
2019-07-07 11:18:12 +05:30
explanation do | milestone |
2019-07-31 22:56:46 +05:30
_ ( " Sets the milestone to %{milestone_reference}. " ) % { milestone_reference : milestone . to_reference } if milestone
2019-07-07 11:18:12 +05:30
end
2019-10-12 21:52:04 +05:30
execution_message do | milestone |
_ ( " Set the milestone to %{milestone_reference}. " ) % { milestone_reference : milestone . to_reference } if milestone
end
2019-07-07 11:18:12 +05:30
params '%"milestone"'
types Issue , MergeRequest
condition do
current_user . can? ( :" admin_ #{ quick_action_target . to_ability_name } " , project ) &&
find_milestones ( project , state : 'active' ) . any?
end
parse_params do | milestone_param |
extract_references ( milestone_param , :milestone ) . first ||
find_milestones ( project , title : milestone_param . strip ) . first
end
command :milestone do | milestone |
@updates [ :milestone_id ] = milestone . id if milestone
end
2019-07-31 22:56:46 +05:30
desc _ ( 'Remove milestone' )
2019-07-07 11:18:12 +05:30
explanation do
2019-07-31 22:56:46 +05:30
_ ( " Removes %{milestone_reference} milestone. " ) % { milestone_reference : quick_action_target . milestone . to_reference ( format : :name ) }
2019-07-07 11:18:12 +05:30
end
2019-10-12 21:52:04 +05:30
execution_message do
_ ( " Removed %{milestone_reference} milestone. " ) % { milestone_reference : quick_action_target . milestone . to_reference ( format : :name ) }
end
2019-07-07 11:18:12 +05:30
types Issue , MergeRequest
condition do
quick_action_target . persisted? &&
quick_action_target . milestone_id? &&
current_user . can? ( :" admin_ #{ quick_action_target . to_ability_name } " , project )
end
command :remove_milestone do
@updates [ :milestone_id ] = nil
end
2019-09-04 21:01:54 +05:30
desc _ ( 'Copy labels and milestone from other issue or merge request in this project' )
2019-07-07 11:18:12 +05:30
explanation do | source_issuable |
2019-07-31 22:56:46 +05:30
_ ( " Copy labels and milestone from %{source_issuable_reference}. " ) % { source_issuable_reference : source_issuable . to_reference }
2019-07-07 11:18:12 +05:30
end
params '#issue | !merge_request'
types Issue , MergeRequest
condition do
current_user . can? ( :" update_ #{ quick_action_target . to_ability_name } " , quick_action_target )
end
parse_params do | issuable_param |
extract_references ( issuable_param , :issue ) . first ||
extract_references ( issuable_param , :merge_request ) . first
end
command :copy_metadata do | source_issuable |
2019-10-12 21:52:04 +05:30
if can_copy_metadata? ( source_issuable )
2019-07-07 11:18:12 +05:30
@updates [ :add_label_ids ] = source_issuable . labels . map ( & :id )
@updates [ :milestone_id ] = source_issuable . milestone . id if source_issuable . milestone
2019-10-12 21:52:04 +05:30
@execution_message [ :copy_metadata ] = _ ( " Copied labels and milestone from %{source_issuable_reference}. " ) % { source_issuable_reference : source_issuable . to_reference }
2019-07-07 11:18:12 +05:30
end
end
2019-07-31 22:56:46 +05:30
desc _ ( 'Set time estimate' )
2019-07-07 11:18:12 +05:30
explanation do | time_estimate |
2019-10-12 21:52:04 +05:30
formatted_time_estimate = format_time_estimate ( time_estimate )
_ ( " Sets time estimate to %{time_estimate}. " ) % { time_estimate : formatted_time_estimate } if formatted_time_estimate
end
execution_message do | time_estimate |
formatted_time_estimate = format_time_estimate ( time_estimate )
_ ( " Set time estimate to %{time_estimate}. " ) % { time_estimate : formatted_time_estimate } if formatted_time_estimate
2019-07-07 11:18:12 +05:30
end
params '<1w 3d 2h 14m>'
types Issue , MergeRequest
condition do
current_user . can? ( :" admin_ #{ quick_action_target . to_ability_name } " , project )
end
parse_params do | raw_duration |
Gitlab :: TimeTrackingFormatter . parse ( raw_duration )
end
command :estimate do | time_estimate |
if time_estimate
@updates [ :time_estimate ] = time_estimate
end
end
2019-07-31 22:56:46 +05:30
desc _ ( 'Add or subtract spent time' )
2019-07-07 11:18:12 +05:30
explanation do | time_spent , time_spent_date |
2019-10-12 21:52:04 +05:30
spend_time_message ( time_spent , time_spent_date , false )
2019-07-07 11:18:12 +05:30
end
2019-10-12 21:52:04 +05:30
execution_message do | time_spent , time_spent_date |
spend_time_message ( time_spent , time_spent_date , true )
end
2019-07-07 11:18:12 +05:30
params '<time(1h30m | -1h30m)> <date(YYYY-MM-DD)>'
types Issue , MergeRequest
condition do
current_user . can? ( :" admin_ #{ quick_action_target . to_ability_name } " , quick_action_target )
end
parse_params do | raw_time_date |
Gitlab :: QuickActions :: SpendTimeAndDateSeparator . new ( raw_time_date ) . execute
end
command :spend do | time_spent , time_spent_date |
if time_spent
@updates [ :spend_time ] = {
duration : time_spent ,
user_id : current_user . id ,
spent_at : time_spent_date
}
end
end
2019-07-31 22:56:46 +05:30
desc _ ( 'Remove time estimate' )
explanation _ ( 'Removes time estimate.' )
2019-10-12 21:52:04 +05:30
execution_message _ ( 'Removed time estimate.' )
2019-07-07 11:18:12 +05:30
types Issue , MergeRequest
condition do
quick_action_target . persisted? &&
current_user . can? ( :" admin_ #{ quick_action_target . to_ability_name } " , project )
end
command :remove_estimate do
@updates [ :time_estimate ] = 0
end
2019-07-31 22:56:46 +05:30
desc _ ( 'Remove spent time' )
explanation _ ( 'Removes spent time.' )
2019-10-12 21:52:04 +05:30
execution_message _ ( 'Removed spent time.' )
2019-07-07 11:18:12 +05:30
condition do
quick_action_target . persisted? &&
current_user . can? ( :" admin_ #{ quick_action_target . to_ability_name } " , project )
end
types Issue , MergeRequest
command :remove_time_spent do
@updates [ :spend_time ] = { duration : :reset , user_id : current_user . id }
end
2019-07-31 22:56:46 +05:30
desc _ ( " Lock the discussion " )
2019-10-12 21:52:04 +05:30
explanation _ ( " Locks the discussion. " )
execution_message _ ( " Locked the discussion. " )
2019-07-07 11:18:12 +05:30
types Issue , MergeRequest
condition do
quick_action_target . persisted? &&
! quick_action_target . discussion_locked? &&
current_user . can? ( :" admin_ #{ quick_action_target . to_ability_name } " , quick_action_target )
end
command :lock do
@updates [ :discussion_locked ] = true
end
2019-07-31 22:56:46 +05:30
desc _ ( " Unlock the discussion " )
2019-10-12 21:52:04 +05:30
explanation _ ( " Unlocks the discussion. " )
execution_message _ ( " Unlocked the discussion. " )
2019-07-07 11:18:12 +05:30
types Issue , MergeRequest
condition do
quick_action_target . persisted? &&
quick_action_target . discussion_locked? &&
current_user . can? ( :" admin_ #{ quick_action_target . to_ability_name } " , quick_action_target )
end
command :unlock do
@updates [ :discussion_locked ] = false
end
2019-10-12 21:52:04 +05:30
private
def assignee_users_sentence ( users )
if quick_action_target . allows_multiple_assignees?
users
else
[ users . first ]
end . map ( & :to_reference ) . to_sentence
end
def assignees_for_removal ( users )
assignees = quick_action_target . assignees
if users . present? && quick_action_target . allows_multiple_assignees?
assignees & users
else
assignees
end
end
def can_copy_metadata? ( source_issuable )
source_issuable . present? && source_issuable . project_id == quick_action_target . project_id
end
def format_time_estimate ( time_estimate )
Gitlab :: TimeTrackingFormatter . output ( time_estimate )
end
def spend_time_message ( time_spent , time_spent_date , paste_tense )
return unless time_spent
if time_spent > 0
verb = paste_tense ? _ ( 'Added' ) : _ ( 'Adds' )
value = time_spent
else
verb = paste_tense ? _ ( 'Subtracted' ) : _ ( 'Subtracts' )
value = - time_spent
end
_ ( " %{verb} %{time_spent_value} spent time. " ) % { verb : verb , time_spent_value : format_time_estimate ( value ) }
end
2019-07-07 11:18:12 +05:30
end
end
end
end