File size: 6,888 Bytes
7d944a4
d129093
 
 
 
 
 
7d944a4
 
 
 
 
d129093
 
 
 
08f99ec
cbf8e75
 
d129093
 
 
 
 
 
 
 
 
 
 
 
 
c7eac83
d129093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d944a4
 
 
 
 
 
 
 
 
 
 
 
 
 
08f99ec
7d944a4
08f99ec
7d944a4
 
 
 
 
 
08f99ec
7d944a4
08f99ec
7d944a4
 
 
 
 
 
 
 
08f99ec
 
 
7d944a4
 
 
 
 
 
 
d129093
 
 
7d944a4
 
 
 
 
08f99ec
 
 
 
 
 
7d944a4
08f99ec
7d944a4
 
 
 
 
 
 
08f99ec
7d944a4
08f99ec
 
 
 
 
 
 
 
7d944a4
08f99ec
7d944a4
 
 
 
d129093
 
 
7d944a4
 
 
 
 
 
 
 
 
 
 
 
 
 
08f99ec
7d944a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08f99ec
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import requests
import os
from datetime import datetime, timedelta
from groq import Groq
from dotenv import load_dotenv
import logging

# Configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

# Load environment variables
load_dotenv()

# Configuration (Use environment variables instead of hardcoding)
GITHUB_TOKEN = "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"
GROQ_API_KEY = "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"
REPOSITORIES = [
    "falcosecurity/rules",
    "SigmaHQ/sigma",
    "reversinglabs/reversinglabs-yara-rules",
    "elastic/detection-rules",
    "sublime-security/sublime-rules",
    "Yamato-Security/hayabusa-rules",
    "anvilogic-forge/armory",
    "chainguard-dev/osquery-defense-kit",
    "splunk/security_content",
    "Neo23x0/signature-base",
    "SlimKQL/Hunting-Queries-Detection-Rules"
]
DAYS_BACK = 1

# GitHub API base URL
GITHUB_API_URL = "https://api.github.com"

# Groq client setup
groq_client = Groq(api_key=GROQ_API_KEY)

# FastAPI app
app = FastAPI(docs_url=None, redoc_url=None)

class RepositoryDetails(BaseModel):
    repo_name: str
    repo_url: str
    changes: str
    description: str
    context: str

def fetch_repository_changes(repo: str, days_back: int) -> list[str]:
    """
    Fetch recent commits and pull requests for a repository.
    """
    try:
        logger.debug(f"Fetching changes for repository: {repo}")
        since_date = (datetime.now() - timedelta(days=days_back)).isoformat()
        headers = {
            "Authorization": f"token {GITHUB_TOKEN}",
            "Accept": "application/vnd.github.v3+json"
        }

        # Fetch commits
        commits_url = f"{GITHUB_API_URL}/repos/{repo}/commits"
        commits_params = {"since": since_date}
        logger.debug(f"Fetching commits from: {commits_url} with params: {commits_params}")
        commits_response = requests.get(commits_url, headers=headers, params=commits_params)
        commits_response.raise_for_status()
        commits = commits_response.json()
        logger.debug(f"Found {len(commits)} commits for {repo}")

        # Fetch pull requests
        prs_url = f"{GITHUB_API_URL}/repos/{repo}/pulls"
        prs_params = {"state": "all", "sort": "updated", "direction": "desc"}
        logger.debug(f"Fetching pull requests from: {prs_url} with params: {prs_params}")
        prs_response = requests.get(prs_url, headers=headers, params=prs_params)
        prs_response.raise_for_status()
        prs = prs_response.json()
        logger.debug(f"Found {len(prs)} pull requests for {repo}")

        # Extract changes
        changes = []
        for commit in commits:
            changes.append(f"Commit: {commit['commit']['message']}")
        for pr in prs:
            updated_at = datetime.strptime(pr["updated_at"], "%Y-%m-%dT%H:%M:%SZ")
            if updated_at >= datetime.now() - timedelta(days=days_back):
                changes.append(f"PR: {pr['title']} - {pr['body'] or 'No description'}")

        logger.debug(f"Total changes for {repo}: {len(changes)}")
        return changes

    except requests.exceptions.RequestException as e:
        logger.error(f"Error fetching changes for {repo}: {e}")
        raise HTTPException(status_code=500, detail=f"Error fetching changes for {repo}: {e}")

def summarize_changes_with_deepseek(repo: str, changes: list[str]) -> dict:
    """
    Use Groq's DeepSeek model to summarize changes and provide insights.
    """
    try:
        logger.debug(f"Summarizing changes for repository: {repo}")
        prompt = f"""
        Analyze the following changes made to detection rules in the GitHub repository {repo}:
        {', '.join(changes)}
        
        Provide a detailed response with two sections:
        - Description: Summarize what changes were made.
        - Context: Explain why these changes might be required.
        """
        logger.debug(f"Sending prompt to DeepSeek: {prompt[:100]}...")  # Truncate for brevity in logs
        response = groq_client.chat.completions.create(
            model="deepseek-chat",
            messages=[{"role": "user", "content": prompt}],
            max_tokens=500,
            temperature=0.7
        )
        summary = response.choices[0].message.content
        logger.debug(f"Received summary from DeepSeek: {summary[:100]}...")

        # Extract description and context with fallback
        description = "Description not found."
        context = "Context not found."
        if "Description:" in summary and "Context:" in summary:
            description = summary.split("Description:")[1].split("Context:")[0].strip()
            context = summary.split("Context:")[1].strip()
        else:
            description = summary  # Fallback to full summary if sections aren't clear

        return {"description": description, "context": context}

    except Exception as e:
        logger.error(f"Error summarizing changes for {repo}: {e}")
        raise HTTPException(status_code=500, detail=f"Error summarizing changes for {repo}: {e}")

@app.get("/monitor", response_model=list[RepositoryDetails])
async def monitor_repositories():
    """
    Single API endpoint to fetch and summarize changes for all repositories.
    """
    try:
        logger.debug("Starting to monitor repositories")
        results = []
        for repo in REPOSITORIES:
            logger.debug(f"Processing repository: {repo}")
            changes = fetch_repository_changes(repo, DAYS_BACK)

            if changes:
                logger.debug(f"Summarizing changes for {repo}")
                summary = summarize_changes_with_deepseek(repo, changes)
                results.append(RepositoryDetails(
                    repo_name=f"{repo} (+{len(changes)})",
                    repo_url=f"https://github.com/{repo}",
                    changes="\n".join(changes),
                    description=summary["description"],
                    context=summary["context"]
                ))
            else:
                logger.debug(f"No changes detected for {repo}")
                results.append(RepositoryDetails(
                    repo_name=f"{repo} (No changes)",
                    repo_url=f"https://github.com/{repo}",
                    changes="No changes detected in the last 7 days.",
                    description="No changes detected.",
                    context="No context available."
                ))

        logger.debug("Finished monitoring repositories")
        return results

    except Exception as e:
        logger.error(f"Error in monitor_repositories: {e}")
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)