Skip to content

Commit

Permalink
meowmeowcat
Browse files Browse the repository at this point in the history
  • Loading branch information
kylemumma committed Jun 12, 2024
1 parent 82d79fd commit c725423
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
37 changes: 26 additions & 11 deletions snuba/migrations/autogeneration/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import os
import subprocess

import requests
import yaml

def generate(storage_path: str) -> None:
repo_path = subprocess.run(
["git", "rev-parse", "--show-toplevel"], capture_output=True
).stdout.decode("utf-8")
storage_path = os.path.abspath(os.path.expanduser(storage_path))
if not storage_path.startswith(repo_path):
raise ValueError("Storage path is not in the git repository")

rel_path = os.path.relpath(storage_path, repo_path)
old_storage = ""
new_storage = ""

def generate(local_storage_path: str) -> tuple[str, str]:
local_repo_path = (
subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True)
.stdout.decode("utf-8")
.strip()
)
local_storage_path = os.path.abspath(os.path.expanduser(local_storage_path))
if not local_storage_path.startswith(local_repo_path):
raise ValueError(
f"Storage path '{local_storage_path}' is not in the git repository '{local_repo_path}'"
)

# get local storage
rel_storage_path = os.path.relpath(local_storage_path, local_repo_path)
with open(os.path.join(local_repo_path, rel_storage_path), "r") as f:
new_storage = yaml.safe_load(f)

# get remote storage
REMOTE_SNUBA_REPO = "https://raw.githubusercontent.com/getsentry/snuba/master"
res = requests.get(f"{REMOTE_SNUBA_REPO}/{rel_storage_path}")
old_storage = yaml.safe_load(res.text)

return old_storage, new_storage
17 changes: 17 additions & 0 deletions tests/migrations/test_autogeneration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from snuba.migrations.autogeneration import generate


def test_basic() -> None:
old_storage, new_storage = generate(
"snuba/datasets/configuration/events/storages/errors.yaml"
)
assert old_storage, new_storage


def test_error() -> None:
with pytest.raises(
ValueError, match=r"Storage path .* is not in the git repository .*"
):
generate("/Users/bloop/hello")

0 comments on commit c725423

Please sign in to comment.