summaryrefslogtreecommitdiffstats
path: root/client/bug_open.py
diff options
context:
space:
mode:
Diffstat (limited to 'client/bug_open.py')
-rwxr-xr-xclient/bug_open.py64
1 files changed, 40 insertions, 24 deletions
diff --git a/client/bug_open.py b/client/bug_open.py
index e9ecb19..db15793 100755
--- a/client/bug_open.py
+++ b/client/bug_open.py
@@ -11,15 +11,22 @@ required.
-b, --body STRING The long description of the bug
"""
import os, re, tempfile, subprocess
-from docopt import docopt
-import json, requests
from bug_show import show_ticket
+from docopt import docopt
+import json, requests, sys
+import configparser
-if __name__ == '__main__':
- print(docopt(__doc__))
-
-def call(args):
+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']:
@@ -33,38 +40,49 @@ def call(args):
body = ''
if not(summary and body):
- (summary, body) = editor_prompt(summary, body)
+ summary, body = editor_prompt(summary, body)
ticket = {
'summary': summary,
'body': body,
- 'token': 'TOKENHERE'
+ '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': 'text/plain',
+ 'Accept': 'application/json',
}
- payload = json.dumps(ticket)
- r = requests.post(api_endpoint, data=payload, headers=headers, verify=False)
+ req = requests.post(api_endpoint, data=payload, headers=headers, verify=False)
- t = json.loads(r.text).get('ticket')
+ 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)
- print(t)
- print(show_ticket(t))
+ 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(summary, body):
- editor = os.environ.get('EDITOR','vim')
- message=''
+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
@@ -80,8 +98,6 @@ def editor_prompt(summary, body):
tmp.write(message.encode("utf-8"))
tmp.flush()
- regx = re.compile('^(.+?)\n\n(.+)$', re.S)
-
subprocess.call([editor, tmp.name])
tmp.seek(0)
@@ -89,13 +105,13 @@ def editor_prompt(summary, body):
tmp.close()
data = data[:-263] # Strip the commented out message
- data = data.lstrip().rstrip() # Strip opening and ending whitespace
+ data = data.lstrip().rstrip()
regmatch = regx.match(data)
if len(regmatch.groups()) != 2:
- exit("Error: summary and body not separated properly, aborting")
+ sys.exit("Error: summary and body not separated properly, aborting")
summary = regmatch.group(1)
body = regmatch.group(2)
- return (summary, body)
+ return summary, body