2012-03-29 17:03:05 +02:00
|
|
|
class GitlabMerge
|
2012-04-22 14:19:09 +02:00
|
|
|
attr_accessor :project, :merge_request, :user
|
2012-03-29 17:03:05 +02:00
|
|
|
|
2012-03-30 19:49:34 +02:00
|
|
|
def initialize(merge_request, user)
|
|
|
|
self.user = user
|
2012-03-29 17:03:05 +02:00
|
|
|
self.merge_request = merge_request
|
|
|
|
self.project = merge_request.project
|
|
|
|
end
|
|
|
|
|
2012-03-30 07:05:04 +02:00
|
|
|
def can_be_merged?
|
2012-04-22 14:19:09 +02:00
|
|
|
process do |repo, output|
|
2012-03-30 07:05:04 +02:00
|
|
|
!(output =~ /Automatic merge failed/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-29 17:03:05 +02:00
|
|
|
def merge
|
2012-04-22 14:19:09 +02:00
|
|
|
process do |repo, output|
|
2012-03-30 07:05:04 +02:00
|
|
|
if output =~ /Automatic merge failed/
|
|
|
|
false
|
|
|
|
else
|
|
|
|
repo.git.push({}, "origin", merge_request.target_branch)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-22 14:19:09 +02:00
|
|
|
def process
|
2012-04-23 09:54:19 +02:00
|
|
|
Grit::Git.with_timeout(30.seconds) do
|
|
|
|
# Make sure tmp/merge_repo exists
|
|
|
|
lock_path = File.join(Rails.root, "tmp", "merge_repo")
|
|
|
|
FileUtils.mkdir_p(lock_path) unless File.exists?(File.join(Rails.root, "tmp", "merge_repo"))
|
|
|
|
|
|
|
|
File.open(File.join(lock_path, "#{project.path}.lock"), "w+") do |f|
|
2012-04-21 12:23:44 +02:00
|
|
|
f.flock(File::LOCK_EX)
|
|
|
|
|
2012-04-22 15:14:01 +02:00
|
|
|
unless project.satellite.exists?
|
2012-04-22 15:49:45 +02:00
|
|
|
raise "You should run: rake gitlab:app:enable_automerge"
|
2012-04-21 12:23:44 +02:00
|
|
|
end
|
2012-04-22 15:14:01 +02:00
|
|
|
|
|
|
|
project.satellite.clear
|
|
|
|
|
|
|
|
Dir.chdir(project.satellite.path) do
|
2012-04-21 12:23:44 +02:00
|
|
|
merge_repo = Grit::Repo.new('.')
|
2012-04-22 14:19:09 +02:00
|
|
|
merge_repo.git.sh "git fetch origin"
|
2012-04-21 12:23:44 +02:00
|
|
|
merge_repo.git.sh "git config user.name \"#{user.name}\""
|
|
|
|
merge_repo.git.sh "git config user.email \"#{user.email}\""
|
2012-04-22 14:19:09 +02:00
|
|
|
merge_repo.git.sh "git checkout -b #{merge_request.target_branch} origin/#{merge_request.target_branch}"
|
2012-04-21 12:23:44 +02:00
|
|
|
output = merge_repo.git.pull({}, "--no-ff", "origin", merge_request.source_branch)
|
|
|
|
yield(merge_repo, output)
|
|
|
|
end
|
2012-04-16 22:49:57 +02:00
|
|
|
end
|
2012-03-29 17:03:05 +02:00
|
|
|
end
|
2012-04-21 12:23:44 +02:00
|
|
|
|
|
|
|
rescue Grit::Git::GitTimeout
|
|
|
|
return false
|
2012-03-29 17:03:05 +02:00
|
|
|
end
|
|
|
|
end
|