From 590d61a2de453b2359c81f7070caae09df6e788d Mon Sep 17 00:00:00 2001
From: Lin Jen-Shin <godfat@godfat.org>
Date: Tue, 8 Nov 2016 02:33:04 +0800
Subject: [PATCH] Also show latest pipeline for ImageForBuildService

---
 app/services/ci/image_for_build_service.rb | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/app/services/ci/image_for_build_service.rb b/app/services/ci/image_for_build_service.rb
index 75d847d5bee..17ce95073cb 100644
--- a/app/services/ci/image_for_build_service.rb
+++ b/app/services/ci/image_for_build_service.rb
@@ -1,11 +1,18 @@
 module Ci
   class ImageForBuildService
     def execute(project, opts)
-      sha = opts[:sha] || ref_sha(project, opts[:ref])
+      ref = opts[:ref]
+      sha = opts[:sha] || ref_sha(project, ref)
 
       pipelines = project.pipelines.where(sha: sha)
-      pipelines = pipelines.where(ref: opts[:ref]) if opts[:ref]
-      image_name = image_for_status(pipelines.status)
+
+      latest_pipeline = if ref
+                          pipelines.latest_for(ref)
+                        else
+                          pipelines.latest
+                        end.first
+
+      image_name = image_for_status(latest_pipeline.status)
 
       image_path = Rails.root.join('public/ci', image_name)
       OpenStruct.new(path: image_path, name: image_name)
-- 
GitLab