Commit dcbb875c authored by Valery Sizov's avatar Valery Sizov

fast automerge: base implementation

parent ae565d24
...@@ -101,6 +101,9 @@ module Project::HooksTrait ...@@ -101,6 +101,9 @@ module Project::HooksTrait
# Execute web hooks # Execute web hooks
self.execute_web_hooks(oldrev, newrev, ref, user) self.execute_web_hooks(oldrev, newrev, ref, user)
# Create repo satellite
self.create_repo_satellite unless self.satellite_exists?
end end
end end
end end
...@@ -37,6 +37,23 @@ module Project::RepositoryTrait ...@@ -37,6 +37,23 @@ module Project::RepositoryTrait
end end
end end
def path_to_repo_satellite
File.join(Rails.root, "tmp", "repo_satellites", self.path)
end
def satellite_exists?
File.exist? path_to_repo_satellite
end
def create_repo_satellite
`git clone #{url_to_repo} #{path_to_repo_satellite}`
Dir.chdir(path_to_repo_satellite) do
primary_branch = Grit::Repo.new(".").heads.first.name #usually it`s master
`git checkout -b __parking_branch`
`git branch -D #{primary_branch}`
end
end
def write_hook(name, content) def write_hook(name, content)
hook_file = File.join(path_to_repo, 'hooks', name) hook_file = File.join(path_to_repo, 'hooks', name)
......
class GitlabMerge class GitlabMerge
attr_accessor :project, :merge_path, :merge_request, :user attr_accessor :project, :merge_request, :user
def initialize(merge_request, user) def initialize(merge_request, user)
self.user = user self.user = user
self.merge_request = merge_request self.merge_request = merge_request
self.project = merge_request.project self.project = merge_request.project
self.merge_path = File.join(Rails.root, "tmp", "merge_repo", project.path, merge_request.id.to_s)
FileUtils.rm_rf(merge_path)
FileUtils.mkdir_p merge_path
end end
def can_be_merged? def can_be_merged?
pull do |repo, output| process do |repo, output|
!(output =~ /Automatic merge failed/) !(output =~ /Automatic merge failed/)
end end
end end
def merge def merge
pull do |repo, output| process do |repo, output|
if output =~ /Automatic merge failed/ if output =~ /Automatic merge failed/
false false
else else
...@@ -27,19 +24,20 @@ class GitlabMerge ...@@ -27,19 +24,20 @@ class GitlabMerge
end end
end end
def pull def process
Grit::Git.with_timeout(30.seconds) do Grit::Git.with_timeout(30.seconds) do
File.open(File.join(Rails.root, "tmp", "merge_repo", "#{project.path}.lock"), "w+") do |f| File.open(File.join(Rails.root, "tmp", "merge_repo", "#{project.path}.lock"), "w+") do |f|
f.flock(File::LOCK_EX) f.flock(File::LOCK_EX)
self.project.repo.git.clone({:branch => merge_request.target_branch}, project.url_to_repo, merge_path) unless project.satellite_exists?
unless File.exist?(self.merge_path) raise "You should run: rake gitlab_enable_automerge"
raise "Gitlab user do not have access to repo. You should run: rake gitlab_enable_automerge"
end end
Dir.chdir(merge_path) do Dir.chdir(project.path_to_repo_satellite) do
merge_repo = Grit::Repo.new('.') merge_repo = Grit::Repo.new('.')
merge_repo.git.sh "git fetch origin"
merge_repo.git.sh "git config user.name \"#{user.name}\"" merge_repo.git.sh "git config user.name \"#{user.name}\""
merge_repo.git.sh "git config user.email \"#{user.email}\"" merge_repo.git.sh "git config user.email \"#{user.email}\""
merge_repo.git.sh "git checkout -b #{merge_request.target_branch} origin/#{merge_request.target_branch}"
output = merge_repo.git.pull({}, "--no-ff", "origin", merge_request.source_branch) output = merge_repo.git.pull({}, "--no-ff", "origin", merge_request.source_branch)
yield(merge_repo, output) yield(merge_repo, output)
end end
......
...@@ -6,6 +6,13 @@ namespace :gitlab do ...@@ -6,6 +6,13 @@ namespace :gitlab do
git.admin_all_repo git.admin_all_repo
end end
Project.find_each do |project|
if project.repo_exists? && !project.satellite_exists?
puts "Creating satellite for #{project.name}...".green
project.create_repo_satellite
end
end
puts "Done!".green puts "Done!".green
end end
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