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 ...@@ -16,7 +16,7 @@ module CreatesCommit
flash[:notice] = success_notice || "Your changes have been successfully committed." flash[:notice] = success_notice || "Your changes have been successfully committed."
if create_merge_request? 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" target = different_project? ? "project" : "branch"
flash[:notice] << " You can now submit a merge request to get this change into the original #{target}." flash[:notice] << " You can now submit a merge request to get this change into the original #{target}."
end end
...@@ -62,6 +62,17 @@ module CreatesCommit ...@@ -62,6 +62,17 @@ module CreatesCommit
) )
end 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? def different_project?
@mr_source_project != @mr_target_project @mr_source_project != @mr_target_project
end end
...@@ -75,7 +86,7 @@ module CreatesCommit ...@@ -75,7 +86,7 @@ module CreatesCommit
end end
def set_commit_variables def set_commit_variables
@mr_source_branch = @target_branch @mr_source_branch ||= @target_branch
if can?(current_user, :push_code, @project) if can?(current_user, :push_code, @project)
# Edit file in this project # Edit file in this project
...@@ -89,7 +100,7 @@ module CreatesCommit ...@@ -89,7 +100,7 @@ module CreatesCommit
else else
# Merge request to this project # Merge request to this project
@mr_target_project = @project @mr_target_project = @project
@mr_target_branch = @ref @mr_target_branch ||= @ref
end end
else else
# Edit file in fork # Edit file in fork
......
...@@ -93,10 +93,13 @@ class Projects::CommitController < Projects::ApplicationController ...@@ -93,10 +93,13 @@ class Projects::CommitController < Projects::ApplicationController
end end
def assign_revert_commit_vars def assign_revert_commit_vars
@commit = project.commit(params[:id])
@target_branch = params[:target_branch] @target_branch = params[:target_branch]
@mr_source_branch = @commit.revert_branch_name
@mr_target_branch = @target_branch
@commit_params = { @commit_params = {
revert_commit_id: params[:id], commit: @commit,
create_merge_request: params[:create_merge_request].present? || different_project?
} }
end end
end end
...@@ -622,22 +622,24 @@ class Repository ...@@ -622,22 +622,24 @@ class Repository
merge_commit_sha merge_commit_sha
end 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 source_sha = find_branch(base_branch).target
target_sha = find_branch(target_branch).try(:target) target_sha = find_branch(target_branch).try(:target)
# First make revert in temp branch # First make revert in temp branch
unless target_sha status = target_sha ? true : revert_commit(user, commit_id, target_branch, base_branch, commit_message)
revert_commit(user, commit_id, target_branch, base_branch, commit_message)
end
# Make the revert happen in the target branch # Make the revert happen in the target branch
source_sha = find_branch(target_branch).target source_sha = find_branch(target_branch).target
target_sha = find_branch(base_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) if has_changes && !create_mr
revert_commit(user, commit_id, base_branch, base_branch, commit_message) status = revert_commit(user, commit_id, base_branch, base_branch, commit_message)
end end
::File.open('/Users/ruben/Desktop/log.txt', 'w') { |f| f.puts "HAS CHANGES: #{has_changes && status}" }
has_changes && status
end end
def revert_commit(user, commit_id, target_branch, base_branch, commit_message) def revert_commit(user, commit_id, target_branch, base_branch, commit_message)
......
...@@ -4,8 +4,9 @@ module Commits ...@@ -4,8 +4,9 @@ module Commits
def execute def execute
@source_project = params[:source_project] || @project @source_project = params[:source_project] || @project
@target_branch = params[:target_branch] @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 # Check push permissions to branch
validate validate
...@@ -23,10 +24,13 @@ module Commits ...@@ -23,10 +24,13 @@ module Commits
raw_repo = repository.rugged raw_repo = repository.rugged
# Create branch with revert commit # Create branch with revert commit
reverted = repository.revert(current_user, @commit_to_revert.id, reverted = repository.revert(current_user, @commit.id,
@commit_to_revert.revert_branch_name, @target_branch, @commit.revert_branch_name, @target_branch,
@commit_to_revert.revert_message) @commit.revert_message, @create_merge_request)
repository.rm_branch(current_user, @commit_to_revert.revert_branch_name)
unless @create_merge_request
repository.rm_branch(current_user, @commit.revert_branch_name)
end
reverted reverted
end 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