Skip to content

Commit 6fcf9c4

Browse files
authored
Merge pull request #2 from suyog19/dev
feat: main deploy workflow + repo mapping model
2 parents e9a1e1b + 5efb58d commit 6fcf9c4

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

app/database.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,14 @@ def init_db(retries: int = 5, delay: int = 3):
6464
)
6565
""")
6666

67+
cur.execute("""
68+
CREATE TABLE IF NOT EXISTS repo_mappings (
69+
id SERIAL PRIMARY KEY,
70+
issue_key VARCHAR(50) NOT NULL UNIQUE,
71+
repo_name VARCHAR(200) NOT NULL,
72+
target_branch VARCHAR(100) NOT NULL DEFAULT 'main',
73+
created_at TIMESTAMP NOT NULL DEFAULT NOW()
74+
)
75+
""")
76+
6777
logger.info("Database initialized — tables ready")

app/main.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
from fastapi import FastAPI
44
from dotenv import load_dotenv
55

6+
from fastapi import HTTPException
7+
from pydantic import BaseModel
8+
69
from app.database import init_db
710
from app.telegram import send_message
811
from app.webhooks import router as webhooks_router
12+
from app.repo_mapping import get_mapping, get_all_mappings, add_mapping
913

1014
load_dotenv()
1115

@@ -50,3 +54,31 @@ def health_check():
5054
def debug_telegram():
5155
send_message("debug", "test", "Manual test from /debug/send-telegram")
5256
return {"sent": True}
57+
58+
59+
# ---------------------------------------------------------------------------
60+
# Repo mapping endpoints
61+
# ---------------------------------------------------------------------------
62+
63+
class RepoMappingIn(BaseModel):
64+
issue_key: str
65+
repo_name: str
66+
target_branch: str = "main"
67+
68+
69+
@app.get("/debug/repo-mappings")
70+
def list_repo_mappings():
71+
return get_all_mappings()
72+
73+
74+
@app.get("/debug/repo-mappings/{issue_key}")
75+
def inspect_repo_mapping(issue_key: str):
76+
mapping = get_mapping(issue_key)
77+
if not mapping:
78+
raise HTTPException(status_code=404, detail=f"No mapping found for '{issue_key}'")
79+
return mapping
80+
81+
82+
@app.post("/debug/repo-mappings")
83+
def create_repo_mapping(body: RepoMappingIn):
84+
return add_mapping(body.issue_key, body.repo_name, body.target_branch)

app/repo_mapping.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
 (0)