Changing the exit code of scm check script.
authorHuang Hao <hao.h.huang@intel.com>
Tue, 9 Sep 2014 10:16:39 +0000 (18:16 +0800)
committerHuang Hao <hao.h.huang@intel.com>
Wed, 10 Sep 2014 03:57:59 +0000 (11:57 +0800)
If scm check/update found errors in scm text, it should return
non-zero, otherwise it should exit with 0.

Removing the try/catch block of GerritError, because if this
error happens, then we can't see comments from Gerrit. So just
keep the traceback in Jenkins console, it's better for debugging.

Returns 0 if http status code is 2XX, otherwise it means error
happens, then we should return non-zero.

Change-Id: I4cb7c01ad95e6185c1236e8933d59f6005ad8b8f
Fixes: #2106

job_monitor_scm_meta_git.py

index f574e03..2a5ac44 100755 (executable)
@@ -11,9 +11,7 @@ from distutils.sysconfig import get_python_lib
 
 sys.path.insert(0, get_python_lib())
 
-import requests
-
-from common.gerrit import Gerrit, GerritError, get_gerrit_event
+from common.gerrit import Gerrit, get_gerrit_event
 from common.git import clone_gitproject, fetch_change
 from common.iris_rest_client import IrisRestClient
 
@@ -33,6 +31,13 @@ GERRIT_SSHPORT = os.getenv('GERRIT_SSHPORT')
 GERRIT_SILENT_MODE = int(os.getenv('GERRIT_SILENT_MODE'))
 
 
+def is_http_ok(status):
+    """
+    Returns True if HTTP status is OK
+    """
+    return status >= 200 and status < 300
+
+
 def scm_check(client, gerrit, events, domains, gittrees):
     """
     Print scm check information and return errors, check successfully
@@ -43,27 +48,15 @@ def scm_check(client, gerrit, events, domains, gittrees):
     print result
     print '########################################'
 
-    if result['status'] == 200:
-        try:
-            gerrit.review(commit=events['patchset_revision'], verified=1)
-            return 0
-        except GerritError, err:
-            print >> sys.stderr, 'Error posting review comment '\
-                                 'back to Gerrit: %s' % str(err)
-            # return 1 if this exception is not caused by invalid commit
-            if 'no such patch set' not in str(err):
-                return 1
-    elif result['status'] in [406, 500]:
-        try:
-            gerrit.review(commit=events['patchset_revision'],
-                          message=result['detail'], verified=-1)
-            return 0
-        except GerritError, err:
-            print >> sys.stderr, 'Error posting review comment '\
-                                 'back to Gerrit: %s' % str(err)
-            # return 1 if this exception is not caused by invalid commit
-            if 'no such patch set' not in str(err):
-                return 1
+    if is_http_ok(result['status']):
+        gerrit.review(commit=events['patchset_revision'],
+                      message="SCM syntax & semantic check OK",
+                      verified=1)
+        return 0
+
+    gerrit.review(commit=events['patchset_revision'],
+                  message=result['detail'],
+                  verified=-1)
     return 1
 
 
@@ -73,10 +66,7 @@ def scm_update(client, domains, gittrees):
     result = client.scm_update(domains, gittrees)
     print result
     print '########################################'
-
-    if result['status'] == 200:
-        return 0
-    return 1
+    return 0 if is_http_ok(result['status']) else 1
 
 
 def main():
@@ -117,5 +107,6 @@ def main():
     else:
         return 1
 
+
 if __name__ == '__main__':
     sys.exit(main())