mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
Added GitAuthError (#6493)
* Specify SSH protocol for `git_remote` * Update experiments.py * Added "a valid" * Format * Added GitRemoteAuthError * Added GitRemoteAuthError * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Removed unused import * Lint * Removed GitRemoteAuthError * Reformatted * Updated error message * Use shorter title * Fix URL * flake8 * Flake8 * Removed extra line * Apply suggestions from code review Co-authored-by: Jorge Orpinel <[email protected]> * valid -> authenticated * Added test * Update experiments.py * Removed authenticated * Minor edit Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jorge Orpinel <[email protected]>
This commit is contained in:
co-authored by
Jorge Orpinel
pre-commit-ci[bot]
parent
d2d6c730c4
commit
5c8a642500
@@ -1151,9 +1151,9 @@ def add_parser(subparsers, parent_parser):
|
||||
nargs="?",
|
||||
default=None,
|
||||
help=(
|
||||
"Optional Git remote name or Git URL. If provided, experiments "
|
||||
"from the specified Git repository will be listed instead of "
|
||||
"local experiments."
|
||||
"Optional Git remote name or Git URL. "
|
||||
"If provided, experiments from the specified Git repository "
|
||||
" will be listed instead of local ones."
|
||||
),
|
||||
metavar="[<git_remote>]",
|
||||
)
|
||||
|
||||
@@ -47,6 +47,15 @@ class InvalidRemoteSCMRepo(SCMError):
|
||||
super().__init__(msg)
|
||||
|
||||
|
||||
class GitAuthError(SCMError):
|
||||
def __init__(self, url: str):
|
||||
super().__init__(
|
||||
f"HTTP Git authentication is not supported: '{url}'"
|
||||
"\nSee https://dvc.org/doc//user-guide/"
|
||||
"troubleshooting#git-auth"
|
||||
)
|
||||
|
||||
|
||||
class Base:
|
||||
"""Base class for source control management driver implementations."""
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ from funcy import cached_property
|
||||
|
||||
from dvc.path_info import PathInfo
|
||||
from dvc.progress import Tqdm
|
||||
from dvc.scm.base import InvalidRemoteSCMRepo, SCMError
|
||||
from dvc.scm.base import GitAuthError, InvalidRemoteSCMRepo, SCMError
|
||||
from dvc.utils import relpath
|
||||
|
||||
from ..objects import GitObject
|
||||
@@ -355,7 +355,7 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
yield os.fsdecode(key)
|
||||
|
||||
def iter_remote_refs(self, url: str, base: Optional[str] = None):
|
||||
from dulwich.client import get_transport_and_path
|
||||
from dulwich.client import HTTPUnauthorized, get_transport_and_path
|
||||
from dulwich.errors import NotGitRepository
|
||||
from dulwich.porcelain import get_remote_repo
|
||||
|
||||
@@ -376,6 +376,8 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
yield from (os.fsdecode(ref) for ref in client.get_refs(path))
|
||||
except NotGitRepository as exc:
|
||||
raise InvalidRemoteSCMRepo(url) from exc
|
||||
except HTTPUnauthorized:
|
||||
raise GitAuthError(url)
|
||||
|
||||
def get_refs_containing(self, rev: str, pattern: Optional[str] = None):
|
||||
raise NotImplementedError
|
||||
@@ -388,7 +390,7 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
force: bool = False,
|
||||
on_diverged: Optional[Callable[[str, str], bool]] = None,
|
||||
):
|
||||
from dulwich.client import get_transport_and_path
|
||||
from dulwich.client import HTTPUnauthorized, get_transport_and_path
|
||||
from dulwich.errors import NotGitRepository, SendPackError
|
||||
from dulwich.porcelain import (
|
||||
DivergedBranches,
|
||||
@@ -447,6 +449,8 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
)
|
||||
except (NotGitRepository, SendPackError) as exc:
|
||||
raise SCMError("Git failed to push '{src}' to '{url}'") from exc
|
||||
except HTTPUnauthorized:
|
||||
raise GitAuthError(url)
|
||||
|
||||
def _push_dest_refs(
|
||||
self, src: Optional[str], dest: str
|
||||
|
||||
@@ -88,3 +88,17 @@ def checkpoint_stage(tmp_dir, scm, dvc, mocker):
|
||||
scm.commit("init")
|
||||
stage.iterations = DEFAULT_ITERATIONS
|
||||
return stage
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def http_auth_patch(mocker):
|
||||
from dulwich.client import HTTPUnauthorized
|
||||
|
||||
url = "https://0.0.0.0"
|
||||
client = mocker.MagicMock()
|
||||
client.get_refs.side_effect = HTTPUnauthorized("", url)
|
||||
client.send_pack.side_effect = HTTPUnauthorized("", url)
|
||||
|
||||
patch = mocker.patch("dulwich.client.get_transport_and_path")
|
||||
patch.return_value = (client, url)
|
||||
return url
|
||||
|
||||
@@ -252,3 +252,37 @@ def test_push_pull_cache(
|
||||
path = os.path.join(dvc.odb.local.cache_dir, hash_[:2], hash_[2:])
|
||||
assert os.path.exists(path)
|
||||
assert open(path).read() == str(x)
|
||||
|
||||
|
||||
def test_auth_error_list(tmp_dir, scm, dvc, http_auth_patch):
|
||||
from dvc.scm.base import GitAuthError
|
||||
|
||||
with pytest.raises(
|
||||
GitAuthError,
|
||||
match=f"HTTP Git authentication is not supported: '{http_auth_patch}'",
|
||||
):
|
||||
dvc.experiments.ls(git_remote=http_auth_patch)
|
||||
|
||||
|
||||
def test_auth_error_pull(tmp_dir, scm, dvc, http_auth_patch):
|
||||
from dvc.scm.base import GitAuthError
|
||||
|
||||
with pytest.raises(
|
||||
GitAuthError,
|
||||
match=f"HTTP Git authentication is not supported: '{http_auth_patch}'",
|
||||
):
|
||||
dvc.experiments.pull(http_auth_patch, "foo")
|
||||
|
||||
|
||||
def test_auth_error_push(tmp_dir, scm, dvc, exp_stage, http_auth_patch):
|
||||
from dvc.scm.base import GitAuthError
|
||||
|
||||
results = dvc.experiments.run(exp_stage.addressing, params=["foo=2"])
|
||||
exp = first(results)
|
||||
ref_info = first(exp_refs_by_rev(scm, exp))
|
||||
|
||||
with pytest.raises(
|
||||
GitAuthError,
|
||||
match=f"HTTP Git authentication is not supported: '{http_auth_patch}'",
|
||||
):
|
||||
dvc.experiments.push(http_auth_patch, ref_info.name)
|
||||
|
||||
Reference in New Issue
Block a user