From b306a52114bb155538dcee3b0eab815ac8d2655d Mon Sep 17 00:00:00 2001
From: Connor Shea <connor.james.shea@gmail.com>
Date: Tue, 12 Jul 2016 11:05:48 -0600
Subject: [PATCH] Add with_warnings? method to Pipelines and add tests.

---
 CHANGELOG                                     |  1 +
 .../projects/merge_requests_controller.rb     |  2 ++
 app/models/ci/pipeline.rb                     |  6 +++++
 spec/models/ci/pipeline_spec.rb               | 26 +++++++++++++++++++
 4 files changed, 35 insertions(+)

diff --git a/CHANGELOG b/CHANGELOG
index a5d26db7626..0b7a60324d7 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -23,6 +23,7 @@ v 8.10.0 (unreleased)
   - Store when and yaml variables in builds table
   - Display last commit of deleted branch in push events !4699 (winniehell)
   - Escape file extension when parsing search results !5141 (winniehell)
+  - Add "passing with warnings" to the merge request pipeline possible statuses, this happens when builds that allow failures have failed. !5004
   - Apply the trusted_proxies config to the rack request object for use with rack_attack
   - Upgrade to Rails 4.2.7. !5236
   - Allow to pull code with deploy key from public projects
diff --git a/app/controllers/projects/merge_requests_controller.rb b/app/controllers/projects/merge_requests_controller.rb
index df659bb8c3b..1d99aeeb672 100644
--- a/app/controllers/projects/merge_requests_controller.rb
+++ b/app/controllers/projects/merge_requests_controller.rb
@@ -286,6 +286,8 @@ class Projects::MergeRequestsController < Projects::ApplicationController
       status = pipeline.status
       coverage = pipeline.try(:coverage)
 
+      status = "success_with_warnings" if pipeline.success? && pipeline.with_warnings?
+
       status ||= "preparing"
     else
       ci_service = @merge_request.source_project.ci_service
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index aca8607f4e8..e862978d7b4 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -146,6 +146,12 @@ module Ci
       end
     end
 
+    def with_warnings?
+      builds.latest.any? do |build|
+        build.failed? && build.allow_failure
+      end
+    end
+
     def config_processor
       return nil unless ci_yaml_file
       return @config_processor if defined?(@config_processor)
diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb
index c29e4811385..20520b0a111 100644
--- a/spec/models/ci/pipeline_spec.rb
+++ b/spec/models/ci/pipeline_spec.rb
@@ -502,4 +502,30 @@ describe Ci::Pipeline, models: true do
       end
     end
   end
+
+  describe '#with_warnings?' do
+    subject { pipeline.with_warnings? }
+
+    context 'build which is allowed to fail fails' do
+      before do
+        FactoryGirl.create :ci_build, :success, pipeline: pipeline, name: 'rspec'
+        FactoryGirl.create :ci_build, :allowed_to_fail, :failed, pipeline: pipeline, name: 'rubocop'
+      end
+      
+      it 'returns true' do
+        is_expected.to be_truthy
+      end
+    end
+
+    context 'build which is allowed to fail succeeds' do
+      before do
+        FactoryGirl.create :ci_build, :success, pipeline: pipeline, name: 'rspec'
+        FactoryGirl.create :ci_build, :allowed_to_fail, :success, pipeline: pipeline, name: 'rubocop'
+      end
+      
+      it 'returns false' do
+        is_expected.to be_falsey
+      end
+    end
+  end
 end
-- 
2.30.9