gitlab-org--gitlab-foss/lib/gitlab/satellite/merge_action.rb
2012-10-26 00:19:01 +02:00

83 lines
2.8 KiB
Ruby

module Gitlab
module Satellite
class MergeAction < Action
attr_accessor :merge_request, :user
def initialize(merge_request, user)
super merge_request.project
@merge_request = merge_request
@user = user
end
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
merge_repo.git.push({raise: true}, :origin, merge_request.target_branch)
# remove source branch
if merge_request.should_remove_source_branch && !project.root_ref?(merge_request.source_branch)
# will raise CommandFailed when push fails
merge_repo.git.push({raise: true}, :origin, ":#{merge_request.source_branch}")
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
repo.git.checkout({b: true}, merge_request.target_branch, "origin/#{merge_request.target_branch}")
# merge the source branch from Gitolite into the satellite
# will raise CommandFailed when merge fails
repo.git.pull({no_ff: true, raise: true}, :origin, merge_request.source_branch)
rescue Grit::Git::CommandFailed => ex
Gitlab::GitLogger.error(ex.message)
false
end
# * Clears the satellite
# * Updates the satellite from Gitolite
# * Sets up Git variables for the user
def prepare_satellite!(repo)
project.satellite.clear
repo.git.reset(hard: true)
repo.git.fetch({}, :origin)
repo.git.config({}, "user.name", user.name)
repo.git.config({}, "user.email", user.email)
end
end
end
end