Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
feat(entity): import services and libraries into entity model
Browse files Browse the repository at this point in the history
  • Loading branch information
jarosevcik committed Oct 20, 2021
1 parent 2c8bb9b commit e61a0ab
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 4 deletions.
4 changes: 1 addition & 3 deletions test/globalsearch/test_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

def _initialize(client, models):
for model, index in models:
client.create_index(
uid=index, options={"name": model.__name__, "primaryKey": "id"}
)
client.create_index(uid=index, options={"primaryKey": "id"})


def test_index_model():
Expand Down
Empty file.
Empty file.
110 changes: 110 additions & 0 deletions zoo/entities/management/commands/import_entities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import structlog
from django.core.management.base import BaseCommand

from zoo.entities.enums import Kind
from zoo.entities.models import Entity, Link
from zoo.libraries.models import Library
from zoo.services.models import Service

log = structlog.get_logger()


class Command(BaseCommand):
help = "Create entities from services and libraries"

def handle(self, *args, **options):
self.service_to_entity()
self.library_to_entity()

@staticmethod
def service_to_entity():
with open("service_exceptions.csv", "w+") as exceptions:
exceptions.write(
"Service name;Service owner;Service repository;Exception\n"
)
for service in Service.objects.all():
if service.repository:
log.info("entity.import_services.processing", service=service)
try:
entity = Entity.objects.create(
name=service.name,
owner=service.owner,
description=service.description,
kind=Kind.COMPONENT,
type="service",
source=service.repository,
tags=service.tags,
service=service,
)

if service.docs_url:
Link.objects.create(
name="Documentation",
url=service.docs_url,
entity=entity,
)

if service.slack_channel:
Link.objects.create(
name="Discussion",
url=f"https://app.slack.com/client/T024Z3H2Y/{service.slack_channel}",
entity=entity,
)
except Exception as err:
exceptions.write(
f"{service.get('name', '')};{service.get('owner', '')};{service.get('repository', '')};{repr((err))}\n"
)
else:
exceptions.write(
f"{service.get('name', '')};{service.get('owner', '')};;Missing repository for service\n"
)

@staticmethod
def library_to_entity():
with open("library_exceptions.csv", "w+") as exceptions:
exceptions.write(
"Library name;Library owner;Library repository;Exception\n"
)
for library in Library.objects.all():
if library.repository:
log.info("entity.import_libraries.processing", library=library)
try:
entity = Entity.objects.create(
name=library.name,
owner=library.owner,
description=library.description,
kind=Kind.COMPONENT,
type="library",
source=library.repository,
tags=library.tags,
library=library,
)

if library.docs_url:
Link.objects.create(
name="Documentation",
url=library.docs_url,
entity=entity,
)

if library.slack_channel:
Link.objects.create(
name="Discussion",
url=f"https://app.slack.com/client/T024Z3H2Y/{library.slack_channel}",
entity=entity,
)

if library.library_url:
Link.objects.create(
name="Library URL",
url=library.library_url,
entity=entity,
)
except Exception as err:
exceptions.write(
f"{library.get('name', '')};{library.get('owner', '')};{library.get('repository', '')};{repr((err))}\n"
)
else:
exceptions.write(
f"{library.get('name', '')};{library.get('owner', '')};;Missing repository for library\n"
)
24 changes: 24 additions & 0 deletions zoo/entities/migrations/0003_allow_blank_on_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.2.19 on 2021-10-13 09:52

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("entities", "0002_allow_null_product_owner_on_group"),
]

operations = [
migrations.AlterField(
model_name="entity",
name="group",
field=models.OneToOneField(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="entities.Group",
),
),
]
85 changes: 85 additions & 0 deletions zoo/entities/migrations/0004_import_objects_to_entity_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import structlog
from django.db import migrations

from zoo.entities.enums import Kind

log = structlog.get_logger()


def service_to_entity(apps, _):
Service = apps.get_model("services", "Service")
Entity = apps.get_model("entities", "Entity")
Link = apps.get_model("entities", "Link")
for service in Service.objects.all():
if service.repository:
log.info("entity.import_services.processing", service=service)
entity = Entity.objects.create(
name=service.name,
owner=service.owner,
description=service.description,
kind=Kind.COMPONENT,
type="service",
source=service.repository,
tags=service.tags,
service=service,
)

if service.docs_url:
Link.objects.create(
name="Documentation", url=service.docs_url, entity=entity
)

if service.slack_channel:
Link.objects.create(
name="Discussion",
url=f"https://app.slack.com/client/T024Z3H2Y/{service.slack_channel}",
entity=entity,
)


def library_to_entity(apps, _):
Library = apps.get_model("libraries", "Library")
Entity = apps.get_model("entities", "Entity")
Link = apps.get_model("entities", "Link")
for library in Library.objects.all():
if library.repository:
log.info("entity.import_libraries.processing", library=library)
entity = Entity.objects.create(
name=library.name,
owner=library.owner,
description=library.description,
kind=Kind.COMPONENT,
type="library",
source=library.repository,
tags=library.tags,
library=library,
)

if library.docs_url:
Link.objects.create(
name="Documentation", url=library.docs_url, entity=entity
)

if library.slack_channel:
Link.objects.create(
name="Discussion",
url=f"https://app.slack.com/client/T024Z3H2Y/{library.slack_channel}",
entity=entity,
)

if library.library_url:
Link.objects.create(
name="Library URL", url=library.library_url, entity=entity
)


class Migration(migrations.Migration):

dependencies = [
("entities", "0003_allow_blank_on_group"),
]

operations = [
migrations.RunPython(service_to_entity),
migrations.RunPython(library_to_entity),
]
3 changes: 2 additions & 1 deletion zoo/entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class Meta:
group = models.OneToOneField(
"entities.Group",
on_delete=models.CASCADE,
default=None,
null=True,
blank=True,
)
source = models.ForeignKey(
"repos.Repository",
Expand Down

0 comments on commit e61a0ab

Please sign in to comment.