Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api/dependency sources #187

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/outdated/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
register(factories.ReleaseVersionFactory)
register(factories.ProjectFactory)
register(factories.MaintainerFactory)
register(factories.DependencySourceFactory)
register(UserFactory)


Expand Down
25 changes: 3 additions & 22 deletions api/outdated/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from subprocess import run
from __future__ import annotations

from uuid import uuid4

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

Expand Down Expand Up @@ -60,29 +59,11 @@ def pre_save(self, model_instance, add):
return super().pre_save(model_instance, add)


def validate_repo_exists(value: str) -> None:
"""Validate the existance of a remote git repository."""
url = "https://" + value

if value.startswith("file://") and settings.ENV == "test":
url = value

result = run(
["/usr/bin/git", "ls-remote", url],
capture_output=True,
check=False,
shell=False,
)
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."
21 changes: 16 additions & 5 deletions api/outdated/outdated/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,32 @@ class Meta:
class ProjectFactory(DjangoModelFactory):
name = Faker("uuid4")
repo = Sequence(lambda n: "github.com/userorcompany/%s/" % n)
repo_type = "public"

class Meta:
model = models.Project


class DependencySourceFactory(DjangoModelFactory):
project = SubFactory(ProjectFactory)
path = random.choice(
["/pyproject.toml", "/api/pyproject.toml", "/ember/pnpm-lock.yaml"]
)

@post_generation
def versioned_dependencies(self, create, extracted, **kwargs):
def versions(self, create, extracted, **kwargs):
if not create:
return # pragma: no cover
if extracted:
for versioned_dependency in extracted:
self.versioned_dependencies.add(versioned_dependency)
for version in extracted:
self.versions.add(version)

class Meta:
model = models.Project
model = models.DependencySource


class MaintainerFactory(DjangoModelFactory):
project = SubFactory(ProjectFactory)
source = SubFactory(DependencySourceFactory)
user = SubFactory(UserFactory)

class Meta:
Expand Down
207 changes: 78 additions & 129 deletions 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.6 on 2023-10-24 14:19
# Generated by Django 4.2.6 on 2024-01-12 15:45

from django.db import migrations, models
import django.db.models.deletion
Expand All @@ -8,172 +8,121 @@


class Migration(migrations.Migration):

initial = True

dependencies = [
("user", "0001_initial"),
('user', '0001_initial'),
]

