TPE-502 Skip prerelease create if the same is in the queue 50/164950/2
authorhyokeun <hyokeun.jeon@samsung.com>
Fri, 22 Dec 2017 04:04:05 +0000 (13:04 +0900)
committerhyokeun <hyokeun.jeon@samsung.com>
Fri, 22 Dec 2017 04:37:02 +0000 (13:37 +0900)
Change-Id: I6e56d241344d39a82ca1bdef46d293c5bf41ef9e

common/buildtrigger.py
job_pre_release_obs.py

index 87cfa5e..32073d6 100644 (file)
@@ -196,3 +196,21 @@ def remote_jenkins_build_job( url, username, password, jobname, token=None, data
         except Exception as e:
             raise Exception(e)
 
+def get_pending_builds_with_parameter_for_job(job):
+
+    assert job
+    build_queue_data = {}
+    cred = {'url': os.getenv('JENKINS_URL_INTERNAL'), \
+            'username': os.getenv('JENKINS_USER'), \
+            'password': os.getenv('JENKINS_PW')}
+    jenkinsinst = get_jenkins_instance(job, cred)
+    jq = jenkinsinst.get_queue()
+    for pending_build in jq.get_queue_items_for_job(job):
+        build_queue_data[pending_build.queue_id] = []
+        for action in pending_build._data.get('actions', []):
+            if type(action) is dict and 'parameters' in action:
+                for param in action['parameters']:
+                    build_queue_data[pending_build.queue_id].append(param)
+
+    return build_queue_data
+
index a24d4f2..aca13fa 100755 (executable)
@@ -30,7 +30,7 @@ import datetime
 import xml.etree.ElementTree as ElementTree
 
 from common.repomaker import find_files, RepoMaker, RepoMakerError
-from common.buildtrigger import trigger_info, trigger_next
+from common.buildtrigger import trigger_info, trigger_next, get_pending_builds_with_parameter_for_job
 from common.buildservice import BuildService
 from common.backenddb import BackendDB
 from common.prerelease import get_info_from_prerelease_name
@@ -503,6 +503,25 @@ def main(action):
           action - cleanup or create_images
     """
 
+    content = trigger_info(os.getenv("TRIGGER_INFO"))
+
+    try:
+        # Skip this if the same build is in the queue
+        pending_build_queue = get_pending_builds_with_parameter_for_job(os.getenv('JOB_NAME'))
+        for _queue in pending_build_queue:
+            for param in pending_build_queue[_queue]:
+                name = param['name']
+                if name == 'TRIGGER_INFO' and 'value' in param:
+                    pending_info = trigger_info(param['value'], show=False)
+                    if pending_info.get('project') == content.get('project') \
+                        and pending_info.get('event_type') == 'OBS_REPO_PUBLISHED':
+                        print 'SKIP THIS BUILD due to the same build %s is in the queue(%s)' \
+                            % (pending_info.get('project'), _queue)
+                        return -1
+    except Exception as err:
+        print repr(err)
+        print 'Fail to check queue. Going forward accepted.'
+
     print '---[JOB STARTED: %s ]-------------------------' % action
     global buildmonitor_enabled
     buildmonitor_enabled = os.getenv("BUILDMONITOR_ENABLED", "0") != "0"
@@ -519,8 +538,6 @@ def main(action):
     base_path = os.getenv('PATH_REPO_BASE')
     live_repo_base = os.getenv('PATH_LIVE_REPO_BASE')
 
-    content = trigger_info(os.getenv("TRIGGER_INFO"))
-
     project = content.get("project") or content.get("sourceproject")
 
     build = BuildService(obs_api, obs_user, obs_passwd)