Files
lab3/create_gitea_repo.py
T

59 lines
1.3 KiB
Python

import os
import sys
import requests
API_URL = "https://git.vyatsu.ru/api/v1"
REPO_NAME = "lab3"
if len(sys.argv) > 1:
REPO_NAME = sys.argv[1]
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)