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.
This commit is contained in:
MasterofJOKers 2022-10-25 23:37:37 +02:00
parent 9bcd668171
commit f0b455326e
2 changed files with 62 additions and 1 deletions

View File

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

View File

@ -7,6 +7,7 @@ description = JIRA command line interface
packages = find:
install_requires =
click
clintermission
python-editor
jira
pyaml