Browse Source

[gerrit_util] Add explicit authenticator param

We need this to figure out which authenticator to use, so we need to
override the initial "bootstrap" authenticator.

Bug: b/348024314
Change-Id: I52c5b1db83bc4e2a0a1ec2a07155d352b593cde4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5651288
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
Commit-Queue: Allen Li <ayatane@chromium.org>
Allen Li 1 year ago
parent
commit
24e9380bc5
1 changed files with 13 additions and 4 deletions
  1. 13 4
      gerrit_util.py

+ 13 - 4
gerrit_util.py

@@ -826,7 +826,9 @@ def CreateHttpConn(host,
                    reqtype='GET',
                    reqtype='GET',
                    headers: Optional[Dict[str, str]] = None,
                    headers: Optional[Dict[str, str]] = None,
                    body: Optional[Dict] = None,
                    body: Optional[Dict] = None,
-                   timeout=300) -> HttpConn:
+                   timeout=300,
+                   *,
+                   authenticator: Optional[Authenticator] = None) -> HttpConn:
     """Opens an HTTPS connection to a Gerrit service, and sends a request."""
     """Opens an HTTPS connection to a Gerrit service, and sends a request."""
     headers = headers or {}
     headers = headers or {}
     bare_host = host.partition(':')[0]
     bare_host = host.partition(':')[0]
@@ -850,7 +852,8 @@ def CreateHttpConn(host,
                     req_headers=headers,
                     req_headers=headers,
                     req_body=rendered_body)
                     req_body=rendered_body)
 
 
-    authenticator = Authenticator.get()
+    if authenticator is None:
+        authenticator = Authenticator.get()
     # TODO(crbug.com/1059384): Automatically detect when running on cloudtop.
     # TODO(crbug.com/1059384): Automatically detect when running on cloudtop.
     if isinstance(authenticator, GceAuthenticator):
     if isinstance(authenticator, GceAuthenticator):
         print('If you\'re on a cloudtop instance, export '
         print('If you\'re on a cloudtop instance, export '
@@ -1658,7 +1661,11 @@ class EmailRecord(TypedDict):
     preferred: bool  # This should be NotRequired[bool] in 3.11+
     preferred: bool  # This should be NotRequired[bool] in 3.11+
 
 
 
 
-def GetAccountEmails(host, account_id='self') -> Optional[List[EmailRecord]]:
+def GetAccountEmails(host,
+                     account_id='self',
+                     *,
+                     authenticatator: Optional[Authenticator] = None
+                     ) -> Optional[List[EmailRecord]]:
     """Returns all emails for this account, and an indication of which of these
     """Returns all emails for this account, and an indication of which of these
     is preferred.
     is preferred.
 
 
@@ -1672,7 +1679,9 @@ def GetAccountEmails(host, account_id='self') -> Optional[List[EmailRecord]]:
 
 
     Returns None if account is not found (i.e. Gerrit returned 404).
     Returns None if account is not found (i.e. Gerrit returned 404).
     """
     """
-    conn = CreateHttpConn(host, '/accounts/%s/emails' % account_id)
+    conn = CreateHttpConn(host,
+                          '/accounts/%s/emails' % account_id,
+                          authenticator=authenticator)
     resp = ReadHttpJsonResponse(conn, accept_statuses=[200, 404])
     resp = ReadHttpJsonResponse(conn, accept_statuses=[200, 404])
     if resp is None:
     if resp is None:
         return None
         return None