[git_cl] Extract GitAuthConfigChanger

Makes it easier to extend for following CLs, keeps things organized

Change-Id: I153501ead2ad9592eca94f7fe2bc7344ea6efc22
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5688608
Commit-Queue: Allen Li <ayatane@chromium.org>
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
changes/08/5688608/3
Allen Li 10 months ago committed by LUCI CQ
parent 93d8c0178f
commit 6b6a0ff40b

@ -3666,56 +3666,68 @@ def DownloadGerritHook(force):
def ConfigureGitRepoAuth() -> None:
"""Configure the current Git repo authentication."""
logging.debug('Configuring current Git repo authentication...')
cwd: str = os.getcwd()
cl = Changelist()
c = GitAuthConfigChanger()
c.apply()
# chromium-review.googlesource.com
gerrit_host: str = cl.GetGerritHost()
# chromium
shortname: str = gerrit_host.split('.')[0][:-len('-review')]
# These depend on what the user set for their remote
# https://chromium.googlesource.com/chromium/tools/depot_tools.git
remote_url: str = Changelist().GetRemoteUrl()
parts: urllib.parse.SplitResult = urllib.parse.urlsplit(remote_url)
# https://chromium.googlesource.com/
base_url: str = parts._replace(path='/', query='', fragment='').geturl()
should_use_sso: bool = gerrit_util.ShouldUseSSO(gerrit_host)
# Credential helper
if should_use_sso:
scm.GIT.SetConfig(cwd,
f'credential.{base_url}.helper',
None,
modify_all=True)
else:
scm.GIT.SetConfig(cwd,
f'credential.{base_url}.helper',
'',
modify_all=True)
scm.GIT.SetConfig(cwd,
f'credential.{base_url}.helper',
'luci',
append=True)
# SSO
if should_use_sso:
scm.GIT.SetConfig(cwd, 'protocol.sso.allow', 'always')
scm.GIT.SetConfig(cwd,
f'url.sso://{shortname}/.insteadOf',
base_url,
modify_all=True)
else:
scm.GIT.SetConfig(cwd, 'protocol.sso.allow', None)
scm.GIT.SetConfig(cwd,
f'url.sso://{shortname}/.insteadOf',
None,
modify_all=True)
# Override potential global gitcookie config
scm.GIT.SetConfig(cwd, 'http.gitcookies', '', modify_all=True)
class GitAuthConfigChanger(object):
"""Changes Git auth config as needed for Gerrit."""
def __init__(self):
cl = Changelist()
# chromium-review.googlesource.com
gerrit_host: str = cl.GetGerritHost()
# chromium
self._shortname: str = gerrit_host.split('.')[0][:-len('-review')]
# These depend on what the user set for their remote
# https://chromium.googlesource.com/chromium/tools/depot_tools.git
remote_url: str = cl.GetRemoteUrl()
parts: urllib.parse.SplitResult = urllib.parse.urlsplit(remote_url)
# https://chromium.googlesource.com/
self._base_url: str = parts._replace(path='/', query='',
fragment='').geturl()
self._should_use_sso: bool = gerrit_util.ShouldUseSSO(gerrit_host)
def apply(self):
"""Apply config changes."""
logging.debug('Configuring current Git repo authentication...')
cwd: str = os.getcwd()
# Credential helper
if self._should_use_sso:
scm.GIT.SetConfig(cwd,
f'credential.{self._base_url}.helper',
None,
modify_all=True)
else:
scm.GIT.SetConfig(cwd,
f'credential.{self._base_url}.helper',
'',
modify_all=True)
scm.GIT.SetConfig(cwd,
f'credential.{self._base_url}.helper',
'luci',
append=True)
# SSO
if self._should_use_sso:
scm.GIT.SetConfig(cwd, 'protocol.sso.allow', 'always')
scm.GIT.SetConfig(cwd,
f'url.sso://{self._shortname}/.insteadOf',
base_url,
modify_all=True)
else:
scm.GIT.SetConfig(cwd, 'protocol.sso.allow', None)
scm.GIT.SetConfig(cwd,
f'url.sso://{self._shortname}/.insteadOf',
None,
modify_all=True)
# Override potential global gitcookie config
scm.GIT.SetConfig(cwd, 'http.gitcookies', '', modify_all=True)
class _GitCookiesChecker(object):

Loading…
Cancel
Save