From 7ad311892c444b4170649c36e463234c92d7f6f1 Mon Sep 17 00:00:00 2001
From: Sebastian Frank <frank@basiskonfiguration.de>
Date: Thu, 13 Jun 2019 12:39:12 +0200
Subject: [PATCH] better way to find last push

---
 bootstrap.php | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/bootstrap.php b/bootstrap.php
index b120d39..24707e2 100644
--- a/bootstrap.php
+++ b/bootstrap.php
@@ -43,17 +43,18 @@ if (COCKPIT_ADMIN && !COCKPIT_API_REQUEST) {
       curl_close($ch);
       $unfiltered_deploys = json_decode($unfiltered_deploys);
       $build = $settings['build'];
+      $last_push = '';
+      $last_promote = '';
       if ($unfiltered_deploys && is_array($unfiltered_deploys)) {
         foreach($unfiltered_deploys as $deploy) {
           if ($deploy->{target} == $branch && ($deploy->event == 'push' || ($deploy->event == 'promote' && $deploy->deploy_to == $settings['target']))) {
             // find latest successful build
-            if (!$build) {
-              if ($deploy->event == 'push' && $deploy->status == 'success') {
-                $build = $deploy->number;
-              } else {
-                $build = $deploy->parent;
-              }
+            if (!$last_push && $deploy->event == 'push' && $deploy->status == 'success') {
+              $last_push = $deploy->number;
+            } elseif (!$last_promote) {
+              $last_promote = $deploy->parent;
             }
+            
             $limit--;
             if ($limit>0) {
               $deploys[] = $deploy; 
@@ -62,6 +63,10 @@ if (COCKPIT_ADMIN && !COCKPIT_API_REQUEST) {
         }
       }
 
+      if (!$build) {
+        $build = $last_push ? $last_push : $last_promote;
+      }
+
       // Parse dates and check if any deploy is on building status.
       $building = false;
       foreach ($deploys as $idx => $deploy) {