|
| 1 | +import logging |
| 2 | +from app.database import get_conn |
| 3 | + |
| 4 | +logger = logging.getLogger("orchestrator") |
| 5 | + |
| 6 | + |
| 7 | +def get_mapping(issue_key: str) -> dict | None: |
| 8 | + with get_conn() as conn: |
| 9 | + with conn.cursor() as cur: |
| 10 | + cur.execute( |
| 11 | + "SELECT issue_key, repo_name, target_branch FROM repo_mappings WHERE issue_key = %s", |
| 12 | + (issue_key,), |
| 13 | + ) |
| 14 | + row = cur.fetchone() |
| 15 | + if not row: |
| 16 | + logger.warning("No repo mapping found for issue_key: %s", issue_key) |
| 17 | + return None |
| 18 | + return {"issue_key": row[0], "repo_name": row[1], "target_branch": row[2]} |
| 19 | + |
| 20 | + |
| 21 | +def get_all_mappings() -> list[dict]: |
| 22 | + with get_conn() as conn: |
| 23 | + with conn.cursor() as cur: |
| 24 | + cur.execute("SELECT issue_key, repo_name, target_branch FROM repo_mappings ORDER BY id") |
| 25 | + rows = cur.fetchall() |
| 26 | + return [{"issue_key": r[0], "repo_name": r[1], "target_branch": r[2]} for r in rows] |
| 27 | + |
| 28 | + |
| 29 | +def add_mapping(issue_key: str, repo_name: str, target_branch: str = "main") -> dict: |
| 30 | + with get_conn() as conn: |
| 31 | + with conn.cursor() as cur: |
| 32 | + cur.execute( |
| 33 | + """ |
| 34 | + INSERT INTO repo_mappings (issue_key, repo_name, target_branch) |
| 35 | + VALUES (%s, %s, %s) |
| 36 | + ON CONFLICT (issue_key) DO UPDATE |
| 37 | + SET repo_name = EXCLUDED.repo_name, |
| 38 | + target_branch = EXCLUDED.target_branch |
| 39 | + RETURNING issue_key, repo_name, target_branch |
| 40 | + """, |
| 41 | + (issue_key, repo_name, target_branch), |
| 42 | + ) |
| 43 | + row = cur.fetchone() |
| 44 | + return {"issue_key": row[0], "repo_name": row[1], "target_branch": row[2]} |
0 commit comments