[git_cl] Extract construction inference

Potentially makes it easier to test (needs more deps extracted and
injected though).

Change-Id: I8c551b39c97ec95291995e614c760073b10a4615
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5689682
Commit-Queue: Allen Li <ayatane@chromium.org>
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
changes/82/5689682/3
Allen Li 12 months ago committed by LUCI CQ
parent 902a9de39e
commit c5b7ca4f7f

@ -3666,30 +3666,51 @@ def DownloadGerritHook(force):
def ConfigureGitRepoAuth() -> None:
"""Configure the current Git repo authentication."""
c = GitAuthConfigChanger()
c = GitAuthConfigChanger.infer_and_create()
c.apply()
class GitAuthConfigChanger(object):
"""Changes Git auth config as needed for Gerrit."""
def __init__(self):
cl = Changelist()
def __init__(
self,
*,
host_shortname: str,
remote_url: str,
):
"""Create a new GitAuthConfigChanger.
Args:
host_shortname: Gerrit host shortname, e.g., chromium
remote_url: Git repository's remote URL, e.g.,
https://chromium.googlesource.com/chromium/tools/depot_tools.git
"""
self._shortname: str = host_shortname
parts: urllib.parse.SplitResult = urllib.parse.urlsplit(remote_url)
# Base URL looks like https://chromium.googlesource.com/
self._base_url: str = parts._replace(path='/', query='',
fragment='').geturl()
self._should_use_sso: bool = gerrit_util.ShouldUseSSO(gerrit_host)
@classmethod
def infer_and_create(cls) -> 'GitAuthConfigChanger':
"""Create a GitAuthConfigChanger by inferring from env."""
cl = Changelist()
# chromium-review.googlesource.com
gerrit_host: str = cl.GetGerritHost()
# chromium
self._shortname: str = gerrit_host.split('.')[0][:-len('-review')]
host_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)
return cls(
host_shortname=host_shortname,
remote_url=remote_url,
)
def apply(self):
"""Apply config changes."""

Loading…
Cancel
Save