debian-mirror-gitlab/lib/gitlab/quick_actions/merge_request_actions.rb

273 lines
10 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
module Gitlab
module QuickActions
module MergeRequestActions
extend ActiveSupport::Concern
include Gitlab::QuickActions::Dsl
included do
# MergeRequest only quick actions definitions
2020-04-22 19:07:51 +05:30
desc do
2021-03-08 18:12:59 +05:30
if preferred_strategy = preferred_auto_merge_strategy(quick_action_target)
_("Merge automatically (%{strategy})") % { strategy: preferred_strategy.humanize }
2020-04-22 19:07:51 +05:30
else
2021-03-08 18:12:59 +05:30
_("Merge immediately")
2020-04-22 19:07:51 +05:30
end
end
explanation do
2021-03-08 18:12:59 +05:30
if preferred_strategy = preferred_auto_merge_strategy(quick_action_target)
_("Schedules to merge this merge request (%{strategy}).") % { strategy: preferred_strategy.humanize }
2020-04-22 19:07:51 +05:30
else
2021-03-08 18:12:59 +05:30
_('Merges this merge request immediately.')
2020-04-22 19:07:51 +05:30
end
end
execution_message do
2021-03-08 18:12:59 +05:30
if preferred_strategy = preferred_auto_merge_strategy(quick_action_target)
_("Scheduled to merge this merge request (%{strategy}).") % { strategy: preferred_strategy.humanize }
2020-04-22 19:07:51 +05:30
else
2021-03-08 18:12:59 +05:30
_('Merged this merge request.')
2020-04-22 19:07:51 +05:30
end
end
2019-07-07 11:18:12 +05:30
types MergeRequest
condition do
2021-03-08 18:12:59 +05:30
quick_action_target.persisted? &&
merge_orchestration_service.can_merge?(quick_action_target)
2019-07-07 11:18:12 +05:30
end
command :merge do
@updates[:merge] = params[:merge_request_diff_head_sha]
end
2021-03-08 18:12:59 +05:30
types MergeRequest
desc do
_('Rebase source branch')
end
explanation do
_('Rebase source branch on the target branch.')
end
condition do
merge_request = quick_action_target
next false unless merge_request.open?
next false unless merge_request.source_branch_exists?
access_check = ::Gitlab::UserAccess
.new(current_user, container: merge_request.source_project)
access_check.can_push_to_branch?(merge_request.source_branch)
end
command :rebase do
if quick_action_target.cannot_be_merged?
@execution_message[:rebase] = _('This merge request cannot be rebased while there are conflicts.')
next
end
if quick_action_target.rebase_in_progress?
@execution_message[:rebase] = _('A rebase is already in progress.')
next
end
# This will be used to avoid simultaneous "/merge" and "/rebase" actions
@updates[:rebase] = true
branch = quick_action_target.source_branch
@execution_message[:rebase] = _('Scheduled a rebase of branch %{branch}.') % { branch: branch }
end
2021-01-29 00:20:46 +05:30
desc 'Toggle the Draft status'
2019-07-07 11:18:12 +05:30
explanation do
noun = quick_action_target.to_ability_name.humanize(capitalize: false)
2019-10-12 21:52:04 +05:30
if quick_action_target.work_in_progress?
2021-01-29 00:20:46 +05:30
_("Unmarks this %{noun} as a draft.")
2019-10-12 21:52:04 +05:30
else
2021-01-29 00:20:46 +05:30
_("Marks this %{noun} as a draft.")
2019-10-12 21:52:04 +05:30
end % { noun: noun }
2019-07-07 11:18:12 +05:30
end
2019-10-12 21:52:04 +05:30
execution_message do
noun = quick_action_target.to_ability_name.humanize(capitalize: false)
if quick_action_target.work_in_progress?
2021-01-29 00:20:46 +05:30
_("Unmarked this %{noun} as a draft.")
2019-10-12 21:52:04 +05:30
else
2021-01-29 00:20:46 +05:30
_("Marked this %{noun} as a draft.")
2019-10-12 21:52:04 +05:30
end % { noun: noun }
end
2019-07-07 11:18:12 +05:30
types MergeRequest
condition do
quick_action_target.respond_to?(:work_in_progress?) &&
# Allow it to mark as WIP on MR creation page _or_ through MR notes.
(quick_action_target.new_record? || current_user.can?(:"update_#{quick_action_target.to_ability_name}", quick_action_target))
end
2021-01-29 00:20:46 +05:30
command :draft, :wip do
2019-07-07 11:18:12 +05:30
@updates[:wip_event] = quick_action_target.work_in_progress? ? 'unwip' : 'wip'
end
2019-10-12 21:52:04 +05:30
desc _('Set target branch')
2019-07-07 11:18:12 +05:30
explanation do |branch_name|
2019-10-12 21:52:04 +05:30
_('Sets target branch to %{branch_name}.') % { branch_name: branch_name }
end
execution_message do |branch_name|
_('Set target branch to %{branch_name}.') % { branch_name: branch_name }
2019-07-07 11:18:12 +05:30
end
params '<Local branch name>'
types MergeRequest
condition do
quick_action_target.respond_to?(:target_branch) &&
(current_user.can?(:"update_#{quick_action_target.to_ability_name}", quick_action_target) ||
quick_action_target.new_record?)
end
parse_params do |target_branch_param|
target_branch_param.strip
end
command :target_branch do |branch_name|
@updates[:target_branch] = branch_name if project.repository.branch_exists?(branch_name)
end
2020-06-23 00:09:42 +05:30
desc _('Submit a review')
explanation _('Submit the current review.')
types MergeRequest
condition do
quick_action_target.persisted?
end
command :submit_review do
next if params[:review_id]
result = DraftNotes::PublishService.new(quick_action_target, current_user).execute
@execution_message[:submit_review] = if result[:status] == :success
_('Submitted the current review.')
else
result[:message]
end
end
2021-01-03 14:25:43 +05:30
desc _('Approve a merge request')
explanation _('Approve the current merge request.')
types MergeRequest
condition do
quick_action_target.persisted? && quick_action_target.can_be_approved_by?(current_user)
end
command :approve do
2021-06-08 01:23:25 +05:30
success = MergeRequests::ApprovalService.new(project: quick_action_target.project, current_user: current_user).execute(quick_action_target)
2021-01-03 14:25:43 +05:30
next unless success
@execution_message[:approve] = _('Approved the current merge request.')
end
2021-03-08 18:12:59 +05:30
desc do
if quick_action_target.allows_multiple_reviewers?
_('Assign reviewer(s)')
else
_('Assign reviewer')
end
end
explanation do |users|
reviewers = reviewers_to_add(users)
_('Assigns %{reviewer_users_sentence} as %{reviewer_text}.') % { reviewer_users_sentence: reviewer_users_sentence(users),
reviewer_text: 'reviewer'.pluralize(reviewers.size) }
end
execution_message do |users = nil|
reviewers = reviewers_to_add(users)
if reviewers.blank?
_("Failed to assign a reviewer because no user was found.")
else
_('Assigned %{reviewer_users_sentence} as %{reviewer_text}.') % { reviewer_users_sentence: reviewer_users_sentence(users),
reviewer_text: 'reviewer'.pluralize(reviewers.size) }
end
end
params do
quick_action_target.allows_multiple_reviewers? ? '@user1 @user2' : '@user'
end
types MergeRequest
condition do
2021-03-11 19:13:27 +05:30
current_user.can?(:"admin_#{quick_action_target.to_ability_name}", project)
2021-03-08 18:12:59 +05:30
end
parse_params do |reviewer_param|
extract_users(reviewer_param)
end
2021-03-11 19:13:27 +05:30
command :assign_reviewer, :reviewer, :request_review do |users|
2021-03-08 18:12:59 +05:30
next if users.empty?
if quick_action_target.allows_multiple_reviewers?
@updates[:reviewer_ids] ||= quick_action_target.reviewers.map(&:id)
@updates[:reviewer_ids] |= users.map(&:id)
else
@updates[:reviewer_ids] = [users.first.id]
end
end
desc do
if quick_action_target.allows_multiple_reviewers?
_('Remove all or specific reviewer(s)')
else
_('Remove reviewer')
end
end
explanation do |users = nil|
reviewers = reviewers_for_removal(users)
_("Removes %{reviewer_text} %{reviewer_references}.") %
{ reviewer_text: 'reviewer'.pluralize(reviewers.size), reviewer_references: reviewers.map(&:to_reference).to_sentence }
end
execution_message do |users = nil|
reviewers = reviewers_for_removal(users)
_("Removed %{reviewer_text} %{reviewer_references}.") %
{ reviewer_text: 'reviewer'.pluralize(reviewers.size), reviewer_references: reviewers.map(&:to_reference).to_sentence }
end
params do
quick_action_target.allows_multiple_reviewers? ? '@user1 @user2' : ''
end
types MergeRequest
condition do
quick_action_target.persisted? &&
quick_action_target.reviewers.any? &&
current_user.can?(:"admin_#{quick_action_target.to_ability_name}", project)
end
parse_params do |unassign_reviewer_param|
# When multiple users are assigned, all will be unassigned if multiple reviewers are no longer allowed
extract_users(unassign_reviewer_param) if quick_action_target.allows_multiple_reviewers?
end
command :unassign_reviewer, :remove_reviewer do |users = nil|
if quick_action_target.allows_multiple_reviewers? && users&.any?
@updates[:reviewer_ids] ||= quick_action_target.reviewers.map(&:id)
@updates[:reviewer_ids] -= users.map(&:id)
else
@updates[:reviewer_ids] = []
end
end
end
def reviewer_users_sentence(users)
reviewers_to_add(users).map(&:to_reference).to_sentence
end
def reviewers_for_removal(users)
reviewers = quick_action_target.reviewers
if users.present? && quick_action_target.allows_multiple_reviewers?
users
else
reviewers
end
end
def reviewers_to_add(users)
return if users.blank?
if quick_action_target.allows_multiple_reviewers?
users
else
[users.first]
end
2019-07-07 11:18:12 +05:30
end
2020-04-22 19:07:51 +05:30
def merge_orchestration_service
@merge_orchestration_service ||= MergeRequests::MergeOrchestrationService.new(project, current_user)
end
def preferred_auto_merge_strategy(merge_request)
merge_orchestration_service.preferred_auto_merge_strategy(merge_request)
end
2019-07-07 11:18:12 +05:30
end
end
end