Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,7 +43,7 @@ app.add_middleware(
|
|
| 43 |
|
| 44 |
# Load environment variables
|
| 45 |
APP_ID = os.getenv("GITHUB_APP_ID")
|
| 46 |
-
PRIVATE_KEY_PATH = os.getenv("GITHUB_PRIVATE_KEY_PATH")
|
| 47 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 48 |
SUPABASE_SECRET_KEY = os.getenv("SUPABASE_SECRET_KEY")
|
| 49 |
DEFAULT_FIREBASE_ID = os.getenv("DEFAULT_FIREBASE_ID", "JDfZoVuGJhdLBEvR7rVCQKBG9r02")
|
|
@@ -125,7 +125,7 @@ def save_installation(org: str, installation_id: int):
|
|
| 125 |
|
| 126 |
|
| 127 |
def get_installation_id(org: str) -> Optional[int]:
|
| 128 |
-
"""Get installation ID for an organization"""
|
| 129 |
installations = load_installations()
|
| 130 |
org_data = installations.get(org)
|
| 131 |
|
|
@@ -134,6 +134,22 @@ def get_installation_id(org: str) -> Optional[int]:
|
|
| 134 |
return None
|
| 135 |
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
def ensure_user_exists(firebase_id: str):
|
| 138 |
if not supabase:
|
| 139 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
|
@@ -437,6 +453,13 @@ async def github_callback(request: Request, background_tasks: BackgroundTasks):
|
|
| 437 |
save_installation(org_name, installation_id)
|
| 438 |
sync_result = None
|
| 439 |
if supabase:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
# Run heavy sync in background so callback returns immediately
|
| 441 |
background_tasks.add_task(
|
| 442 |
sync_installation_to_supabase,
|
|
@@ -563,9 +586,9 @@ def get_commits(data: InsightsRequest):
|
|
| 563 |
if not supabase:
|
| 564 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
| 565 |
|
| 566 |
-
installation_id =
|
| 567 |
if not installation_id:
|
| 568 |
-
raise HTTPException(status_code=404, detail=f"No installation found for {data.
|
| 569 |
|
| 570 |
try:
|
| 571 |
# Fetch commits from GitHub
|
|
@@ -624,9 +647,9 @@ def get_pull_requests(data: InsightsRequest):
|
|
| 624 |
if not supabase:
|
| 625 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
| 626 |
|
| 627 |
-
installation_id =
|
| 628 |
if not installation_id:
|
| 629 |
-
raise HTTPException(status_code=404, detail=f"No installation found for {data.
|
| 630 |
|
| 631 |
try:
|
| 632 |
# Fetch pull requests from GitHub
|
|
@@ -697,9 +720,9 @@ def get_issues(data: InsightsRequest):
|
|
| 697 |
if not supabase:
|
| 698 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
| 699 |
|
| 700 |
-
installation_id =
|
| 701 |
if not installation_id:
|
| 702 |
-
raise HTTPException(status_code=404, detail=f"No installation found for {data.
|
| 703 |
|
| 704 |
try:
|
| 705 |
# Fetch issues from GitHub
|
|
@@ -767,9 +790,9 @@ def sync_github_to_supabase(data: GitHubSyncRequest):
|
|
| 767 |
if not supabase:
|
| 768 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
| 769 |
|
| 770 |
-
installation_id =
|
| 771 |
if not installation_id:
|
| 772 |
-
raise HTTPException(status_code=404, detail=f"No installation found for {data.
|
| 773 |
|
| 774 |
try:
|
| 775 |
synced = sync_installation_to_supabase(data.firebase_id, data.org, installation_id)
|
|
@@ -792,9 +815,9 @@ def get_contributors(data: InsightsRequest):
|
|
| 792 |
if not supabase:
|
| 793 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
| 794 |
|
| 795 |
-
installation_id =
|
| 796 |
if not installation_id:
|
| 797 |
-
raise HTTPException(status_code=404, detail=f"No installation found for {data.
|
| 798 |
|
| 799 |
try:
|
| 800 |
# Fetch contributors from GitHub
|
|
@@ -848,9 +871,9 @@ def get_repository_overview(data: InsightsRequest):
|
|
| 848 |
if not github_auth:
|
| 849 |
raise HTTPException(status_code=500, detail="GitHub App not configured")
|
| 850 |
|
| 851 |
-
installation_id =
|
| 852 |
if not installation_id:
|
| 853 |
-
raise HTTPException(status_code=404, detail=f"No installation found for {data.
|
| 854 |
|
| 855 |
try:
|
| 856 |
token = token_manager.get_token(installation_id)
|
|
@@ -890,9 +913,9 @@ def get_repository_activity(data: InsightsRequest):
|
|
| 890 |
if not github_auth:
|
| 891 |
raise HTTPException(status_code=500, detail="GitHub App not configured")
|
| 892 |
|
| 893 |
-
installation_id =
|
| 894 |
if not installation_id:
|
| 895 |
-
raise HTTPException(status_code=404, detail=f"No installation found for {data.
|
| 896 |
|
| 897 |
try:
|
| 898 |
logger.info(f"Fetching activity for {data.org}/{data.repo}")
|
|
@@ -926,11 +949,11 @@ def test_sync(data: GitHubSyncRequest):
|
|
| 926 |
"""
|
| 927 |
logger.info(f"Test sync called for firebase_id={data.firebase_id}, org={data.org}")
|
| 928 |
|
| 929 |
-
installation_id =
|
| 930 |
if not installation_id:
|
| 931 |
return {
|
| 932 |
"success": False,
|
| 933 |
-
"error": f"No installation found for {data.
|
| 934 |
"available_orgs": list(load_installations().keys())
|
| 935 |
}
|
| 936 |
|
|
@@ -952,4 +975,7 @@ def test_sync(data: GitHubSyncRequest):
|
|
| 952 |
}
|
| 953 |
|
| 954 |
|
|
|
|
|
|
|
|
|
|
| 955 |
|
|
|
|
| 43 |
|
| 44 |
# Load environment variables
|
| 45 |
APP_ID = os.getenv("GITHUB_APP_ID")
|
| 46 |
+
PRIVATE_KEY_PATH = os.getenv("GITHUB_PRIVATE_KEY_PATH", "private-key.pem")
|
| 47 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 48 |
SUPABASE_SECRET_KEY = os.getenv("SUPABASE_SECRET_KEY")
|
| 49 |
DEFAULT_FIREBASE_ID = os.getenv("DEFAULT_FIREBASE_ID", "JDfZoVuGJhdLBEvR7rVCQKBG9r02")
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
def get_installation_id(org: str) -> Optional[int]:
|
| 128 |
+
"""Get installation ID for an organization (legacy from file system)"""
|
| 129 |
installations = load_installations()
|
| 130 |
org_data = installations.get(org)
|
| 131 |
|
|
|
|
| 134 |
return None
|
| 135 |
|
| 136 |
|
| 137 |
+
def get_installation_id_for_user(firebase_id: str) -> Optional[int]:
|
| 138 |
+
"""Get installation ID for a user from Supabase Users table"""
|
| 139 |
+
if not supabase:
|
| 140 |
+
logger.error("Supabase not configured")
|
| 141 |
+
return None
|
| 142 |
+
|
| 143 |
+
try:
|
| 144 |
+
result = supabase.table("Users").select("installation_id").eq("firebase_id", firebase_id).limit(1).execute()
|
| 145 |
+
if result.data and result.data[0].get("installation_id"):
|
| 146 |
+
return result.data[0]["installation_id"]
|
| 147 |
+
except Exception as e:
|
| 148 |
+
logger.error(f"Failed to get installation_id for user {firebase_id}: {str(e)}")
|
| 149 |
+
|
| 150 |
+
return None
|
| 151 |
+
|
| 152 |
+
|
| 153 |
def ensure_user_exists(firebase_id: str):
|
| 154 |
if not supabase:
|
| 155 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
|
|
|
| 453 |
save_installation(org_name, installation_id)
|
| 454 |
sync_result = None
|
| 455 |
if supabase:
|
| 456 |
+
# Save installation_id to Users table
|
| 457 |
+
try:
|
| 458 |
+
supabase.table("Users").update({"installation_id": installation_id}).eq("firebase_id", firebase_id).execute()
|
| 459 |
+
logger.info(f"Saved installation_id {installation_id} for user {firebase_id}")
|
| 460 |
+
except Exception as e:
|
| 461 |
+
logger.error(f"Failed to save installation_id to Users table: {str(e)}")
|
| 462 |
+
|
| 463 |
# Run heavy sync in background so callback returns immediately
|
| 464 |
background_tasks.add_task(
|
| 465 |
sync_installation_to_supabase,
|
|
|
|
| 586 |
if not supabase:
|
| 587 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
| 588 |
|
| 589 |
+
installation_id = get_installation_id_for_user(data.firebase_id)
|
| 590 |
if not installation_id:
|
| 591 |
+
raise HTTPException(status_code=404, detail=f"No GitHub installation found for user {data.firebase_id}")
|
| 592 |
|
| 593 |
try:
|
| 594 |
# Fetch commits from GitHub
|
|
|
|
| 647 |
if not supabase:
|
| 648 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
| 649 |
|
| 650 |
+
installation_id = get_installation_id_for_user(data.firebase_id)
|
| 651 |
if not installation_id:
|
| 652 |
+
raise HTTPException(status_code=404, detail=f"No GitHub installation found for user {data.firebase_id}")
|
| 653 |
|
| 654 |
try:
|
| 655 |
# Fetch pull requests from GitHub
|
|
|
|
| 720 |
if not supabase:
|
| 721 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
| 722 |
|
| 723 |
+
installation_id = get_installation_id_for_user(data.firebase_id)
|
| 724 |
if not installation_id:
|
| 725 |
+
raise HTTPException(status_code=404, detail=f"No GitHub installation found for user {data.firebase_id}")
|
| 726 |
|
| 727 |
try:
|
| 728 |
# Fetch issues from GitHub
|
|
|
|
| 790 |
if not supabase:
|
| 791 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
| 792 |
|
| 793 |
+
installation_id = get_installation_id_for_user(data.firebase_id)
|
| 794 |
if not installation_id:
|
| 795 |
+
raise HTTPException(status_code=404, detail=f"No GitHub installation found for user {data.firebase_id}")
|
| 796 |
|
| 797 |
try:
|
| 798 |
synced = sync_installation_to_supabase(data.firebase_id, data.org, installation_id)
|
|
|
|
| 815 |
if not supabase:
|
| 816 |
raise HTTPException(status_code=500, detail="Supabase not configured")
|
| 817 |
|
| 818 |
+
installation_id = get_installation_id_for_user(data.firebase_id)
|
| 819 |
if not installation_id:
|
| 820 |
+
raise HTTPException(status_code=404, detail=f"No GitHub installation found for user {data.firebase_id}")
|
| 821 |
|
| 822 |
try:
|
| 823 |
# Fetch contributors from GitHub
|
|
|
|
| 871 |
if not github_auth:
|
| 872 |
raise HTTPException(status_code=500, detail="GitHub App not configured")
|
| 873 |
|
| 874 |
+
installation_id = get_installation_id_for_user(data.firebase_id)
|
| 875 |
if not installation_id:
|
| 876 |
+
raise HTTPException(status_code=404, detail=f"No GitHub installation found for user {data.firebase_id}")
|
| 877 |
|
| 878 |
try:
|
| 879 |
token = token_manager.get_token(installation_id)
|
|
|
|
| 913 |
if not github_auth:
|
| 914 |
raise HTTPException(status_code=500, detail="GitHub App not configured")
|
| 915 |
|
| 916 |
+
installation_id = get_installation_id_for_user(data.firebase_id)
|
| 917 |
if not installation_id:
|
| 918 |
+
raise HTTPException(status_code=404, detail=f"No GitHub installation found for user {data.firebase_id}")
|
| 919 |
|
| 920 |
try:
|
| 921 |
logger.info(f"Fetching activity for {data.org}/{data.repo}")
|
|
|
|
| 949 |
"""
|
| 950 |
logger.info(f"Test sync called for firebase_id={data.firebase_id}, org={data.org}")
|
| 951 |
|
| 952 |
+
installation_id = get_installation_id_for_user(data.firebase_id)
|
| 953 |
if not installation_id:
|
| 954 |
return {
|
| 955 |
"success": False,
|
| 956 |
+
"error": f"No GitHub installation found for user {data.firebase_id}",
|
| 957 |
"available_orgs": list(load_installations().keys())
|
| 958 |
}
|
| 959 |
|
|
|
|
| 975 |
}
|
| 976 |
|
| 977 |
|
| 978 |
+
# ============================================================================
|
| 979 |
+
# RUN SERVER
|
| 980 |
+
# ============================================================================
|
| 981 |
|