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 Oct 4, 2023
1 parent fb87442 commit d94a781
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 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
12 changes: 11 additions & 1 deletion api/outdated/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from subprocess import run
from uuid import uuid4

from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db import models

Expand Down Expand Up @@ -56,11 +58,19 @@ def pre_save(self, model_instance, add):
return super().pre_save(model_instance, add)


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})


class RepositoryURLField(models.CharField):
default_validators = [
RegexValidator(
regex=r"^([-_\w]+\.[-._\w]+)\/([-_\w]+)\/([-_\w]+)$",
message="Invalid repository url",
)
),
validate_repo_exists,
]
description = "Field for git repository URLs."
2 changes: 1 addition & 1 deletion api/outdated/outdated/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2023-10-04 13:07
# Generated by Django 4.2.5 on 2023-10-04 13:11

from django.db import migrations, models
import django.db.models.deletion
Expand Down

0 comments on commit d94a781

Please sign in to comment.