Skip to content

Commit

Permalink
feat(api): validate a repositories existance
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Sep 8, 2023
1 parent 165020a commit 825f6f6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
3 changes: 3 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ ENV HOME=/home/outdated

ENV PYTHONUNBUFFERED=1
ENV DJANGO_SETTINGS_MODULE outdated.settings

# prevent git from asking for credentials on wrong urls
ENV GIT_ASKPASS true
ENV APP_HOME=/app

COPY pyproject.toml poetry.lock $APP_HOME/
Expand Down
5 changes: 4 additions & 1 deletion api/outdated/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class Meta:


class RepositoryURLField(models.URLField):
default_validators = [validators.RepositoryURLValidator()]
default_validators = [
validators.RepositoryURLValidator(),
validators.validate_repo_exists,
]


class UniqueBooleanField(models.BooleanField):
Expand Down
11 changes: 10 additions & 1 deletion api/outdated/outdated/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import date, timedelta
from os.path import basename, dirname

from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.functions import Lower

Expand Down Expand Up @@ -93,9 +94,17 @@ def version(self):
return f"{self.release_version.version}.{self.patch_version}"


def repo_url_unique(value: str):
existing_urls = [p.clone_path for p in Project.objects.all()]
if Project(repo=value).clone_path in existing_urls:
raise ValidationError(
"Repository already exists.", params={"value": value}, code="unique"
)


class Project(UUIDModel):
name = models.CharField(max_length=100, db_index=True)
repo = RepositoryURLField(unique=True)
repo = RepositoryURLField(unique=True, validators=[repo_url_unique])
versioned_dependencies = models.ManyToManyField(Version, blank=True)

class Meta:
Expand Down
13 changes: 12 additions & 1 deletion api/outdated/validators.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
from django.core.validators import RegexValidator
from subprocess import run

from django.core.validators import RegexValidator, ValidationError
from django.utils.deconstruct import deconstructible


@deconstructible
class RepositoryURLValidator(RegexValidator):
regex = r"(http[s]?)(://)([\w\.@\:/\-~]+)(\.git)"
message = "Enter a valid http or https url to a git repository."


def validate_repo_exists(value: str):
"""
Validate the existance of a remote git repository
"""
result = run(["git", "ls-remote", value], capture_output=True)
if result.returncode != 0:
raise ValidationError("Repository does not exist.", params={"value": value})

0 comments on commit 825f6f6

Please sign in to comment.