Exit current pre-release-create if the same build is in the queue 06/168106/1
authorhyokeun <hyokeun.jeon@samsung.com>
Wed, 24 Jan 2018 06:56:49 +0000 (15:56 +0900)
committerhyokeun <hyokeun.jeon@samsung.com>
Wed, 24 Jan 2018 06:56:49 +0000 (15:56 +0900)
Change-Id: Id9123dd0f8a0a8babb9be4848a9fa00d8d2ba00b

common/buildtrigger.py
job_pre_release_obs.py

index e5149bb..161c702 100644 (file)
@@ -25,6 +25,7 @@ import urllib3
 from common.utils import unicode_to_str
 # remote jenkins build job
 import requests
+import ast
 
 
 class LocalError(Exception):
@@ -197,21 +198,15 @@ 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):
+def get_pending_builds_with_parameter():
 
-    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
+
+    path = '%s/queue/api/python' % (cred['url'])
+    path = '%s?tree=items[id,task[name],actions[parameters[name,value]]]' % path
+    resp = requests.post(path, auth=requests.auth.HTTPBasicAuth(cred['username'], cred['password']))
+    ret_obj = ast.literal_eval(resp.text)
+    return ret_obj.get("items")
 
index aca13fa..6784e84 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, get_pending_builds_with_parameter_for_job
+from common.buildtrigger import trigger_info, trigger_next, get_pending_builds_with_parameter
 from common.buildservice import BuildService
 from common.backenddb import BackendDB
 from common.prerelease import get_info_from_prerelease_name
@@ -497,6 +497,35 @@ def get_unresolvable_broken_packages(unresolvable_broken_failed_status):
 
     return unresolvable_broken_packages
 
+def pending_build_exists(project_name):
+
+    try:
+        pending_build_queue = get_pending_builds_with_parameter()
+        if not pending_build_queue:
+            return False
+        for _queue in pending_build_queue:
+            if 'task' not in _queue or 'name' not in _queue.get('task') \
+                or os.getenv('JOB_NAME') != _queue.get('task').get('name'):
+                continue
+            for action in _queue.get('actions'):
+                if not 'parameters' in action:
+                    continue
+                for param in action.get('parameters'):
+                    name = param.get('name')
+                    value = param.get('value')
+                    if name == 'TRIGGER_INFO' and value:
+                        pending_info = trigger_info(value, show=False)
+                        if pending_info.get('project') == project_name \
+                            and pending_info.get('event_type') == 'OBS_REPO_PUBLISHED':
+                            print 'Found the same %s pending build is in the queue(%s)' \
+                                % (pending_info.get('project'), _queue)
+                            return True
+    except Exception as err:
+        print repr(err)
+        print 'Fail to check queue. Going forward accepted.'
+
+    return False
+
 def main(action):
     """Script entry point.
        Parameters:
@@ -505,22 +534,10 @@ def main(action):
 
     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.'
+    # Skip this if the same build is in the queue
+    if pending_build_exists(content.get('project')) == True:
+        print 'SKIP THIS BUILD due to the same build is in the queue'
+        return 0
 
     print '---[JOB STARTED: %s ]-------------------------' % action
     global buildmonitor_enabled