From 55b9004e186cfaec0aba4f6c5eaac74d89b6e46b Mon Sep 17 00:00:00 2001
From: Pavel Korshunov <pavel.korshunov@idiap.ch>
Date: Thu, 29 Mar 2018 15:23:35 +0200
Subject: [PATCH] cancel duplicated pipelines, wait before quering pipelines

---
 release/release_bob.py | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/release/release_bob.py b/release/release_bob.py
index e7a1037..7b8e0e5 100755
--- a/release/release_bob.py
+++ b/release/release_bob.py
@@ -135,21 +135,33 @@ def commit_files(gitpkg, files_list, message='Updated files', dry_run=False):
         gitpkg.commits.create(data)
 
 
+def get_last_nonskip_pipeline(gitpkg, before_last=False):
+    # sleep for 10 seconds to ensure that if a pipeline was just submitted,
+    # we can retrieve it
+    time.sleep(10)
+    if before_last:
+        # take the pipeline before the last
+        return gitpkg.pipelines.list(per_page=2, page=1)[1]
+    else:
+        # otherwise take the last pipeline
+        return gitpkg.pipelines.list(per_page=1, page=1)[0]
+
+
 def just_build_package(gitpkg, dry_run=False):
     # we assume the last pipeline is with commit [skip ci]
     # so, we take the pipeline that can be re-built, which the previous to the last one
-    last_pipeline = gitpkg.pipelines.list(per_page=2, page=1)[1]
+    last_pipeline = get_last_nonskip_pipeline(gitpkg, before_last=True)
 
     # check that the chosen pipeline is the one we are looking for
     latest_tag_name = gitpkg.tags.list(per_page=1, page=1)[0].name
     # the pipeline should be the one built for the latest tag, so check if it is the correct choice
     if last_pipeline.ref != latest_tag_name:
-        raise ValueError('While deploying package, found pipeline {0} but it does not match '
-                         'the latest tag {1}'.format(last_pipeline.id, latest_tag_name))
+        raise ValueError('While deploying {0}, found pipeline {1} but it does not match '
+                         'the latest tag {2}'.format(gitpkg.name, last_pipeline.id, latest_tag_name))
     # the pipeline should have succeeded, otherwise we cannot release
     if last_pipeline.status != 'success':
-        raise ValueError('While deploying package, found pipeline {0} but its status '
-                         'is "{1}" instead of the expected "sucess"'.format(last_pipeline.id, last_pipeline.status))
+        raise ValueError('While deploying {0}, found pipeline {1} but its status is "{2}" instead '
+                         'of the expected "sucess"'.format(gitpkg.name, last_pipeline.id, last_pipeline.status))
 
     print("Retrying pipeline {0}".format(last_pipeline.id))
     if not dry_run:
@@ -159,12 +171,7 @@ def just_build_package(gitpkg, dry_run=False):
 def wait_for_pipeline_to_finish(gitpkg, tag, dry_run=False):
     sleep_step = 30
     max_sleep = 60 * 60  # one hour
-    if tag == 'none':
-        # take the pipeline before the last
-        pipeline = gitpkg.pipelines.list(per_page=2, page=1)[1]
-    else:
-        # otherwise just take the last pipeline
-        pipeline = gitpkg.pipelines.list(per_page=1, page=1)[0]
+    pipeline = get_last_nonskip_pipeline(gitpkg, before_last=True)
 
     pipeline_id = pipeline.id
 
@@ -192,6 +199,12 @@ def wait_for_pipeline_to_finish(gitpkg, tag, dry_run=False):
     print('Pipeline {0} of package {1} succeeded. Continue processing.'.format(pipeline_id, gitpkg.name))
 
 
+def cancel_last_pipeline(gitpkg):
+    pipeline = get_last_nonskip_pipeline(gitpkg)
+    print('Cancelling the last pipeline {0} of project {1}'.format(pipeline.id, gitpkg.name))
+    pipeline.cancel()
+
+
 def release_package(gitpkg, tag_name, tag_comments_list, dry_run=False):
     # if there is nothing to release, just rebuild the package
     if tag_name == 'none':
@@ -206,6 +219,9 @@ def release_package(gitpkg, tag_name, tag_comments_list, dry_run=False):
     # commit and push changes
     commit_files(gitpkg, {'README.rst': readme_content, 'version.txt': version_number},
                  'Increased stable version to %s' % version_number, dry_run)
+    if not dry_run:
+        # cancel running the pipeline triggered by the last commit
+        cancel_last_pipeline(gitpkg)
 
     # 2. Tag package with new tag and push
     print("Creating tag {}".format(tag_name))
-- 
GitLab