Repository navigation
Expand file tree
/
Copy pathgithub_loader.py
More file actions
88 lines (70 loc) · 3.08 KB
/
Copy pathgithub_loader.py
File metadata and controls
88 lines (70 loc) · 3.08 KB
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
import base64
from typing import Any, Dict, List, Union
import requests
from langchain_core.documents import Document
from config import (
GITHUB_API_URL,
GITHUB_OWNER,
GITHUB_REPO,
GITHUB_DIR_PATH,
settings
)
def validate_github_env() -> None:
"""Validate that all required GitHub environment variables are present."""
missing = [
var_name
for var_name, value in [
("GITHUB_OWNER", GITHUB_OWNER),
("GITHUB_REPO", GITHUB_REPO),
]
if not value
]
if missing:
raise ValueError(f"Missing required environment variables: {', '.join(missing)}")
def _github_api_request(endpoint: str) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
"""Helper method to make authenticated requests to the GitHub API."""
validate_github_env()
headers = {"Accept": "application/vnd.github.v3+json"}
if settings.github_token:
headers["Authorization"] = f"Bearer {settings.github_token}"
url = f"{GITHUB_API_URL}/repos/{GITHUB_OWNER}/{GITHUB_REPO}/contents/{endpoint.lstrip('/')}"
response = requests.get(url, headers=headers, timeout=10)
if response.status_code != 200:
raise RuntimeError(
f"GitHub API Error [{response.status_code}]: {response.text}"
)
return response.json()
def fetch_github_directory_contents(dir_path: str = GITHUB_DIR_PATH) -> List[Dict[str, Any]]:
"""Fetch directory metadata list from GitHub repository."""
contents = _github_api_request(dir_path)
if not isinstance(contents, list):
raise TypeError(f"Expected a directory list at '{dir_path}', got a single file object instead.")
return contents
def fetch_github_file_content(file_path: str) -> str:
"""Fetch and decode plain text content of a file from GitHub repository."""
file_data = _github_api_request(file_path)
if isinstance(file_data, list):
raise TypeError(f"Expected a file path at '{file_path}', but found a directory.")
encoded_content = file_data.get("content", "")
decoded_bytes = base64.b64decode(encoded_content)
return decoded_bytes.decode("utf-8", errors="ignore")
def load_github_documents(dir_path: str = GITHUB_DIR_PATH) -> List[Document]:
"""Load documents from a GitHub directory into a list of Document objects."""
pages = fetch_github_directory_contents(dir_path)
documents = []
for page in pages:
if page.get("type") == "file":
content = fetch_github_file_content(page["path"])
documents.append(Document(
page_content=content,
metadata={"source": page["name"], "path": page["path"]}
))
return documents
if __name__ == "__main__":
try:
documents = load_github_documents()
print(f"Loaded {len(documents)} document(s) from '{GITHUB_DIR_PATH}' in '{GITHUB_OWNER}/{GITHUB_REPO}'.\n")
for doc in documents:
print(f"Document Name: {doc.metadata['source']}\nContent Preview:\n{doc.page_content[:200]}...\n")
except (ValueError, RuntimeError, TypeError) as err:
print(f"Error: {err}")