Skip to content

Commit

Permalink
changed to local head instead of remote
Browse files Browse the repository at this point in the history
  • Loading branch information
kylemumma committed Jun 17, 2024
1 parent 035833b commit 02616f0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
57 changes: 37 additions & 20 deletions snuba/migrations/autogeneration/main.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
import os
import subprocess

import requests
import yaml


def generate(local_storage_path: str) -> tuple[str, str]:
local_repo_path = (
subprocess.run(
["git", "rev-parse", "--show-toplevel"], capture_output=True, check=True
def generate(storage_path: str) -> tuple[str, str]:
storage_path = os.path.abspath(os.path.expanduser(storage_path))

# get the version of the storage at HEAD
try:
repo_path = (
subprocess.run(
[
"git",
"rev-parse",
"--show-toplevel",
],
cwd=os.path.dirname(storage_path),
capture_output=True,
check=True,
)
.stdout.decode("utf-8")
.strip()
)
.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}'"
if not repo_path.endswith("snuba"):
raise ValueError(
"expected git repo to end with 'snuba' but got: " + repo_path
)
assert storage_path.startswith(repo_path) # should always hold
rel_storage_path = storage_path[len(repo_path) + 1 :]
old_storage = (
subprocess.run(
["git", "show", f"HEAD:{rel_storage_path}"],
capture_output=True,
check=True,
)
.stdout.decode("utf-8")
.strip()
)
except subprocess.CalledProcessError as e:
raise ValueError(e.stderr.decode("utf-8")) from e
old_storage = yaml.safe_load(old_storage)

# 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:
# get the user-provided (modified) storage
with open(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
4 changes: 1 addition & 3 deletions tests/migrations/test_autogeneration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ def test_basic() -> None:


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

0 comments on commit 02616f0

Please sign in to comment.