3laba/3lb/gitea_api.py
2026-05-05 19:53:04 +03:00

56 lines
1.8 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
import requests
from dotenv import load_dotenv
# 1. Загружаем токен из файла .env
load_dotenv()
TOKEN = os.getenv("GITEA_TOKEN")
if not TOKEN:
print(" Ошибка: Токен не найден! Проверь файл .env")
exit()
# Заголовки для авторизации (стандарт Gitea)
headers = {
"Authorization": f"token {TOKEN}",
"Content-Type": "application/json"
}
def check_user():
print("--- Шаг 1: Проверка профиля ---")
url = "https://git.vyatsu.ru/api/v1/user"
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
print(f" Успешный вход! Логин: {data.get('login')}")
else:
print(f" Ошибка авторизации: {response.status_code}")
def create_repo(repo_name):
print(f"\n--- Шаг 2: Создание репозитория '{repo_name}' ---")
url = "https://git.vyatsu.ru/api/v1/user/repos"
payload = {
"name": repo_name,
"description": "Лабораторная работа по сетям",
"private": False,
"auto_init": True # Создаст сразу README.md
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 201:
print(f" Репозиторий создан: {response.json().get('html_url')}")
elif response.status_code == 422:
print(" Репозиторий с таким именем уже существует.")
else:
print(f" Что-то пошло не так: {response.text}")
if __name__ == "__main__":
check_user()
# В названии репозитория укажи что-то уникальное, чтобы не пересекаться с другими
create_repo("zadanie3")