From f0b455326eab799705f9ff8b36d588b9189f7e08 Mon Sep 17 00:00:00 2001 From: MasterofJOKers Date: Tue, 25 Oct 2022 23:37:37 +0200 Subject: [PATCH] Add comment creation, editing and deletion This needed changes in showing an issue's comments, because we want to make it easy to target a specific comment and therefore we need to show the comment id. If the user doesn't provide a comment id, we show a list of comments and let the user chose the comment to edit/delete. --- jiracli/cli.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++- setup.cfg | 1 + 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/jiracli/cli.py b/jiracli/cli.py index 73473e1..e497ebf 100644 --- a/jiracli/cli.py +++ b/jiracli/cli.py @@ -466,8 +466,68 @@ def show(client, issue): for i, comment in enumerate(issue.fields.comment.comments): if i != 0: print('') - print(f" {comment.created} - {comment.author}") + print(f" {comment.created} - {comment.author} - {comment.id}") body = indent('\n'.join(wrap(comment.body, replace_whitespace=False)), ' ' * 4) print(body) print('') + + +@issue.group() +def comment(): + pass + + +@comment.command('create') +@pass_issue +@pass_client +def comment_create(client, issue): + """Create a comment on the issue""" + # TODO add some help text for the syntax in the description + # TODO add all issue info to the description, so we can autocomplete in the + # editor + text = prompt(f"Comment text to add as new comment to {issue.key}") + client.add_comment(issue.key, text) + + +def get_comment(client, issue, comment_id): + if not comment_id: + from clintermission import cli_select_item + options = [] + for comment in issue.fields.comment.comments: + # TODO make this roughly terminal width or add a shorten() method + # with … at the end + body = comment.body.replace('\n', ' ')[:64] + 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']) + + +@comment.command('edit') +@click.argument('comment_id', required=False) +@pass_issue +@pass_client +def comment_edit(client, issue, comment_id): + """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}", + initial_content=comment.body) + if text == comment.body: + print("No changes. Aborting.") + return + + comment.update(body=text) + + +@comment.command('delete') +@click.argument('comment_id', required=False) +@pass_issue +@pass_client +def comment_delete(client, issue, comment_id): + comment = get_comment(client, issue, comment_id) + + # TODO print formatted comment for verification + click.confirm("Do you really want to delete this ^ ?", abort=True) + comment.delete() diff --git a/setup.cfg b/setup.cfg index 97a6ba1..f7e2be9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -7,6 +7,7 @@ description = JIRA command line interface packages = find: install_requires = click + clintermission python-editor jira pyaml