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

121 lines
3.5 KiB
Ruby
Raw Normal View History

module CreatesCommit
extend ActiveSupport::Concern
2017-08-17 22:00:37 +05:30
def set_start_branch_to_branch_name
branch_exists = @repository.find_branch(@branch_name)
@start_branch = @branch_name if branch_exists
end
def create_commit(service, success_path:, failure_path:, failure_view: nil, success_notice: nil)
2017-08-17 22:00:37 +05:30
if can?(current_user, :push_code, @project)
@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
commit_params = @commit_params.merge(
2017-08-17 22:00:37 +05:30
start_project: @project,
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)
2017-08-17 22:00:37 +05:30
success_path = final_success_path(success_path)
respond_to do |format|
2017-08-17 22:00:37 +05:30
format.html { redirect_to success_path }
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
format.json { render json: { message: "failed", filePath: failure_path } }
end
end
end
def authorize_edit_tree!
2016-04-02 18:10:28 +05:30
return if can_collaborate_with_project?
access_denied!
end
private
2016-04-02 18:10:28 +05:30
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"
flash[:notice] << " You can now submit a merge request to get this change into the original #{target}."
end
end
end
def final_success_path(success_path)
2017-08-17 22:00:37 +05:30
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-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
def new_merge_request_path
new_namespace_project_merge_request_path(
2017-08-17 22:00:37 +05:30
@project_to_commit_into.namespace,
@project_to_commit_into,
merge_request: {
2017-08-17 22:00:37 +05:30
source_project_id: @project_to_commit_into.id,
target_project_id: @project.id,
source_branch: @branch_name,
target_branch: @start_branch
}
)
end
2016-04-02 18:10:28 +05:30
def existing_merge_request_path
2017-08-17 22:00:37 +05:30
namespace_project_merge_request_path(@project.namespace, @project, @merge_request)
2016-04-02 18:10:28 +05:30
end
def merge_request_exists?
return @merge_request if defined?(@merge_request)
2017-08-17 22:00:37 +05:30
@merge_request = 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)
2016-04-02 18:10:28 +05:30
end
def different_project?
2017-08-17 22:00:37 +05:30
@project_to_commit_into != @project
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? &&
(different_project? || @start_branch != @branch_name)
end
end