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

Save merge commit id when MR is merged

parent 720e52d9
...@@ -193,7 +193,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -193,7 +193,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
end end
def revert def revert
@repository.find_or_create_branch(@merge_request.reverse_branch_name, @merge_request.target_branch) @repository.revert_merge(current_user, @merge_request.merge_commit_sha, @merge_request.reverse_branch_name)
url_params = { merge_request: { url_params = { merge_request: {
source_branch: @merge_request.reverse_branch_name, source_branch: @merge_request.reverse_branch_name,
......
...@@ -137,8 +137,8 @@ class Repository ...@@ -137,8 +137,8 @@ class Repository
find_branch(branch_name) find_branch(branch_name)
end end
def find_or_create_branch(name, ref) def find_or_create_branch(user, name, ref)
find_branch(name) or add_branch(name, ref) find_branch(name) or add_branch(user, name, ref)
end end
def add_tag(tag_name, ref, message = nil) def add_tag(tag_name, ref, message = nil)
...@@ -605,6 +605,7 @@ class Repository ...@@ -605,6 +605,7 @@ class Repository
def merge(user, source_sha, target_branch, options = {}) def merge(user, source_sha, target_branch, options = {})
our_commit = rugged.branches[target_branch].target our_commit = rugged.branches[target_branch].target
their_commit = rugged.lookup(source_sha) their_commit = rugged.lookup(source_sha)
merge_commit_sha = nil
raise "Invalid merge target" if our_commit.nil? raise "Invalid merge target" if our_commit.nil?
raise "Invalid merge source" if their_commit.nil? raise "Invalid merge source" if their_commit.nil?
...@@ -619,8 +620,31 @@ class Repository ...@@ -619,8 +620,31 @@ class Repository
update_ref: ref update_ref: ref
) )
Rugged::Commit.create(rugged, actual_options) merge_commit_sha = Rugged::Commit.create(rugged, actual_options)
end end
merge_commit_sha
end
def revert_merge(user, merge_commit_id, revert_branch_name)
find_or_create_branch(user, revert_branch_name, merge_commit_id)
new_index = rugged.revert_commit(merge_commit_id, merge_commit_id, mainline: 1)
committer = user_to_committer(user)
commit_with_hooks(user, revert_branch_name) do |ref|
options = {
message: 'Revert MR',
author: committer,
committer: committer,
tree: new_index.write_tree(rugged),
parents: [rugged.lookup(merge_commit_id)],
update_ref: ref
}
Rugged::Commit.create(rugged, options)
end
end end
def merged_to_root_ref?(branch_name) def merged_to_root_ref?(branch_name)
......
...@@ -34,7 +34,8 @@ module MergeRequests ...@@ -34,7 +34,8 @@ module MergeRequests
committer: committer committer: committer
} }
repository.merge(current_user, merge_request.source_sha, merge_request.target_branch, options) commit_id = repository.merge(current_user, merge_request.source_sha, merge_request.target_branch, options)
merge_request.update(merge_commit_sha: commit_id)
rescue StandardError => e rescue StandardError => e
merge_request.update(merge_error: "Something went wrong during merge") merge_request.update(merge_error: "Something went wrong during merge")
Rails.logger.error(e.message) Rails.logger.error(e.message)
......
class AddMergeCommitShaToMergeRequests < ActiveRecord::Migration
def change
add_column :merge_requests, :merge_commit_sha, :string
end
end
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
# merge_params :text # merge_params :text
# merge_when_build_succeeds :boolean default(FALSE), not null # merge_when_build_succeeds :boolean default(FALSE), not null
# merge_user_id :integer # merge_user_id :integer
# merge_commit_sha :string
# #
FactoryGirl.define do FactoryGirl.define do
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
# merge_params :text # merge_params :text
# merge_when_build_succeeds :boolean default(FALSE), not null # merge_when_build_succeeds :boolean default(FALSE), not null
# merge_user_id :integer # merge_user_id :integer
# merge_commit_sha :string
# #
require 'spec_helper' require 'spec_helper'
......
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