operations = [
migrations.CreateModel(
name="Dependency",
name='Dependency',
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("name", models.CharField(max_length=100)),
(
"provider",
models.CharField(
choices=[("PIP", "PIP"), ("NPM", "NPM")], max_length=10
),
),
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('name', models.CharField(max_length=100)),
('provider', models.CharField(choices=[('PIP', 'PIP'), ('NPM', 'NPM')], max_length=10)),
],
options={
"ordering": ["name", "id"],
"unique_together": {("name", "provider")},
'ordering': ['name', 'id'],
},
),
migrations.CreateModel(
name="ReleaseVersion",
name='DependencySource',
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("major_version", models.IntegerField()),
("minor_version", models.IntegerField()),
("end_of_life", models.DateField(blank=True, null=True)),
(
"dependency",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="outdated.dependency",
),
),
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('path', models.CharField()),
],
options={
"ordering": [
"end_of_life",
"dependency__name",
"major_version",
"minor_version",
],
"unique_together": {("dependency", "major_version", "minor_version")},
'abstract': False,
},
),
migrations.CreateModel(
name="Version",
name='Maintainer',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('is_primary', outdated.models.UniqueBooleanField(default=False)),
],
),
migrations.CreateModel(
name='Project',
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("patch_version", models.IntegerField()),
("release_date", models.DateField(blank=True, null=True)),
(
"release_version",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="outdated.releaseversion",
),
),
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('name', models.CharField(db_index=True, max_length=100)),
('repo', outdated.models.RepositoryURLField(max_length=100)),
('repo_type', models.CharField(choices=[('public', 'public'), ('access-token', 'access-token')], max_length=25)),
],
options={
"ordering": [
"release_version__end_of_life",
"release_version__dependency__name",
"release_version__major_version",
"release_version__minor_version",
"patch_version",
],
"unique_together": {("release_version", "patch_version")},
'ordering': ['name', 'id'],
},
),
migrations.CreateModel(
name="Project",
name='ReleaseVersion',
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("name", models.CharField(db_index=True, max_length=100)),
("repo", outdated.models.RepositoryURLField(max_length=100)),
(
"versioned_dependencies",
models.ManyToManyField(blank=True, to="outdated.version"),
),
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('major_version', models.IntegerField()),
('minor_version', models.IntegerField()),
('end_of_life', models.DateField(blank=True, null=True)),
('dependency', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='outdated.dependency')),
],
options={
"ordering": ["name", "id"],
'ordering': ['end_of_life', 'dependency__name', 'major_version', 'minor_version'],
},
),
migrations.CreateModel(
name="Maintainer",
name='Version',
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("is_primary", outdated.models.UniqueBooleanField(default=False)),
(
"project",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="maintainers",
to="outdated.project",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="user.user"
),
),
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('patch_version', models.IntegerField()),
('release_date', models.DateField(blank=True, null=True)),
('release_version', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='outdated.releaseversion')),
],
options={
'ordering': ['release_version__end_of_life', 'release_version__dependency__name', 'release_version__major_version', 'release_version__minor_version', 'patch_version'],
},
),
migrations.AddConstraint(
model_name="project",
constraint=models.UniqueConstraint(
django.db.models.functions.text.Lower("name"),
name="unique_project_name",
),
model_name='project',
constraint=models.UniqueConstraint(django.db.models.functions.text.Lower('name'), name='unique_project_name'),
),
migrations.AddConstraint(
model_name="project",
constraint=models.UniqueConstraint(
django.db.models.functions.text.Lower("repo"),
name="unique_project_repo",
),
model_name='project',
constraint=models.UniqueConstraint(django.db.models.functions.text.Lower('repo'), name='unique_project_repo'),
),
migrations.AddField(
model_name='maintainer',
name='source',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='maintainers', to='outdated.dependencysource'),
),
migrations.AddField(
model_name='maintainer',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='user.user'),
),
migrations.AddField(
model_name='dependencysource',
name='project',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sources', to='outdated.project'),
),
migrations.AddField(
model_name='dependencysource',
name='versions',
field=models.ManyToManyField(blank=True, to='outdated.version'),
),
migrations.AlterUniqueTogether(
name='dependency',
unique_together={('name', 'provider')},
),
migrations.AlterUniqueTogether(
name='version',
unique_together={('release_version', 'patch_version')},
),
migrations.AlterUniqueTogether(
name='releaseversion',
unique_together={('dependency', 'major_version', 'minor_version')},
),
migrations.AlterUniqueTogether(
name="maintainer",
unique_together={("user", "project")},
name='maintainer',
unique_together={('user', 'source')},
),
]
29 changes: 22 additions & 7 deletions api/outdated/outdated/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ def version(self) -> str:
return f"{self.release_version.release_version}.{self.patch_version}"


REPO_TYPES = [(_, _) for _ in ["public", "access-token"]]


class Project(UUIDModel):
name = models.CharField(max_length=100, db_index=True)

versioned_dependencies = models.ManyToManyField(Version, blank=True)
repo = RepositoryURLField(max_length=100)
repo_type = models.CharField(max_length=25, choices=REPO_TYPES)

@property
def repo_domain(self) -> str:
Expand Down Expand Up @@ -198,21 +200,34 @@ class Meta:

@property
def status(self) -> str:
first = self.versioned_dependencies.first()
first = self.sources.all().values_list("versions", flat=True).first()
return first.release_version.status if first else STATUS_OPTIONS["undefined"]

def __str__(self):
return self.name


class DependencySource(UUIDModel):
path = models.CharField()
project = models.ForeignKey(
Project, on_delete=models.CASCADE, related_name="sources"
)
versions = models.ManyToManyField(Version, blank=True)

@property
def status(self) -> str:
first = self.versions.first()
return first.release_version.status if first else STATUS_OPTIONS["undefined"]


class Maintainer(UUIDModel):
user = models.ForeignKey(User, on_delete=models.CASCADE)
project = models.ForeignKey(
Project,
source = models.ForeignKey(
DependencySource,
on_delete=models.CASCADE,
related_name="maintainers",
)
is_primary = UniqueBooleanField(default=False, together=["project"])
is_primary = UniqueBooleanField(default=False, together=["source"])

class Meta:
unique_together = ("user", "project")
unique_together = ("user", "source")
Loading