diff --git a/gateway/actions.py b/gateway/actions.py index aa3bca5..d66c8da 100644 --- a/gateway/actions.py +++ b/gateway/actions.py @@ -1,14 +1,27 @@ import base64 +import logging import secrets from django.conf import settings +from django.core.exceptions import ValidationError +from django.core.validators import validate_slug from . import github, rap_api from .models import Job, Project, Run, User +logger = logging.getLogger(__name__) + + def create_or_update_projects(): for record in github.get_repo_metadata(settings.GITHUB_ORG): + try: + validate_slug(record["name"]) + except ValidationError: + logger.warning( + "Ignoring GitHub repo with invalid slug name: %s", record["name"] + ) + continue Project.objects.update_or_create( id=record["id"], defaults={ diff --git a/tests/test_actions.py b/tests/test_actions.py index 1129624..f87482c 100644 --- a/tests/test_actions.py +++ b/tests/test_actions.py @@ -59,6 +59,27 @@ def test_create_or_update_projects(): assert p3.description == "Description" +def test_create_or_update_projects_ignores_invalid_slug_repos(caplog): + github_data = [ + {"id": 123, "name": ".github", "description": "Ignored"}, + {"id": 124, "name": "my.repo", "description": "Ignored"}, + {"id": 456, "name": "test-2", "description": "Description"}, + ] + + with mocked_responses(get_data=github_data): + create_or_update_projects() + + assert Project.objects.count() == 1 + assert not Project.objects.filter(name=".github").exists() + assert not Project.objects.filter(name="my.repo").exists() + assert "Ignoring GitHub repo with invalid slug name: .github" in caplog.text + assert "Ignoring GitHub repo with invalid slug name: my.repo" in caplog.text + + project = Project.objects.get(pk=456) + assert project.name == "test-2" + assert project.description == "Description" + + def test_create_or_update_users(): User = get_user_model() github_data_1 = [