From f930ab3577aadb3e6793c28e3da3074dee2a9607 Mon Sep 17 00:00:00 2001 From: Olivier Mehani Date: Thu, 16 Jan 2025 16:34:36 +1100 Subject: [PATCH] pushlog: add basic models (bug 1940610) --- pyproject.toml | 1 + src/lando/pushlog/migrations/0001_initial.py | 85 ++++++++++++++++++++ src/lando/pushlog/migrations/__init__.py | 0 src/lando/pushlog/models/__init__.py | 9 +++ src/lando/pushlog/models/commit.py | 35 ++++++++ src/lando/pushlog/models/push.py | 34 ++++++++ src/lando/pushlog/tests/test_models.py | 9 +++ src/lando/settings.py | 1 + 8 files changed, 174 insertions(+) create mode 100644 src/lando/pushlog/migrations/0001_initial.py create mode 100644 src/lando/pushlog/migrations/__init__.py create mode 100644 src/lando/pushlog/models/__init__.py create mode 100644 src/lando/pushlog/models/commit.py create mode 100644 src/lando/pushlog/models/push.py create mode 100644 src/lando/pushlog/tests/test_models.py diff --git a/pyproject.toml b/pyproject.toml index 45ea86bb..520cea15 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -73,6 +73,7 @@ testpaths = [ "src/lando/api", "src/lando/dockerflow", "src/lando/main/tests", + "src/lando/pushlog/tests", "src/lando/tests", "src/lando/ui/tests", ] diff --git a/src/lando/pushlog/migrations/0001_initial.py b/src/lando/pushlog/migrations/0001_initial.py new file mode 100644 index 00000000..966f112b --- /dev/null +++ b/src/lando/pushlog/migrations/0001_initial.py @@ -0,0 +1,85 @@ +# Generated by Django 5.1.4 on 2025-01-16 06:02 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ("main", "0013_alter_repo_scm_type_alter_worker_scm"), + ] + + operations = [ + migrations.CreateModel( + name="File", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=255)), + ], + ), + migrations.CreateModel( + name="Commit", + fields=[ + ("push_id", models.AutoField(primary_key=True, serialize=False)), + ("author", models.CharField(max_length=512)), + ("branch", models.CharField(max_length=255)), + ("desc", models.TextField()), + ("commit", models.CharField(max_length=160)), + ("parents", models.ManyToManyField(blank=True, to="pushlog.commit")), + ("files", models.ManyToManyField(to="pushlog.file")), + ], + ), + migrations.CreateModel( + name="Push", + fields=[ + ("push_id", models.AutoField(primary_key=True, serialize=False)), + ( + "type", + models.CharField( + choices=[("git", "git")], default="git", max_length=3 + ), + ), + ("date", models.DateField(auto_now_add=True)), + ("user", models.EmailField(max_length=320)), + ("commits", models.ManyToManyField(to="pushlog.commit")), + ( + "repo", + models.ForeignKey( + on_delete=django.db.models.deletion.DO_NOTHING, to="main.repo" + ), + ), + ], + ), + migrations.CreateModel( + name="Tag", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=255)), + ( + "commit", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="pushlog.commit" + ), + ), + ], + ), + ] diff --git a/src/lando/pushlog/migrations/__init__.py b/src/lando/pushlog/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/lando/pushlog/models/__init__.py b/src/lando/pushlog/models/__init__.py new file mode 100644 index 00000000..a6418a53 --- /dev/null +++ b/src/lando/pushlog/models/__init__.py @@ -0,0 +1,9 @@ +from .commit import Commit, File, Tag +from .push import Push + +__all__ = [ + "Commit", + "File", + "Push", + "Tag", +] diff --git a/src/lando/pushlog/models/commit.py b/src/lando/pushlog/models/commit.py new file mode 100644 index 00000000..39f16860 --- /dev/null +++ b/src/lando/pushlog/models/commit.py @@ -0,0 +1,35 @@ +from django.db import models + +MAX_FILENAME_LENGTH = 255 + + +class File(models.Model): + name = models.CharField(max_length=MAX_FILENAME_LENGTH) + + +class Commit(models.Model): + push_id = models.AutoField(primary_key=True) + + # Assuming a max email address length (see Push model), and then some space for a long name. + author = models.CharField(max_length=512) + + # Branch names are limited by how long a filename the filesystem support. This is + # generally 255 bytes. + branch = models.CharField(max_length=MAX_FILENAME_LENGTH) + + desc = models.TextField() + + files = models.ManyToManyField(File) + # "files": [ + # "other-licenses/android/res_init.c" + # ], + + commit = models.CharField(max_length=160) + + parents = models.ManyToManyField("self", blank=True) + +class Tag(models.Model): + # Tag names are limited by how long a filename the filesystem support. This is + # generally 255 bytes. + name = models.CharField(max_length=MAX_FILENAME_LENGTH) + commit = models.ForeignKey(Commit, on_delete=models.CASCADE) diff --git a/src/lando/pushlog/models/push.py b/src/lando/pushlog/models/push.py new file mode 100644 index 00000000..90986274 --- /dev/null +++ b/src/lando/pushlog/models/push.py @@ -0,0 +1,34 @@ +from django.db import models + +from lando.main.models import Repo + +from .commit import Commit + +PUSH_SCM_TYPE_GIT = "git" +PUSH_SCM_TYPES = [PUSH_SCM_TYPE_GIT] + + +class Push(models.Model): + push_id = models.AutoField(primary_key=True) + + type = models.CharField( + max_length=3, choices={v: v for v in PUSH_SCM_TYPES}, default=PUSH_SCM_TYPE_GIT + ) + + date = models.DateField( + auto_now=False, + auto_now_add=True, + ) + # Maximum total lengths are defined in RFC-5321 [0]: 64 for the local-part, and 255 + # for the domain. + # [0] https://datatracker.ietf.org/doc/html/rfc5321#section-4.5.3.1.1 + user = models.EmailField(max_length=64 + 1 + 255) + + commits = models.ManyToManyField(Commit) + + repo = models.ForeignKey( + Repo, + # We don't want to delete the PushLog, even if we were to delete the repo + # object. + on_delete=models.DO_NOTHING, + ) diff --git a/src/lando/pushlog/tests/test_models.py b/src/lando/pushlog/tests/test_models.py new file mode 100644 index 00000000..cf124bf1 --- /dev/null +++ b/src/lando/pushlog/tests/test_models.py @@ -0,0 +1,9 @@ +import pytest + +from lando.pushlog.models import Commit + + +@pytest.mark.django_db() +def test__pushlog__models__Commit(): + c = Commit() + assert c diff --git a/src/lando/settings.py b/src/lando/settings.py index 62186812..d98a7c8a 100644 --- a/src/lando/settings.py +++ b/src/lando/settings.py @@ -47,6 +47,7 @@ "compressor", "mozilla_django_oidc", "lando.main", + "lando.pushlog", "lando.utils", "lando.api", "lando.dockerflow",