healthchecks_healthchecks/hc/api/migrations/0061_webhook_values.py
2023-10-25 18:12:12 +03:00

64 lines
2.0 KiB
Python

# Generated by Django 2.2.1 on 2019-05-29 20:37
from __future__ import annotations
import json
from typing import Any
from django.apps.registry import Apps
from django.db import migrations
def normalize_webhook_values(apps: Apps, schema_editor: Any) -> None:
Channel = apps.get_model("api", "Channel")
for ch in Channel.objects.filter(kind="webhook").only("value"):
# The old format of url_down, url_up, post_data separated by newlines:
if not ch.value.startswith("{"):
parts = ch.value.split("\n")
url_down = parts[0]
url_up = parts[1] if len(parts) > 1 else ""
post_data = parts[2] if len(parts) > 2 else ""
ch.value = json.dumps(
{
"method_down": "POST" if post_data else "GET",
"url_down": url_down,
"body_down": post_data,
"headers_down": {},
"method_up": "POST" if post_data else "GET",
"url_up": url_up,
"body_up": post_data,
"headers_up": {},
}
)
ch.save()
continue
doc = json.loads(ch.value)
# Legacy "post_data" in doc -- use the legacy fields
if "post_data" in doc:
ch.value = json.dumps(
{
"method_down": "POST" if doc["post_data"] else "GET",
"url_down": doc["url_down"],
"body_down": doc["post_data"],
"headers_down": doc["headers"],
"method_up": "POST" if doc["post_data"] else "GET",
"url_up": doc["url_up"],
"body_up": doc["post_data"],
"headers_up": doc["headers"],
}
)
ch.save()
continue
class Migration(migrations.Migration):
dependencies = [("api", "0060_tokenbucket")]
operations = [
migrations.RunPython(normalize_webhook_values, migrations.RunPython.noop)
]