-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrequesttracker.py
More file actions
81 lines (59 loc) · 2.54 KB
/
requesttracker.py
File metadata and controls
81 lines (59 loc) · 2.54 KB
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
import re
import rt
from itertools import chain
from errbot.utils import ValidationException
from errbot import BotPlugin, botcmd, re_botcmd
CONFIG_TEMPLATE = {'USER': '',
'PASSWORD': '',
'REST_URL': '',
'DISPLAY_URL': '',
'MINIMUM_TICKET_ID': 1}
class RT(BotPlugin):
"""Request Tracker plugin for Err"""
tracker = None
re_find_ticket = r'(^| |(https?\:\/\/.+=))(\d{1,})( |\?|\.|,|:|\!|$)'
def get_configuration_template(self):
return CONFIG_TEMPLATE
def configure(self, configuration):
if configuration is not None and configuration != {}:
config = dict(chain(CONFIG_TEMPLATE.items(),
configuration.items()))
else:
config = CONFIG_TEMPLATE
super(RT, self).configure(config)
def check_configuration(self, config):
rt_login = False
for key in ['REST_URL', 'DISPLAY_URL', 'USER', 'PASSWORD']:
if key not in config:
raise ValidationException("missing config value: " + key)
try:
tracker = rt.Rt('%s/REST/1.0/' % config['REST_URL'])
rt_login = tracker.login(config['USER'], config['PASSWORD'])
except Exception as error:
raise ValidationException("Cannot connect to RT as %s: %s." % (
config['USER'], format(error),
))
if rt_login is False:
raise ValidationException("Authentication failed")
@re_botcmd(pattern=re_find_ticket, prefixed=False, flags=re.IGNORECASE)
def find_ticket(self, message, match):
""" Look up ticket metadata (works without prefix). Example: 12345 """
url = match.group(2)
ticket = match.group(3)
if url and url != self.config['DISPLAY_URL']:
return
if int(ticket) >= self.config['MINIMUM_TICKET_ID']:
return self.ticket_summary(ticket)
def ticket_summary(self, ticket_id):
self.tracker = rt.Rt(self.config['REST_URL'])
self.tracker.login(self.config['USER'], self.config['PASSWORD'])
try:
ticket = self.tracker.get_ticket(ticket_id)
return "[%s](%s) in %s from %s" % (
format(ticket.get("Subject", "No subject")),
format(self.config['DISPLAY_URL'] + ticket_id),
format(ticket.get("Queue")),
format(', '.join(ticket.get("Requestors")))
)
except:
return "Sorry, that ticket does not exist or I cannot access it."