From c5b7ca4f7fea1b066dd6a355a93d3d35d88bc202 Mon Sep 17 00:00:00 2001 From: Allen Li Date: Wed, 10 Jul 2024 21:09:30 +0000 Subject: [PATCH] [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 Reviewed-by: Yiwei Zhang --- git_cl.py | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/git_cl.py b/git_cl.py index 572feef43..d89b27cf7 100755 --- a/git_cl.py +++ b/git_cl.py @@ -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."""