Skip to content

Commit

Permalink
Merge pull request #1014 from thunderstore-io/installers-import
Browse files Browse the repository at this point in the history
Import package installers from ecosystem schema
  • Loading branch information
MythicManiac authored Jan 28, 2024
2 parents 1af5e01 + 7a20c67 commit 7614e0e
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 7 deletions.
28 changes: 28 additions & 0 deletions django/thunderstore/core/tests/test_exception_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest

from thunderstore.core.utils import ExceptionLogger


@pytest.mark.django_db
def test_exception_logger_continues(settings, mocker):
mocked = mocker.patch("thunderstore.core.utils.capture_exception")
settings.ALWAYS_RAISE_EXCEPTIONS = False
exception = AssertionError("Test")

with ExceptionLogger(continue_on_error=True):
raise exception

assert mocked.called_once_with(exception)


@pytest.mark.django_db
def test_exception_logger_raises(settings, mocker):
mocked = mocker.patch("thunderstore.core.utils.capture_exception")
settings.ALWAYS_RAISE_EXCEPTIONS = False
exception = AssertionError("Test")

with pytest.raises(AssertionError, match="Test"):
with ExceptionLogger(continue_on_error=False):
raise exception

assert mocked.called_once_with(exception)
14 changes: 14 additions & 0 deletions django/thunderstore/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ def capture_exception(error=None, *args, **kwargs):
capture_sentry_exception(error, *args, **kwargs)


class ExceptionLogger:
def __init__(self, continue_on_error: bool):
self.continue_on_error = continue_on_error

def __enter__(self):
pass

def __exit__(self, exc_type, exc_val, exc_tb):
if exc_val is not None:
capture_exception(exc_val)
if self.continue_on_error:
return True


def make_full_url(request: Optional[HttpRequest], path: str):
"""Build an URL relative to a request using the proper request scheme"""
url = path
Expand Down
6 changes: 3 additions & 3 deletions django/thunderstore/repository/admin/package_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
@admin.register(PackageInstaller)
class PackageInstallerAdmin(admin.ModelAdmin):
readonly_fields = ("identifier",)
list_display_links = ("pk", "identifier")
list_display = ("pk", "identifier")
search_fields = ("pk", "identifier")
list_display = ("identifier", "name")
list_display_links = ("identifier", "name")
search_fields = ("identifier", "name")

def has_add_permission(self, request: HttpRequest, obj=None) -> bool:
return False
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.1.7 on 2024-01-27 06:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("repository", "0049_add_package_installer"),
]

operations = [
migrations.AddField(
model_name="packageinstaller",
name="description",
field=models.TextField(default=""),
preserve_default=False,
),
migrations.AddField(
model_name="packageinstaller",
name="name",
field=models.TextField(default=""),
preserve_default=False,
),
]
2 changes: 2 additions & 0 deletions django/thunderstore/repository/models/package_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

class PackageInstaller(models.Model):
identifier = models.SlugField(unique=True, db_index=True, editable=False)
name = models.TextField()
description = models.TextField()

def __str__(self):
return self.identifier
Expand Down
8 changes: 8 additions & 0 deletions django/thunderstore/schema_import/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ class SchemaGame(BaseModel):
thunderstore: Optional[SchemaCommunity]


class SchemaPackageInstaller(BaseModel):
name: str
description: str


class Schema(BaseModel):
schema_version: str = Field(alias="schemaVersion")
games: Dict[str, SchemaGame]
communities: Dict[str, SchemaCommunity]
package_installers: Optional[Dict[str, SchemaPackageInstaller]] = Field(
None, alias="packageInstallers"
)
38 changes: 35 additions & 3 deletions django/thunderstore/schema_import/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
PackageCategory,
PackageListingSection,
)
from thunderstore.schema_import.schema import Schema, SchemaCommunity
from thunderstore.core.utils import ExceptionLogger
from thunderstore.repository.models import PackageInstaller
from thunderstore.schema_import.schema import (
Schema,
SchemaCommunity,
SchemaPackageInstaller,
)


def get_slogan_from_display_name(name: str) -> str:
Expand Down Expand Up @@ -77,12 +83,38 @@ def import_community(identifier: str, schema: SchemaCommunity):

def import_schema_communities(schema: Schema):
for identifier, community in schema.communities.items():
import_community(identifier, community)
with ExceptionLogger(continue_on_error=True):
import_community(identifier, community)


@transaction.atomic
def import_installer(identifier: str, schema: SchemaPackageInstaller):
if not (
installer := PackageInstaller.objects.filter(identifier=identifier).first()
):
installer = PackageInstaller(identifier=identifier)
installer.name = schema.name
installer.description = schema.description
installer.save()


def import_schema_package_installers(schema: Schema):
if schema.package_installers is None:
return

for identifier, installer in schema.package_installers.items():
with ExceptionLogger(continue_on_error=True):
import_installer(identifier, installer)


def sync_thunderstore_schema():
response = requests.get(
settings.ECOSYSTEM_SCHEMA_URL, headers={"accept-encoding": "gzip"}
)
schema = Schema.parse_obj(response.json())
import_schema_communities(schema)

with ExceptionLogger(continue_on_error=True):
import_schema_communities(schema)

with ExceptionLogger(continue_on_error=True):
import_schema_package_installers(schema)
26 changes: 25 additions & 1 deletion django/thunderstore/schema_import/tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
PackageCategory,
PackageListingSection,
)
from thunderstore.schema_import.sync import get_slogan_from_display_name
from thunderstore.repository.models import PackageInstaller
from thunderstore.schema_import.schema import Schema, SchemaPackageInstaller
from thunderstore.schema_import.sync import (
get_slogan_from_display_name,
import_schema_package_installers,
)
from thunderstore.schema_import.tasks import sync_ecosystem_schema


Expand Down Expand Up @@ -41,3 +46,22 @@ def test_schema_sync():
assert com_count == Community.objects.count()
assert cat_count == PackageCategory.objects.count()
assert sec_count == PackageListingSection.objects.count()


@pytest.mark.django_db
def test_import_schema_installers():
schema = Schema(
schemaVersion="0.0.1",
games=dict(),
communities=dict(),
packageInstallers={
"foo": SchemaPackageInstaller(
name="Foo installer",
description="This installs foo packages",
),
},
)
assert PackageInstaller.objects.count() == 0
import_schema_package_installers(schema)
assert PackageInstaller.objects.count() == 1
assert PackageInstaller.objects.first().identifier == "foo"

0 comments on commit 7614e0e

Please sign in to comment.