diff --git a/.gitignore b/.gitignore index bd218e7..f274c47 100644 --- a/.gitignore +++ b/.gitignore @@ -63,7 +63,7 @@ __pycache__/ .Python env/ develop-eggs/ -dist/ +# dist/ downloads/ eggs/ lib/ diff --git a/.python-version b/.python-version deleted file mode 100644 index 33f465d..0000000 --- a/.python-version +++ /dev/null @@ -1 +0,0 @@ -3.9.1 \ No newline at end of file diff --git a/README.md b/README.md index 0eceeb9..41b8285 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ Use python version > 3.8 +## Build + +```bash +pyinstaller -F github_mirror.py +``` + ## Argumetns: @@ -25,7 +31,7 @@ Use python version > 3.8 - ```Please provide github token to get access to private repositories``` [github tokens](https://github.com/settings/tokens) -- -l GITURL, --giturl GITURL ```Provide git url. Default link``` https://git.mywistr.com +- -l GITURL, --giturl GITURL ```Provide git url. Default link``` https://git.mywistr.ru ## Usage diff --git a/core/argument_parser.py b/core/argument_parser.py index 29a80aa..e50f446 100644 --- a/core/argument_parser.py +++ b/core/argument_parser.py @@ -1,6 +1,6 @@ from argparse import ArgumentParser -GIT_URL = 'https://git.mywistr.com' +GIT_URL = 'https://git.mywistr.ru' USAGE = '''github_mirror [-h] [-g GROUP] (-u URLS [URLS ...] | -f FILE) -t TOKEN [-T GitHubTOKEN] -------------------------------------------------- diff --git a/core/repo_creator.py b/core/repo_creator.py index 80bb51a..67dc1da 100644 --- a/core/repo_creator.py +++ b/core/repo_creator.py @@ -1,5 +1,6 @@ import random from typing import Union +from http import HTTPStatus import requests from requests import Response @@ -9,13 +10,22 @@ from core.utils import logger class RepositoryCreator: - def __init__(self, gitlab_url: str, headers: dict): - self.gitlab_url = gitlab_url + def __init__(self, git_url: str, headers: dict) -> None: + self.git_url = git_url self.headers = headers - self.HTTP_201_CREATED = 201 - self.HTTP_200_OK = 200 - def __gitlab_request(self, method: str, url: str, data: dict = None) -> Union[Response, None]: + def create_repository_mirror(self, github_url: str, group_id: str, auth_token: str) -> None: + """ + Base action for one thread. Creates repository, add mirror url and triggers pull at te end + + :param github_url: GitGub url which will be mirrored + :param group_id: Gitlab group id which contains created repository + :param auth_token: GitGub token to access private repositories + """ + + self.__create_new_project(github_url, group_id, auth_token) + + def __git_request(self, method: str, url: str, data: dict = None) -> Union[Response, None]: """ Create request to gitlab @@ -51,28 +61,16 @@ class RepositoryCreator: if auth_token: git_data['auth_token'] = auth_token - request = self.__gitlab_request('POST', f'{self.gitlab_url}/api/v1/repos/migrate', git_data) + request = self.__git_request('POST', f'{self.git_url}/api/v1/repos/migrate', git_data) try: - if request.status_code == self.HTTP_201_CREATED: - repo_data = request.json() - name_with_namespace = repo_data.get('full_name', None) + if request.status_code == HTTPStatus.CREATED: + response = request.json() + name_with_namespace = response.get('full_name', None) if name_with_namespace: logger.info(f'Repository {name_with_namespace} has been created') else: - logger.info(f'Repository {repo_data["name"]} has been created') - return repo_data['id'] + logger.info(f'Repository {response["name"]} has been created') else: logger.error(f'Cant create {name} project. Status code: {request.status_code}. Reason: {request.text}') except AttributeError: pass - - def create_repository_mirror(self, github_url: str, group_id: str, auth_token: str): - """ - Base action for one thread. Creates repository, add mirror url and triggers pull at te end - - :param github_url: GitGub url which will be mirrored - :param group_id: Gitlab group id which contains created repository - :param auth_token: GitGub token to access private repositories - """ - - self.__create_new_project(github_url, group_id, auth_token) diff --git a/core/utils.py b/core/utils.py index ab6abb1..8b1ba3a 100644 --- a/core/utils.py +++ b/core/utils.py @@ -27,7 +27,7 @@ else: logger.addHandler(console_handler) -def threads_ready_statistic(threads: List[Thread]): +def threads_ready_statistic(threads: List[Thread]) -> None: """ Getting information how many threads are running right now diff --git a/dist/github_mirror b/dist/github_mirror new file mode 100755 index 0000000..bdad877 Binary files /dev/null and b/dist/github_mirror differ diff --git a/github_mirror.py b/github_mirror.py index 6953aa7..47ad0ae 100644 --- a/github_mirror.py +++ b/github_mirror.py @@ -6,7 +6,7 @@ from core.repo_creator import RepositoryCreator from core.utils import logger, threads_ready_statistic -def main(): +def main() -> None: parser = create_parser() args = parser.parse_args(sys.argv[1:]) @@ -24,10 +24,10 @@ def main(): # parse gitlab group of repositories if it exists group_id = args.group if args.group else None - git_url = args.giturl # if not provided used default value https://git.mywistr.com + git_url = args.giturl # if not provided used default value https://git.mywistr.ru headers = {'Authorization': f'token {args.token}'} # git user token must be provided - repository_creator = RepositoryCreator(gitlab_url=git_url, headers=headers) + repository_creator = RepositoryCreator(git_url=git_url, headers=headers) github_token = args.githubtoken if args.githubtoken else None # used for access to personal GitHub repositories diff --git a/requirements.txt b/requirements.txt index 663bd1f..8ca829e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -requests \ No newline at end of file +requests +pyinstaller \ No newline at end of file