From ead0cf51deb10a53068be114e574be34a188b71b Mon Sep 17 00:00:00 2001 From: MasterofJOKers Date: Tue, 25 Oct 2022 23:51:14 +0200 Subject: [PATCH] Move Comment fetching into click callback Instead of calling the get_comment() we now have a callback for click, that's called to validate and transform the given comment id into a Comment object from JIRA. --- jiracli/cli.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/jiracli/cli.py b/jiracli/cli.py index e497ebf..834794e 100644 --- a/jiracli/cli.py +++ b/jiracli/cli.py @@ -490,7 +490,12 @@ def comment_create(client, issue): client.add_comment(issue.key, text) -def get_comment(client, issue, comment_id): +def get_comment(ctx, param, comment_id): + """click callback returning a Comment for the comment_id value given by the user""" + from jira.exceptions import JIRAError + + issue = ctx.obj['issue'] + if not comment_id: from clintermission import cli_select_item options = [] @@ -501,18 +506,21 @@ def get_comment(client, issue, comment_id): options.append((f"{comment.created} - {comment.author} - {body}", comment.id)) _, comment_id = cli_select_item(options) - return client.comment(issue.key, comment_id, expand=['body']) + client = ctx.obj['client'] + try: + return client.comment(issue.key, comment_id, expand=['body']) + except JIRAError as e: + raise click.ClickException(f"Problem retrieving {comment_id} for issue {issue.key}: {e.text}") @comment.command('edit') -@click.argument('comment_id', required=False) +@click.argument('comment', required=False, callback=get_comment) @pass_issue @pass_client -def comment_edit(client, issue, comment_id): +def comment_edit(client, issue, comment): """Edit the text of an issues comment""" - comment = get_comment(client, issue, comment_id) - text = prompt(f"Comment text to set as new body for comment {comment_id} on {issue.key}", + text = prompt(f"Comment text to set as new body for comment {comment.id} on {issue.key}", initial_content=comment.body) if text == comment.body: print("No changes. Aborting.") @@ -522,12 +530,10 @@ def comment_edit(client, issue, comment_id): @comment.command('delete') -@click.argument('comment_id', required=False) +@click.argument('comment', required=False, callback=get_comment) @pass_issue @pass_client -def comment_delete(client, issue, comment_id): - comment = get_comment(client, issue, comment_id) - +def comment_delete(client, issue, comment): # TODO print formatted comment for verification click.confirm("Do you really want to delete this ^ ?", abort=True) comment.delete()