38 أسطر
1022 B
Python
38 أسطر
1022 B
Python
import os
|
|
import base64
|
|
from typing import Dict, Any
|
|
from helpers import get_headers, build_client
|
|
|
|
USERNAME = os.getenv("GITPASHA__USERNAME", "")
|
|
BASE_URL = os.getenv(
|
|
"GITPASHA_BASE_URL",
|
|
"https://app.gitpasha.com/api/v1"
|
|
).rstrip("/")
|
|
|
|
|
|
def api_create_readme(repo: str, content: str) -> Dict[str, Any]:
|
|
if "/" not in repo:
|
|
if not USERNAME:
|
|
raise ValueError("GITPASHA__USERNAME not set in .env")
|
|
repo = f"{USERNAME}/{repo}"
|
|
|
|
with build_client() as client:
|
|
url = f"{BASE_URL}/repos/{repo}/contents/README.md"
|
|
encoded = base64.b64encode(content.encode("utf-8")).decode("utf-8")
|
|
body = {
|
|
"message": "Add README",
|
|
"content": encoded,
|
|
}
|
|
res = client.put(
|
|
url,
|
|
headers=get_headers(),
|
|
json=body
|
|
)
|
|
|
|
if res.status_code < 300:
|
|
return res.json()
|
|
return {
|
|
"error": "Failed to create README",
|
|
"response": res.text,
|
|
}
|