From f9e86d4c52ac4475d3e020a4d015284667baf31b Mon Sep 17 00:00:00 2001 From: Lenka Segura Date: Jul 29 2019 19:44:04 +0000 Subject: Per repo config storage and api token handling This commit changes the config into being stored keyed by the repo url. This way the configuration file can hold configuration for many working repos. Also, it will properly require the API token for those commands that need it. Signed-off-by: Lenka Segura --- diff --git a/cranc/commands/create.py b/cranc/commands/create.py index 29d89f3..a2fcb75 100644 --- a/cranc/commands/create.py +++ b/cranc/commands/create.py @@ -23,7 +23,10 @@ def create(): @click.pass_context def create_pr(ctx, repo_to, title, branch_to, branch_from, repo_from): """this command creates a new pull request""" - api_key = ctx.obj['api_key'] + try: + api_token = ctx.obj['api_token'] + except KeyError: + raise click.UsageError('Missing required API token to create a PR') project = ctx.obj['project'] instance_url = ctx.obj['instance_url'] repo_to = utils.get_dict_from_str(repo_to) diff --git a/cranc/commands/get.py b/cranc/commands/get.py index 00dd80e..fb343c1 100644 --- a/cranc/commands/get.py +++ b/cranc/commands/get.py @@ -26,7 +26,7 @@ def pr_list(ctx, status, assignee, author): :param author: filters the author of the requests :return: """ - api_key = ctx.obj['api_key'] + api_token = ctx.obj.get('api_token') project = ctx.obj['project'] instance_url = ctx.obj['instance_url'] project = ctx.obj['project'] @@ -53,7 +53,7 @@ def pr_list(ctx, status, assignee, author): @click.pass_context def issue_list(ctx, status, tags, assignee, author, milestones, priority, no_stones, since): """Prints list of issues""" - api_key = ctx.obj['api_key'] + api_token = ctx.obj.get('api_token') project = ctx.obj['project'] instance_url = ctx.obj['instance_url'] PAGURE = libpagure.Pagure(pagure_token=api_token, diff --git a/cranc/commands/merge.py b/cranc/commands/merge.py index de96001..acf0752 100644 --- a/cranc/commands/merge.py +++ b/cranc/commands/merge.py @@ -20,7 +20,10 @@ def merge(): @click.pass_context def merge_pr(ctx, request_id): """This command merges a pull request""" - api_key = ctx.obj['api_key'] + try: + api_token = ctx.obj['api_token'] + except KeyError: + raise click.UsageError('Missing required API token to merge PRs') project = ctx.obj['project'] instance_url = ctx.obj['instance_url'] PAGURE = libpagure.Pagure( diff --git a/cranc/cranc.py b/cranc/cranc.py index b930571..292bd76 100644 --- a/cranc/cranc.py +++ b/cranc/cranc.py @@ -29,8 +29,8 @@ from cranc import utils CONFIG = os.path.expanduser('~/.config/cranc') -class ApiKey(click.ParamType): - name = 'api-key' +class ApiToken(click.ParamType): + name = 'api-token' def convert(self, value, param, ctx): found = re.match(r'[0-9a-f]{32}', value) @@ -50,8 +50,8 @@ _log = logging.getLogger(__name__) @click.group() @click.option( - '--api-key', '-a', - type=ApiKey(), + '--api-token', '-a', + type=ApiToken(), help='your API key for Pagure.io', ) @click.option( @@ -67,12 +67,16 @@ _log = logging.getLogger(__name__) type=click.Path(), default='~/.config/cranc.cfg') @click.pass_context -def cranc(ctx, api_key, repo_url, instance_url, config_file): +def cranc(ctx, api_token, repo_url, instance_url, config_file): filename = os.path.expanduser(config_file) ctx.obj = { 'config_file': filename, } + if not repo_url: + repo_url = utils.get_repo() + ctx.obj['repo_url'] = repo_url + if os.path.exists(filename): try: with open(filename) as cfg: @@ -80,25 +84,26 @@ def cranc(ctx, api_key, repo_url, instance_url, config_file): except json.decoder.JSONDecodeError: _log.info('No pre-existing cranc json config') + if repo_url in json_cfg: + ctx.obj.update(json_cfg[repo_url]) - ctx.obj.update(json_cfg) - if api_key: - ctx.obj['api_key'] = api_key - if repo_url: - ctx.obj['repo_url'] = repo_url + if api_token: + ctx.obj['api_token'] = api_token if instance_url: ctx.obj['instance_url'] = instance_url if ctx.obj.get('repo_url'): ctx.obj['project'] = utils.project_name(ctx.obj['repo_url']) + if 'instance_url' not in ctx.obj: + ctx.obj['instance_url'] = utils.guess_instance_url(repo_url) @cranc.command() @click.pass_context def config(ctx): config_file = ctx.obj['config_file'] - api_key = click.prompt('Please enter your API token', - default=ctx.obj.get('api_key', '') + api_token = click.prompt('Please enter your API token', + default=ctx.obj.get('api_token', '') ) repo_url = click.prompt('Please enter your repo url', default=ctx.obj.get('repo_url', utils.get_repo()) @@ -109,9 +114,11 @@ def config(ctx): utils.guess_instance_url(repo_url))) with open(config_file, 'w') as cfg: cfg_content = json.dumps({ - 'api_key': api_key, - 'repo_url': repo_url, - 'instance_url': instance_url + repo_url: { + 'api_token': api_token, + 'repo_url': repo_url, + 'instance_url': instance_url + } }, sort_keys=True, indent=4) cfg.write(cfg_content)