Commit b36319a1 authored by Rubén Dávila's avatar Rubén Dávila Committed by Robert Speicher

Make MRs with revert commit work.

parent 80613993
......@@ -16,7 +16,7 @@ module CreatesCommit
flash[:notice] = success_notice || "Your changes have been successfully committed."
if create_merge_request?
success_path = new_merge_request_path
success_path = merge_request_exists? ? existent_merge_request_path : new_merge_request_path
target = different_project? ? "project" : "branch"
flash[:notice] << " You can now submit a merge request to get this change into the original #{target}."
end
......@@ -62,6 +62,17 @@ module CreatesCommit
)
end
def existent_merge_request_path
namespace_project_merge_request_path(@mr_target_project.namespace, @mr_target_project, @merge_request)
end
def merge_request_exists?
@merge_request = @mr_target_project.merge_requests.opened.where(
source_branch: @mr_source_branch,
target_branch: @mr_target_branch
).first
end
def different_project?
@mr_source_project != @mr_target_project
end
......@@ -75,7 +86,7 @@ module CreatesCommit
end
def set_commit_variables
@mr_source_branch = @target_branch
@mr_source_branch ||= @target_branch
if can?(current_user, :push_code, @project)
# Edit file in this project
......@@ -89,7 +100,7 @@ module CreatesCommit
else
# Merge request to this project
@mr_target_project = @project
@mr_target_branch = @ref
@mr_target_branch ||= @ref
end
else
# Edit file in fork
......
......@@ -93,10 +93,13 @@ class Projects::CommitController < Projects::ApplicationController
end
def assign_revert_commit_vars
@commit = project.commit(params[:id])
@target_branch = params[:target_branch]
@mr_source_branch = @commit.revert_branch_name
@mr_target_branch = @target_branch
@commit_params = {
revert_commit_id: params[:id],
commit: @commit,
create_merge_request: params[:create_merge_request].present? || different_project?
}
end
end
......@@ -622,22 +622,24 @@ class Repository
merge_commit_sha
end
def revert(user, commit_id, target_branch, base_branch, commit_message)
def revert(user, commit_id, target_branch, base_branch, commit_message, create_mr = false)
source_sha = find_branch(base_branch).target
target_sha = find_branch(target_branch).try(:target)
# First make revert in temp branch
unless target_sha
revert_commit(user, commit_id, target_branch, base_branch, commit_message)
end
status = target_sha ? true : revert_commit(user, commit_id, target_branch, base_branch, commit_message)
# Make the revert happen in the target branch
source_sha = find_branch(target_branch).target
target_sha = find_branch(base_branch).target
has_changes = is_there_something_to_merge?(source_sha, target_sha)
if is_there_something_to_merge?(source_sha, target_sha)
revert_commit(user, commit_id, base_branch, base_branch, commit_message)
if has_changes && !create_mr
status = revert_commit(user, commit_id, base_branch, base_branch, commit_message)
end
::File.open('/Users/ruben/Desktop/log.txt', 'w') { |f| f.puts "HAS CHANGES: #{has_changes && status}" }
has_changes && status
end
def revert_commit(user, commit_id, target_branch, base_branch, commit_message)
......
......@@ -5,7 +5,8 @@ module Commits
def execute
@source_project = params[:source_project] || @project
@target_branch = params[:target_branch]
@commit_to_revert = @source_project.commit(params[:revert_commit_id])
@commit = params[:commit]
@create_merge_request = params[:create_merge_request]
# Check push permissions to branch
validate
......@@ -23,10 +24,13 @@ module Commits
raw_repo = repository.rugged
# Create branch with revert commit
reverted = repository.revert(current_user, @commit_to_revert.id,
@commit_to_revert.revert_branch_name, @target_branch,
@commit_to_revert.revert_message)
repository.rm_branch(current_user, @commit_to_revert.revert_branch_name)
reverted = repository.revert(current_user, @commit.id,
@commit.revert_branch_name, @target_branch,
@commit.revert_message, @create_merge_request)
unless @create_merge_request
repository.rm_branch(current_user, @commit.revert_branch_name)
end
reverted
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment