Check exceptional cases when triggering qb. 04/319204/1 sandbox/xuhy/qb_trigger
authorxuhy <huayong.xu@samsung.com>
Thu, 17 Oct 2024 08:41:57 +0000 (16:41 +0800)
committerxuhy <huayong.xu@samsung.com>
Thu, 17 Oct 2024 08:41:57 +0000 (16:41 +0800)
Change-Id: I05d053bcba00d5200c085ab73cd20903eaead80e

gitbuildsys/cmd_qbbuild.py

index 8727546f10a9544aefb720afa3b6c08dc904b82c..0ad3a1f1f964e21a85deef77f84fbc3fd439d033 100644 (file)
@@ -41,27 +41,45 @@ BUILD_REQUEST_XML_TEMPLATE = '''<?xml version='1.0' encoding='UTF-8'?>
 </com.pmease.quickbuild.BuildRequest>
 '''
 
-def trigger_qb(session, url, file_path):
+
+def trigger_qb(session, url, conf_path, file_path):
     """request qb to build"""
     req_url = "%s/rest/build_requests" % (url)
-    log.info("url: %s." % req_url)
-    with open(file_path, 'rb') as fobj:
-        resp = session.post(req_url, data=fobj)
+    log.info(f"request build url: {req_url}")
+
+    resp = {}
+    try:
+        with open(file_path, 'rb') as fobj:
+            resp = session.post(req_url, data=fobj)
+    except requests.exceptions.RequestException as e:
+        log.error(f"Request exception occurred: {e}")
+        return
 
-    conv_text = resp.text
-    log.info("build info: ", conv_text.strip())
+    if resp.status_code != 200:
+        log.error(f"response code: {resp.status_code}")
+        log.error(f"Failed to trigger QB build, url is {url}, configuration path is {conf_path}.")
+    else:
+        log.info(f"build info: {resp.text.strip()}")
 
 
 def request_build(session, url, conf_path, txt_file):
     """generate qb build request xml file."""
     # query configuration id from qb server.
     full_url = "%s/rest/ids?configuration_path=%s" % (url, conf_path)
-    resp = session.get(full_url, verify=False)
-    log.info("response: ", resp.text)
+    log.info(f"query configuration id url is {full_url}")
+    resp = {}
+    try:
+        resp = session.get(full_url, verify=False)
+        if resp.status_code != 200:
+            log.error(f"response code: {resp.status_code}")
+            log.error(f"Failed to query configuration from QB, url is {url}, configuration path is {conf_path}.")
+            return
+    except requests.exceptions.RequestException as e:
+        log.error(f"Request exception occurred: {e}")
+        return
 
     # replace configuration id in template.
     replaced = re.sub("{{configurationId}}", resp.text.strip(), BUILD_REQUEST_XML_TEMPLATE)
-    log.info("replaced: ", replaced)
 
     # read commit id list.
     commit_id_list = ""
@@ -73,11 +91,10 @@ def request_build(session, url, conf_path, txt_file):
 
     # replace commit id list in template.
     replaced = re.sub("{{commitIdList}}", commit_id_list, replaced)
-    log.info("replaced: ", replaced)
 
     # create a tmp dir.
     tmp = utils.Temp(prefix='gbs_', dirn=configmgr.get('tmpdir', 'general'), directory=True)
-    log.info("temp dir: ", tmp.path)
+    log.info(f"temp dir: {tmp.path}")
 
     # write build request into an xml.
     xml_file = tmp.path + '/gbs_buldrequest.xml'
@@ -85,19 +102,21 @@ def request_build(session, url, conf_path, txt_file):
         file.write(replaced)
 
     # trigger qb to build.
-    trigger_qb(session, url, xml_file)
+    trigger_qb(session, url, conf_path, xml_file)
 
 
 def main(args):
     """The main body"""
-    log.info(args.qb_account_conf)
-    log.info(args.qb_conf_path)
-    log.info(args.commit_id_file)
-
     conf_path = args.qb_account_conf
     if (not os.path.isabs(conf_path)):
         conf_path = os.path.abspath(os.path.expanduser(conf_path))
-    log.info(conf_path)
+
+    if not os.path.exists(conf_path):
+        log.error(f"conf path {conf_path} does not exist.")
+        return
+    else:
+        log.info(f"conf path is {conf_path}")
+
     with open(conf_path, 'r') as file:
         lines = file.readlines()
 
@@ -109,14 +128,30 @@ def main(args):
         key, value = line.split('=', 1)
         value = value.strip().strip('\'"')
         config[key] = value
-    log.info(config)
 
-    session = requests.Session()
-    session.auth = (config['user'], config['passwd'])
+    if 'url' not in config or config['url'] == '':
+        log.error(f'url is null in {conf_path}')
+        return
+
+    if 'user' not in config or config['user'] == '':
+        log.error(f'user is null in {conf_path}')
+        return
+
+    if 'passwd' not in config or config['passwd'] == '':
+        log.error(f'password is null in {conf_path}')
+        return
 
     txt_path = args.commit_id_file
     if (not os.path.isabs(txt_path)):
         txt_path = os.path.abspath(os.path.expanduser(txt_path))
-    log.info(txt_path)
+
+    if not os.path.exists(txt_path):
+        log.error(f"txt path {txt_path} does not exist.")
+        return
+    else:
+        log.info(f"txt path is {txt_path}")
+
+    session = requests.Session()
+    session.auth = (config['user'], config['passwd'])
 
     request_build(session, config['url'], args.qb_conf_path, txt_path)