From 21136baa77369d5990ef5db4af26d688aedc8320 Mon Sep 17 00:00:00 2001
From: Kamil Trzcinski <ayufan@ayufan.eu>
Date: Wed, 13 Apr 2016 20:51:03 +0200
Subject: [PATCH] Update handling of skipped status

---
 app/models/ci/build.rb           |  2 +-
 app/models/ci/commit.rb          | 23 ++++++++---------------
 app/models/concerns/ci_status.rb | 10 +++++++++-
 3 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index 085ecc6951c..c0b334d3600 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -116,7 +116,7 @@ module Ci
     end
 
     def retried?
-      !self.commit.latest.include?(self)
+      !self.commit.statuses.latest.include?(self)
     end
 
     def retry
diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb
index e2bf4d62541..00a95dd05be 100644
--- a/app/models/ci/commit.rb
+++ b/app/models/ci/commit.rb
@@ -125,16 +125,12 @@ module Ci
       end
     end
 
-    def latest
-      statuses.latest
-    end
-
     def retried
       @retried ||= (statuses.order(id: :desc) - statuses.latest)
     end
 
     def coverage
-      coverage_array = latest.map(&:coverage).compact
+      coverage_array = statuses.latest.map(&:coverage).compact
       if coverage_array.size >= 1
         '%.2f' % (coverage_array.reduce(:+) / coverage_array.size)
       end
@@ -169,18 +165,15 @@ module Ci
     private
 
     def update_state
-      reload
-      self.status = if yaml_errors.present?
-                      'failed'
+      statuses.reload
+      self.status = if yaml_errors.blank?
+                      statuses.latest.status || 'skipped'
                     else
-                      latest.status
+                      'failed'
                     end
-      self.started_at = statuses.minimum(:started_at)
-      self.finished_at = statuses.maximum(:finished_at)
-      self.duration = begin
-        duration_array = latest.map(&:duration).compact
-        duration_array.reduce(:+).to_i
-      end
+      self.started_at = statuses.started_at
+      self.finished_at = statuses.finished_at
+      self.duration = statuses.latest.duration
       save
     end
 
diff --git a/app/models/concerns/ci_status.rb b/app/models/concerns/ci_status.rb
index fd86d2f7553..8190b2a20c6 100644
--- a/app/models/concerns/ci_status.rb
+++ b/app/models/concerns/ci_status.rb
@@ -15,7 +15,7 @@ module CiStatus
       skipped = all.skipped.select('count(*)').to_sql
 
       deduce_status = "(CASE
-        WHEN (#{builds})=0 THEN 'skipped'
+        WHEN (#{builds})=0 THEN NULL
         WHEN (#{builds})=(#{success})+(#{ignored}) THEN 'success'
         WHEN (#{builds})=(#{pending}) THEN 'pending'
         WHEN (#{builds})=(#{canceled}) THEN 'canceled'
@@ -35,6 +35,14 @@ module CiStatus
       duration_array = all.map(&:duration).compact
       duration_array.reduce(:+).to_i
     end
+
+    def started_at
+      all.minimum(:started_at)
+    end
+
+    def finished_at
+      all.minimum(:finished_at)
+    end
   end
 
   included do
-- 
GitLab