2012-10-26 00:19:01 +02:00
|
|
|
module Gitlab
|
|
|
|
module Satellite
|
2012-10-26 02:50:24 +02:00
|
|
|
# GitLab server-side merge
|
2012-10-26 00:19:01 +02:00
|
|
|
class MergeAction < Action
|
2012-10-26 00:24:02 +02:00
|
|
|
attr_accessor :merge_request
|
2012-10-26 00:19:01 +02:00
|
|
|
|
2012-10-26 00:26:47 +02:00
|
|
|
def initialize(user, merge_request)
|
|
|
|
super user, merge_request.project
|
2012-10-26 00:19:01 +02:00
|
|
|
@merge_request = merge_request
|
|
|
|
end
|
|
|
|
|
2012-10-26 02:50:24 +02:00
|
|
|
# Checks if a merge request can be executed without user interaction
|
2012-10-26 00:19:01 +02:00
|
|
|
def can_be_merged?
|
|
|
|
in_locked_and_timed_satellite do |merge_repo|
|
|
|
|
merge_in_satellite!(merge_repo)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Merges the source branch into the target branch in the satellite and
|
|
|
|
# pushes it back to Gitolite.
|
|
|
|
# It also removes the source branch if requested in the merge request.
|
|
|
|
#
|
|
|
|
# Returns false if the merge produced conflicts
|
|
|
|
# Returns false if pushing from the satellite to Gitolite failed or was rejected
|
|
|
|
# Returns true otherwise
|
|
|
|
def merge!
|
|
|
|
in_locked_and_timed_satellite do |merge_repo|
|
|
|
|
if merge_in_satellite!(merge_repo)
|
|
|
|
# push merge back to Gitolite
|
|
|
|
# will raise CommandFailed when push fails
|
2012-10-26 01:44:50 +02:00
|
|
|
merge_repo.git.push({raise: true, timeout: true}, :origin, merge_request.target_branch)
|
2012-10-26 00:19:01 +02:00
|
|
|
|
|
|
|
# remove source branch
|
|
|
|
if merge_request.should_remove_source_branch && !project.root_ref?(merge_request.source_branch)
|
|
|
|
# will raise CommandFailed when push fails
|
2012-10-26 01:44:50 +02:00
|
|
|
merge_repo.git.push({raise: true, timeout: true}, :origin, ":#{merge_request.source_branch}")
|
2012-10-26 00:19:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# merge, push and branch removal successful
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Grit::Git::CommandFailed => ex
|
|
|
|
Gitlab::GitLogger.error(ex.message)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Merges the source_branch into the target_branch in the satellite.
|
|
|
|
#
|
|
|
|
# Note: it will clear out the satellite before doing anything
|
|
|
|
#
|
|
|
|
# Returns false if the merge produced conflicts
|
|
|
|
# Returns true otherwise
|
|
|
|
def merge_in_satellite!(repo)
|
|
|
|
prepare_satellite!(repo)
|
|
|
|
|
|
|
|
# create target branch in satellite at the corresponding commit from Gitolite
|
2012-10-26 01:44:50 +02:00
|
|
|
repo.git.checkout({raise: true, timeout: true, b: true}, merge_request.target_branch, "origin/#{merge_request.target_branch}")
|
2012-10-26 00:19:01 +02:00
|
|
|
|
|
|
|
# merge the source branch from Gitolite into the satellite
|
|
|
|
# will raise CommandFailed when merge fails
|
2012-10-26 01:44:50 +02:00
|
|
|
repo.git.pull({raise: true, timeout: true, no_ff: true}, :origin, merge_request.source_branch)
|
2012-10-26 00:19:01 +02:00
|
|
|
rescue Grit::Git::CommandFailed => ex
|
|
|
|
Gitlab::GitLogger.error(ex.message)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|