mirror of
https://gitflic.ru/project/dbi471/git-switch.git
synced 2025-07-01 17:55:25 +00:00
Refactoring starts
This commit is contained in:
parent
4880ce9ea4
commit
e9b35c5a7b
7
main.py
7
main.py
@ -154,7 +154,7 @@ def clone_repos(repos,
|
||||
|
||||
try:
|
||||
logger.info(f'Creating repository {org}-{name} on gitflic.')
|
||||
# Создаем репозиторий на гитфлике
|
||||
# Создаём репозиторий на гитфлике
|
||||
gitflic_repo = requests.post(
|
||||
'http://localhost:8047/project',
|
||||
headers={
|
||||
@ -206,9 +206,10 @@ if __name__ == '__main__':
|
||||
user = g.get_user()
|
||||
repos = user.get_repos()
|
||||
|
||||
i = 0
|
||||
logger.info('Existing repos:')
|
||||
for i, repo in enumerate(repos):
|
||||
logger.info('Existing repos:')
|
||||
logger.info(f'ORG: {get_org_name(repo)} - REPO: {repo.name}\n')
|
||||
logger.info(f'ORG: {get_org_name(repo)} - REPO: {repo.name}')
|
||||
|
||||
clone_repos(repos, token, dst_folder, gitflic_token, is_private)
|
||||
|
||||
|
88
refactor_main.py
Normal file
88
refactor_main.py
Normal file
@ -0,0 +1,88 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# noinspection GrazieInspection
|
||||
"""
|
||||
Программа для клонирования репозиториев с https://github.com/ на https://gitflic.ru
|
||||
Программа была написана: @dikola
|
||||
Программа была отрефакторена: @SantaSpeen
|
||||
"""
|
||||
|
||||
# Удобная и встроенная в Python библиотека логирования. Подробнее: https://docs.python.org/3.10/library/logging.html
|
||||
import logging
|
||||
import sys
|
||||
import time
|
||||
|
||||
# Библиотека для работы с аргументами. Подробнее: https://click.palletsprojects.com/en/8.0.x
|
||||
import click
|
||||
# Библиотека для работы c GitHub API
|
||||
from github import Github, AuthenticatedUser, PaginatedList
|
||||
# Библиотека для работы с Gitflic API
|
||||
from gitflic import GitflicAuth, Gitflic
|
||||
|
||||
# Инициализируем логирование
|
||||
log_format = "%(asctime)s - %(name)-5s - %(levelname)-5s - %(message)s"
|
||||
logging.basicConfig(level=logging.INFO,
|
||||
format=log_format)
|
||||
log = logging.getLogger(name="GitSwitch")
|
||||
|
||||
# Настройка логирование в файл.
|
||||
fh = logging.FileHandler('git-switch.log')
|
||||
fh.setLevel(logging.INFO)
|
||||
fh.setFormatter(logging.Formatter(log_format))
|
||||
log.addHandler(fh)
|
||||
|
||||
|
||||
class GitSwitch:
|
||||
|
||||
# noinspection PyTypeChecker
|
||||
def __init__(self, gf_token: str, gh_token: str, dist_folder: str, is_private: bool):
|
||||
self.gf_token = gf_token
|
||||
self.gh_token = gh_token
|
||||
|
||||
self.dist_folder = dist_folder
|
||||
self.is_private = is_private
|
||||
|
||||
self.gh: Gitflic = None
|
||||
self.gf: Github = None
|
||||
self.gh_user: AuthenticatedUser = None
|
||||
|
||||
self.github_repos: PaginatedList = None
|
||||
|
||||
def authorization(self):
|
||||
""" Авторизуемся и получаем список репозиториев с GitHub """
|
||||
gf_session = GitflicAuth(self.gf_token)
|
||||
self.gf = Gitflic(gf_session)
|
||||
log.info(f"Logged into GitFlic as {self.gf.call('/user/me')['username']}")
|
||||
self.gh = Github(self.gh_token)
|
||||
self.gh_user = self.gh.get_user()
|
||||
log.info(f"Logged into Github as {self.gh_user.login}")
|
||||
|
||||
self.github_repos = self.gh_user.get_repos()
|
||||
|
||||
def start(self):
|
||||
self.authorization()
|
||||
|
||||
log.info("Fouded 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"Repositories found: {self.github_repos.totalCount}")
|
||||
|
||||
|
||||
# Инициализируем наши аргументы
|
||||
@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)
|
||||
def main(**kwargs):
|
||||
log.info("New log start.")
|
||||
log.info(f"Local time: {time.asctime()}")
|
||||
log.info(f"GitSwitch start with: {sys.argv} argumets.")
|
||||
|
||||
gs = GitSwitch(**kwargs)
|
||||
gs.start()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -1,4 +1,6 @@
|
||||
GitPython==3.1.27
|
||||
pygit2==1.9.0
|
||||
pygit2==1.9.1
|
||||
PyGithub==1.55
|
||||
gitflic==0.11
|
||||
requests==2.27.1
|
||||
click~=8.0.4
|
Loading…
x
Reference in New Issue
Block a user