#!../flask/bin/python """ usage: bug open [options] If no arguments are given it will open your $EDITOR where the first line is the summary following a newline and then the body of the report. Both are required. -h, --help Print this help text -s, --summary STRING A short summary of the bug -b, --body STRING The long description of the bug """ import os, re, tempfile, subprocess from bug_show import show_ticket from docopt import docopt import json, requests, sys import configparser def entrypoint(args): print(args) editor = os.environ.get('EDITOR', 'vim') c = configparser.ConfigParser() c.read('config') config = c[args['--uri']] access_token = config['access_token'] api_endpoint = args['--uri'] + '/api/1.0/ticket' if args['--summary']: summary = args['--summary'] else: summary = '' if args['--body']: body = args['--body'] else: body = '' if not(summary and body): summary, body = editor_prompt(summary, body) ticket = { 'summary': summary, 'body': body, 'token': access_token } open(api_endpoint, summary, ticket) def open(api_endpoint, access_token, data): payload = json.dumps(data) print(data) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', } req = requests.post(api_endpoint, data=payload, headers=headers, verify=False) if req.status_code != 201: print("Ticket not opened. Status code '{}'".format(req.status_code)) sys.exit(req.text) else: response = json.loads(req.text) if 'ticket' in response: ticket = response.get('ticket') else: sys.exit('aborting: ticket not found in response? Something\'s borked') print(ticket) print(show_ticket(ticket)) def editor_prompt(editor, summary, body): regx = re.compile('^(.+?)\n\n(.+)$', re.S) message='' if summary: message += summary if body: message += '\n\n' + body message += """ # Please enter the summary on a single line, followed # by an empty line, then followed by the body of the # ticket. # # Both the summary and body are required. If either of # them are missing, or they aren't separated properly # the submission will be aborted. """ tmp = tempfile.NamedTemporaryFile() tmp.write(message.encode("utf-8")) tmp.flush() subprocess.call([editor, tmp.name]) tmp.seek(0) data = tmp.read().decode("utf-8") tmp.close() data = data[:-263] # Strip the commented out message data = data.lstrip().rstrip() regmatch = regx.match(data) if len(regmatch.groups()) != 2: sys.exit("Error: summary and body not separated properly, aborting") summary = regmatch.group(1) body = regmatch.group(2) return summary, body