From d94a7814b9d0d3bca71f13813b5f8eb631c2e190 Mon Sep 17 00:00:00 2001 From: Arthur Deierlein Date: Fri, 8 Sep 2023 14:38:17 +0200 Subject: [PATCH] feat(api): validate a repositories existance --- api/Dockerfile | 3 +++ api/outdated/models.py | 12 +++++++++++- api/outdated/outdated/migrations/0001_initial.py | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/api/Dockerfile b/api/Dockerfile index 15f0d67e..cce0f4fc 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -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/ diff --git a/api/outdated/models.py b/api/outdated/models.py index 2a54d592..1babffd9 100644 --- a/api/outdated/models.py +++ b/api/outdated/models.py @@ -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 @@ -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." diff --git a/api/outdated/outdated/migrations/0001_initial.py b/api/outdated/outdated/migrations/0001_initial.py index cff32b16..2ef20117 100644 --- a/api/outdated/outdated/migrations/0001_initial.py +++ b/api/outdated/outdated/migrations/0001_initial.py @@ -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