summaryrefslogtreecommitdiff
path: root/pygerrit/client.py
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@sonymobile.com>2012-10-16 18:47:38 +0900
committerDavid Pursehouse <david.pursehouse@sonymobile.com>2012-10-17 13:29:12 +0900
commit947d08f46a82b68aa5466eb3792902ae4dba36ed (patch)
treea9e882b16e3fd0601448078517bcbae36c3f5a6c /pygerrit/client.py
parentaf8071f6eb09a401d87420048e8c4f9dd9f9679d (diff)
downloadpygerrit-947d08f46a82b68aa5466eb3792902ae4dba36ed.tar.gz
Add query functionality
Add a method that calls `gerrit query` and returns the results as a list of Change objects. Change-Id: Ie58a61e569d457c56109e34041e9db95147de91c
Diffstat (limited to 'pygerrit/client.py')
-rw-r--r--pygerrit/client.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/pygerrit/client.py b/pygerrit/client.py
index b501b92..5b1a24d 100644
--- a/pygerrit/client.py
+++ b/pygerrit/client.py
@@ -24,10 +24,13 @@ THE SOFTWARE.
"""
+from json import JSONDecoder
from Queue import Queue, Empty, Full
+from pygerrit import escape_string
from pygerrit.error import GerritError
from pygerrit.events import GerritEventFactory
+from pygerrit.models import Change
from pygerrit.ssh import GerritSSHClient
from pygerrit.stream import GerritStream
@@ -42,6 +45,37 @@ class GerritClient(object):
self._stream = None
self._ssh_client = GerritSSHClient(host)
+ def query(self, term):
+ """ Run `gerrit query` with the given term.
+
+ Return a list of results as `Change` objects.
+
+ """
+ results = []
+ command = ["query", "--current-patch-set", "--all-approvals",
+ "--format JSON", "--commit-message"]
+ if isinstance(term, list):
+ command += [escape_string(" ".join(term))]
+ else:
+ command += [escape_string(term)]
+ _stdin, stdout, _stderr = self._ssh_client.run_gerrit_command(command)
+ decoder = JSONDecoder()
+ for line in stdout.read().splitlines():
+ # Gerrit's response to the query command contains one or more
+ # lines of JSON-encoded strings. The last one is a status
+ # dictionary containing the key "type" whose value indicates
+ # whether or not the operation was successful.
+ # According to http://goo.gl/h13HD it should be safe to use the
+ # presence of the "type" key to determine whether the dictionary
+ # represents a change or if it's the query status indicator.
+ data = decoder.decode(line)
+ if "type" in data:
+ if data["type"] == "error":
+ raise GerritError("Query error: %s" % data["message"])
+ else:
+ results.append(Change(data))
+ return results
+
def start_event_stream(self):
""" Start streaming events from `gerrit stream-events`. """
if not self._stream: