BUGFIX: python-jenkinsapi limitation 24/136724/1
authorhyokeun <hyokeun.jeon@samsung.com>
Mon, 3 Jul 2017 04:45:07 +0000 (13:45 +0900)
committerhyokeun <hyokeun.jeon@samsung.com>
Mon, 3 Jul 2017 04:46:52 +0000 (13:46 +0900)
Problem: get_job() uses jenkins's system url which differs from internal url.
Todo: Use api/python requests instead.

Change-Id: I06a2297cc223574b3729d97c6b99531e144fc12c

job_submit.py

index 28c09a1..9d8acad 100755 (executable)
@@ -31,6 +31,7 @@ import xml.etree.cElementTree as ElementTree
 from xml.sax.saxutils import escape
 from time import sleep
 import datetime
+import requests
 
 from osc import core
 from gitbuildsys.errors import ObsError
@@ -617,17 +618,40 @@ def entry(option):
     # First store current build and all the rest queued builds
     build_queue = []
     job = os.getenv('JOB_NAME')
-    cred = {'url': os.getenv('REMOTE_JENKINS_URL'), \
-            'username': os.getenv('REMOTE_JENKINS_USER'), \
-            'password': os.getenv('REMOTE_JENKINS_PW')}
+    cred = {'url': os.getenv('JENKINS_URL_INTERNAL'), \
+            'username': os.getenv('JENKINS_USER'), \
+            'password': os.getenv('JENKINS_PW')}
     jenkinsinst = get_jenkins_instance(job, cred)
-    curr_build = jenkinsinst.get_job(job).get_build(int(os.getenv('BUILD_NUMBER')))
-    up_id = curr_build.get_upstream_build_number()
-    if up_id is None: up_id = 0
+    try:
+        curr_build = jenkinsinst.get_job(job).get_build(int(os.getenv('BUILD_NUMBER')))
+    except Exception as err:
+        print str(err)
+        curr_build = None
+
     curr_data = {}
-    for k in curr_build.get_actions()['parameters']:
-        curr_data[k['name']] = k['value']
-    build_queue.append({'%d-%d' % (up_id, curr_build._data['queueId']): curr_data})
+    up_id = 0
+    if curr_build:
+        up_id = curr_build.get_upstream_build_number()
+        if up_id is None:
+            up_id = 0
+        for k in curr_build.get_actions()['parameters']:
+            curr_data[k['name']] = k['value']
+        build_queue.append({'%d-%d' % (up_id, curr_build._data['queueId']): curr_data})
+    else:
+        #FIXME: python-jenkinsapi fail to retrive internal url. Use api/python call instead.
+        path = '%s/job/%s/%s/api/python' % (cred['url'], os.getenv('JOB_NAME'), os.getenv('BUILD_NUMBER'))
+        path = path.replace('://', '://%s:%s@' % (cred['username'], cred['password']))
+        resp = requests.get(path)
+        ret_obj = ast.literal_eval(resp.text)
+        for a in ret_obj['actions']:
+            if 'causes' in a:
+                if up_id == 0 and 'upstreamBuild' in a['causes'][0]:
+                    up_id = int(a['causes'][0]['upstreamBuild'])
+            if 'parameters' in a:
+                for p in a['parameters']:
+                    curr_data[p['name']] = p['value']
+        build_queue.append({'%d-%d' % (up_id, int(ret_obj['queueId'])): curr_data})
+
     #TODO: Keep original process
     if up_id != 0:
         jq = jenkinsinst.get_queue()
@@ -919,9 +943,9 @@ def main(build_type, build, event, sr_count):
         trigger_next("BUILD-MONITOR_%d" % sr_count, bm_data)
 
 if __name__ == '__main__':
-    try:
+    #try:
         sys.exit(entry(sys.argv))
-    except Exception as err:
-        print err
-        sys.exit(1)
+    #except Exception as err:
+    #    print err
+    #    sys.exit(1)