From 9c03c1c545d1afeaf12d8ee1c204936cdf8c55e1 Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Fri, 9 Jan 2015 19:10:01 -0800
Subject: [PATCH] Make automerge via satellite

---
 app/assets/javascripts/merge_request.js.coffee      | 13 +++++++++++++
 .../projects/merge_requests_controller.rb           |  7 ++++---
 app/views/projects/merge_requests/_show.html.haml   |  2 +-
 app/views/projects/merge_requests/automerge.js.haml |  3 +--
 app/workers/auto_merge_worker.rb                    | 13 +++++++++++++
 5 files changed, 32 insertions(+), 6 deletions(-)
 create mode 100644 app/workers/auto_merge_worker.rb

diff --git a/app/assets/javascripts/merge_request.js.coffee b/app/assets/javascripts/merge_request.js.coffee
index 9e3ca45ce..5bcbd5685 100644
--- a/app/assets/javascripts/merge_request.js.coffee
+++ b/app/assets/javascripts/merge_request.js.coffee
@@ -135,3 +135,16 @@ class @MergeRequest
     this.$('.automerge_widget').hide()
     this.$('.merge-in-progress').hide()
     this.$('.automerge_widget.already_cannot_be_merged').show()
+
+  mergeInProgress: ->
+    $.ajax
+      type: 'GET'
+      url: $('.merge-request').data('url')
+      success: (data) =>
+        switch data.state
+          when 'merged'
+            location.reload()
+          else
+            setTimeout(merge_request.mergeInProgress, 3000)
+      dataType: 'json'
+
diff --git a/app/controllers/projects/merge_requests_controller.rb b/app/controllers/projects/merge_requests_controller.rb
index d23461821..3f702b0af 100644
--- a/app/controllers/projects/merge_requests_controller.rb
+++ b/app/controllers/projects/merge_requests_controller.rb
@@ -27,6 +27,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
 
     respond_to do |format|
       format.html
+      format.json { render json: @merge_request }
       format.diff { render text: @merge_request.to_diff(current_user) }
       format.patch { render text: @merge_request.to_patch(current_user) }
     end
@@ -104,15 +105,15 @@ class Projects::MergeRequestsController < Projects::ApplicationController
     if @merge_request.unchecked?
       @merge_request.check_if_can_be_merged
     end
-    render json: {merge_status: @merge_request.merge_status_name}
+
+    render json: { merge_status: @merge_request.merge_status_name }
   end
 
   def automerge
     return access_denied! unless allowed_to_merge?
 
     if @merge_request.open? && @merge_request.can_be_merged?
-      @merge_request.should_remove_source_branch = params[:should_remove_source_branch]
-      @merge_request.automerge!(current_user, params[:commit_message])
+      AutoMergeWorker.perform_async(@merge_request.id, current_user.id, params)
       @status = true
     else
       @status = false
diff --git a/app/views/projects/merge_requests/_show.html.haml b/app/views/projects/merge_requests/_show.html.haml
index f8d267333..8e31a7e3f 100644
--- a/app/views/projects/merge_requests/_show.html.haml
+++ b/app/views/projects/merge_requests/_show.html.haml
@@ -1,4 +1,4 @@
-.merge-request
+.merge-request{'data-url' => project_merge_request_path(@project, @merge_request)}
   = render "projects/merge_requests/show/mr_title"
   %hr
   = render "projects/merge_requests/show/mr_box"
diff --git a/app/views/projects/merge_requests/automerge.js.haml b/app/views/projects/merge_requests/automerge.js.haml
index e01ff662e..a53cbb150 100644
--- a/app/views/projects/merge_requests/automerge.js.haml
+++ b/app/views/projects/merge_requests/automerge.js.haml
@@ -1,7 +1,6 @@
 -if @status
   :plain
-    location.reload();
+    merge_request.mergeInProgress();
 -else
   :plain
     merge_request.alreadyOrCannotBeMerged()
-
diff --git a/app/workers/auto_merge_worker.rb b/app/workers/auto_merge_worker.rb
new file mode 100644
index 000000000..a6dd73eee
--- /dev/null
+++ b/app/workers/auto_merge_worker.rb
@@ -0,0 +1,13 @@
+class AutoMergeWorker
+  include Sidekiq::Worker
+
+  sidekiq_options queue: :default
+
+  def perform(merge_request_id, current_user_id, params)
+    params = params.with_indifferent_access
+    current_user = User.find(current_user_id)
+    merge_request = MergeRequest.find(merge_request_id)
+    merge_request.should_remove_source_branch = params[:should_remove_source_branch]
+    merge_request.automerge!(current_user, params[:commit_message])
+  end
+end
-- 
2.30.9