Remove bug assignee dependency on LP E-mail.

* jeepyb/cmd/update_bug.py(set_in_progress): Instead of querying
Launchpad for E-mail addresses matching the change author, look in
Gerrit to find an OpenID and look the user up with the
getByOpenIDIdentifier call.

Change-Id: Ifb457237cd02f5f3db4e95f26c87c08cf698de9a
Reviewed-on: https://review.openstack.org/17488
Reviewed-by: Monty Taylor <mordred@inaugust.com>
Reviewed-by: Clark Boylan <clark.boylan@gmail.com>
Approved: Jeremy Stanley <fungi@yuggoth.org>
Reviewed-by: Jeremy Stanley <fungi@yuggoth.org>
Tested-by: Jenkins
diff --git a/jeepyb/cmd/update_bug.py b/jeepyb/cmd/update_bug.py
index d82a7c1..8dca541 100644
--- a/jeepyb/cmd/update_bug.py
+++ b/jeepyb/cmd/update_bug.py
@@ -19,6 +19,7 @@
 
 from launchpadlib.launchpad import Launchpad
 from launchpadlib.uris import LPNET_SERVICE_ROOT
+import jeepyb.gerritdb
 import os
 import argparse
 import re
@@ -55,15 +56,41 @@
 def set_in_progress(bugtask, launchpad, uploader, change_url):
     """Set bug In progress with assignee being the uploader"""
 
-    # Retrieve uploader from Launchpad. Use email as search key if
-    # provided, and only set if there is a clear match.
+    # Retrieve uploader from Launchpad by correlating Gerrit E-mail
+    # address to OpenID, and only set if there is a clear match.
     try:
         searchkey = uploader[uploader.rindex("(") + 1:-1]
     except ValueError:
         searchkey = uploader
-    persons = launchpad.people.findPerson(text=searchkey)
-    if len(persons) == 1:
-        bugtask.assignee = persons[0]
+
+    # The counterintuitive query is due to odd database schema choices
+    # in Gerrit. For example, an account with a secondary E-mail
+    # address added looks like...
+    # select email_address,external_id from account_external_ids
+    #     where account_id=1234;
+    # +-----------------+-----------------------------------------+
+    # | email_address   | external_id                             |
+    # +-----------------+-----------------------------------------+
+    # | plugh@xyzzy.com | https://login.launchpad.net/+id/fR0bnU1 |
+    # | bar@foo.org     | mailto:bar@foo.org                      |
+    # | NULL            | username:quux                           |
+    # +-----------------+-----------------------------------------+
+    # ...thus we need a join on a secondary query to search against
+    # all the user's configured E-mail addresses.
+    #
+    query = """SELECT t.external_id FROM account_external_ids t
+            INNER JOIN (
+                SELECT t.account_id FROM account_external_ids t
+                WHERE t.email_address = %s )
+            original ON t.account_id = original.account_id
+            AND t.external_id LIKE 'https://%%'"""
+
+    cursor = jeepyb.gerritdb.connect().cursor()
+    cursor.execute(query, searchkey)
+    data = cursor.fetchone()
+    if data:
+        bugtask.assignee = launchpad.people.getByOpenIDIdentifier(
+            identifier=data[0])
 
     bugtask.status = "In Progress"
     bugtask.lp_save()