Browse Source

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
changes/96/318096/3
Tristan Cacqueray 3 years ago
parent
commit
2240fc3450
1 changed files with 10 additions and 1 deletions
  1. 10
    1
      gerritbot/bot.py

+ 10
- 1
gerritbot/bot.py View File

@@ -182,9 +182,18 @@ class Gerrit(threading.Thread):
182 182
             # Delay before attempting again.
183 183
             time.sleep(1)
184 184
 
185
+    @staticmethod
186
+    def _get_username(patchset):
187
+        # Try to find username
188
+        for key in ('name', 'username', 'email'):
189
+            username = patchset['uploader'].get(key)
190
+            if username:
191
+                return username
192
+        return "UNKNOWN USER"
193
+
185 194
     def patchset_created(self, channel, data):
186 195
         msg = '%s proposed %s %s: %s  %s' % (
187
-            data['patchSet']['uploader']['name'],
196
+            self._get_username(data['patchSet']),
188 197
             data['change']['project'],
189 198
             data['change']['branch'],
190 199
             data['change']['subject'],

Loading…
Cancel
Save