From f2a0c6f6bf768db8837283ad65fe6790b9105d26 Mon Sep 17 00:00:00 2001
From: Douwe Maan <douwe@selenight.nl>
Date: Wed, 23 Nov 2016 20:00:58 +0800
Subject: [PATCH] Correctly determine mergeability of MR with no discussions

---
 app/models/merge_request.rb                   |  6 ++-
 ...-mrs-without-discussions-are-mergeable.yml |  4 ++
 spec/models/merge_request_spec.rb             | 54 +++++++++++++++++++
 3 files changed, 63 insertions(+), 1 deletion(-)
 create mode 100644 changelogs/unreleased/24863-mrs-without-discussions-are-mergeable.yml

diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 6c3c093d084..fdf54cc8a7e 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -494,10 +494,14 @@ class MergeRequest < ActiveRecord::Base
     discussions_resolvable? && diff_discussions.none?(&:to_be_resolved?)
   end
 
+  def discussions_to_be_resolved?
+    discussions_resolvable? && !discussions_resolved?
+  end
+
   def mergeable_discussions_state?
     return true unless project.only_allow_merge_if_all_discussions_are_resolved?
 
-    discussions_resolved?
+    !discussions_to_be_resolved?
   end
 
   def hook_attrs
diff --git a/changelogs/unreleased/24863-mrs-without-discussions-are-mergeable.yml b/changelogs/unreleased/24863-mrs-without-discussions-are-mergeable.yml
new file mode 100644
index 00000000000..9bdb9411135
--- /dev/null
+++ b/changelogs/unreleased/24863-mrs-without-discussions-are-mergeable.yml
@@ -0,0 +1,4 @@
+---
+title: Correctly determine mergeability of MR with no discussions
+merge_request: 
+author: 
diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb
index 0b4277b1edd..58ccd056328 100644
--- a/spec/models/merge_request_spec.rb
+++ b/spec/models/merge_request_spec.rb
@@ -937,6 +937,16 @@ describe MergeRequest, models: true do
           expect(merge_request.mergeable_discussions_state?).to be_falsey
         end
       end
+
+      context 'with no discussions' do
+        before do
+          merge_request.notes.destroy_all
+        end
+
+        it 'returns true' do
+          expect(merge_request.mergeable_discussions_state?).to be_truthy
+        end
+      end
     end
 
     context 'when project.only_allow_merge_if_all_discussions_are_resolved == false' do
@@ -1198,6 +1208,50 @@ describe MergeRequest, models: true do
         end
       end
     end
+
+    describe "#discussions_to_be_resolved?" do
+      context "when discussions are not resolvable" do
+        before do
+          allow(subject).to receive(:discussions_resolvable?).and_return(false)
+        end
+
+        it "returns false" do
+          expect(subject.discussions_to_be_resolved?).to be false
+        end
+      end
+
+      context "when discussions are resolvable" do
+        before do
+          allow(subject).to receive(:discussions_resolvable?).and_return(true)
+
+          allow(first_discussion).to receive(:resolvable?).and_return(true)
+          allow(second_discussion).to receive(:resolvable?).and_return(false)
+          allow(third_discussion).to receive(:resolvable?).and_return(true)
+        end
+
+        context "when all resolvable discussions are resolved" do
+          before do
+            allow(first_discussion).to receive(:resolved?).and_return(true)
+            allow(third_discussion).to receive(:resolved?).and_return(true)
+          end
+
+          it "returns false" do
+            expect(subject.discussions_to_be_resolved?).to be false
+          end
+        end
+
+        context "when some resolvable discussions are not resolved" do
+          before do
+            allow(first_discussion).to receive(:resolved?).and_return(true)
+            allow(third_discussion).to receive(:resolved?).and_return(false)
+          end
+
+          it "returns true" do
+            expect(subject.discussions_to_be_resolved?).to be true
+          end
+        end
+      end
+    end
   end
 
   describe '#conflicts_can_be_resolved_in_ui?' do
-- 
GitLab