65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
from pathlib import Path
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from byteb4rb1e.utils.http import client as http_client
|
|
|
|
|
|
GITHUB_API = "https://api.github.com"
|
|
|
|
|
|
def http_headers(token: Optional[str]) -> Dict[str, str]:
|
|
headers = {
|
|
"Accept": "application/vnd.github+json",
|
|
"User-Agent": "sphinx-h5p-worker1"
|
|
}
|
|
if token:
|
|
# Use standard PAT header; token not logged anywhere.
|
|
headers["Authorization"] = f"Bearer {token}"
|
|
return headers
|
|
|
|
|
|
def blob_sha(path: Path) -> str:
|
|
"""Calculate Git blob SHA-1 for a file, matching GitHub API 'sha'."""
|
|
data = path.read_bytes()
|
|
header = f"blob {len(data)}\0".encode("utf-8")
|
|
store = header + data
|
|
return hashlib.sha1(store).hexdigest()
|
|
|
|
|
|
def list_org_repos(org: str, token: Optional[str]) -> List[Dict[str, Any]]:
|
|
repos: List[Dict[str, Any]] = []
|
|
page = 1
|
|
per_page = 100
|
|
while True:
|
|
url = f"{GITHUB_API}/orgs/{org}/repos"
|
|
resp = http_client.get(
|
|
url,
|
|
params={"page": page, "per_page": per_page, "type": "public"},
|
|
headers=http_headers(token),
|
|
)
|
|
if resp.status_code != 200:
|
|
raise RuntimeError(f"Failed to list repos for org {org}: {resp.status_code} {resp.text}")
|
|
batch = resp.json()
|
|
if not batch:
|
|
break
|
|
repos.extend(batch)
|
|
page += 1
|
|
return repos
|
|
|
|
|
|
def fetch_file(
|
|
org: str,
|
|
repo: str,
|
|
path: str,
|
|
token: str
|
|
) -> http_client.HttpResponse:
|
|
"""
|
|
"""
|
|
url = f"{GITHUB_API}/repos/{org}/{repo}/{path}"
|
|
|
|
return http_client.get(
|
|
url,
|
|
headers=http_headers(token),
|
|
)
|