add the code
Browse files- github_search.py +121 -0
github_search.py
CHANGED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from typing import List, Optional
|
| 3 |
+
|
| 4 |
+
from config import settings
|
| 5 |
+
from schemas import RetrievedEvidence, SourceType
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
GITHUB_SEARCH_API = "https://api.github.com/search/issues"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def clean_text(text: Optional[str]) -> str:
|
| 12 |
+
if not text:
|
| 13 |
+
return ""
|
| 14 |
+
return str(text).strip()
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def build_github_query(
|
| 18 |
+
message: str,
|
| 19 |
+
error_message: Optional[str] = None,
|
| 20 |
+
language: Optional[str] = None,
|
| 21 |
+
framework: Optional[str] = None,
|
| 22 |
+
) -> str:
|
| 23 |
+
parts = []
|
| 24 |
+
|
| 25 |
+
if framework:
|
| 26 |
+
parts.append(clean_text(framework))
|
| 27 |
+
|
| 28 |
+
if language:
|
| 29 |
+
parts.append(clean_text(language))
|
| 30 |
+
|
| 31 |
+
if error_message:
|
| 32 |
+
parts.append(f'"{clean_text(error_message)}"')
|
| 33 |
+
|
| 34 |
+
if message:
|
| 35 |
+
parts.append(clean_text(message))
|
| 36 |
+
|
| 37 |
+
parts.append("is:issue")
|
| 38 |
+
|
| 39 |
+
return " ".join(part for part in parts if part).strip()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def search_github(
|
| 43 |
+
message: str,
|
| 44 |
+
error_message: Optional[str] = None,
|
| 45 |
+
language: Optional[str] = None,
|
| 46 |
+
framework: Optional[str] = None,
|
| 47 |
+
max_results: Optional[int] = None,
|
| 48 |
+
) -> List[RetrievedEvidence]:
|
| 49 |
+
query = build_github_query(
|
| 50 |
+
message=message,
|
| 51 |
+
error_message=error_message,
|
| 52 |
+
language=language,
|
| 53 |
+
framework=framework,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
if not query:
|
| 57 |
+
return []
|
| 58 |
+
|
| 59 |
+
headers = {
|
| 60 |
+
"Accept": "application/vnd.github+json",
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
if settings.GITHUB_TOKEN:
|
| 64 |
+
headers["Authorization"] = f"Bearer {settings.GITHUB_TOKEN}"
|
| 65 |
+
|
| 66 |
+
params = {
|
| 67 |
+
"q": query,
|
| 68 |
+
"sort": "reactions",
|
| 69 |
+
"order": "desc",
|
| 70 |
+
"per_page": max_results or settings.MAX_GITHUB_RESULTS,
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
response = requests.get(
|
| 75 |
+
GITHUB_SEARCH_API,
|
| 76 |
+
headers=headers,
|
| 77 |
+
params=params,
|
| 78 |
+
timeout=settings.SEARCH_TIMEOUT_SECONDS,
|
| 79 |
+
)
|
| 80 |
+
response.raise_for_status()
|
| 81 |
+
data = response.json()
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"GitHub search failed: {e}")
|
| 84 |
+
return []
|
| 85 |
+
|
| 86 |
+
items = data.get("items", [])
|
| 87 |
+
evidence_list: List[RetrievedEvidence] = []
|
| 88 |
+
|
| 89 |
+
for item in items:
|
| 90 |
+
title = clean_text(item.get("title"))
|
| 91 |
+
url = clean_text(item.get("html_url"))
|
| 92 |
+
score = item.get("score")
|
| 93 |
+
state = clean_text(item.get("state"))
|
| 94 |
+
repo_full_name = clean_text(item.get("repository_url", "").split("/repos/")[-1])
|
| 95 |
+
|
| 96 |
+
snippet_parts = []
|
| 97 |
+
if repo_full_name:
|
| 98 |
+
snippet_parts.append(f"Repo: {repo_full_name}")
|
| 99 |
+
if state:
|
| 100 |
+
snippet_parts.append(f"State: {state}")
|
| 101 |
+
|
| 102 |
+
comments = item.get("comments")
|
| 103 |
+
if comments is not None:
|
| 104 |
+
snippet_parts.append(f"Comments: {comments}")
|
| 105 |
+
|
| 106 |
+
snippet = " | ".join(snippet_parts)
|
| 107 |
+
|
| 108 |
+
if not title:
|
| 109 |
+
continue
|
| 110 |
+
|
| 111 |
+
evidence_list.append(
|
| 112 |
+
RetrievedEvidence(
|
| 113 |
+
source_type=SourceType.GITHUB,
|
| 114 |
+
title=title,
|
| 115 |
+
snippet=snippet or "GitHub issue/discussion result",
|
| 116 |
+
url=url or None,
|
| 117 |
+
score=float(score) if score is not None else None,
|
| 118 |
+
)
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
return evidence_list
|