From 152f82eaea0dc41dd3898da7da5c46aa3aeb6610 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9my=20Coutable?= <remy@rymai.me>
Date: Thu, 20 Aug 2020 16:30:13 +0200
Subject: [PATCH] Ensure we use a finite loop in scripts/trigger-build
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Rémy Coutable <remy@rymai.me>
---
 scripts/trigger-build | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/scripts/trigger-build b/scripts/trigger-build
index 633e4dda80819..8edf4bb57f73d 100755
--- a/scripts/trigger-build
+++ b/scripts/trigger-build
@@ -321,8 +321,6 @@ module Trigger
     INTERVAL = 60 # seconds
     MAX_DURATION = 3600 * 3 # 3 hours
 
-    attr_reader :project, :id
-
     def self.unscoped_class_name
       name.split('::').last
     end
@@ -334,13 +332,11 @@ module Trigger
     def initialize(project, id)
       @project = project
       @id = id
-      @start = Time.now.to_i
+      @start_time = Time.now.to_i
     end
 
     def wait!
-      loop do
-        raise "#{self.class.unscoped_class_name} timed out after waiting for #{duration} minutes!" if timeout?
-
+      (MAX_DURATION / INTERVAL).times do
         case status
         when :created, :pending, :running
           print "."
@@ -354,14 +350,12 @@ module Trigger
 
         STDOUT.flush
       end
-    end
 
-    def timeout?
-      Time.now.to_i > (@start + MAX_DURATION)
+      raise "#{self.class.unscoped_class_name} timed out after waiting for #{duration} minutes!"
     end
 
     def duration
-      (Time.now.to_i - @start) / 60
+      (Time.now.to_i - start_time) / 60
     end
 
     def status
@@ -372,6 +366,10 @@ module Trigger
       # timeout anyway.
       :running
     end
+
+    private
+
+    attr_reader :project, :id, :start_time
   end
 
   Job = Class.new(Pipeline)
-- 
GitLab