Spaces:
Runtime error
Runtime error
Create github.py
Browse files
github.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import certifi
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import langchain
|
| 6 |
+
from langchain_core.documents import Document
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
| 12 |
+
def fetch_github_repo(owner, repo, endpiont):
|
| 13 |
+
"""Fetches a GitHub repository's details and returns it as a Document."""
|
| 14 |
+
url = f"https://api.github.com/repos/{owner}/{repo}/{endpiont}"
|
| 15 |
+
headers = {
|
| 16 |
+
"Authorization": f"token {GITHUB_TOKEN}",
|
| 17 |
+
"Accept": "application/vnd.github.v3+json",
|
| 18 |
+
}
|
| 19 |
+
response = requests.get(url, headers=headers, verify=certifi.where())
|
| 20 |
+
|
| 21 |
+
if response.status_code == 200:
|
| 22 |
+
data = response.json()
|
| 23 |
+
else:
|
| 24 |
+
print(f"Failed to fetch repository:", response.status_code)
|
| 25 |
+
return []
|
| 26 |
+
print(data)
|
| 27 |
+
return data
|
| 28 |
+
def fetch_github_issues(owner, repo):
|
| 29 |
+
data = fetch_github_repo(owner, repo, "issues")
|
| 30 |
+
return load_issues(data)
|
| 31 |
+
|
| 32 |
+
def load_issues(issues):
|
| 33 |
+
documents = []
|
| 34 |
+
for issue in issues:
|
| 35 |
+
content = f"Issue Title: {issue['title']}\nIssue Body: {issue['body']}\n"
|
| 36 |
+
metadata = {
|
| 37 |
+
"author": issue["user"]["login"],
|
| 38 |
+
"comments": issue["comments"],
|
| 39 |
+
"labels" : issue["labels"],
|
| 40 |
+
"issue_number": issue["number"],
|
| 41 |
+
"created_at": issue["created_at"],
|
| 42 |
+
"url": issue["html_url"],
|
| 43 |
+
}
|
| 44 |
+
data = issue['title']
|
| 45 |
+
if issue['body']:
|
| 46 |
+
data += "\n" + issue['body']
|
| 47 |
+
documents.append(Document(page_content=data, metadata=metadata))
|
| 48 |
+
return documents
|
| 49 |
+
|
| 50 |
+
owner = "Ini-design"
|
| 51 |
+
repo = "register"
|
| 52 |
+
endpoint = "issues"
|
| 53 |
+
fetch_github_repo(owner , repo, endpoint)
|