forked from psu-oit-cis-unixteam/account-reset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rt.py
95 lines (82 loc) · 3.12 KB
/
rt.py
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
"""A super minimal RT search client and parser for PSU Account reset requests"""
import logging
import requests
def get(query, credentials, url):
"""Query RT via the API.
query: the RTQL query
credentials: a user, pass dict
url: base RT url"""
url = "{}/search/ticket".format(url)
query_string = {'query': query, 'orderby': '-Created', 'format': 's'}
response = requests.post(url, data=credentials, params=query_string)
if response.ok:
return parse_search(response.text)
else:
raise Exception("RT: Search Failed")
def parse_search(response):
"""Parse RT search results for account reset requests.
response: an RT API response"""
message = split_response(response)
for req in message:
# the ticket id is the first chunk before ': '
ticket = req.split(': ')[0]
# the account is the last word in req
uid = req.split(' ')[-1]
logging.debug('RT: Yielding ticket=%s and user=%s', ticket, uid)
yield (ticket, uid)
def split_response(rt_response):
"""RT sends it's own 'status' in addition to content.
This function returns the message and raises an exception on failure"""
response = rt_response.split('\n')
# This is the RT request status, not HTTP status per se
if '200 Ok' in response[0]:
# we skip the first and last lines in response as they're ''
message = response[2:-1] # it may be possible to do [2:-2] here
logging.info("RT: response='%s'", message)
return message
else:
raise Exception("RT: response indicates failure...")
def comment(ticket, text, credentials, url):
"""Post a comment to a ticket at the url
ticket: ticket id
comment: comment text
credentials: a user, pass dict
url: base RT url"""
url = "{0}/ticket/{1}/comment".format(url, ticket)
content = "id: {0}\nAction: comment\nText: {1}".format(ticket, text)
post_data = credentials
post_data['content'] = content
response = requests.post(url, data=post_data)
message = split_response(response.text)
if 'Message recorded' in message[0]:
return True
else:
return False
def edit(ticket, values, credentials, url):
"""Edit a ticket
ticket: ticket id
values: a dict of ticket keys to update with corresponding values
credentials: a user, pass dict
url: base RT url"""
url = "{0}/ticket/{1}/edit".format(url, ticket)
post_data = credentials
edits = list()
for key in values.iterkeys():
edits.append("{0}: {1}".format(key, values[key]))
post_data['content'] = "\n".join(edits)
response = requests.post(url, data=post_data)
message = split_response(response.text)
if 'updated' in message[0]:
return True
else:
return False
def move(ticket, queue, credentials, url, unown=True):
"""Move a ticket
ticket: ticket id
queue: the new queue for the ticket
credentials: a user, pass dict
url: base RT url"""
values = {"Queue": queue}
if unown:
values['Owner'] = "Nobody"
return edit(ticket, values, credentials, url)