Skip to content

Instantly share code, notes, and snippets.

@glombard
Created December 1, 2014 18:37
Show Gist options
  • Save glombard/d90a4953405bea68732d to your computer and use it in GitHub Desktop.
Save glombard/d90a4953405bea68732d to your computer and use it in GitHub Desktop.
Get the Gerrit users emails as a string list using ssh and gsql query.
class GerritUsersListProvider(object):
def get_users(self):
"""Gets the Gerrit users emails as a string list."""
server = os.environ.get('GERRIT_SERVER', GERRIT_SERVER)
logging.debug('Querying all user accounts from ' + server)
text = subprocess.check_output([
'ssh', '-p', '29418', server, 'gerrit', 'gsql', '--format',
'JSON_SINGLE', '-c', "'select preferred_email from accounts'"])
accounts_json = json.loads(text)
return self.json_to_list(accounts_json)
def json_to_list(self, accounts_json):
"""Converts the JSON returned by Gerrit to a simple array of emails."""
users = [row['columns']['preferred_email']
for row in accounts_json
if 'columns' in row and 'preferred_email' in row['columns']]
return users
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment