diff --git a/.env_sample b/.env_sample new file mode 100644 index 0000000..1c87b9f --- /dev/null +++ b/.env_sample @@ -0,0 +1,3 @@ +GITHUB_USERNAME=your_github_username +GITHUB_TOKEN=your_github_token +REPO_NAME=test_repo diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..986eb66 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv +.idea + +.env diff --git a/README.md b/README.md new file mode 100644 index 0000000..5854e31 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +# GitHub API Test + +## Описание +Этот проект создаёт, проверяет и удаляет репозиторий на GitHub с помощью GitHub API. + +![github_repo.png](img/github_repo.png) +![github_api_test.png](img/github_api_test.png) + +## Установка и настройка + +### 1. Клонируйте репозиторий: + +```bash +git clone https://github.com/QuadDarv1ne/github_api_test.git +cd github_api_test +``` + +#### Структура проекта: +``` +github_api_test/ +│ +├── img/ +│ ├── github_repo.png +│ └── github_api_test.png +│ +├── test_api.py # Основной скрипт +├── .env_sample # Файл с переменными окружения (.env) +├── .gitignore +├── requirements.txt # Зависимости +└── README.md # Инструкция по установке и запуску +``` + +### 2. Установка необходимых зависимостей + +```bash +pip install -r requirements.txt +``` + +### 3. Запуск проекта + +```bash +python test_api.py +``` + +Этот проект предоставляет воспроизводимый тест, который можно запустить на любом компьютере. + + + +**Автор:** © Дуплей Максим Игоревич + +**Дата:** 12.09.2024 + +**Версия 1.0** \ No newline at end of file diff --git a/img/github_api_test.png b/img/github_api_test.png new file mode 100644 index 0000000..c9aeccd Binary files /dev/null and b/img/github_api_test.png differ diff --git a/img/github_repo.png b/img/github_repo.png new file mode 100644 index 0000000..8940a63 Binary files /dev/null and b/img/github_repo.png differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..df7458c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +requests +python-dotenv diff --git a/test_api.py b/test_api.py new file mode 100644 index 0000000..540a351 --- /dev/null +++ b/test_api.py @@ -0,0 +1,52 @@ +import os +import requests +from dotenv import load_dotenv + +# Загрузка переменных окружения +load_dotenv() + +# Переменные окружения +GITHUB_API_URL = "https://api.github.com" +GITHUB_USERNAME = os.getenv('GITHUB_USERNAME') +GITHUB_TOKEN = os.getenv('GITHUB_TOKEN') +REPO_NAME = os.getenv('REPO_NAME') + +# Заголовки с аутентификацией +headers = { + "Authorization": f"token {GITHUB_TOKEN}" +} + +def create_repo(): + url = f"{GITHUB_API_URL}/user/repos" + data = { + "name": REPO_NAME, + "private": False + } + response = requests.post(url, json=data, headers=headers) + if response.status_code == 201: + print(f"Репозиторий {REPO_NAME} успешно создан.") + else: + print(f"Ошибка при создании репозитория: {response.status_code} - {response.json()}") + +def check_repo(): + url = f"{GITHUB_API_URL}/users/{GITHUB_USERNAME}/repos" + response = requests.get(url, headers=headers) + repos = response.json() + repo_names = [repo['name'] for repo in repos] + if REPO_NAME in repo_names: + print(f"Репозиторий {REPO_NAME} найден.") + else: + print(f"Репозиторий {REPO_NAME} не найден.") + +def delete_repo(): + url = f"{GITHUB_API_URL}/repos/{GITHUB_USERNAME}/{REPO_NAME}" + response = requests.delete(url, headers=headers) + if response.status_code == 204: + print(f"Репозиторий {REPO_NAME} успешно удалён.") + else: + print(f"Ошибка при удалении репозитория: {response.status_code} - {response.json()}") + +if __name__ == "__main__": + create_repo() + check_repo() + delete_repo()