diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 1e8d9908f0a1f7eefe0ba6189103527d8a0c396c..1b3d6079d2cdeeef0e9d9926a446cfb8276cbb32 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -476,7 +476,7 @@ class MergeRequest < ActiveRecord::Base
   end
 
   def ci_commit
-    if last_commit
+    if last_commit and source_project
       source_project.ci_commit(last_commit.id)
     end
   end
diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb
index 90af75ff0e3ff84dce44a33e7d00c4d6898edfef..567c911425c8d526e67c962e8d5f1b59a6d2cb8e 100644
--- a/spec/models/merge_request_spec.rb
+++ b/spec/models/merge_request_spec.rb
@@ -193,4 +193,29 @@ describe MergeRequest do
   it_behaves_like 'a Taskable' do
     subject { create :merge_request, :simple }
   end
+
+  describe '#ci_commit' do
+    describe 'when the source project exists' do
+      it 'returns the latest commit' do
+        commit    = double(:commit, id: '123abc')
+        ci_commit = double(:ci_commit)
+
+        allow(subject).to receive(:last_commit).and_return(commit)
+
+        expect(subject.source_project).to receive(:ci_commit).
+          with('123abc').
+          and_return(ci_commit)
+
+        expect(subject.ci_commit).to eq(ci_commit)
+      end
+    end
+
+    describe 'when the source project does not exist' do
+      it 'returns nil' do
+        allow(subject).to receive(:source_project).and_return(nil)
+
+        expect(subject.ci_commit).to be_nil
+      end
+    end
+  end
 end