[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 1 year ago committed by LUCI CQ
parent 93d8c0178f
commit 6b6a0ff40b

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

Loading…
Cancel
Save