summaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/concerns/creates_commit.rb57
-rw-r--r--app/controllers/projects/blob_controller.rb6
-rw-r--r--app/controllers/projects/commit_controller.rb32
3 files changed, 47 insertions, 48 deletions
diff --git a/app/controllers/concerns/creates_commit.rb b/app/controllers/concerns/creates_commit.rb
index f897f828cec..9ac8197e45a 100644
--- a/app/controllers/concerns/creates_commit.rb
+++ b/app/controllers/concerns/creates_commit.rb
@@ -4,10 +4,9 @@ module CreatesCommit
def create_commit(service, success_path:, failure_path:, failure_view: nil, success_notice: nil)
set_commit_variables
- start_branch = @mr_target_branch
commit_params = @commit_params.merge(
start_project: @mr_target_project,
- start_branch: start_branch,
+ start_branch: @mr_target_branch,
target_branch: @mr_source_branch
)
@@ -17,12 +16,16 @@ module CreatesCommit
if result[:status] == :success
update_flash_notice(success_notice)
+ success_path = final_success_path(success_path)
+
respond_to do |format|
- format.html { redirect_to final_success_path(success_path) }
- format.json { render json: { message: "success", filePath: final_success_path(success_path) } }
+ format.html { redirect_to success_path }
+ format.json { render json: { message: "success", filePath: success_path } }
end
else
flash[:alert] = result[:message]
+ failure_path = failure_path.call if failure_path.respond_to?(:call)
+
respond_to do |format|
format.html do
if failure_view
@@ -58,9 +61,13 @@ module CreatesCommit
end
def final_success_path(success_path)
- return success_path unless create_merge_request?
+ 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)
- merge_request_exists? ? existing_merge_request_path : new_merge_request_path
+ success_path
+ end
end
def new_merge_request_path
@@ -92,42 +99,26 @@ module CreatesCommit
end
def create_merge_request?
- # XXX: Even if the field is set, if we're checking the same branch
+ # 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? || @ref != @target_branch)
+ (different_project? || @mr_target_branch != @mr_source_branch)
end
- # TODO: We should really clean this up
def set_commit_variables
- @mr_source_project =
- if can?(current_user, :push_code, @project)
- # Edit file in this project
- @project
- else
- # Merge request from fork to this project
- current_user.fork_of(@project)
- end
+ if can?(current_user, :push_code, @project)
+ @mr_source_project = @project
+ @target_branch ||= @ref
+ else
+ @mr_source_project = current_user.fork_of(@project)
+ @target_branch ||= @mr_source_project.repository.next_branch('patch')
+ end
# Merge request to this project
@mr_target_project = @project
- @mr_target_branch = @ref || @target_branch
-
- @mr_source_branch = guess_mr_source_branch
- end
+ @mr_target_branch ||= @ref || @target_branch
- def guess_mr_source_branch
- # XXX: Happens when viewing a commit without a branch. In this case,
- # @target_branch would be the default branch for @mr_source_project,
- # however we want a generated new branch here. Thus we can't use
- # @target_branch, but should pass nil to indicate that we want a new
- # branch instead of @target_branch.
- return if
- create_merge_request? &&
- # XXX: Don't understand why rubocop prefers this indention
- @mr_source_project.repository.branch_exists?(@target_branch)
-
- @target_branch
+ @mr_source_branch = @target_branch
end
end
diff --git a/app/controllers/projects/blob_controller.rb b/app/controllers/projects/blob_controller.rb
index f9a5ef46786..21ed0660762 100644
--- a/app/controllers/projects/blob_controller.rb
+++ b/app/controllers/projects/blob_controller.rb
@@ -24,7 +24,7 @@ class Projects::BlobController < Projects::ApplicationController
def create
create_commit(Files::CreateService, success_notice: "The file has been successfully created.",
- success_path: namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @file_path)),
+ success_path: -> { namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @file_path)) },
failure_view: :new,
failure_path: namespace_project_new_blob_path(@project.namespace, @project, @ref))
end
@@ -40,7 +40,7 @@ class Projects::BlobController < Projects::ApplicationController
def update
@path = params[:file_path] if params[:file_path].present?
- create_commit(Files::UpdateService, success_path: after_edit_path,
+ create_commit(Files::UpdateService, success_path: -> { after_edit_path },
failure_view: :edit,
failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
@@ -62,7 +62,7 @@ class Projects::BlobController < Projects::ApplicationController
def destroy
create_commit(Files::DestroyService, success_notice: "The file has been successfully deleted.",
- success_path: namespace_project_tree_path(@project.namespace, @project, @target_branch),
+ success_path: -> { namespace_project_tree_path(@project.namespace, @project, @target_branch) },
failure_view: :show,
failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
end
diff --git a/app/controllers/projects/commit_controller.rb b/app/controllers/projects/commit_controller.rb
index e10d7992db7..cc67f688d51 100644
--- a/app/controllers/projects/commit_controller.rb
+++ b/app/controllers/projects/commit_controller.rb
@@ -51,23 +51,35 @@ class Projects::CommitController < Projects::ApplicationController
def revert
assign_change_commit_vars
- return render_404 if @target_branch.blank?
+ return render_404 if @start_branch.blank?
+
+ @target_branch = create_new_branch? ? @commit.revert_branch_name : @start_branch
+
+ @mr_target_branch = @start_branch
create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully reverted.",
- success_path: successful_change_path, failure_path: failed_change_path)
+ success_path: -> { successful_change_path }, failure_path: failed_change_path)
end
def cherry_pick
assign_change_commit_vars
- return render_404 if @target_branch.blank?
+ return render_404 if @start_branch.blank?
+
+ @target_branch = create_new_branch? ? @commit.cherry_pick_branch_name : @start_branch
+
+ @mr_target_branch = @start_branch
create_commit(Commits::CherryPickService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully cherry-picked.",
- success_path: successful_change_path, failure_path: failed_change_path)
+ success_path: -> { successful_change_path }, failure_path: failed_change_path)
end
private
+ def create_new_branch?
+ params[:create_merge_request].present? || !can?(current_user, :push_code, @project)
+ end
+
def successful_change_path
referenced_merge_request_url || namespace_project_commits_url(@project.namespace, @project, @target_branch)
end
@@ -78,7 +90,7 @@ class Projects::CommitController < Projects::ApplicationController
def referenced_merge_request_url
if merge_request = @commit.merged_merge_request(current_user)
- namespace_project_merge_request_url(@project.namespace, @project, merge_request)
+ namespace_project_merge_request_url(merge_request.target_project.namespace, merge_request.target_project, merge_request)
end
end
@@ -94,7 +106,7 @@ class Projects::CommitController < Projects::ApplicationController
@diffs = commit.diffs(opts)
@notes_count = commit.notes.count
-
+
@environment = EnvironmentsFinder.new(@project, current_user, commit: @commit).execute.last
end
@@ -118,11 +130,7 @@ class Projects::CommitController < Projects::ApplicationController
end
def assign_change_commit_vars
- @commit = project.commit(params[:id])
- @target_branch = params[:target_branch]
- @commit_params = {
- commit: @commit,
- create_merge_request: params[:create_merge_request].present? || different_project?
- }
+ @start_branch = params[:start_branch]
+ @commit_params = { commit: @commit }
end
end