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.
This commit is contained in:
MasterofJOKers 2022-10-25 23:51:14 +02:00
parent f0b455326e
commit ead0cf51de
1 changed files with 16 additions and 10 deletions

View File

@ -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()