From d4ea6911e8f43b2c19cd75d7ee2efdfe96fc6bd7 Mon Sep 17 00:00:00 2001 From: Vivek Anand Date: Mar 07 2017 12:09:08 +0000 Subject: [PATCH 1/4] Abort immediately when project name format is wrong with instructions Signed-off-by: Vivek Anand --- diff --git a/pagure_importer/commands/github.py b/pagure_importer/commands/github.py index ff48ca1..d050784 100644 --- a/pagure_importer/commands/github.py +++ b/pagure_importer/commands/github.py @@ -11,11 +11,25 @@ from pagure_importer.utils.exceptions import ( GithubRepoNotFound ) + +def proper_gh_project(ctx, param, value): + ''' Check whether the github project name given via --project + is in the right format of not ''' + if not value: + return + value = value.strip().strip('/') + if value.count('/') != 1: + click.echo('The name of the github project should be of the form:') + click.echo('/ or /') + ctx.exit() + + @app.command() @click.option('--username', prompt='Enter your Github Username', help="Github username") @click.option('--project', prompt='Enter github project name like pypingou/pagure', + callback=proper_gh_project, help="Github project like pypingou/pagure") @click.option('--gencsv', is_flag=True, default=False) @click.option('--status', type=click.Choice(['all', 'open', 'closed']), From 682f731c530481dc14c84a8820ceb25d134a6c3f Mon Sep 17 00:00:00 2001 From: Vivek Anand Date: Mar 07 2017 12:58:14 +0000 Subject: [PATCH 2/4] Check repo is present on GH before asking for more info Currently, it won't work if someone wants to import private project from github to pagure. But, pagure doesn't support private repository. So, that situation is unlikely to happen --- diff --git a/pagure_importer/commands/github.py b/pagure_importer/commands/github.py index d050784..53b366e 100644 --- a/pagure_importer/commands/github.py +++ b/pagure_importer/commands/github.py @@ -1,4 +1,7 @@ import click + +from github import Github + import pagure_importer from pagure_importer.app import app, REPO_PATH from pagure_importer.utils.importer_github import GithubImporter @@ -7,9 +10,6 @@ from pagure_importer.utils import ( ) import pagure_importer.utils.git as gitutils -from pagure_importer.utils.exceptions import ( - GithubRepoNotFound -) def proper_gh_project(ctx, param, value): @@ -22,15 +22,20 @@ def proper_gh_project(ctx, param, value): click.echo('The name of the github project should be of the form:') click.echo('/ or /') ctx.exit() + github_obj = Github() + repo = github_obj.get_repo(value) + if not hasattr(repo, 'name'): + click.echo("Repo doesn't exist or is private") + ctx.exit() @app.command() @click.option('--username', prompt='Enter your Github Username', help="Github username") @click.option('--project', - prompt='Enter github project name like pypingou/pagure', + prompt='Github project name like pypingou/pagure', callback=proper_gh_project, - help="Github project like pypingou/pagure") + help="Github project name like pypingou/pagure") @click.option('--gencsv', is_flag=True, default=False) @click.option('--status', type=click.Choice(['all', 'open', 'closed']), default='all', @@ -62,11 +67,6 @@ def github(username, project, nopush, status, gencsv): repo = github_importer.github.get_repo( github_importer.github_project_name) - try: - repo_name = repo.name - except: - raise GithubRepoNotFound( - 'Repo not found, project name wrong') github_importer.import_issues(repo, status=status) # update the local git repo From b9ca0b496d8bf5519917a2a34a056cb4e96c2191 Mon Sep 17 00:00:00 2001 From: Vivek Anand Date: Mar 08 2017 08:53:01 +0000 Subject: [PATCH 3/4] Move validate_gh_project to git utils Signed-off-by: Vivek Anand --- diff --git a/pagure_importer/commands/github.py b/pagure_importer/commands/github.py index 53b366e..d1fdc3a 100644 --- a/pagure_importer/commands/github.py +++ b/pagure_importer/commands/github.py @@ -6,35 +6,19 @@ import pagure_importer from pagure_importer.app import app, REPO_PATH from pagure_importer.utils.importer_github import GithubImporter from pagure_importer.utils import ( - gh_get_contributors, gh_get_issue_users, gh_assemble_users, + gh_get_contributors, gh_get_issue_users, + gh_assemble_users, validate_gh_project, ) import pagure_importer.utils.git as gitutils -def proper_gh_project(ctx, param, value): - ''' Check whether the github project name given via --project - is in the right format of not ''' - if not value: - return - value = value.strip().strip('/') - if value.count('/') != 1: - click.echo('The name of the github project should be of the form:') - click.echo('/ or /') - ctx.exit() - github_obj = Github() - repo = github_obj.get_repo(value) - if not hasattr(repo, 'name'): - click.echo("Repo doesn't exist or is private") - ctx.exit() - - @app.command() @click.option('--username', prompt='Enter your Github Username', help="Github username") @click.option('--project', prompt='Github project name like pypingou/pagure', - callback=proper_gh_project, + callback=validate_gh_project, help="Github project name like pypingou/pagure") @click.option('--gencsv', is_flag=True, default=False) @click.option('--status', type=click.Choice(['all', 'open', 'closed']), diff --git a/pagure_importer/utils/__init__.py b/pagure_importer/utils/__init__.py index 2e11f18..2b5f3c6 100644 --- a/pagure_importer/utils/__init__.py +++ b/pagure_importer/utils/__init__.py @@ -291,3 +291,20 @@ def get_secure_filename(attachment, filename): filename = '%s-%s' % (hashlib.sha256(attachment).hexdigest(), werkzeug.secure_filename(str(filename))) return filename + + +def validate_gh_project(ctx, param, value): + ''' Validate github project name, whether it's in the right format or not + and if the repo exists or not ''' + if not value: + return + value = value.strip().strip('/') + if value.count('/') != 1: + click.echo('The name of the github project should be of the form:') + click.echo('/ or /') + ctx.exit() + github_obj = Github() + repo = github_obj.get_repo(value) + if not hasattr(repo, 'name'): + click.echo("Repo doesn't exist or is private") + ctx.exit() From 682ed3c3844911c869460310357377005198ac8a Mon Sep 17 00:00:00 2001 From: Vivek Anand Date: Mar 08 2017 09:16:04 +0000 Subject: [PATCH 4/4] Remove unused import, move back to try/except from hasattr Using hasattr is returning github.GithubException.Unknownobjectexception. With this commit, we will catch that exception and return a reasonable msg. --- diff --git a/pagure_importer/commands/github.py b/pagure_importer/commands/github.py index d1fdc3a..f66aea8 100644 --- a/pagure_importer/commands/github.py +++ b/pagure_importer/commands/github.py @@ -1,7 +1,5 @@ import click -from github import Github - import pagure_importer from pagure_importer.app import app, REPO_PATH from pagure_importer.utils.importer_github import GithubImporter diff --git a/pagure_importer/utils/__init__.py b/pagure_importer/utils/__init__.py index 2b5f3c6..4ea0c8b 100644 --- a/pagure_importer/utils/__init__.py +++ b/pagure_importer/utils/__init__.py @@ -13,7 +13,7 @@ import werkzeug from urllib.parse import urlparse from configparser import ConfigParser from github import Github -from github.GithubException import TwoFactorException +from github.GithubException import TwoFactorException, UnknownObjectException from pagure_importer.utils.exceptions import FileNotFound, EmailNotFound from pagure_importer.app import REPO_PATH @@ -305,6 +305,9 @@ def validate_gh_project(ctx, param, value): ctx.exit() github_obj = Github() repo = github_obj.get_repo(value) - if not hasattr(repo, 'name'): + + try: + repo.name + except UnknownObjectException: click.echo("Repo doesn't exist or is private") ctx.exit()