Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,18 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import requests
|
| 4 |
import os
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
from groq import Groq
|
| 7 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
|
@@ -44,66 +52,120 @@ class RepositoryDetails(BaseModel):
|
|
| 44 |
context: str
|
| 45 |
|
| 46 |
def fetch_repository_changes(repo: str, days_back: int) -> list[str]:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
def summarize_changes_with_deepseek(repo: str, changes: list[str]) -> dict:
|
| 68 |
-
prompt = f"""
|
| 69 |
-
The following changes were made to detection rules in the GitHub repository {repo}.
|
| 70 |
-
Provide a detailed description of the changes and explain the context of why these changes are required:
|
| 71 |
-
{changes}
|
| 72 |
"""
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
@app.get("/monitor", response_model=list[RepositoryDetails])
|
| 88 |
async def monitor_repositories():
|
| 89 |
-
|
| 90 |
-
for
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
from fastapi import FastAPI, HTTPException
|
| 5 |
from pydantic import BaseModel
|
| 6 |
import requests
|
| 7 |
import os
|
| 8 |
from datetime import datetime, timedelta
|
| 9 |
from groq import Groq
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
+
import logging
|
| 12 |
+
|
| 13 |
+
# Configure logging
|
| 14 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
# Load environment variables
|
| 18 |
load_dotenv()
|
|
|
|
| 52 |
context: str
|
| 53 |
|
| 54 |
def fetch_repository_changes(repo: str, days_back: int) -> list[str]:
|
| 55 |
+
"""
|
| 56 |
+
Fetch recent commits and pull requests for a repository.
|
| 57 |
+
"""
|
| 58 |
+
try:
|
| 59 |
+
logger.debug(f"Fetching changes for repository: {repo}")
|
| 60 |
+
since_date = (datetime.now() - timedelta(days=days_back)).isoformat()
|
| 61 |
+
headers = {
|
| 62 |
+
"Authorization": f"token {GITHUB_TOKEN}",
|
| 63 |
+
"Accept": "application/vnd.github.v3+json"
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
# Fetch commits
|
| 67 |
+
commits_url = f"{GITHUB_API_URL}/repos/{repo}/commits"
|
| 68 |
+
commits_params = {"since": since_date}
|
| 69 |
+
logger.debug(f"Fetching commits from: {commits_url}")
|
| 70 |
+
commits_response = requests.get(commits_url, headers=headers, params=commits_params)
|
| 71 |
+
commits_response.raise_for_status() # Raise an exception for HTTP errors
|
| 72 |
+
commits = commits_response.json()
|
| 73 |
+
logger.debug(f"Found {len(commits)} commits for {repo}")
|
| 74 |
+
|
| 75 |
+
# Fetch pull requests
|
| 76 |
+
prs_url = f"{GITHUB_API_URL}/repos/{repo}/pulls"
|
| 77 |
+
prs_params = {"state": "all", "sort": "updated", "direction": "desc"}
|
| 78 |
+
logger.debug(f"Fetching pull requests from: {prs_url}")
|
| 79 |
+
prs_response = requests.get(prs_url, headers=headers, params=prs_params)
|
| 80 |
+
prs_response.raise_for_status() # Raise an exception for HTTP errors
|
| 81 |
+
prs = prs_response.json()
|
| 82 |
+
logger.debug(f"Found {len(prs)} pull requests for {repo}")
|
| 83 |
+
|
| 84 |
+
# Extract changes
|
| 85 |
+
changes = []
|
| 86 |
+
for commit in commits:
|
| 87 |
+
changes.append(f"Commit: {commit['commit']['message']}")
|
| 88 |
+
for pr in prs:
|
| 89 |
+
changes.append(f"PR: {pr['title']} - {pr['body']}")
|
| 90 |
+
|
| 91 |
+
logger.debug(f"Total changes for {repo}: {len(changes)}")
|
| 92 |
+
return changes
|
| 93 |
+
|
| 94 |
+
except requests.exceptions.RequestException as e:
|
| 95 |
+
logger.error(f"Error fetching changes for {repo}: {e}")
|
| 96 |
+
raise HTTPException(status_code=500, detail=f"Error fetching changes for {repo}: {e}")
|
| 97 |
|
| 98 |
def summarize_changes_with_deepseek(repo: str, changes: list[str]) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
"""
|
| 100 |
+
Use Groq's DeepSeek model to summarize changes and provide insights.
|
| 101 |
+
"""
|
| 102 |
+
try:
|
| 103 |
+
logger.debug(f"Summarizing changes for repository: {repo}")
|
| 104 |
+
prompt = f"""
|
| 105 |
+
The following changes were made to detection rules in the GitHub repository {repo}.
|
| 106 |
+
Provide a detailed description of the changes and explain the context of why these changes are required:
|
| 107 |
+
{changes}
|
| 108 |
+
"""
|
| 109 |
+
logger.debug(f"Sending prompt to DeepSeek: {prompt}")
|
| 110 |
+
response = groq_client.chat.completions.create(
|
| 111 |
+
model="deepseek-chat",
|
| 112 |
+
messages=[{"role": "user", "content": prompt}],
|
| 113 |
+
max_tokens=500,
|
| 114 |
+
temperature=0.7
|
| 115 |
+
)
|
| 116 |
+
summary = response.choices[0].message.content
|
| 117 |
+
logger.debug(f"Received summary from DeepSeek: {summary}")
|
| 118 |
+
|
| 119 |
+
# Extract description and context from the summary
|
| 120 |
+
description = summary.split("Description:")[1].split("Context:")[0].strip()
|
| 121 |
+
context = summary.split("Context:")[1].strip()
|
| 122 |
+
logger.debug(f"Extracted description: {description}")
|
| 123 |
+
logger.debug(f"Extracted context: {context}")
|
| 124 |
+
|
| 125 |
+
return {
|
| 126 |
+
"description": description,
|
| 127 |
+
"context": context
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
logger.error(f"Error summarizing changes for {repo}: {e}")
|
| 132 |
+
raise HTTPException(status_code=500, detail=f"Error summarizing changes for {repo}: {e}")
|
| 133 |
|
| 134 |
@app.get("/monitor", response_model=list[RepositoryDetails])
|
| 135 |
async def monitor_repositories():
|
| 136 |
+
"""
|
| 137 |
+
Single API endpoint to fetch and summarize changes for all repositories.
|
| 138 |
+
"""
|
| 139 |
+
try:
|
| 140 |
+
logger.debug("Starting to monitor repositories")
|
| 141 |
+
results = []
|
| 142 |
+
for repo in REPOSITORIES:
|
| 143 |
+
logger.debug(f"Processing repository: {repo}")
|
| 144 |
+
changes = fetch_repository_changes(repo, DAYS_BACK)
|
| 145 |
+
|
| 146 |
+
if changes:
|
| 147 |
+
logger.debug(f"Summarizing changes for {repo}")
|
| 148 |
+
summary = summarize_changes_with_deepseek(repo, changes)
|
| 149 |
+
results.append(RepositoryDetails(
|
| 150 |
+
repo_name=f"{repo} (+{len(changes)}, ✎{len(changes)})",
|
| 151 |
+
repo_url=f"https://github.com/{repo}",
|
| 152 |
+
changes="\n".join(changes),
|
| 153 |
+
description=summary["description"],
|
| 154 |
+
context=summary["context"]
|
| 155 |
+
))
|
| 156 |
+
else:
|
| 157 |
+
logger.debug(f"No changes detected for {repo}")
|
| 158 |
+
results.append(RepositoryDetails(
|
| 159 |
+
repo_name=f"{repo} (No changes)",
|
| 160 |
+
repo_url=f"https://github.com/{repo}",
|
| 161 |
+
changes="No changes detected in the last 7 days.",
|
| 162 |
+
description="No changes detected.",
|
| 163 |
+
context="No context available."
|
| 164 |
+
))
|
| 165 |
+
|
| 166 |
+
logger.debug("Finished monitoring repositories")
|
| 167 |
+
return results
|
| 168 |
+
|
| 169 |
+
except Exception as e:
|
| 170 |
+
logger.error(f"Error in monitor_repositories: {e}")
|
| 171 |
+
raise HTTPException(status_code=500, detail=str(e))
|