From a8e4f3ed888f27629a5e35174cdb19cb6c751c16 Mon Sep 17 00:00:00 2001
From: Valery Sizov <vsv2711@gmail.com>
Date: Mon, 16 Apr 2012 23:05:08 +0300
Subject: [PATCH] Auto-merge: 'already can not be merged' alert

---
 app/assets/javascripts/merge_requests.js     | 8 +++++++-
 app/controllers/merge_requests_controller.rb | 8 ++++++--
 app/views/merge_requests/automerge.js.haml   | 9 +++++++--
 app/views/merge_requests/show.html.haml      | 5 +++++
 4 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/app/assets/javascripts/merge_requests.js b/app/assets/javascripts/merge_requests.js
index eba9c9cab4..132a05739d 100644
--- a/app/assets/javascripts/merge_requests.js
+++ b/app/assets/javascripts/merge_requests.js
@@ -7,7 +7,7 @@ var MergeRequest = {
     function(opts) {
       this.opts = opts;
 
-      if($(".automerge_widget").lenght){
+      if($(".automerge_widget").length){
         $.get(opts.url_to_automerge_check, function(data){
           $(".automerge_widget").hide();
           $(".automerge_widget." + data.state).show();
@@ -48,5 +48,11 @@ var MergeRequest = {
           $('.status').removeClass("loading");
         },
         dataType: "script"});
+    },
+
+  already_cannot_be_merged:
+    function(){
+        $(".automerge_widget").hide();
+        $(".automerge_widget.already_cannot_be_merged").show();
     }
 }
diff --git a/app/controllers/merge_requests_controller.rb b/app/controllers/merge_requests_controller.rb
index 18ad578fc8..1077cef718 100644
--- a/app/controllers/merge_requests_controller.rb
+++ b/app/controllers/merge_requests_controller.rb
@@ -104,9 +104,13 @@ class MergeRequestsController < ApplicationController
   end
 
   def automerge
-    render_404 unless @merge_request.open? && @merge_request.can_be_merged?
     return access_denied! unless can?(current_user, :accept_mr, @project)
-    @merge_request.automerge!(current_user)
+    if @merge_request.open? && @merge_request.can_be_merged?
+      @merge_request.automerge!(current_user)
+      @status = true
+    else
+      @status = false
+    end
   end
 
   def destroy
diff --git a/app/views/merge_requests/automerge.js.haml b/app/views/merge_requests/automerge.js.haml
index 5827a518b5..93e184455a 100644
--- a/app/views/merge_requests/automerge.js.haml
+++ b/app/views/merge_requests/automerge.js.haml
@@ -1,2 +1,7 @@
-:plain
-  location.reload();
+-if @status
+  :plain
+    location.reload();
+-else
+  :plain
+    MergeRequest.already_cannot_be_merged()
+
diff --git a/app/views/merge_requests/show.html.haml b/app/views/merge_requests/show.html.haml
index aef74b8dce..1b23e5b225 100644
--- a/app/views/merge_requests/show.html.haml
+++ b/app/views/merge_requests/show.html.haml
@@ -71,6 +71,11 @@
     .alert-message
       %p
         %strong Checking for ability to automatically merge… 
+  
+  .automerge_widget.already_cannot_be_merged{:style => "display:none"}
+    .alert-message
+      %p
+        %strong This merge request already can not be merged
 
 
 
-- 
2.30.9