Commit 78d620b3 authored by randx's avatar randx

Raw gitlab automerge feature

parent 8ee0993f
......@@ -2,7 +2,7 @@ class MergeRequestsController < ApplicationController
before_filter :authenticate_user!
before_filter :project
before_filter :module_enabled
before_filter :merge_request, :only => [:edit, :update, :destroy, :show, :commits, :diffs]
before_filter :merge_request, :only => [:edit, :update, :destroy, :show, :commits, :diffs, :automerge]
layout "project"
# Authorize
......@@ -95,6 +95,22 @@ class MergeRequestsController < ApplicationController
end
end
def automerge
message = ""
if GitlabMerge.new(@merge_request).merge
@merge_request.update_attributes(
:author_id_of_changes => current_user.id,
:closed => true
)
@merge_request.reload_code
message = "Successfully merged"
else
message = "Can not be merged"
end
redirect_to [@merge_request.project, @merge_request], :alert => message
end
def destroy
@merge_request.destroy
......
......@@ -102,6 +102,7 @@ Gitlab::Application.routes.draw do
resources :merge_requests do
member do
get :diffs
get :automerge
end
collection do
......
class GitlabMerge
attr_accessor :project, :merge_path, :merge_request
def initialize(merge_request)
self.merge_request = merge_request
self.project = merge_request.project
self.merge_path = File.join(Rails.root, "tmp", "merge_repo", project.path)
FileUtils.rm_rf(merge_path)
FileUtils.mkdir_p merge_path
end
def merge
self.project.repo.git.clone({:branch => merge_request.target_branch}, project.url_to_repo, merge_path)
output = ""
Dir.chdir(merge_path) do
merge_repo = Grit::Repo.new('.')
output = merge_repo.git.pull({}, "origin", merge_request.source_branch)
if output =~ /Automatic merge failed/
return false
else
merge_repo.git.push({}, "origin", merge_request.target_branch)
return true
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