summaryrefslogtreecommitdiffstats
path: root/client/bug_open.py
blob: db15793714a969dc4091c7b8921c671d40ccd7e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!../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