Skip to content

Commit

Permalink
refactor: validate repos with git commands & improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
bra-i-am committed May 24, 2024
1 parent 7c574c0 commit bf35153
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"')
27 changes: 12 additions & 15 deletions tutordistro/distro/share/domain/cloud_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bf35153

Please sign in to comment.