From b4ef158a63807c850c1a17c75d66cd166c309047 Mon Sep 17 00:00:00 2001
From: Lin Jen-Shin <godfat@godfat.org>
Date: Thu, 27 Oct 2016 00:48:32 +0800
Subject: [PATCH] Still show status from pipelines, see:

https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/7034#note_17397201
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/7034#note_17397461
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6801#note_17468470
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/7034#note_17482654
---
 app/helpers/ci_status_helper.rb              | 19 ++++++++++--------
 app/models/commit.rb                         | 21 +++++++++++++-------
 app/views/projects/commits/_commit.html.haml |  4 ++--
 3 files changed, 27 insertions(+), 17 deletions(-)

diff --git a/app/helpers/ci_status_helper.rb b/app/helpers/ci_status_helper.rb
index 7d1b41b8fbe..7851949ccf0 100644
--- a/app/helpers/ci_status_helper.rb
+++ b/app/helpers/ci_status_helper.rb
@@ -54,15 +54,18 @@ module CiStatusHelper
     custom_icon(icon_name)
   end
 
-  def render_commit_ref_status(commit, ref = nil, **args)
-    pipeline = commit.pipelines_for(ref).last
-    render_pipeline_status(pipeline, **args)
-  end
-
-  def render_commit_status(commit, tooltip_placement: 'auto left')
+  def render_commit_status(commit, ref: nil, tooltip_placement: 'auto left')
     project = commit.project
-    path = pipelines_namespace_project_commit_path(project.namespace, project, commit)
-    render_status_with_link('commit', commit.status, path, tooltip_placement: tooltip_placement)
+    path = pipelines_namespace_project_commit_path(
+      project.namespace,
+      project,
+      commit)
+
+    render_status_with_link(
+      'commit',
+      commit.status_for(ref),
+      path,
+      tooltip_placement: tooltip_placement)
   end
 
   def render_pipeline_status(pipeline, tooltip_placement: 'auto left')
diff --git a/app/models/commit.rb b/app/models/commit.rb
index 02e06657306..337b7445b46 100644
--- a/app/models/commit.rb
+++ b/app/models/commit.rb
@@ -51,12 +51,14 @@ class Commit
   end
 
   attr_accessor :raw
+  attr_reader :statuses
 
   def initialize(raw_commit, project)
     raise "Nil as raw commit passed" unless raw_commit
 
     @raw = raw_commit
     @project = project
+    @statuses = {}
   end
 
   def id
@@ -225,17 +227,22 @@ class Commit
     )
   end
 
-  def pipelines_for(ref)
-    project.pipelines.where(sha: sha, ref: ref)
-  end
-
   def pipelines
-    @pipeline ||= project.pipelines.where(sha: sha)
+    project.pipelines.where(sha: sha)
   end
 
   def status
-    return @status if defined?(@status)
-    @status ||= pipelines.status
+    status_for(nil)
+  end
+
+  def status_for(ref)
+    if statuses.key?(ref)
+      statuses[ref]
+    elsif ref
+      statuses[ref] = pipelines.where(ref: ref).status
+    else
+      statuses[ref] = pipelines.status
+    end
   end
 
   def revert_branch_name
diff --git a/app/views/projects/commits/_commit.html.haml b/app/views/projects/commits/_commit.html.haml
index 00bf812f33f..9303243b119 100644
--- a/app/views/projects/commits/_commit.html.haml
+++ b/app/views/projects/commits/_commit.html.haml
@@ -20,13 +20,13 @@
             = commit.short_id
           - if commit.status
             .visible-xs-inline
-              = render_commit_ref_status(commit, ref)
+              = render_commit_status(commit, ref: ref)
           - if commit.description?
             %a.text-expander.hidden-xs.js-toggle-button ...
 
         .commit-actions.hidden-xs
           - if commit.status
-            = render_commit_ref_status(commit, ref)
+            = render_commit_status(commit, ref: ref)
           = clipboard_button(clipboard_text: commit.id)
           = link_to commit.short_id, namespace_project_commit_path(project.namespace, project, commit), class: "commit-short-id btn btn-transparent"
           = link_to_browse_code(project, commit)
-- 
GitLab