Spaces:
Runtime error
Runtime error
Create ap.py
Browse files
ap.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
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()
|
11 |
+
|
12 |
+
# Configuration
|
13 |
+
GITHUB_TOKEN = os.getenv("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")
|
14 |
+
GROQ_API_KEY = os.getenv("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")
|
15 |
+
REPOSITORIES = [
|
16 |
+
"falcosecurity/rules",
|
17 |
+
"SigmaHQ/sigma",
|
18 |
+
"reversinglabs/reversinglabs-yara-rules",
|
19 |
+
"elastic/detection-rules",
|
20 |
+
"sublime-security/sublime-rules",
|
21 |
+
"Yamato-Security/hayabusa-rules",
|
22 |
+
"anvilogic-forge/armory",
|
23 |
+
"chainguard-dev/osquery-defense-kit",
|
24 |
+
"splunk/security_content",
|
25 |
+
"Neo23x0/signature-base",
|
26 |
+
"SlimKQL/Hunting-Queries-Detection-Rules"
|
27 |
+
]
|
28 |
+
DAYS_BACK = 7
|
29 |
+
|
30 |
+
# GitHub API base URL
|
31 |
+
GITHUB_API_URL = "https://api.github.com"
|
32 |
+
|
33 |
+
# Groq client setup
|
34 |
+
groq_client = Groq(api_key=GROQ_API_KEY)
|
35 |
+
|
36 |
+
# FastAPI app
|
37 |
+
app = FastAPI(docs_url=None, redoc_url=None)
|
38 |
+
|
39 |
+
class RepositoryDetails(BaseModel):
|
40 |
+
repo_name: str
|
41 |
+
repo_url: str
|
42 |
+
changes: str
|
43 |
+
description: str
|
44 |
+
context: str
|
45 |
+
|
46 |
+
def fetch_repository_changes(repo: str, days_back: int) -> list[str]:
|
47 |
+
since_date = (datetime.now() - timedelta(days=days_back)).isoformat()
|
48 |
+
headers = {
|
49 |
+
"Authorization": f"token {GITHUB_TOKEN}",
|
50 |
+
"Accept": "application/vnd.github.v3+json"
|
51 |
+
}
|
52 |
+
commits_url = f"{GITHUB_API_URL}/repos/{repo}/commits"
|
53 |
+
commits_params = {"since": since_date}
|
54 |
+
commits_response = requests.get(commits_url, headers=headers, params=commits_params)
|
55 |
+
commits = commits_response.json()
|
56 |
+
prs_url = f"{GITHUB_API_URL}/repos/{repo}/pulls"
|
57 |
+
prs_params = {"state": "all", "sort": "updated", "direction": "desc"}
|
58 |
+
prs_response = requests.get(prs_url, headers=headers, params=prs_params)
|
59 |
+
prs = prs_response.json()
|
60 |
+
changes = []
|
61 |
+
for commit in commits:
|
62 |
+
changes.append(f"Commit: {commit['commit']['message']}")
|
63 |
+
for pr in prs:
|
64 |
+
changes.append(f"PR: {pr['title']} - {pr['body']}")
|
65 |
+
return changes
|
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 |
+
response = groq_client.chat.completions.create(
|
74 |
+
model="deepseek-chat",
|
75 |
+
messages=[{"role": "user", "content": prompt}],
|
76 |
+
max_tokens=500,
|
77 |
+
temperature=0.7
|
78 |
+
)
|
79 |
+
summary = response.choices[0].message.content
|
80 |
+
description = summary.split("Description:")[1].split("Context:")[0].strip()
|
81 |
+
context = summary.split("Context:")[1].strip()
|
82 |
+
return {
|
83 |
+
"description": description,
|
84 |
+
"context": context
|
85 |
+
}
|
86 |
+
|
87 |
+
@app.get("/monitor", response_model=list[RepositoryDetails])
|
88 |
+
async def monitor_repositories():
|
89 |
+
results = []
|
90 |
+
for repo in REPOSITORIES:
|
91 |
+
changes = fetch_repository_changes(repo, DAYS_BACK)
|
92 |
+
if changes:
|
93 |
+
summary = summarize_changes_with_deepseek(repo, changes)
|
94 |
+
results.append(RepositoryDetails(
|
95 |
+
repo_name=f"{repo} (+{len(changes)}, ✎{len(changes)})",
|
96 |
+
repo_url=f"https://github.com/{repo}",
|
97 |
+
changes="\n".join(changes),
|
98 |
+
description=summary["description"],
|
99 |
+
context=summary["context"]
|
100 |
+
))
|
101 |
+
else:
|
102 |
+
results.append(RepositoryDetails(
|
103 |
+
repo_name=f"{repo} (No changes)",
|
104 |
+
repo_url=f"https://github.com/{repo}",
|
105 |
+
changes="No changes detected in the last 7 days.",
|
106 |
+
description="No changes detected.",
|
107 |
+
context="No context available."
|
108 |
+
))
|
109 |
+
return results
|