Files
bugsink/bsmain/management/commands/fetch_event_schema_json.py
Klaas van Schelven ae9cb209a5 Create 'bsmain' (for bugsink-main) app to hold commands
As had been noted on some of the commands, 'ingest' was not the best place for
them.  However, [project-level apps are not supported in
Django](https://forum.djangoproject.com/t/allow-project-to-have-management-commands/5220/2)
So just create a 'main' app. I want to qualify it as 'myproject-main' though, to avoid
further unqualified global namespace pollution. And I want to avoid prefixing with 'bugsink'
b/c that's annoying for tab-completion. So 'bs' it is.

I've moved all commands over; even though a case could be made that the "feeding" commands
(raise_exception, send_json, stress_test) are somewhat related to ingestion, that's not
a very good case :-)
2025-01-23 11:55:34 +01:00

71 lines
3.0 KiB
Python

import json
import requests
import fastjsonschema
from subprocess import run, PIPE
from django.conf import settings
from django.core.management.base import BaseCommand
def alter_schema(schema):
# alter the schema to make it fit what the actual documentation says; see issues/utils.py' get_values() for details
for key in ['exception', 'threads', 'breadcrumbs']:
# no idea why that anyOf[0] is there (since there's no anyOf[1]), but it's there.
definition = schema['anyOf'][0]['properties'][key]
schema['anyOf'][0]['properties'][key] = {
"anyOf": [
definition,
definition['properties']['values'],
]
}
class Command(BaseCommand):
help = "Fetches the event schema JSON from the API and saves it to disk. Used during development."
def handle(self, *args, **options):
# Fetch the event schema JSON from the API and save it to disk
json_result = requests.get(
"https://raw.githubusercontent.com/getsentry/sentry-data-schemas/main/relay/event.schema.json")
json_result.raise_for_status()
with open(settings.BASE_DIR / "api/event.schema.json", "w") as f:
f.write(json_result.text)
# Fetch the license from GitHub and save it to disk (with annotation about the source)
license_url = "https://raw.githubusercontent.com/getsentry/sentry-data-schemas/main/LICENSE"
license_result = requests.get(license_url)
license_result.raise_for_status()
with open(settings.BASE_DIR / "api/LICENSE", "w") as f:
f.write("""This licence applies to the files: event.schema.json (and its derivatives)
The source of this file is: %s
""" % license_url)
f.write(license_result.text)
# Generate fastjsonschema code from the schema and save it to disk
schema = json.loads(json_result.text)
alter_schema(schema)
with open(settings.BASE_DIR / "api/event.schema.altered.json", "w") as f:
json.dump(schema, f, indent=2)
# use_formats=False for "uint64", see https://github.com/horejsek/python-fastjsonschema/issues/108
# use_default=False to avoid fastjsonschema adding default values to the data it validates (bwegh)
code = fastjsonschema.compile_to_code(schema, use_formats=False, use_default=False, detailed_exceptions=False)
with open(settings.BASE_DIR / "bugsink/event_schema.py", "w") as f:
f.write("""# This file is generated by fetch_event_schema_json.py
# it is based on api/event.schema.altered.json
# the following license applies:
""")
f.write("\n".join("# " + line for line in license_result.text.splitlines()) + "\n\n")
f.write(code)
# put it through 'black' to make it fit on screen at least, and perhaps to get useable diffs too.
# (we don't bother putting it in requirements.txt, since it's used so rarely)
run(["black", settings.BASE_DIR / "bugsink/event_schema.py"], stdout=PIPE, stderr=PIPE)