Skip to content

Commit 47ca83c

Browse files
committed
scm.git: add support for cloning private repo (bug 2037928) r=sheehan,shtrom
- add entry to clone test repo locally - update scm.git to use authenticated URL - move existing path authentication functionality to reusable method Pull request: #1138
1 parent a0e035e commit 47ca83c

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

src/lando/main/scm/git.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ def __init__(self, path: str, default_branch: str = "main", **kwargs):
7777
self.default_branch = default_branch
7878
super().__init__(path)
7979

80+
@staticmethod
81+
def authenticate_path_if_possible(url: str) -> str:
82+
"""Return authenticated URL if it is a GitHub URL."""
83+
if GitHub.is_supported_url(url):
84+
return GitHub(url).authenticated_url
85+
return url
86+
8087
@classmethod
8188
@override
8289
def scm_type(cls) -> SCMType:
@@ -91,9 +98,11 @@ def scm_name(cls) -> str:
9198

9299
@override
93100
def clone(self, source: str):
94-
"""Clone a repository from a source."""
101+
"""Clone a repository from a source (pull_path)."""
102+
pull_path = self.authenticate_path_if_possible(source)
103+
95104
# When cloning, self.path doesn't exist yet, so we need to use another CWD.
96-
self._git_run("clone", source, self.path, cwd="/")
105+
self._git_run("clone", pull_path, self.path, cwd="/")
97106
self._git_run("checkout", self.default_branch, cwd=self.path)
98107
self._git_repo_config()
99108

@@ -121,8 +130,7 @@ def push(
121130
if force_push:
122131
push_command += ["--force"]
123132

124-
if GitHub.is_supported_url(push_path):
125-
push_path = GitHub(push_path).authenticated_url
133+
push_path = self.authenticate_path_if_possible(push_path)
126134

127135
push_command += [push_path]
128136

@@ -466,6 +474,9 @@ def update_repo(
466474

467475
self.clean_repo(attributes_override=attributes_override)
468476
# Fetch all refs at the given pull_path, and overwrite the `origin` references.
477+
478+
pull_path = self.authenticate_path_if_possible(pull_path)
479+
469480
self._git_run(
470481
"fetch",
471482
"--prune",

src/lando/main/tests/test_scm_helpers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from pathlib import Path
44
from typing import Callable
5+
from unittest import mock
56

67
import pytest
78

@@ -752,3 +753,23 @@ def test_strip_git_version_info_lines():
752753
"blah",
753754
"blah",
754755
]
756+
757+
758+
@pytest.mark.parametrize(
759+
"url,is_supported_url,result",
760+
(
761+
("some_supported_url", True, "authenticated_url"),
762+
("some_unsupported_url", False, "some_unsupported_url"),
763+
),
764+
)
765+
@mock.patch("lando.main.scm.git.GitHub")
766+
def test_GitSCM__authenticate_path_if_possible(github, is_supported_url, url, result):
767+
mock_github = mock.MagicMock()
768+
type(mock_github).authenticated_url = mock.PropertyMock(return_value=result)
769+
github.return_value = mock_github
770+
github.is_supported_url.return_value = is_supported_url
771+
772+
authenticated_url = GitSCM.authenticate_path_if_possible(url)
773+
assert github.is_supported_url.call_count == 1
774+
github.is_supported_url.assert_called_with(url)
775+
assert authenticated_url == result

src/lando/utils/management/commands/create_environment_repos.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727
"scm_type": GitSCM.scm_type(),
2828
"pr_enabled": True,
2929
},
30+
{
31+
"name": "git-repo-private",
32+
"url": "https://github.com/mozilla-conduit/test-repo-private.git",
33+
"push_path": "https://github.com/mozilla-conduit/test-repo.git",
34+
"required_permission": SCM_LEVEL_1,
35+
"scm_type": GitSCM.scm_type(),
36+
"hooks_enabled": False,
37+
"pr_enabled": True,
38+
},
3039
{
3140
"name": "test-repo-git",
3241
"url": "http://git.test/test-repo",

0 commit comments

Comments
 (0)