lab3/API.py

66 lines
2.0 KiB
Python
Raw Permalink 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
from dotenv import load_dotenv
import requests
# 1. Загрузка токена
load_dotenv()
TOKEN = os.getenv("GITEA_TOKEN")
if not TOKEN:
print("ОШИБКА: Токен не найден в .env файле!")
print("Убедитесь, что в папке с скриптом есть файл .env с содержимым:")
print("GITEA_TOKEN=ваш_токен_здесь")
exit(1)
headers = {"Authorization": f"token {TOKEN}"}
# 2. Проверка токена
print("Проверяем токен...")
try:
user_info = requests.get(
"https://git.vyatsu.ru/api/v1/user",
headers=headers,
timeout=10
)
user_info.raise_for_status() # Проверит HTTP ошибки
# Проверка структуры ответа
if not user_info.json().get("login"):
print("Неожиданный формат ответа от API:")
print(user_info.json())
exit(1)
print(f"Токен работает. Ваш логин: {user_info.json()['login']}")
except Exception as e:
print(f"ОШИБКА при проверке токена: {e}")
print("Ответ сервера:", user_info.text if 'user_info' in locals() else "Нет ответа")
exit(1)
# 3. Создание issue
print("\nПытаемся создать issue...")
repo_owner = "stud179126"
repo_name = "lab3"
new_issue = {
"title": "Тестовый issue из Python",
"body": "Этот issue создан через API скриптом!"
}
try:
response = requests.post(
f"https://git.vyatsu.ru/api/v1/repos/{repo_owner}/{repo_name}/issues",
headers=headers,
json=new_issue,
timeout=10
)
if response.status_code == 201:
print("✅ Issue успешно создан!")
print("Ссылка:", response.json().get("html_url", "не найдена"))
else:
print(f"❌ Ошибка {response.status_code}:")
print(response.text)
except Exception as e:
print(f"ОШИБКА при создании issue: {e}")