mirror of
https://gitflic.ru/project/dbi471/git-switch.git
synced 2026-02-16 02:20:48 +00:00
Add field apply_organisations
This commit is contained in:
@@ -9,15 +9,21 @@
|
||||
|
||||
# Удобная и встроенная в Python библиотека логирования. Подробнее: https://docs.python.org/3.10/library/logging.html
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
# Библиотека для работы с аргументами. Подробнее: https://click.palletsprojects.com/en/8.0.x
|
||||
from typing import NoReturn, Union
|
||||
|
||||
import click
|
||||
# Библиотека для работы c GitHub API
|
||||
from github import Github, AuthenticatedUser, PaginatedList
|
||||
# Библиотека для работы с Gitflic API
|
||||
from gitflic import GitflicAuth, Gitflic
|
||||
# Библиотека для работы с git
|
||||
from git import Repo
|
||||
|
||||
|
||||
# Инициализируем логирование
|
||||
log_format = "%(asctime)s - %(name)-5s - %(levelname)-5s - %(message)s"
|
||||
@@ -35,12 +41,13 @@ log.addHandler(fh)
|
||||
class GitSwitch:
|
||||
|
||||
# noinspection PyTypeChecker
|
||||
def __init__(self, gf_token: str, gh_token: str, dist_folder: str, is_private: bool):
|
||||
def __init__(self, gf_token: str, gh_token: str, dist_folder: str, apply_private: bool, apply_organisations: bool):
|
||||
self.gf_token = gf_token
|
||||
self.gh_token = gh_token
|
||||
|
||||
self.dist_folder = dist_folder
|
||||
self.is_private = is_private
|
||||
self.clone_folder = dist_folder
|
||||
self.apply_private = apply_private
|
||||
self.apply_organisations = apply_organisations
|
||||
|
||||
self.gh: Gitflic = None
|
||||
self.gf: Github = None
|
||||
@@ -48,7 +55,9 @@ class GitSwitch:
|
||||
|
||||
self.github_repos: PaginatedList = None
|
||||
|
||||
def authorization(self):
|
||||
self.get_login = lambda repo_info: repo_info.organization.login if repo_info.organization else self.gh_user.login
|
||||
|
||||
def authorization(self) -> NoReturn:
|
||||
""" Авторизуемся и получаем список репозиториев с GitHub """
|
||||
gf_session = GitflicAuth(self.gf_token)
|
||||
self.gf = Gitflic(gf_session)
|
||||
@@ -59,22 +68,57 @@ class GitSwitch:
|
||||
|
||||
self.github_repos = self.gh_user.get_repos()
|
||||
|
||||
def start(self):
|
||||
def get_github_repo(self, repo_info) -> Union[Repo, None]:
|
||||
login = self.get_login(repo_info)
|
||||
name = repo_info.name
|
||||
path = os.path.join(self.clone_folder, login, name)
|
||||
proto = "https://"
|
||||
if not os.path.exists(path):
|
||||
clone_url = repo_info.clone_url.replace(proto, proto + self.gh_token + '@')
|
||||
log.info(f'Clone {login} : {name}; Clone path: {path};')
|
||||
return Repo.clone_from(clone_url, path)
|
||||
|
||||
log.info(f"Path: {path} already exists.")
|
||||
return None
|
||||
|
||||
def get_gitflic_repo(self) -> Union[dict, None]:
|
||||
""" Я хуй знает что тут писать. Какой нахуй localhost? Бесплатный блять host или чё? """
|
||||
pass
|
||||
|
||||
def push_into_gitflic(self, repo, url) -> bool:
|
||||
return False
|
||||
|
||||
def clone(self):
|
||||
for repo_info in self.github_repos:
|
||||
github_repo = self.get_github_repo(repo_info)
|
||||
if not github_repo: continue
|
||||
gitflic_repo = self.get_gitflic_repo()
|
||||
if not gitflic_repo: continue
|
||||
if self.push_into_gitflic(github_repo, gitflic_repo['sshTransportUrl']):
|
||||
log.info(f"Repository {self.get_login(repo_info)}-{repo_info.name} successfully cloned.")
|
||||
|
||||
def start(self) -> NoReturn:
|
||||
self.authorization()
|
||||
|
||||
log.info("Fouded github repositories:")
|
||||
log.info("GitHub repositories:")
|
||||
for repo in self.github_repos:
|
||||
login = repo.organization.login if repo.organization else self.gh_user.login
|
||||
log.info(f'GitGub => {login} : {repo.name}')
|
||||
log.info(f'GitGub => {self.get_login(repo)} : {repo.name}')
|
||||
log.info(f"Repositories found: {self.github_repos.totalCount}")
|
||||
|
||||
if input("Do you agree to copying these repositories to GitFlic? (y/n) ").lower() != "y":
|
||||
log.info("Stopped by the user.")
|
||||
exit(0)
|
||||
|
||||
self.clone()
|
||||
|
||||
|
||||
# Инициализируем наши аргументы
|
||||
@click.command()
|
||||
@click.option("--gf_token", help="Your GitFlic token.", type=str, required=True)
|
||||
@click.option("--gh_token", help="Your GitHub token.", type=str, required=True)
|
||||
@click.option("--dist_folder", help="A folder to clone repositories into.", default="./cloned-repos", required=False)
|
||||
@click.option("--is_private", help="Sets the mode for copying repositories.", default=True, required=False)
|
||||
@click.option("--dist_folder", help="Directory where to download repositories.", default="./cloned-repos", required=False)
|
||||
@click.option("--apply_private", help="Need to copy private repositories or not.", default=False, required=False)
|
||||
@click.option("--apply_organisations", help="Need to copy organisations repositories or not.", default=False, required=False)
|
||||
def main(**kwargs):
|
||||
log.info("New log start.")
|
||||
log.info(f"Local time: {time.asctime()}")
|
||||
|
||||
Reference in New Issue
Block a user