3laba/3lb/gittea_api.py

62 lines
2.2 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()
API_TOKEN = os.getenv("GITEA_TOKEN")
BASE_URL = "https://git.vyatsu.ru/api/v1"
if not API_TOKEN:
raise ValueError("Критическая ошибка: GITEA_TOKEN отсутствует в .env")
# Настраиваем сессию, чтобы не прописывать headers каждый раз
session = requests.Session()
session.headers.update({
"Authorization": f"token {API_TOKEN}",
"Accept": "application/json"
})
def get_profile_info():
"""Проверка подключения к серверу"""
print("[*] Соединение с сервером git.vyatsu.ru...")
try:
response = session.get(f"{BASE_URL}/user")
response.raise_for_status()
user_data = response.json()
print(f"[+] Авторизация пройдена: {user_data.get('full_name')} (@{user_data.get('login')})")
except requests.exceptions.RequestException as e:
print(f"[-] Сбой подключения: {e}")
def deploy_new_repository(name):
"""Регистрация нового проекта в Gitea"""
print(f"[*] Попытка регистрации проекта '{name}'...")
settings = {
"name": name,
"description": "Автоматически созданный репозиторий (Lab Net)",
"private": False,
"auto_init": True,
"readme": "Default"
}
resp = session.post(f"{BASE_URL}/user/repos", json=settings)
if resp.status_code == 201:
repo_url = resp.json().get('html_url')
print(f"[Успех] Проект доступен по адресу: {repo_url}")
elif resp.status_code == 422:
print("[!] Внимание: Имя уже занято, выберите другое.")
else:
print(f"[Ошибка] Код ответа: {resp.status_code} | {resp.text}")
if __name__ == "__main__":
# Запускаем диагностику и создание
get_profile_info()
# Можно добавить ввод имени через input для разнообразия
target_name = "lab_work_3_network"
deploy_new_repository(target_name)