1 import suds |
1 import suds |
2 import cobalt |
2 import sys |
|
3 import time |
|
4 import re |
|
5 import irc as Irc |
|
6 from configfile import Config |
3 |
7 |
4 suds_active = False |
8 suds_active = False |
5 btannounce_active = False |
9 btannounce_active = False |
6 btannounce_timeout = 0 |
10 btannounce_timeout = 0 |
7 |
11 |
8 def is_active(): |
12 def is_active(): |
9 return suds_active |
13 return suds_active |
10 |
14 |
|
15 def get_ticket_data (bot, replyto, ticket, withlink): |
|
16 if suds_active == False: |
|
17 print "suds is not active" |
|
18 return |
|
19 |
|
20 data = {} |
|
21 try: |
|
22 data = get_issue (ticket) |
|
23 except Exception, e: |
|
24 bot.privmsg (replyto, "Failed to get info for issue %s: %s" % (ticket, `e`)) |
|
25 |
|
26 if data: |
|
27 if data['view_state']['name'] == 'private': |
|
28 allowprivate = False |
|
29 |
|
30 for channel in bot.channels: |
|
31 if channel.get_value ('name') == replyto and channel.get_value ('btprivate', False): |
|
32 allowprivate = True |
|
33 break |
|
34 |
|
35 if not allowprivate: |
|
36 bot.privmsg (replyto, 'Error: ticket %s is private' % ticket) |
|
37 return |
|
38 |
|
39 bot.privmsg (replyto, "Issue %s: %s: Reporter: %s, assigned to: %s, status: %s (%s)" % \ |
|
40 (ticket, \ |
|
41 data.summary, \ |
|
42 data.reporter.name if hasattr (data.reporter, 'name') else "<unknown>", \ |
|
43 data.handler.name if hasattr (data, 'handler') else "nobody", \ |
|
44 data.status.name, \ |
|
45 data.resolution.name)) |
|
46 |
|
47 if withlink: |
|
48 bot.privmsg (replyto, "Read all about it here: " + get_ticket_url (ticket)) |
|
49 |
|
50 def process_message (bot, line, replyto): |
|
51 # Check for tracker url in the message |
|
52 url = Config.get_node ('bt').get_value ('url') |
|
53 http_regex = re.compile (r'.*http(s?)://%s/view\.php\?id=([0-9]+).*' % url) |
|
54 http_match = http_regex.match (line) |
|
55 |
|
56 if http_match: |
|
57 get_ticket_data (bot, replyto, http_match.group (2), False) |
|
58 |
11 def init(): |
59 def init(): |
|
60 global suds_active |
|
61 global suds_client |
|
62 |
12 try: |
63 try: |
13 print 'Initializing MantisBT connection...' |
64 print 'Initializing MantisBT connection...' |
14 suds_import = suds.xsd.doctor.Import ('http://schemas.xmlsoap.org/soap/encoding/', 'http://schemas.xmlsoap.org/soap/encoding/') |
65 suds_import = suds.xsd.doctor.Import ('http://schemas.xmlsoap.org/soap/encoding/', 'http://schemas.xmlsoap.org/soap/encoding/') |
15 suds_client = suds.client.Client ('https://zandronum.com/tracker/api/soap/mantisconnect.php?wsdl', plugins=[suds.xsd.doctor.ImportDoctor (suds_import)]) |
66 suds_client = suds.client.Client ('https://zandronum.com/tracker/api/soap/mantisconnect.php?wsdl', plugins=[suds.xsd.doctor.ImportDoctor (suds_import)]) |
16 suds_active = True |
67 suds_active = True |
18 print 'Failed to establish MantisBT connection: ' + `e` |
69 print 'Failed to establish MantisBT connection: ' + `e` |
19 pass |
70 pass |
20 |
71 |
21 if suds_active: |
72 if suds_active: |
22 sys.stdout.write ('Retrieving latest tracker ticket... ') |
73 sys.stdout.write ('Retrieving latest tracker ticket... ') |
23 user, password = bt_credentials() |
74 user, password = credentials() |
24 btannounce_id = suds_client.service.mc_issue_get_biggest_id (user, password, 0) |
75 btannounce_id = suds_client.service.mc_issue_get_biggest_id (user, password, 0) |
25 btannounce_active = True |
76 btannounce_active = True |
26 bt_updatechecktimeout() |
77 update_checktimeout() |
27 print btannounce_id |
78 print btannounce_id |
28 |
79 |
29 def update_checktimeout(): |
80 def update_checktimeout(): |
30 global btannounce_timeout |
81 global btannounce_timeout |
31 btannounce_timeout = time.time() + (Config.get_node ('bt').get_value ('checkinterval', default=5) * 60) |
82 btannounce_timeout = time.time() + (Config.get_node ('bt').get_value ('checkinterval', default=5) * 60) |
34 bt = Config.get_node ('bt') |
85 bt = Config.get_node ('bt') |
35 user = bt.get_value ('username', '') |
86 user = bt.get_value ('username', '') |
36 password = bt.get_value ('password', '') |
87 password = bt.get_value ('password', '') |
37 return [user, password] |
88 return [user, password] |
38 |
89 |
39 def get_issue(ticket): |
90 def get_issue (ticket): |
40 global suds_client |
91 global suds_client |
41 user, password = bt_credentials() |
92 user, password = credentials() |
42 return suds_client.service.mc_issue_get (user, password, ticket) |
93 return suds_client.service.mc_issue_get (user, password, ticket) |
43 |
94 |
44 def poll(): |
95 def poll(): |
45 global btannounce_timeout |
96 global btannounce_timeout |
46 global btannounce_id |
97 global btannounce_id |
47 |
98 |
48 if time.time() >= btannounce_timeout: |
99 if time.time() >= btannounce_timeout: |
49 bt_updatechecktimeout() |
100 update_checktimeout() |
50 newid = btannounce_id |
101 newid = btannounce_id |
51 try: |
102 try: |
52 user, password = bt_credentials() |
103 user, password = credentials() |
53 newid = suds_client.service.mc_issue_get_biggest_id (user, password, 0) |
104 newid = suds_client.service.mc_issue_get_biggest_id (user, password, 0) |
54 except Exception as e: |
105 except Exception as e: |
55 pass |
106 pass |
56 |
107 |
57 while newid > btannounce_id: |
108 while newid > btannounce_id: |
58 try: |
109 try: |
59 btannounce_id += 1 |
110 btannounce_id += 1 |
60 data = bt_getissue (btannounce_id) |
111 data = get_issue (btannounce_id) |
61 |
112 |
62 for client in cobalt.all_clients: |
113 for client in Irc.all_clients: |
63 announce_new_ticket (client, data) |
114 announce_new_issue (client, data) |
64 except Exception as e: |
115 except Exception as e: |
65 pass |
116 pass |
66 |
117 |
67 def get_ticket_url (ticket): |
118 def get_ticket_url (ticket): |
68 url = Config.get_node ('bt').get_value ('url') |
119 url = Config.get_node ('bt').get_value ('url') |
69 return 'https://%s/view.php?id=%s' % (url, ticket) |
120 return 'https://%s/view.php?id=%s' % (url, ticket) |
70 |
|
71 |
121 |
72 # |
122 # |
73 # Print a ticket announce to appropriate channels |
123 # Print a ticket announce to appropriate channels |
74 # |
124 # |
75 def announce_new_issue (bot, data): |
125 def announce_new_issue (bot, data): |
82 |
132 |
83 for channel in self.channels: |
133 for channel in self.channels: |
84 if channel.get_value ('btannounce', False): |
134 if channel.get_value ('btannounce', False): |
85 if not isprivate or (channel.get_value ('btprivate', False)): |
135 if not isprivate or (channel.get_value ('btprivate', False)): |
86 self.write ("PRIVMSG %s :[%s] New issue %s, reported by %s: %s: %s" % \ |
136 self.write ("PRIVMSG %s :[%s] New issue %s, reported by %s: %s: %s" % \ |
87 (channel['name'], data['project']['name'], idstring, reporter, |
137 (channel.get_value ('name'), |
88 data['summary'], self.get_ticket_url (idstring))) |
138 data['project']['name'], |
89 #fi |
139 idstring, |
90 #fi |
140 reporter, |
91 #done |
141 data['summary'], |
|
142 get_ticket_url (idstring))) |
|
143 |
|
144 def update_issue (ticket_id, ticket_data): |
|
145 btuser, btpassword = credentials() |
|
146 suds_client.service.mc_issue_update (btuser, btpassword, ticket_id, ticket_data) |
|
147 |
|
148 def post_note (ticket_id, message): |
|
149 btuser, btpassword = credentials() |
|
150 suds_client.service.mc_issue_note_add (btuser, btpassword, ticket_id, { 'text': message }) |