Add _get_username method to discover username

This change mitigate this exception:
  File "gerritbot/bot.py", line 154, in change_created
    data['patchSet']['uploader']['name'],
KeyError: 'name'

Change-Id: Ia1f394d6ff0e158df16461cf3150761f6ec12f29
This commit is contained in:
Tristan Cacqueray 2016-05-18 09:40:09 -04:00
parent 033a83b870
commit 2240fc3450
1 changed files with 10 additions and 1 deletions

View File

@ -182,9 +182,18 @@ class Gerrit(threading.Thread):
# Delay before attempting again.
time.sleep(1)
@staticmethod
def _get_username(patchset):
# Try to find username
for key in ('name', 'username', 'email'):
username = patchset['uploader'].get(key)
if username:
return username
return "UNKNOWN USER"
def patchset_created(self, channel, data):
msg = '%s proposed %s %s: %s %s' % (
data['patchSet']['uploader']['name'],
self._get_username(data['patchSet']),
data['change']['project'],
data['change']['branch'],
data['change']['subject'],