From 468121a42c9b7dcb78e8362dff8f595a07a435d3 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 28 2017 14:48:37 +0000 Subject: Add the possibility to comment on a ticket offline Signed-off-by: Pierre-Yves Chibon --- diff --git a/pag_off/app.py b/pag_off/app.py index 1046d1d..4786291 100644 --- a/pag_off/app.py +++ b/pag_off/app.py @@ -105,6 +105,21 @@ def do_view(args, config): print(pag_off.utils.ticket2str(ticket)) +def do_comment(args, config): + """ Allows the user to comment on a specific ticket + """ + _log.debug('project: %s', args.project) + _log.debug('ticket: %s', args.ticket_id) + + location = os.path.expanduser(config.get('main', 'location')) + ticket_fold = os.path.join(location, args.project) + _log.debug('folder: %s', ticket_fold) + ticket, filepath = pag_off.utils.load_tickets( + ticket_fold, ticket_id=args.ticket_id) + comment = input('Comment: ') + print(pag_off.utils.add_comment(ticket, filepath, comment, config)) + + def parse_arguments(): """ Set-up the argument parsing. """ parser = argparse.ArgumentParser( @@ -167,6 +182,20 @@ def parse_arguments(): help="Identifier of the ticket in this project") parser_view.set_defaults(func=do_view) + # COMMENT + parser_comment = subparsers.add_parser( + 'comment', + help='Comment on a ticket in the specified repository') + parser_comment.add_argument( + 'project', + help="Name of the project on pagure, can be: , " + "/project, fork// or " + "fork///") + parser_comment.add_argument( + 'ticket_id', + help="Identifier of the ticket in this project") + parser_comment.set_defaults(func=do_comment) + return parser.parse_args() diff --git a/pag_off/utils.py b/pag_off/utils.py index b133b52..fe43681 100644 --- a/pag_off/utils.py +++ b/pag_off/utils.py @@ -8,9 +8,11 @@ """ +import datetime import json import logging import os +import subprocess import arrow @@ -18,6 +20,24 @@ import arrow _log = logging.getLogger(__name__) +def _run_shell_cmd(command, directory, return_stdout=False): + """ Invoke the specified shall command + + """ + proc = subprocess.Popen( + command, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + cwd=directory) + stdout, stderr = proc.communicate() + if proc.returncode != 0: + error_msg = ('The command "{0}" failed with "{1}"' + .format(' '.join(command), stderr)) + raise Exception(error_msg) + if return_stdout: + return stdout.strip() + + def load_tickets(ticket_fold, status='Open', ticket_id=None, tags=None): """ Load the tickets present in the specified folder, filter them with the given filters and return a dict of @@ -137,3 +157,42 @@ Last update:{last_updated} }) return tmpl + + +def add_comment(ticket, filepath, comment, config): + """ Adds a given comment to the specified ticket. """ + tmpl = { + 'comment': comment, + 'date_created': datetime.datetime.utcnow().strftime('%s'), + 'edited_on': None, + 'editor': None, + 'id': None, + 'notification': False, + 'parent': None, + 'user': { + 'name': config.get('user', 'name'), + 'default_email': config.get('user', 'default_email'), + } + } + ticket['comments'].append(tmpl) + print(ticket2str(ticket)) + conf = input('Confirm comment [y/N]: ') + if conf.lower() not in ['yes', 'y']: + return 'canceled' + + ticket['last_updated'] = datetime.datetime.utcnow().strftime('%s') + + with open(filepath, 'w') as stream: + stream.write(json.dumps( + ticket, sort_keys=True, indent=4, + separators=(',', ': ')) + ) + folder, uid = filepath.rsplit('/', 1) + _run_shell_cmd( + ['git', 'commit', '-m', + 'Updated issue %s: %s' % (uid, ticket['title']), + uid + ], + directory=folder + ) + return 'done'