Browse Source

Start work on import management command

David Burke 2 years ago
parent
commit
4a0acd5aec

+ 0 - 0
glitchtip/importer/__init__.py


+ 2 - 0
glitchtip/importer/exceptions.py

@@ -0,0 +1,2 @@
+class ImporterException(Exception):
+    pass

+ 59 - 0
glitchtip/importer/importer.py

@@ -0,0 +1,59 @@
+import requests
+import tablib
+from django.urls import reverse
+
+from organizations_ext.admin import OrganizationResource
+from projects.admin import ProjectResource
+
+
+from .exceptions import ImporterException
+
+
+class GlitchTipImporter:
+    def __init__(self, url: str, auth_token: str, organization_slug: str):
+        self.api_root_url = reverse("api-root-view")
+        self.url = url
+        self.headers = {"Authorization": f"Bearer {auth_token}"}
+        self.organization_slug = organization_slug
+        self.organization_id = None
+        self.organization_url = reverse(
+            "organization-detail", kwargs={"slug": self.organization_slug}
+        )
+        self.projects_url = reverse(
+            "organization-projects-list",
+            kwargs={"organization_slug": self.organization_slug},
+        )
+        self.check_auth()
+
+    def run(self):
+        self.check_auth()
+        self.import_organization()
+        self.import_projects()
+
+    def get(self, url):
+        return requests.get(url, headers=self.headers)
+
+    def import_organization(self):
+        resource = OrganizationResource()
+        res = self.get(self.url + self.organization_url)
+        data = res.json()
+        self.organization_id = data["id"]
+        dataset = tablib.Dataset()
+        dataset.dict = [data]
+        resource.import_data(dataset, raise_errors=True)
+
+    def import_projects(self):
+        resource = ProjectResource()
+        res = self.get(self.url + self.projects_url)
+        data = res.json()
+        for obj in data:
+            obj["organization"] = self.organization_id
+        dataset = tablib.Dataset()
+        dataset.dict = data
+        resource.import_data(dataset, raise_errors=True)
+
+    def check_auth(self):
+        res = requests.get(self.url + self.api_root_url, headers=self.headers)
+        data = res.json()
+        if res.status_code != 200 or not data["user"]:
+            raise ImporterException("Bad auth token")

+ 0 - 0
glitchtip/importer/management/__init__.py


+ 0 - 0
glitchtip/importer/management/commands/__init__.py


+ 20 - 0
glitchtip/importer/management/commands/import.py

@@ -0,0 +1,20 @@
+from django.core.management.base import BaseCommand
+from glitchtip.importer.importer import GlitchTipImporter
+
+
+class Command(BaseCommand):
+    help = "Import data from another GlitchTip instance or Sentry"
+
+    def add_arguments(self, parser):
+        parser.add_argument("url", type=str)
+        parser.add_argument("auth_token", type=str)
+        parser.add_argument("organization_slug", type=str)
+
+    def handle(self, *args, **options):
+        url = options["url"].rstrip("/")
+        if not url.startswith("http"):
+            url = "https://" + url
+        importer = GlitchTipImporter(
+            options["url"], options["auth_token"], options["organization_slug"]
+        )
+        importer.run()

+ 0 - 0
glitchtip/importer/models.py


+ 0 - 0
glitchtip/importer/tests.py


+ 2 - 0
glitchtip/settings.py

@@ -197,6 +197,7 @@ INSTALLED_APPS += [
     "drf_yasg",
     "dj_rest_auth",
     "dj_rest_auth.registration",
+    "import_export",
     "storages",
     "glitchtip",
     "alerts",
@@ -208,6 +209,7 @@ INSTALLED_APPS += [
     "issues",
     "users",
     "user_reports",
+    "glitchtip.importer",
     "glitchtip.uptime",
     "performance",
     "projects",

+ 1 - 1
glitchtip/urls.py

@@ -66,7 +66,7 @@ urlpatterns = [
         TemplateView.as_view(template_name="robots.txt", content_type="text/plain"),
     ),
     path("api/", RedirectView.as_view(url="/profile/auth-tokens")),
-    path("api/0/", APIRootView.as_view()),
+    path("api/0/", APIRootView.as_view(), name="api-root-view"),
     path("api/0/", include(router.urls)),
 ]
 

Some files were not shown because too many files changed in this diff