debian-mirror-gitlab/app/controllers/concerns/creates_commit.rb

145 lines
4.4 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
module CreatesCommit
extend ActiveSupport::Concern
2018-03-17 18:26:18 +05:30
include Gitlab::Utils::StrongMemoize
2018-03-17 18:26:18 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2021-04-29 21:17:54 +05:30
def create_commit(service, success_path:, failure_path:, failure_view: nil, success_notice: nil, target_project: nil)
target_project ||= @project
if user_access(target_project).can_push_to_branch?(branch_name_or_ref)
@project_to_commit_into = target_project
2017-08-17 22:00:37 +05:30
@branch_name ||= @ref
else
2021-04-29 21:17:54 +05:30
@project_to_commit_into = current_user.fork_of(target_project)
2017-08-17 22:00:37 +05:30
@branch_name ||= @project_to_commit_into.repository.next_branch('patch')
end
@start_branch ||= @ref || @branch_name
2021-09-04 01:27:46 +05:30
start_project = @project_to_commit_into
2021-04-29 21:17:54 +05:30
commit_params = @commit_params.merge(
2021-04-29 21:17:54 +05:30
start_project: start_project,
2017-08-17 22:00:37 +05:30
start_branch: @start_branch,
branch_name: @branch_name
)
2017-08-17 22:00:37 +05:30
result = service.new(@project_to_commit_into, current_user, commit_params).execute
if result[:status] == :success
2016-04-02 18:10:28 +05:30
update_flash_notice(success_notice)
2021-04-29 21:17:54 +05:30
success_path = final_success_path(success_path, target_project)
2017-08-17 22:00:37 +05:30
respond_to do |format|
2017-08-17 22:00:37 +05:30
format.html { redirect_to success_path }
2019-07-07 11:18:12 +05:30
format.json { render json: { message: _("success"), filePath: success_path } }
end
else
flash[:alert] = result[:message]
2017-08-17 22:00:37 +05:30
failure_path = failure_path.call if failure_path.respond_to?(:call)
respond_to do |format|
format.html do
if failure_view
render failure_view
else
redirect_to failure_path
end
end
2019-07-07 11:18:12 +05:30
format.json { render json: { message: _("failed"), filePath: failure_path } }
end
end
end
2018-03-17 18:26:18 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
def authorize_edit_tree!
2018-03-27 19:54:05 +05:30
return if can_collaborate_with_project?(project, ref: branch_name_or_ref)
access_denied!
end
private
2016-04-02 18:10:28 +05:30
def update_flash_notice(success_notice)
2019-07-07 11:18:12 +05:30
flash[:notice] = success_notice || _("Your changes have been successfully committed.")
2016-04-02 18:10:28 +05:30
if create_merge_request?
2019-07-07 11:18:12 +05:30
flash[:notice] =
if merge_request_exists?
nil
else
mr_message =
if different_project?
_("You can now submit a merge request to get this change into the original project.")
else
_("You can now submit a merge request to get this change into the original branch.")
end
2023-03-04 22:38:38 +05:30
flash[:notice] += " #{mr_message}"
2019-07-07 11:18:12 +05:30
end
2016-04-02 18:10:28 +05:30
end
end
2021-04-29 21:17:54 +05:30
def final_success_path(success_path, target_project)
2017-08-17 22:00:37 +05:30
if create_merge_request?
2021-04-29 21:17:54 +05:30
merge_request_exists? ? existing_merge_request_path : new_merge_request_path(target_project)
2017-08-17 22:00:37 +05:30
else
success_path = success_path.call if success_path.respond_to?(:call)
2016-04-02 18:10:28 +05:30
2017-08-17 22:00:37 +05:30
success_path
end
2016-04-02 18:10:28 +05:30
end
2018-03-17 18:26:18 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2021-04-29 21:17:54 +05:30
def new_merge_request_path(target_project)
2017-09-10 17:25:29 +05:30
project_new_merge_request_path(
2017-08-17 22:00:37 +05:30
@project_to_commit_into,
merge_request: {
2022-08-27 11:52:29 +05:30
target_project_id: @project_to_commit_into.default_merge_request_target.id,
2017-08-17 22:00:37 +05:30
source_branch: @branch_name,
target_branch: @start_branch
}
)
end
2018-03-17 18:26:18 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2016-04-02 18:10:28 +05:30
def existing_merge_request_path
2018-03-17 18:26:18 +05:30
project_merge_request_path(@project, @merge_request) # rubocop:disable Gitlab/ModuleWithInstanceVariables
2016-04-02 18:10:28 +05:30
end
2018-03-17 18:26:18 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-04-02 18:10:28 +05:30
def merge_request_exists?
2018-03-17 18:26:18 +05:30
strong_memoize(:merge_request) do
MergeRequestsFinder.new(current_user, project_id: @project.id)
.execute
.opened
.find_by(
source_project_id: @project_to_commit_into,
source_branch: @branch_name,
target_branch: @start_branch)
end
2016-04-02 18:10:28 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2016-04-02 18:10:28 +05:30
def different_project?
2018-03-17 18:26:18 +05:30
@project_to_commit_into != @project # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
def create_merge_request?
2017-08-17 22:00:37 +05:30
# Even if the field is set, if we're checking the same branch
# as the target branch in the same project,
# we don't want to create a merge request.
params[:create_merge_request].present? &&
2018-03-17 18:26:18 +05:30
(different_project? || @start_branch != @branch_name) # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
2018-03-27 19:54:05 +05:30
def branch_name_or_ref
@branch_name || @ref # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
end