2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-18 04:03:34 -05:00
|
|
|
module CreatesCommit
|
|
|
|
extend ActiveSupport::Concern
|
2017-11-17 07:27:16 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2015-12-18 04:03:34 -05:00
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2015-12-18 04:03:34 -05:00
|
|
|
def create_commit(service, success_path:, failure_path:, failure_view: nil, success_notice: nil)
|
2018-02-28 03:06:18 -05:00
|
|
|
if user_access(@project).can_push_to_branch?(branch_name_or_ref)
|
2017-04-19 20:37:44 -04:00
|
|
|
@project_to_commit_into = @project
|
|
|
|
@branch_name ||= @ref
|
|
|
|
else
|
|
|
|
@project_to_commit_into = current_user.fork_of(@project)
|
|
|
|
@branch_name ||= @project_to_commit_into.repository.next_branch('patch')
|
|
|
|
end
|
|
|
|
|
|
|
|
@start_branch ||= @ref || @branch_name
|
2015-12-18 04:03:34 -05:00
|
|
|
|
|
|
|
commit_params = @commit_params.merge(
|
2017-04-19 20:37:44 -04:00
|
|
|
start_project: @project,
|
|
|
|
start_branch: @start_branch,
|
|
|
|
branch_name: @branch_name
|
2015-12-18 04:03:34 -05:00
|
|
|
)
|
|
|
|
|
2017-04-19 20:37:44 -04:00
|
|
|
result = service.new(@project_to_commit_into, current_user, commit_params).execute
|
2015-12-18 04:03:34 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
2016-02-09 14:50:25 -05:00
|
|
|
update_flash_notice(success_notice)
|
2015-12-18 04:03:34 -05:00
|
|
|
|
2017-02-16 19:24:56 -05:00
|
|
|
success_path = final_success_path(success_path)
|
|
|
|
|
2015-12-18 04:03:34 -05:00
|
|
|
respond_to do |format|
|
2017-02-16 19:24:56 -05:00
|
|
|
format.html { redirect_to success_path }
|
|
|
|
format.json { render json: { message: "success", filePath: success_path } }
|
2015-12-18 04:03:34 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
flash[:alert] = result[:message]
|
2017-02-16 19:24:56 -05:00
|
|
|
failure_path = failure_path.call if failure_path.respond_to?(:call)
|
|
|
|
|
2015-12-18 04:03:34 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
if failure_view
|
|
|
|
render failure_view
|
|
|
|
else
|
|
|
|
redirect_to failure_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
format.json { render json: { message: "failed", filePath: failure_path } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2015-12-18 04:03:34 -05:00
|
|
|
|
|
|
|
def authorize_edit_tree!
|
2018-02-28 03:06:18 -05:00
|
|
|
return if can_collaborate_with_project?(project, ref: branch_name_or_ref)
|
2015-12-18 04:03:34 -05:00
|
|
|
|
|
|
|
access_denied!
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-02-09 14:50:25 -05:00
|
|
|
def update_flash_notice(success_notice)
|
|
|
|
flash[:notice] = success_notice || "Your changes have been successfully committed."
|
|
|
|
|
|
|
|
if create_merge_request?
|
|
|
|
if merge_request_exists?
|
|
|
|
flash[:notice] = nil
|
|
|
|
else
|
|
|
|
target = different_project? ? "project" : "branch"
|
2018-09-14 01:42:05 -04:00
|
|
|
flash[:notice] = flash[:notice] + " You can now submit a merge request to get this change into the original #{target}."
|
2016-02-09 14:50:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def final_success_path(success_path)
|
2017-02-16 19:24:56 -05:00
|
|
|
if create_merge_request?
|
|
|
|
merge_request_exists? ? existing_merge_request_path : new_merge_request_path
|
|
|
|
else
|
|
|
|
success_path = success_path.call if success_path.respond_to?(:call)
|
2016-02-09 14:50:25 -05:00
|
|
|
|
2017-02-16 19:24:56 -05:00
|
|
|
success_path
|
|
|
|
end
|
2016-02-09 14:50:25 -05:00
|
|
|
end
|
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2015-12-18 04:03:34 -05:00
|
|
|
def new_merge_request_path
|
2017-06-29 13:06:35 -04:00
|
|
|
project_new_merge_request_path(
|
2017-04-19 20:37:44 -04:00
|
|
|
@project_to_commit_into,
|
2015-12-18 04:03:34 -05:00
|
|
|
merge_request: {
|
2017-04-19 20:37:44 -04:00
|
|
|
source_project_id: @project_to_commit_into.id,
|
|
|
|
target_project_id: @project.id,
|
2018-11-07 08:32:20 -05:00
|
|
|
source_branch: @branch_name,
|
2017-04-19 20:37:44 -04:00
|
|
|
target_branch: @start_branch
|
2015-12-18 04:03:34 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2015-12-18 04:03:34 -05:00
|
|
|
|
2016-02-09 14:50:25 -05:00
|
|
|
def existing_merge_request_path
|
2017-11-22 02:50:36 -05:00
|
|
|
project_merge_request_path(@project, @merge_request) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2016-02-05 18:12:41 -05:00
|
|
|
end
|
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-02-05 18:12:41 -05:00
|
|
|
def merge_request_exists?
|
2017-11-17 07:27:16 -05:00
|
|
|
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-02-05 18:12:41 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2016-02-05 18:12:41 -05:00
|
|
|
|
2015-12-18 04:03:34 -05:00
|
|
|
def different_project?
|
2017-11-22 02:50:36 -05:00
|
|
|
@project_to_commit_into != @project # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2015-12-18 04:03:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_merge_request?
|
2017-02-16 19:24:56 -05:00
|
|
|
# Even if the field is set, if we're checking the same branch
|
2017-01-26 09:37:22 -05:00
|
|
|
# as the target branch in the same project,
|
|
|
|
# we don't want to create a merge request.
|
|
|
|
params[:create_merge_request].present? &&
|
2017-11-22 02:50:36 -05:00
|
|
|
(different_project? || @start_branch != @branch_name) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-01-06 09:20:02 -05:00
|
|
|
end
|
2018-02-28 03:06:18 -05:00
|
|
|
|
|
|
|
def branch_name_or_ref
|
|
|
|
@branch_name || @ref # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
end
|
2015-12-18 04:03:34 -05:00
|
|
|
end
|