Ignore any non gerrit lines 39/76539/5
authorSam Hague <shague@redhat.com>
Mon, 1 Oct 2018 17:04:59 +0000 (13:04 -0400)
committerSam Hague <shague@redhat.com>
Mon, 1 Oct 2018 20:13:37 +0000 (20:13 +0000)
The latest ssh infra changes are now returning output like:
"Warning: Permanently added the RSA host key for IP address" to the
output and that is being parsed incorrectly. So drop any lines that
are not gerrit lines. Those lines will have "grantedOn" in them.

Also added a testcase to test the condition.

Change-Id: Iefc109f28eed491aad9fac7bd9093685cba2348d
Signed-off-by: Sam Hague <shague@redhat.com>
tools/distchanges/gerritquery.py
tools/distchanges/logg.py [new file with mode: 0644]
tools/distchanges/tests/test_gerritquery.py [new file with mode: 0644]

index 4e2c6cdd42cb00f687312931e174b5c3d524b4d2..b6cddc5cdb22fd526cab938ec18994779f6609b8 100644 (file)
@@ -74,12 +74,15 @@ class GerritQuery:
 
     @staticmethod
     def print_safe_encoding(string):
-        if type(string) == unicode:
-            encoding = 'utf-8'
-            if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
-                encoding = sys.stdout.encoding
-            return string.encode(encoding or 'utf-8', 'replace')
-        else:
+        try:
+            if type(string) == unicode:
+                encoding = 'utf-8'
+                if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
+                    encoding = sys.stdout.encoding
+                return string.encode(encoding or 'utf-8', 'replace')
+            else:
+                return str(string)
+        except:
             return str(string)
 
     def run_command_status(self, *argv, **kwargs):
@@ -281,13 +284,14 @@ class GerritQuery:
         :return list: Lines of the JSON
         """
         lines = []
-        for line in changes.split("\n"):
-            if line.find('"type":"error","message"') != -1:
-                logger.warn("there was a query error")
-                continue
-            if line.find('stats') == -1:
+        skipped = 0
+        for i, line in enumerate(changes.split("\n")):
+            if line.find('"grantedOn":') != -1:
                 lines.append(line)
-        logger.debug("get_gerrit_lines: found %d lines", len(lines))
+            else:
+                logger.debug("skipping: {}".format(line))
+                skipped += 1
+        logger.debug("get_gerrit_lines: found {} lines, skipped: {}".format(len(lines), skipped))
         return lines
 
     def get_gerrits(self, project, changeid=None, limit=1, msg=None, status=None, comments=False, commitid=None):
@@ -326,6 +330,6 @@ class GerritQuery:
             return gerrits
         try:
             sorted_gerrits = sorted(gerrits, key=itemgetter('grantedOn'), reverse=True)
-        except KeyError, e:
+        except KeyError as e:
             logger.warn("KeyError exception in %s, %s", project, str(e))
         return sorted_gerrits
diff --git a/tools/distchanges/logg.py b/tools/distchanges/logg.py
new file mode 100644 (file)
index 0000000..b6762c8
--- /dev/null
@@ -0,0 +1,35 @@
+# Copyright (c) 2018 Red Hat, Inc. and others.  All rights reserved.
+#
+# This program and the accompanying materials are made available under the
+# terms of the Eclipse Public License v1.0 which accompanies this distribution,
+# and is available at http://www.eclipse.org/legal/epl-v10.html
+
+import logging
+
+logger = None
+ch = None
+fh = None
+
+
+def debug():
+    ch.setLevel(logging.DEBUG)
+    # logger.setLevel(min([ch.level, fh.level]))
+
+
+class Logger:
+    def __init__(self, console_level=logging.INFO, file_level=logging.DEBUG):
+        global logger
+        global ch
+        global fh
+
+        logger = logging.getLogger()
+        formatter = logging.Formatter('%(asctime)s | %(levelname).3s | %(name)-20s | %(lineno)04d | %(message)s')
+        ch = logging.StreamHandler()
+        ch.setLevel(console_level)
+        ch.setFormatter(formatter)
+        logger.addHandler(ch)
+        fh = logging.FileHandler("/tmp/odltools.txt", "w")
+        fh.setLevel(file_level)
+        fh.setFormatter(formatter)
+        logger.addHandler(fh)
+        logger.setLevel(min([ch.level, fh.level]))
diff --git a/tools/distchanges/tests/test_gerritquery.py b/tools/distchanges/tests/test_gerritquery.py
new file mode 100644 (file)
index 0000000..86daf3a
--- /dev/null
@@ -0,0 +1,33 @@
+# Copyright (c) 2018 Red Hat, Inc. and others.  All rights reserved.
+#
+# This program and the accompanying materials are made available under the
+# terms of the Eclipse Public License v1.0 which accompanies this distribution,
+# and is available at http://www.eclipse.org/legal/epl-v10.html
+
+import logging
+import unittest
+from gerritquery import GerritQuery
+import logg
+
+
+REMOTE_URL = GerritQuery.remote_url
+BRANCH = 'stable/oxygen'
+LIMIT = 10
+QLIMIT = 50
+VERBOSE = 0
+PROJECT = "controller"
+
+
+class TestRequest(unittest.TestCase):
+    def setUp(self):
+        logg.Logger(logging.DEBUG, logging.INFO)
+        self.gerritquery = GerritQuery(REMOTE_URL, BRANCH, QLIMIT, VERBOSE)
+
+    def test_get_gerrits(self):
+        changeid = "I41232350532e56340c1fe9853ef7e74e3aa03359"
+        gerrits = self.gerritquery.get_gerrits(PROJECT, changeid, 1, status="merged")
+        print("{}".format(gerrits))
+
+
+if __name__ == '__main__':
+    unittest.main()