lab3/create_gitea_repo.py
2026-05-07 16:28:02 +03:00

57 lines
1.2 KiB
Python

import os
import requests
API_URL = "https://git.vyatsu.ru/api/v1"
REPO_NAME = "API_REPO"
def load_token() -> str:
token = os.getenv("GITEA_TOKEN")
if token:
return token
if os.path.exists(".env"):
with open(".env", "r", encoding="utf-8") as file:
for line in file:
line = line.strip()
if line.startswith("GITEA_TOKEN="):
return line.split("=", 1)[1].strip()
return ""
token = load_token()
if not token:
print("Token not found. Create .env file with GITEA_TOKEN=your_token")
raise SystemExit(1)
headers = {"Authorization": f"token {token}"}
data = {
"name": REPO_NAME,
"description": "Repository created through Gitea API for lab 3",
"private": False,
"auto_init": True,
}
response = requests.post(
f"{API_URL}/user/repos",
headers=headers,
json=data,
timeout=10,
)
print(f"Status code: {response.status_code}")
if response.status_code == 201:
repo = response.json()
print("Repository created")
print(f"Name: {repo.get('full_name')}")
print(f"URL: {repo.get('html_url')}")
elif response.status_code == 409:
print(f"Repository {REPO_NAME} already exists")
else:
print(response.text)