summaryrefslogtreecommitdiffstats
path: root/client/bug_open.py
blob: e9ecb19a2a5252bb24470f91b85346a777d69cbb (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
#!../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 docopt import docopt
import json, requests
from bug_show import show_ticket

if __name__ == '__main__':
	print(docopt(__doc__))

def call(args):
	print(args)
	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': 'TOKENHERE'
	}

	headers = {
		'Content-Type': 'application/json',
		'Accept': 'text/plain',
	}
	payload = json.dumps(ticket)

	r = requests.post(api_endpoint, data=payload, headers=headers, verify=False)

	t = json.loads(r.text).get('ticket')

	print(t)
	print(show_ticket(t))


def editor_prompt(summary, body):
	editor = os.environ.get('EDITOR','vim')
	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()

	regx = re.compile('^(.+?)\n\n(.+)$', re.S)

	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() # Strip opening and ending whitespace
	regmatch = regx.match(data)

	if len(regmatch.groups()) != 2:
		exit("Error: summary and body not separated properly, aborting")

	summary = regmatch.group(1)
	body = regmatch.group(2)

	return (summary, body)