auth: Hoist exception handling in GerritAuthenticator

This moves the handling to the same level as
`raise GitLoginRequiredError()` so we can check for other exit codes
and raise other errors.

Bug: 442666611
Change-Id: Idbb34d6549b47b715bf59d6720362293d5c28039
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6912689
Commit-Queue: Allen Li <ayatane@chromium.org>
Reviewed-by: Gavin Mak <gavinmak@google.com>
This commit is contained in:
Allen Li
2025-09-03 15:31:20 -07:00
committed by LUCI CQ
parent 05b5e754e4
commit 42cfc1ef22

45
auth.py
View File

@@ -229,7 +229,13 @@ class GerritAuthenticator(object):
Raises:
GitLoginRequiredError if user interaction is required.
"""
access_token = self._get_luci_auth_token()
try:
access_token = self._get_luci_auth_token()
except subprocess2.CalledProcessError as e:
# subprocess2.CalledProcessError.__str__ nicely formats
# stdout/stderr.
logging.error('git-credential-luci failed: %s', e)
access_token = None
if access_token:
return access_token
logging.debug('Failed to create access token')
@@ -237,25 +243,18 @@ class GerritAuthenticator(object):
def _get_luci_auth_token(self, use_id_token=False) -> Optional[str]:
logging.debug('Running git-credential-luci')
try:
# TODO(crbug.com/442666611): depot_tools doesn't support
# ReAuth creds from the helper yet.
env = os.environ.copy()
env['LUCI_ENABLE_REAUTH'] = '0'
out, err = subprocess2.check_call_out(
['git-credential-luci', 'get'],
stdin=subprocess2.DEVNULL,
stdout=subprocess2.PIPE,
stderr=subprocess2.PIPE,
env=env)
logging.debug('git-credential-luci stderr:\n%s', err)
for line in out.decode().splitlines():
if line.startswith('password='):
return line[len('password='):].rstrip()
logging.error('git-credential-luci did not return a token')
return None
except subprocess2.CalledProcessError as e:
# subprocess2.CalledProcessError.__str__ nicely formats
# stdout/stderr.
logging.error('git-credential-luci failed: %s', e)
return None
# TODO(crbug.com/442666611): depot_tools doesn't support
# ReAuth creds from the helper yet.
env = os.environ.copy()
env['LUCI_ENABLE_REAUTH'] = '0'
out, err = subprocess2.check_call_out(['git-credential-luci', 'get'],
stdin=subprocess2.DEVNULL,
stdout=subprocess2.PIPE,
stderr=subprocess2.PIPE,
env=env)
logging.debug('git-credential-luci stderr:\n%s', err)
for line in out.decode().splitlines():
if line.startswith('password='):
return line[len('password='):].rstrip()
logging.error('git-credential-luci did not return a token')
return None