TCP_UDP_HTTP_Gitea/gitea_api.py
2026-05-08 02:20:39 +03:00

66 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import requests
from dotenv import load_dotenv
load_dotenv()
TOKEN_READ = os.getenv("GITEA_TOKEN_READ")
TOKEN_WRITE = os.getenv("GITEA_TOKEN_WRITE")
if not TOKEN_READ:
print("Ошибка: токен для чтения не найден. Добавьте GITEA_TOKEN_READ в .env")
exit(1)
headers_read = {"Authorization": f"token {TOKEN_READ}"}
print("=" * 50)
print("Проверка подключения к Gitea API")
print("=" * 50)
response = requests.get("https://git.vyatsu.ru/api/v1/user", headers=headers_read)
if response.status_code == 200:
user_info = response.json()
print(f"Подключение успешно!")
print(f"Пользователь: {user_info.get('login')}")
print(f"Email: {user_info.get('email')}")
print(f"Полное имя: {user_info.get('full_name')}")
print(f"ID: {user_info.get('id')}")
else:
print(f"Ошибка: {response.status_code}")
print(response.text)
exit(1)
# ============================================
# СОЗДАНИЕ РЕПОЗИТОРИЯ
# ============================================
if not TOKEN_WRITE:
print("\n⚠️ Токен для записи не найден. Пропускаем создание репозитория.")
exit(0)
headers_write = {"Authorization": f"token {TOKEN_WRITE}"}
print("\n" + "=" * 50)
print("Создание нового репозитория")
print("=" * 50)
repo_data = {
"name": "my-lab3-repo",
"description": "Создано через API Gitea",
"private": False
}
response = requests.post(
"https://git.vyatsu.ru/api/v1/user/repos",
headers=headers_write,
json=repo_data
)
if response.status_code == 201:
print(f"✅ Репозиторий создан: {response.json()['html_url']}")
elif response.status_code == 422:
print("⚠️ Репозиторий с таким именем уже существует")
else:
print(f"❌ Ошибка: {response.status_code}")
print(response.text)