Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sbdchd committed Apr 25, 2024
1 parent 51e2b39 commit 8ddf209
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 59 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/web_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ jobs:
--health-retries 5
steps:
- uses: actions/checkout@v3
with:
fetch-depth: "0"
- name: Install poetry
run: |
pipx install poetry==1.7.1
Expand All @@ -117,12 +119,10 @@ jobs:
- name: Install dependencies
working-directory: "./web_api"
run: poetry install
- name: Run tests
run: |
curl -sL https://deb.nodesource.com/setup_14.x | bash -
sudo apt-get install -y nodejs
- name: Set up environment variables
run: echo "DATABASE_URL=postgres://postgres:[email protected]:5432/web_api_test" >> $GITHUB_ENV
- name: run squawk
working-directory: "./web_api"
- name: Run squawk
working-directory: "backend"
run: ./s/squawk.py
- uses: sbdchd/squawk-action@v1
with:
pattern: "backend/recipeyak/migrations/*.sql"
version: "latest"
56 changes: 5 additions & 51 deletions web_api/s/squawk.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import os
import re
import subprocess
from dataclasses import dataclass
from pathlib import Path
from shutil import which
from typing import Mapping, Optional
from typing import Optional

SQUAWK_VERSION = "0.5.0"
APP_LABEL = "web_api"
Expand All @@ -19,14 +17,10 @@
log = logging.getLogger(__file__)


def is_installed(name: str) -> bool:
return which(name) is not None
_MIGRATION_REGEX = re.compile(r"^(\d{4,}_\w+)\.py$")


MIGRATION_REGEX = re.compile(r"^(\d{4,}_\w+)\.py$")


def get_migration_id(filepath: str) -> Optional[str]:
def _get_migration_id(filepath: str) -> Optional[str]:
"""
valid migrations:
0001_initial.py
Expand All @@ -38,27 +32,12 @@ def get_migration_id(filepath: str) -> Optional[str]:
For a valid migration 0001_initial.py, return 0001_initial.
"""
filename = Path(filepath).name
match = MIGRATION_REGEX.match(filename)
match = _MIGRATION_REGEX.match(filename)
if match is None:
return None
return match.groups()[0]


@dataclass(frozen=True)
class PRInfo:
owner: str
repo: str
pr_number: str


def get_pr_info(env: Mapping[str, str]) -> Optional[PRInfo]:
circle_pr = env.get("CIRCLE_PULL_REQUEST")
if circle_pr is None:
return None
_, _, _, owner, repo, _, pr_number = circle_pr.split("/")

return PRInfo(owner=owner, repo=repo, pr_number=pr_number)


def main() -> None:
# circle's built in git checkout code clobbers the `master` ref so we do the
Expand All @@ -82,7 +61,7 @@ def main() -> None:
.stdout.decode()
.split()
):
migration_id = get_migration_id(p)
migration_id = _get_migration_id(p)
if migration_id is None:
continue
changed_migrations_ids.append((migration_id, p))
Expand Down Expand Up @@ -125,31 +104,6 @@ def main() -> None:

log.info("sql files found: %s", output_files)

if not output_files:
return

if not is_installed("squawk"):
subprocess.run(["npm", "config", "set", "unsafe-perm", "true"], check=True)
log.info("squawk not found, installing")
subprocess.run(
["npm", "install", "-g", f"squawk-cli@{SQUAWK_VERSION}"], check=True
)

pr_info = get_pr_info(os.environ)
assert pr_info is not None
log.info(pr_info)

os.environ.setdefault("SQUAWK_GITHUB_PR_NUMBER", pr_info.pr_number)
os.environ.setdefault("SQUAWK_GITHUB_REPO_NAME", pr_info.repo)
os.environ.setdefault("SQUAWK_GITHUB_REPO_OWNER", pr_info.owner)

res = subprocess.run(
["squawk", "upload-to-github", *output_files],
capture_output=True,
)
log.info(res)
res.check_returncode()


if __name__ == "__main__":
main()

0 comments on commit 8ddf209

Please sign in to comment.