diff --git a/tutordistro/distro/repository_validator/infrastructure/git_package_repository.py b/tutordistro/distro/repository_validator/infrastructure/git_package_repository.py index 751bb6a..c6233a6 100644 --- a/tutordistro/distro/repository_validator/infrastructure/git_package_repository.py +++ b/tutordistro/distro/repository_validator/infrastructure/git_package_repository.py @@ -5,7 +5,7 @@ """ -import requests +import subprocess from tutordistro.distro.share.domain.cloud_package import CloudPackage from tutordistro.distro.share.domain.cloud_package_repository import CloudPackageRepository @@ -20,6 +20,19 @@ class GitPackageRepository(CloudPackageRepository): the validation method. """ def validate(self, package: CloudPackage) -> None: - response = requests.get(package.to_url(), timeout=5) - if response.status_code != 200: - raise PackageDoesNotExist(f"The package {package.name} or branch doesn't exist or is private") + package_url = package.to_url() + repo_url = package_url.split('/tree/')[0] + version_name = package_url.split('/tree/')[1] + + # Verify that exist the repository + repo_verification_output = subprocess.run(f'git ls-remote {repo_url}', shell=True, capture_output=True) + if repo_verification_output.returncode != 0: + raise PackageDoesNotExist(f'The package "{repo_url}" does not exist or is private') + + # Verify that the branch/tag is valid + branch_verification_output = subprocess.run(f'git ls-remote --heads "{repo_url}" "{version_name}" | grep "refs/heads/{version_name}"', shell=True, capture_output=True) + + tag_verification_output = subprocess.run(f'git ls-remote --tags {repo_url} | grep "refs/tags/{version_name}"', shell=True, capture_output=True) + + if branch_verification_output.returncode != 0 and tag_verification_output.returncode != 0: + raise PackageDoesNotExist(f'Neither branch nor tag "{version_name}" exists on "{repo_url}"') diff --git a/tutordistro/distro/share/domain/cloud_package.py b/tutordistro/distro/share/domain/cloud_package.py index a7ae4f8..eec0993 100644 --- a/tutordistro/distro/share/domain/cloud_package.py +++ b/tutordistro/distro/share/domain/cloud_package.py @@ -53,30 +53,27 @@ def __parse_url(url) -> CloudPackage: version: str = "" pattern = r"git\+(https?://\S+?)(?:#|$)" - result = re.search(pattern, url) - url = result.group(1).replace('@', '/tree/').replace('.git', '') + found_package_url = re.search(pattern, url).group(1) + github_url = found_package_url.replace('@', '/tree/').replace('.git', '') - parsed_url = urlparse(url) + parsed_url = urlparse(github_url) protocol = parsed_url.scheme domain = parsed_url.netloc - path = parsed_url.path - partes_path = path.split('/') - name = partes_path[2] - - if len(partes_path) > 5: - raise PackageDoesNotExist(f"The package {url} or branch doesn't exist or is private") + full_path = parsed_url.path + split_path = full_path.split('/') + package_name = split_path[2] - if '/tree/' in url: - version = partes_path[-1] - if len(partes_path) > 3 and '/tree/' not in url: - raise PackageDoesNotExist(f"The package {url} or branch doesn't exist or is private") + version_name = found_package_url.split("@")[-1] # This is the branch name or tag + + if '/tree/' in github_url: + version = version_name - path = partes_path[1] + path = split_path[1] return CloudPackage( domain=domain, - name=name, + name=package_name, version=version, protocol=protocol, path=path