aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xlinaro_gerrit.py29
1 files changed, 24 insertions, 5 deletions
diff --git a/linaro_gerrit.py b/linaro_gerrit.py
index 91eed88..e29c646 100755
--- a/linaro_gerrit.py
+++ b/linaro_gerrit.py
@@ -31,9 +31,28 @@ class LinaroGerrit:
# https://gerrit-review.googlesource.com/Documentation/rest-api.html#output
return '\n'.join(string.split('\n')[1:])
+ def get_account(self, username, limit=1):
+ """Get account_id from a given query q and limit result n.
+ """
+ url = ('%s/a/accounts/') % (self.base)
+ payload = {'q': 'username:' + username, 'limit': limit}
+ r = requests.get(url, params=payload, **self.reqargs)
+ log.info("Finding user: %s with url %s", username, r.url)
+ try:
+ a = json.loads(self.strip_gerrit_junk(r.content))
+ if a:
+ for data in a:
+ log.info("Found user: %s", data["_account_id"])
+ return data["_account_id"]
+ else:
+ log.info("user %s not found in gerrit", username)
+ return False
+ except ValueError as e:
+ log.warn(e)
+
def list_keys(self, username):
log.info("Listing keys for user: %s", username)
- url = "%s/a/accounts/%s/sshkeys/" % (self.base, username)
+ url = "%s/a/accounts/%s/sshkeys/" % (self.base, self.get_account(username))
r = requests.get(url, **self.reqargs)
keydict = {}
if r.status_code == 200:
@@ -51,7 +70,7 @@ class LinaroGerrit:
def add_key(self, pubkey, username):
log.debug("Adding pubkey %s to user %s", pubkey, username)
- url = "%s/a/accounts/%s/sshkeys/" % (self.base, username)
+ url = "%s/a/accounts/%s/sshkeys/" % (self.base, self.get_account(username))
if not self.dryrun:
r = requests.post(url, data=pubkey.encode("utf-8"), **self.reqargs)
if r.status_code == 201:
@@ -62,7 +81,7 @@ class LinaroGerrit:
def del_key(self, username, key_id):
log.debug("Deleting key %s by id from user %s", key_id, username)
- url = "%s/a/accounts/%s/sshkeys/%i" % (self.base, username, key_id)
+ url = "%s/a/accounts/%s/sshkeys/%i" % (self.base, self.get_account(username), key_id)
if not self.dryrun:
r = requests.delete(url, **self.reqargs)
if r.status_code == 204:
@@ -104,7 +123,7 @@ class LinaroGerrit:
def add_group_member(self, groupname, username):
log.debug("Adding %s to group %s", username, groupname)
- url = "%s/a/groups/%s/members/%s" % (self.base, groupname, username)
+ url = "%s/a/groups/%s/members/%s" % (self.base, groupname, self.get_account(username))
if not self.dryrun:
r = requests.put(url, **self.reqargs)
if r.status_code == 201 or r.status_code == 200:
@@ -117,7 +136,7 @@ class LinaroGerrit:
def delete_group_member(self, groupname, username):
log.debug("Deleting %s from group %s", username, groupname)
- url = "%s/a/groups/%s/members/%s" % (self.base, groupname, username)
+ url = "%s/a/groups/%s/members/%s" % (self.base, groupname, self.get_account(username))
if not self.dryrun:
r = requests.delete(url, **self.reqargs)
if r.status_code == 204: